Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/37648-draft-bar-reload-pin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@object-ui/app-shell': patch
'@objectstack/client': patch
---

fix(app-shell): the view settings draft bar publishes the reloaded draft after a conflict is resolved with "reload"

When a view settings Save was refused because the draft moved and the author chose "reload" in the conflict dialog, the panel re-read the stored view but the draft bar kept the draft version it had read before the reload. Its Publish was then pinned to a draft that no longer existed and was refused until the panel was reopened. The panel now hands the bar the version its reload read found (`RuntimeDraftBar`'s new `reloadedSignal`, `reloadedDraftVersion` and `reloadedHasDraft`), so the Publish promotes the draft now shown, and a reload that found no pending draft takes the bar's indicator down. A reload whose read fails hands the bar nothing, so its Publish stays pinned and is refused rather than sent unpinned.

`client.meta.publishItem`'s `parentVersion` JSDoc now says an empty string pins nothing and is not sent, as `draftVersion`'s does, and the client SDK page shows a publish pinned with `draftVersion` and `parentVersion`.
14 changes: 13 additions & 1 deletion content/docs/api/client-sdk.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,19 @@ const view = await client.meta.getItem('view', 'crm_lead.pipeline');
const draft = await client.meta.getItem('object', 'account', { packageId: 'com.acme.crm', state: 'draft' });
const draftItem = draft.item as { _diagnostics?: { valid: boolean } } | undefined;
console.log(draftItem?._diagnostics?.valid);
await client.meta.publishItem('object', 'account', { message: 'go live' });
const published = await client.meta.publishItem('object', 'account', { message: 'go live' });
// Pin what you publish (ADR-0008): `draftVersion` is the draft you mean, your own
// draft save's receipt `version` (or the draft read's), never a fresh re-read;
// `parentVersion` is the live row you mean to replace, here the one the publish
// above wrote (`null`: no live row is stored). A draft or live row that moved
// since is refused 409 METADATA_CONFLICT and nothing is published. An empty
// string pins nothing.
const saved = await client.meta.saveItem('object', 'account', { ...accountSchema, label: 'Customer' }, { mode: 'draft' });
await client.meta.publishItem('object', 'account', {
message: 'rename',
draftVersion: saved.version,
parentVersion: published.version,
});
await client.meta.rollbackItem('object', 'account', 3);
// The authoring trail, like getHistory and getAudit: without studio.access or manage_metadata → 403 FORBIDDEN (setup.access alone is refused)
const diff = await client.meta.diffItem('object', 'account', { from: 2, to: 5 });
Expand Down
4 changes: 3 additions & 1 deletion packages/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2798,7 +2798,9 @@ export class ObjectStackClient {
* `409 METADATA_CONFLICT` and nothing is promoted. Omitted, the
* publish replaces whatever is live, as before. It pins the live
* row only; the draft's own pin is `draftVersion`, and the two
* may be sent together (the door judges the draft first).
* may be sent together (the door judges the draft first). An
* empty string pins nothing and is not sent, as for
* `draftVersion`.
*/
parentVersion?: string | null;
},
Expand Down
68 changes: 68 additions & 0 deletions ui/packages/app-shell/src/views/RuntimeDraftBar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,71 @@ describe('RuntimeDraftBar — Publish is pinned to the draft it showed (objectst
await waitFor(() => expect(metadataClient.publish).toHaveBeenCalledWith('view', 'my_view'));
});
});

/**
* objectstack#37648 — after the host resolves a refused save with "reload"
* (`reloadedSignal`), the draft the bar shows is the one the host's reload
* read found: the Publish is pinned to that read's version, not to the draft
* the bar read before the reload, and never to a re-read of the bar's own.
*/
describe('RuntimeDraftBar — after the host\'s reload the Publish is pinned to the reloaded draft (objectstack#37648)', () => {
afterEach(() => {
vi.restoreAllMocks();
});

it('takes the reloaded draft\'s version, with no re-read', async () => {
const metadataClient = makeMetadataClient({ item: { columns: ['name'] }, version: 'hmac-sha256:draft-v1' });
const { rerender } = render(
<RuntimeDraftBar type="view" name="my_view" metadataClient={metadataClient} reloadedSignal={0} />,
);
await screen.findByTestId('runtime-draft-publish');
// Another writer moved the draft (v2); the host's save was refused and the author chose "reload".
rerender(
<RuntimeDraftBar
type="view"
name="my_view"
metadataClient={metadataClient}
reloadedSignal={1}
reloadedDraftVersion="hmac-sha256:moved-v2"
reloadedHasDraft
/>,
);
fireEvent.click(await screen.findByTestId('runtime-draft-publish'));

await waitFor(() =>
expect(metadataClient.publish).toHaveBeenCalledWith('view', 'my_view', { draftVersion: 'hmac-sha256:moved-v2' }),
);
expect(metadataClient.getDraft).toHaveBeenCalledTimes(1);
});

it('a reload that found no pending draft takes the indicator down', async () => {
const metadataClient = makeMetadataClient({ item: { columns: ['name'] }, version: 'hmac-sha256:draft-v1' });
const { rerender } = render(
<RuntimeDraftBar type="view" name="my_view" metadataClient={metadataClient} reloadedSignal={0} />,
);
await screen.findByTestId('runtime-draft-publish');
rerender(
<RuntimeDraftBar type="view" name="my_view" metadataClient={metadataClient} reloadedSignal={1} reloadedHasDraft={false} />,
);
await waitFor(() => expect(screen.queryByTestId('runtime-draft-bar')).toBeNull());
});

it('the mount\'s own signal changes nothing (control)', async () => {
const metadataClient = makeMetadataClient({ item: { columns: ['name'] }, version: 'hmac-sha256:draft-v1' });
render(
<RuntimeDraftBar
type="view"
name="my_view"
metadataClient={metadataClient}
reloadedSignal={3}
reloadedDraftVersion="hmac-sha256:other"
reloadedHasDraft={false}
/>,
);
fireEvent.click(await screen.findByTestId('runtime-draft-publish'));
await waitFor(() =>
expect(metadataClient.publish).toHaveBeenCalledWith('view', 'my_view', { draftVersion: 'hmac-sha256:draft-v1' }),
);
});
});

31 changes: 31 additions & 0 deletions ui/packages/app-shell/src/views/RuntimeDraftBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ export interface RuntimeDraftBarProps {
* host that does not know it), the Publish after the save goes unpinned.
*/
savedDraftVersion?: string;
/**
* objectstack#37648 — monotonic counter the host bumps when it replaced its
* buffer with a fresh read of the stored item (a refused save resolved with
* "reload" in the draft-conflict dialog), with {@link reloadedDraftVersion}
* and {@link reloadedHasDraft} from THAT read. The draft the bar now shows
* is the one that read found, so its Publish is pinned to that read's
* version, not to the draft the bar read before the reload (which the
* reload exists because it moved). ⛔ Never a re-read of the bar's own: it
* would pin whatever draft is pending by then, which the host never showed.
*/
reloadedSignal?: number;
/** The draft version the host's reload read (absent: that read named none, and the Publish goes unpinned). */
reloadedDraftVersion?: string;
/** Whether the host's reload read found a pending draft. */
reloadedHasDraft?: boolean;
}

/**
Expand Down Expand Up @@ -100,6 +115,9 @@ export function RuntimeDraftBar({
onAfterChange,
savedSignal,
savedDraftVersion,
reloadedSignal,
reloadedDraftVersion,
reloadedHasDraft,
}: RuntimeDraftBarProps) {
const locale = useMetadataLocale();
const refusalTitles = useRefusalTitles();
Expand Down Expand Up @@ -165,6 +183,19 @@ export function RuntimeDraftBar({
}
}, [savedSignal, savedDraftVersion]);

// objectstack#37648 — the host re-read the stored item into its buffer (a
// conflict resolved with "reload"): the draft shown is the one that read
// found, and the Publish is pinned to it. Skip the initial mount.
const lastReloadedSignal = useRef(reloadedSignal);
useEffect(() => {
if (reloadedSignal === lastReloadedSignal.current) return;
lastReloadedSignal.current = reloadedSignal;
if (reloadedSignal) {
shownDraftVersionRef.current = reloadedDraftVersion;
setHasDraft(reloadedHasDraft === true);
}
}, [reloadedSignal, reloadedDraftVersion, reloadedHasDraft]);

const handlePublish = useCallback(async () => {
if (!name) return;
setBusy(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,3 +465,74 @@ describe('the draft bar\'s Publish after a view settings save is pinned to that
cleanup();
});
});

/**
* objectstack#37648 — a refused save resolved with "reload" re-reads the
* stored view into the panel; the draft that read found is the one the draft
* bar shows from then on, so the panel hands the bar that read's version
* (`reloadedSignal`, `reloadedDraftVersion`, `reloadedHasDraft`). Before, the
* bar kept the version from before the reload, and its Publish was refused
* until the panel was reopened.
*/
describe('after a conflict resolved with "reload", the draft bar takes the reloaded draft (objectstack#37648)', () => {
it('another writer\'s draft: the bar is handed its version', async () => {
const door = viewDoor();
const { outcomes } = mountPanel(door);
await loadedAndEdited();
await act(async () => {
fireEvent.click(saveButton());
});
await waitFor(() => expect(outcomes).toEqual(['saved']));
const mine = door.draftOf(VIEW_ID)!.version;
expect(bar.props?.reloadedSignal).toBe(0);

// Another writer's draft save moves the draft past the panel's receipt.
await persistRuntimeMetadata('view', VIEW_ID, buildViewConfigSaveBody(OBJECT, { id: VIEW_ID, label: 'Theirs', type: 'grid' }), {
metadataClient: door.client,
});
const theirs = door.draftOf(VIEW_ID)!.version;
expect(theirs).not.toBe(mine);

fireEvent.click(screen.getByTestId('mock-inspector-label'));
await act(async () => {
fireEvent.click(saveButton());
});
await waitFor(() => expect(screen.getByTestId('draft-conflict-dialog')).toBeTruthy());
await act(async () => {
fireEvent.click(screen.getByTestId('draft-conflict-reload'));
});
await waitFor(() => expect(outcomes).toEqual(['saved', 'reloaded']));
await waitFor(() => expect(bar.props?.reloadedSignal).toBe(1));
expect(bar.props?.reloadedDraftVersion).toBe(theirs);
expect(bar.props?.reloadedHasDraft).toBe(true);
// The landed-save signal is not moved by the reload.
expect(bar.props?.savedSignal).toBe(1);
cleanup();
});

it('a reload that finds no pending draft says so', async () => {
const door = viewDoor();
const { outcomes } = mountPanel(door);
await loadedAndEdited();
// Another writer's draft lands after the load: the save, pinned to "no draft", is refused.
await persistRuntimeMetadata('view', VIEW_ID, buildViewConfigSaveBody(OBJECT, { id: VIEW_ID, label: 'Theirs', type: 'grid' }), {
metadataClient: door.client,
});
await act(async () => {
fireEvent.click(saveButton());
});
await waitFor(() => expect(screen.getByTestId('draft-conflict-dialog')).toBeTruthy());
// The other draft is published before the author answers.
await door.client.publish('view', VIEW_ID);
expect(door.draftOf(VIEW_ID)).toBeUndefined();
await act(async () => {
fireEvent.click(screen.getByTestId('draft-conflict-reload'));
});
await waitFor(() => expect(outcomes).toEqual(['reloaded']));
await waitFor(() => expect(bar.props?.reloadedSignal).toBe(1));
expect(bar.props?.reloadedHasDraft).toBe(false);
expect(bar.props?.reloadedDraftVersion).toBeUndefined();
cleanup();
});
});

35 changes: 34 additions & 1 deletion ui/packages/app-shell/src/views/ViewConfigPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,19 @@ export function ViewConfigPanel({ open, onClose, mode = 'edit', activeView, obje
setAuthoredReads((n) => n + 1);
onAfterChange?.();
}, [onAfterChange]);
// objectstack#37648 — a refused save resolved with "reload" re-reads the
// stored view into the buffer (`handleAfterChange`); the draft that read
// finds is the one the draft bar now shows, so the read hands the bar its
// version (`reloadedDraft`) and the bar's Publish is pinned to it.
const conflictReloadRef = useRef(false);
const [reloadedDraft, setReloadedDraft] = useState<{ signal: number; version?: string; hasDraft: boolean }>({
signal: 0,
hasDraft: false,
});
const handleConflictReload = useCallback(() => {
conflictReloadRef.current = true;
handleAfterChange();
}, [handleAfterChange]);
// objectstack#36137 — the draft-version guard of this panel's buffer
// (`useDraftSaveGuard`): the pin of the draft it was loaded from, then of
// each save it sent, and the conflict dialog a refused save opens.
Expand All @@ -214,14 +227,15 @@ export function ViewConfigPanel({ open, onClose, mode = 'edit', activeView, obje
forget: forgetDraftVersion,
heldDraftVersion,
dialog: draftConflictDialog,
} = useDraftSaveGuard(metadataClient, handleAfterChange);
} = useDraftSaveGuard(metadataClient, handleConflictReload);
useEffect(() => {
if (!open || !readsAuthored) return;
const id = activeView.id;
let cancelled = false;
setSeed({ for: id, state: 'loading' });
(async () => {
let next: AuthoredSeed;
let foundDraft = false;
try {
// objectstack#36137 — the served draft read is the envelope, so
// its `version` pins this buffer's saves (`draftPinOf`), at the
Expand All @@ -231,6 +245,7 @@ export function ViewConfigPanel({ open, onClose, mode = 'edit', activeView, obje
metadataClient.getDraft('view', id),
]);
const servedDraft = unwrapDraftBody(draftAnswer);
foundDraft = servedDraft !== null;
const authored = authoredBuffer('view', layers as AuthoredLayers, servedDraft);
if (authored.kind === 'unserved') {
next = { for: id, state: 'refused', message: tStudio('engine.studio.refusal.draftUnreadable', locale) };
Expand Down Expand Up @@ -266,6 +281,21 @@ export function ViewConfigPanel({ open, onClose, mode = 'edit', activeView, obje
// next save sends; a read that installs none leaves nothing held.
if (next.state === 'ready') recordDraftRead('view', id, null, next.pin);
else forgetDraftVersion();
// objectstack#37648 — this read answers a conflict's "reload": the
// draft bar takes the draft it found. A read that could not
// install a buffer hands the bar nothing, so its Publish stays
// pinned to the draft it showed and is refused, never unpinned.
if (conflictReloadRef.current) {
conflictReloadRef.current = false;
if (next.state === 'ready') {
const pin = next.pin;
setReloadedDraft((prev) => ({
signal: prev.signal + 1,
version: pin && 'ifMatch' in pin ? pin.ifMatch : undefined,
hasDraft: foundDraft,
}));
}
}
setSeed(next);
})();
return () => { cancelled = true; };
Expand Down Expand Up @@ -506,6 +536,9 @@ export function ViewConfigPanel({ open, onClose, mode = 'edit', activeView, obje
dirty={isDirty}
savedSignal={savedSignal}
savedDraftVersion={savedDraftVersion}
reloadedSignal={reloadedDraft.signal}
reloadedDraftVersion={reloadedDraft.version}
reloadedHasDraft={reloadedDraft.hasDraft}
onAfterChange={handleAfterChange}
/>
)}
Expand Down
Loading