diff --git a/.changeset/36962-owd-overview-pinned-publish.md b/.changeset/36962-owd-overview-pinned-publish.md new file mode 100644 index 00000000000..cda36e0da84 --- /dev/null +++ b/.changeset/36962-owd-overview-pinned-publish.md @@ -0,0 +1,10 @@ +--- +'@object-ui/app-shell': patch +--- + +fix(app-shell): the Studio sharing-defaults overview's Save no longer publishes over a change another admin published since the page loaded + +Clause-②: no +Metadata-protocol: none + +The overview of every object's org-wide sharing default (Studio → Access) publishes each changed object over the published row its load read, as the object page and the extension, hook, action and flow editors already do. When another admin published the same object between the load and the Save, that object's publish is refused and the conflict dialog asks: reload the live value, overwrite it after a confirmation, or keep editing. The other changed objects in the same Save carry on, each still published over the row its own load read, whichever answer an earlier object got, so a second object another admin changed is asked about too. An object kept unsaved stays pinned to the row its edit was made on, so the next Save asks again. An object reloaded is read back; when it had a draft pending before the Save, that draft is kept, now carrying the edit, and its next Save is asked again. Against a server that serves no published version, the publish goes unpinned, as before. diff --git a/ui/packages/app-shell/src/views/studio-design/PackageOwdOverviewPanel.publishPinned-36962.test.tsx b/ui/packages/app-shell/src/views/studio-design/PackageOwdOverviewPanel.publishPinned-36962.test.tsx new file mode 100644 index 00000000000..2274ce93ebb --- /dev/null +++ b/ui/packages/app-shell/src/views/studio-design/PackageOwdOverviewPanel.publishPinned-36962.test.tsx @@ -0,0 +1,395 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * objectstack#36962 — the OWD overview's Save publishes each changed row over + * the published row its load read, never over one another writer published + * since. + * + * The Save is each changed row's draft and then that object's own publish + * (objectstack#36511), so it leaves no draft behind, and two admins who open + * the overview each pass the draft door. The load reads each saved row's + * layers at the package the publish writes, and the publish is pinned to that + * read's `overlayVersion` (`If-Match`): the later Save is refused + * `409 METADATA_CONFLICT` and the author is asked (reload, overwrite, keep + * editing), as the Studio editors are (objectstack#36726). + * + * The other writer is the server double's own move of the live row. The + * door's refusal is parsed by the REAL `MetadataClient`, so the error the + * guard reads is production's. + */ + +import '@testing-library/jest-dom/vitest'; +import * as React from 'react'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import { act, cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react'; +import { MetadataClient, type MetadataError } from '@object-ui/data-objectstack'; +import { t } from '../metadata-admin/i18n'; +import { PackageOwdOverviewPanel } from './PackageOwdOverviewPanel'; +import { StudioSaveHarness, pressStudioSave } from './__tests__/studioSaveHarness'; + +const PACKAGE = 'com.qa.owd'; +const L = (key: string) => t(key, 'en-US'); +const PUBLIC_READ = L('engine.studio.settings.sharingPublicRead'); +const PUBLIC_READ_WRITE = L('engine.studio.settings.sharingPublicReadWrite'); +const WAIT = { timeout: 4000 }; + +type Body = Record; + +interface Publish { + name: string; + packageId: string | undefined; + ifMatch: string | undefined; + ifNoneMatch: string | undefined; +} + +interface Server { + published: Map; + versions: Map; + drafts: Map; + /** A server from before `overlayVersion` was served serves none (the control). */ + servesPublishedVersion: boolean; + publishes: Publish[]; + resets: string[]; + seq: number; +} + +/** The 409 the door answers, run through the real client's error parser. */ +async function conflictRefusal(name: string, head: string | null): Promise { + const client = new MetadataClient({ + baseUrl: 'http://localhost:3000', + fetch: (async () => + new Response( + JSON.stringify({ + error: `object/${name} has been modified since you loaded it. The version token sent is not the current version (current is ${head}).`, + code: 'METADATA_CONFLICT', + currentVersion: head, + }), + { status: 409, headers: { 'content-type': 'application/json' } }, + )) as unknown as typeof fetch, + }); + return client.publish('object', name).then( + () => { + throw new Error('the stub door accepted the publish'); + }, + (e: unknown) => e as MetadataError, + ); +} + +const copy = (v: T): T => JSON.parse(JSON.stringify(v)) as T; + +function newServer(): Server { + const server: Server = { + published: new Map(), + versions: new Map(), + drafts: new Map(), + servesPublishedVersion: true, + publishes: [], + resets: [], + seq: 0, + }; + server.published.set('a_alpha', { name: 'a_alpha', label: 'Alpha', sharingModel: 'private', fields: {} }); + server.versions.set('a_alpha', 'hmac-sha256:alpha0'); + server.published.set('b_beta', { name: 'b_beta', label: 'Beta', sharingModel: 'private', fields: {} }); + server.versions.set('b_beta', 'hmac-sha256:beta0'); + return server; +} + +function renderPanel(server: Server): void { + const client = { + list: async () => [...server.published.values()].map((b) => ({ name: b.name, label: b.label })), + listDrafts: async () => [...server.drafts.keys()].map((name) => ({ name })), + layered: async (_t: string, name: string) => { + const live = server.published.get(name) ?? null; + const version = server.servesPublishedVersion + ? { overlayVersion: live ? (server.versions.get(name) ?? null) : null } + : {}; + return { code: null, overlay: live && copy(live), effective: live && copy(live), ...version }; + }, + getDraft: async (_t: string, name: string) => { + const draft = server.drafts.get(name); + return draft ? { type: 'object', name, item: copy(draft) } : null; + }, + save: async (_t: string, name: string, body: Body) => { + server.drafts.set(name, copy(body)); + return { success: true }; + }, + publish: async (_t: string, name: string, opts?: { packageId?: string; ifMatch?: string; ifNoneMatch?: '*' }) => { + server.publishes.push({ name, packageId: opts?.packageId, ifMatch: opts?.ifMatch, ifNoneMatch: opts?.ifNoneMatch }); + const head = server.published.has(name) ? (server.versions.get(name) ?? null) : null; + if (opts?.ifMatch !== undefined && opts.ifMatch !== head) throw await conflictRefusal(name, head); + if (opts?.ifNoneMatch !== undefined && head !== null) throw await conflictRefusal(name, head); + server.seq += 1; + const version = `hmac-sha256:mine${server.seq}`; + const draft = server.drafts.get(name); + if (draft) { + server.published.set(name, draft); + server.versions.set(name, version); + } + server.drafts.delete(name); + return { success: true, version }; + }, + reset: async (_t: string, name: string, opts?: { state?: string }) => { + if (opts?.state === 'draft') { + server.resets.push(name); + server.drafts.delete(name); + } + return { success: true }; + }, + } as unknown as React.ComponentProps['client']; + render( + + + , + ); +} + +/** Another admin publishes the object's sharing model: the live row and its version move. */ +function anotherAdminPublishes(server: Server, name: string, sharingModel: string): string { + server.seq += 1; + const version = `hmac-sha256:theirs${server.seq}`; + server.published.set(name, { ...(server.published.get(name) ?? server.drafts.get(name))!, sharingModel }); + server.versions.set(name, version); + return version; +} + +async function pick(name: string, option: string): Promise { + await screen.findByTestId(`owd-row-${name}`, undefined, WAIT); + fireEvent.keyDown(screen.getByTestId(`owd-internal-${name}`), { key: 'ArrowDown' }); + fireEvent.click(within(await screen.findByRole('listbox')).getByRole('option', { name: option })); + await screen.findByTestId(`owd-dirty-${name}`); +} + +const sharingOf = (server: Server, name: string) => server.published.get(name)?.sharingModel; +const UNPUBLISHED_DRAFT = L('engine.studio.unpublishedDraft'); + +/** Answer the open conflict dialog, and wait for it to close. */ +async function answer(choice: 'reload' | 'cancel'): Promise { + fireEvent.click(screen.getByTestId(`draft-conflict-${choice}`)); + await waitFor(() => expect(screen.queryByTestId('draft-conflict-dialog')).toBeNull(), WAIT); +} + +/** The next conflict dialog, once the pass reaches it. */ +async function nextDialog(): Promise { + await screen.findByTestId('draft-conflict-dialog', undefined, WAIT); +} + +/** The race: the overview loaded, another admin published Alpha, then this overview's Save. */ +async function raceAlpha(server: Server): Promise { + renderPanel(server); + await pick('a_alpha', PUBLIC_READ_WRITE); + const theirs = anotherAdminPublishes(server, 'a_alpha', 'public_read'); + await pressStudioSave({ settle: false }); + await screen.findByTestId('draft-conflict-dialog', undefined, WAIT); + return theirs; +} + +let server: Server; +beforeEach(() => { + Element.prototype.scrollIntoView = vi.fn(); + server = newServer(); +}); +afterEach(cleanup); + +describe('the OWD overview — two Saves over one object are not last-writer-wins (objectstack#36962)', () => { + it('the later Save is refused at its publish, worded as a live change; keep editing leaves the edit unsaved', async () => { + await raceAlpha(server); + expect(server.publishes.map((p) => [p.name, p.packageId, p.ifMatch, p.ifNoneMatch])).toEqual([ + ['a_alpha', PACKAGE, 'hmac-sha256:alpha0', undefined], + ]); + expect(screen.getByTestId('draft-conflict-dialog')).toHaveTextContent('This changed since you opened it'); + expect(screen.getByTestId('draft-conflict-dialog')).toHaveTextContent('a_alpha'); + + fireEvent.click(screen.getByTestId('draft-conflict-cancel')); + await waitFor(() => expect(screen.queryByTestId('draft-conflict-dialog')).toBeNull(), WAIT); + // Nothing went live: the other admin's value stands, the draft the Save + // wrote is taken back, and the edit stays on the page, named as not saved. + expect(await screen.findByText(/Alpha: Not saved:/, undefined, WAIT)).toBeInTheDocument(); + expect(sharingOf(server, 'a_alpha')).toBe('public_read'); + expect(server.resets).toEqual(['a_alpha']); + expect(server.drafts.size).toBe(0); + expect(screen.getByTestId('owd-dirty-a_alpha')).toBeInTheDocument(); + expect(screen.getByTestId('owd-internal-a_alpha')).toHaveTextContent(PUBLIC_READ_WRITE); + }); + + it('"reload" reads the other admin\'s value back, and the next Save is pinned to it', async () => { + const theirs = await raceAlpha(server); + fireEvent.click(screen.getByTestId('draft-conflict-reload')); + await waitFor(() => expect(screen.queryByTestId('draft-conflict-dialog')).toBeNull(), WAIT); + await waitFor(() => expect(screen.queryByTestId('owd-dirty-a_alpha')).toBeNull(), WAIT); + expect(screen.getByTestId('owd-internal-a_alpha')).toHaveTextContent(PUBLIC_READ); + expect(server.drafts.size).toBe(0); + + await pick('a_alpha', PUBLIC_READ_WRITE); + await pressStudioSave(); + await waitFor(() => expect(sharingOf(server, 'a_alpha')).toBe('public_read_write'), WAIT); + expect(server.publishes[server.publishes.length - 1]).toMatchObject({ name: 'a_alpha', ifMatch: theirs }); + expect(screen.queryByTestId('draft-conflict-dialog')).toBeNull(); + }); + + it('"overwrite" asks again, then re-publishes pinned to the refusal\'s currentVersion', async () => { + const theirs = await raceAlpha(server); + fireEvent.click(screen.getByTestId('draft-conflict-overwrite')); + const before = server.publishes.length; + expect(await screen.findByTestId('draft-conflict-overwrite-confirm')).toBeInTheDocument(); + expect(server.publishes.length).toBe(before); + await act(async () => { + fireEvent.click(screen.getByTestId('draft-conflict-overwrite-confirm')); + }); + await waitFor(() => expect(sharingOf(server, 'a_alpha')).toBe('public_read_write'), WAIT); + expect(server.publishes[server.publishes.length - 1]).toMatchObject({ name: 'a_alpha', ifMatch: theirs }); + }); + + it('one row refused among two (objectstack#36511): the other goes live, and the kept edit stays pinned to the read it was made on', async () => { + renderPanel(server); + await pick('a_alpha', PUBLIC_READ_WRITE); + await pick('b_beta', PUBLIC_READ); + anotherAdminPublishes(server, 'a_alpha', 'public_read'); + await pressStudioSave({ settle: false }); + await screen.findByTestId('draft-conflict-dialog', undefined, WAIT); + fireEvent.click(screen.getByTestId('draft-conflict-cancel')); + + // Beta, after Alpha in the table's order, still went live, pinned to its own read. + await waitFor(() => expect(sharingOf(server, 'b_beta')).toBe('public_read'), WAIT); + await waitFor(() => expect(screen.queryByTestId('owd-dirty-b_beta')).toBeNull(), WAIT); + expect(server.publishes.find((p) => p.name === 'b_beta')).toMatchObject({ ifMatch: 'hmac-sha256:beta0' }); + // Alpha keeps its edit, though the page re-read the table after Beta went live. + expect(screen.getByTestId('owd-dirty-a_alpha')).toBeInTheDocument(); + expect(sharingOf(server, 'a_alpha')).toBe('public_read'); + + // That re-read did not re-pin Alpha's kept edit to the other admin's row: the next Save is asked again. + const before = server.publishes.length; + await pressStudioSave({ settle: false }); + await screen.findByTestId('draft-conflict-dialog', undefined, WAIT); + expect(server.publishes.slice(before).map((p) => [p.name, p.ifMatch])).toEqual([['a_alpha', 'hmac-sha256:alpha0']]); + expect(sharingOf(server, 'a_alpha')).toBe('public_read'); + }); + + it('one editor: each Save is pinned to the version the last one left, so it never conflicts with itself', async () => { + renderPanel(server); + await pick('a_alpha', PUBLIC_READ); + await pressStudioSave(); + await waitFor(() => expect(screen.queryByTestId('owd-dirty-a_alpha')).toBeNull(), WAIT); + const first = server.versions.get('a_alpha'); + await pick('a_alpha', PUBLIC_READ_WRITE); + await pressStudioSave(); + await waitFor(() => expect(sharingOf(server, 'a_alpha')).toBe('public_read_write'), WAIT); + expect(server.publishes.map((p) => p.ifMatch)).toEqual(['hmac-sha256:alpha0', first]); + expect(screen.queryByTestId('draft-conflict-dialog')).toBeNull(); + }); + + it('a row the published list does not hold publishes asserting no published row of its name', async () => { + server.drafts.set('c_gamma', { name: 'c_gamma', label: 'Gamma', sharingModel: 'private', fields: {} }); + renderPanel(server); + await pick('c_gamma', PUBLIC_READ); + await pressStudioSave(); + await waitFor(() => expect(sharingOf(server, 'c_gamma')).toBe('public_read'), WAIT); + expect(server.publishes).toEqual([{ name: 'c_gamma', packageId: PACKAGE, ifMatch: undefined, ifNoneMatch: '*' }]); + }); + + it('control: a server that serves no `overlayVersion` gets an unpinned publish, as before', async () => { + server.servesPublishedVersion = false; + renderPanel(server); + await pick('a_alpha', PUBLIC_READ_WRITE); + anotherAdminPublishes(server, 'a_alpha', 'public_read'); + await pressStudioSave(); + await waitFor(() => expect(sharingOf(server, 'a_alpha')).toBe('public_read_write'), WAIT); + expect(server.publishes.map((p) => [p.ifMatch, p.ifNoneMatch])).toEqual([[undefined, undefined]]); + expect(screen.queryByTestId('draft-conflict-dialog')).toBeNull(); + }); +}); + +describe('the OWD overview — a reload in a pass of several rows (objectstack#36962)', () => { + it('reload on the first row: the row after it is still published over its own load\'s row, and is asked', async () => { + renderPanel(server); + await pick('a_alpha', PUBLIC_READ_WRITE); + await pick('b_beta', PUBLIC_READ_WRITE); + anotherAdminPublishes(server, 'a_alpha', 'public_read'); + anotherAdminPublishes(server, 'b_beta', 'public_read'); + await pressStudioSave({ settle: false }); + await nextDialog(); + expect(screen.getByTestId('draft-conflict-dialog')).toHaveTextContent('a_alpha'); + await answer('reload'); + + // Beta, after the reloaded Alpha in the same pass: pinned to its own load, refused, asked. + await nextDialog(); + expect(screen.getByTestId('draft-conflict-dialog')).toHaveTextContent('b_beta'); + expect(server.publishes.map((p) => [p.name, p.ifMatch, p.ifNoneMatch])).toEqual([ + ['a_alpha', 'hmac-sha256:alpha0', undefined], + ['b_beta', 'hmac-sha256:beta0', undefined], + ]); + expect(sharingOf(server, 'b_beta')).toBe('public_read'); + await answer('cancel'); + + // Alpha reads back the other admin's value; Beta keeps its edit, unsaved. + await waitFor(() => expect(screen.queryByTestId('owd-dirty-a_alpha')).toBeNull(), WAIT); + expect(screen.getByTestId('owd-internal-a_alpha')).toHaveTextContent(PUBLIC_READ); + expect(screen.getByTestId('owd-dirty-b_beta')).toBeInTheDocument(); + expect(sharingOf(server, 'a_alpha')).toBe('public_read'); + expect(sharingOf(server, 'b_beta')).toBe('public_read'); + }); + + it('the first row refused and kept, the second reloaded: the kept edit is still pinned to its own load on the next Save', async () => { + renderPanel(server); + await pick('a_alpha', PUBLIC_READ_WRITE); + await pick('b_beta', PUBLIC_READ_WRITE); + anotherAdminPublishes(server, 'a_alpha', 'public_read'); + anotherAdminPublishes(server, 'b_beta', 'public_read'); + await pressStudioSave({ settle: false }); + await nextDialog(); + await answer('cancel'); + await nextDialog(); + expect(screen.getByTestId('draft-conflict-dialog')).toHaveTextContent('b_beta'); + await answer('reload'); + + await waitFor(() => expect(screen.queryByTestId('owd-dirty-b_beta')).toBeNull(), WAIT); + expect(screen.getByTestId('owd-internal-b_beta')).toHaveTextContent(PUBLIC_READ); + expect(screen.getByTestId('owd-dirty-a_alpha')).toBeInTheDocument(); + + // Beta's reload cleared every pin the guard held; Alpha's next publish is still its own load's. + const before = server.publishes.length; + await pressStudioSave({ settle: false }); + await nextDialog(); + expect(server.publishes.slice(before).map((p) => [p.name, p.ifMatch])).toEqual([['a_alpha', 'hmac-sha256:alpha0']]); + expect(sharingOf(server, 'a_alpha')).toBe('public_read'); + }); + + it('a row with a draft pending before the Save: reload keeps that draft, now carrying the edit, and the next Save is asked again', async () => { + server.drafts.set('a_alpha', { ...server.published.get('a_alpha')!, label: 'Alpha' }); + renderPanel(server); + await pick('a_alpha', PUBLIC_READ_WRITE); + anotherAdminPublishes(server, 'a_alpha', 'public_read'); + await pressStudioSave({ settle: false }); + await nextDialog(); + await answer('reload'); + + // The pending draft is not taken back: it carries the edit, shown as the row's + // value under the unpublished-draft mark, while the live row is the other admin's. + expect(server.resets).toEqual([]); + expect(server.drafts.get('a_alpha')?.sharingModel).toBe('public_read_write'); + await waitFor(() => expect(screen.queryByTestId('owd-dirty-a_alpha')).toBeNull(), WAIT); + expect(screen.getByTestId('owd-internal-a_alpha')).toHaveTextContent(PUBLIC_READ_WRITE); + expect(within(screen.getByTestId('owd-row-a_alpha')).getByText(UNPUBLISHED_DRAFT)).toBeInTheDocument(); + expect(sharingOf(server, 'a_alpha')).toBe('public_read'); + + // Its next Save stays pinned to the version the conflict was raised on, never the re-read's: asked again. + await pick('a_alpha', PUBLIC_READ); + const before = server.publishes.length; + await pressStudioSave({ settle: false }); + await nextDialog(); + expect(server.publishes.slice(before).map((p) => [p.name, p.ifMatch])).toEqual([['a_alpha', 'hmac-sha256:alpha0']]); + expect(server.versions.get('a_alpha')).toMatch(/theirs/); + }); + + it('control: a draft-only row whose name another writer published since is refused, not published over', async () => { + server.drafts.set('c_gamma', { name: 'c_gamma', label: 'Gamma', sharingModel: 'private', fields: {} }); + renderPanel(server); + await pick('c_gamma', PUBLIC_READ_WRITE); + anotherAdminPublishes(server, 'c_gamma', 'public_read'); + await pressStudioSave({ settle: false }); + await nextDialog(); + expect(server.publishes).toEqual([{ name: 'c_gamma', packageId: PACKAGE, ifMatch: undefined, ifNoneMatch: '*' }]); + await answer('cancel'); + expect(sharingOf(server, 'c_gamma')).toBe('public_read'); + expect(screen.getByTestId('owd-dirty-c_gamma')).toBeInTheDocument(); + }); +}); diff --git a/ui/packages/app-shell/src/views/studio-design/PackageOwdOverviewPanel.tsx b/ui/packages/app-shell/src/views/studio-design/PackageOwdOverviewPanel.tsx index ba7a1446f1b..f3046b31b11 100644 --- a/ui/packages/app-shell/src/views/studio-design/PackageOwdOverviewPanel.tsx +++ b/ui/packages/app-shell/src/views/studio-design/PackageOwdOverviewPanel.tsx @@ -21,6 +21,10 @@ * per-object Settings tab writes — then that object's own publish, so the * change is live at once. Cancel puts every dial back. A refused object * keeps its edit on the page, and the reason is shown. + * • Each row's publish is pinned to the published row the load read + * (objectstack#36962, as objectstack#36726 pins the Studio editors): a + * publish over a row another writer published since is refused and the + * author is asked (reload, overwrite, keep editing), never replaced. * • `controlled_by_parent` rows show the master link (read-only) under the * row's dial. * • There is no external column: `externalSharingModel` is retired by @@ -63,7 +67,14 @@ import { readFields } from '../metadata-admin/previews/object-fields-io.js'; import { t, tFormat, type SupportedLocale } from '../metadata-admin/i18n.js'; import { cbpMasterDetailDefaultsRequired, deriveMasterObject, requireCbpMasterDetails } from './owd-sharing.js'; import { toast } from 'sonner'; -import { STUDIO_SAVE_TOAST_ID, publishSavedItem, useStudioSaveEntry, type StudioSaveResult } from './studioSaveBar.js'; +import { type DraftPin, useDraftSaveGuard } from '../metadata-admin/DraftConflictDialog.js'; +import { + STUDIO_SAVE_TOAST_ID, + publishSavedItem, + publishedPinOfLayers, + useStudioSaveEntry, + type StudioSaveResult, +} from './studioSaveBar.js'; /** @@ -91,6 +102,14 @@ interface OwdRow { hasDraft: boolean; /** True when the package's published list held this object at load (objectui#11799). */ saved: boolean; + /** + * objectstack#36962 — the published row this row was loaded at, the pin its + * Save's publish sends (`publishedPinOfLayers` of the row's layers read). A + * row the published list does not hold has no published row: `If-None-Match: + * *`, unless its layers were read and say otherwise. A layers read that + * failed pins nothing. + */ + published: DraftPin | null; } /** @@ -121,6 +140,17 @@ async function readObjectBodies( return [await readLayered(), draftResp]; } +/** + * objectstack#36962 — the pin a row's Save publishes with, from the load's + * reads ({@link OwdRow.published}): the layers it read, else, for a row the + * published list does not hold (its layers not asked while its draft is in + * hand), no published row. A saved row whose layers read failed pins nothing. + */ +function publishedPinOfRow(layered: unknown, saved: boolean): DraftPin | null { + if (layered) return publishedPinOfLayers(layered); + return saved ? null : { ifNoneMatch: '*' }; +} + /** The user's working value for one row ('' = unset). */ interface OwdEdit { internal: string; @@ -216,6 +246,9 @@ function OwdPicker({ ); } +/** The conflict guard's reload, left to the Save that met the conflict (see the guard's use below). */ +function keepPageOnReload(): void {} + /** The classes a dial takes, at the size the native control had. */ const DIAL = 'h-7 min-w-[13rem] px-2 py-1 text-[12px]'; @@ -255,6 +288,19 @@ export function PackageOwdOverviewPanel({ const [saving, setSaving] = React.useState(false); const [saveError, setSaveError] = React.useState(null); const [prereq, setPrereq] = React.useState(null); + // objectstack#36962 — each row's publish is pinned to the published row its + // load read (`recordPublished`), so a Save over a row another writer + // published since opens the conflict dialog instead of replacing it. The + // guard's own reload is not used: the Save that met the conflict re-reads + // the table itself once its pass ends (`reloadKeeping`), keeping the edits + // of the rows it did not save, which a reload of the whole table would drop. + const { recordPublished, publish: publishPinned, dialog: conflictDialog } = useDraftSaveGuard(client, keepPageOnReload); + // The pin each row's edit was made against: a load that keeps a row's edit + // keeps that row pinned to the read the edit was made on, never the fresh one. + // It is the panel's own record, sent to the guard again before each row's + // publish: a conflict's reload clears every pin the guard holds, for every + // row, while this pass goes on with the rows after it. + const editPinsRef = React.useRef(new Map()); // The master a `controlled_by_parent` row derives under is named by its // label (`ux-conventions.labels-over-machine-names`): this package's own // object as this table reads it (its pending draft included), else the @@ -271,7 +317,8 @@ export function PackageOwdOverviewPanel({ navItemLabelText(findByName(servedMasters, master)?.label as I18nLabel | undefined, locale).trim() || master; - const load = React.useCallback(async () => { + // `keep` names the rows whose edits the caller puts back over this load. + const load = React.useCallback(async (keep?: ReadonlySet) => { setLoaded(false); setLoadError(null); try { @@ -328,10 +375,19 @@ export function PackageOwdOverviewPanel({ ), hasDraft: !!draftBody, saved, + published: publishedPinOfRow(layRaw, saved), }; }), ); built.sort((a, b) => a.label.localeCompare(b.label) || a.name.localeCompare(b.name)); + // objectstack#36962 — each row's publish is pinned to what this load read; + // a kept edit stays pinned to the read it was made on. + for (const r of built) { + const kept = keep?.has(r.name) && editPinsRef.current.has(r.name); + const pin = kept ? (editPinsRef.current.get(r.name) ?? null) : r.published; + editPinsRef.current.set(r.name, pin); + recordPublished('object', r.name, packageId, pin); + } setRows(built); // Reset working edits to the freshly loaded baseline. setEdits(Object.fromEntries(built.map((r) => [r.name, { internal: r.internal }]))); @@ -340,7 +396,7 @@ export function PackageOwdOverviewPanel({ } finally { setLoaded(true); } - }, [client, packageId]); + }, [client, packageId, recordPublished]); React.useEffect(() => { void load(); @@ -406,10 +462,15 @@ export function PackageOwdOverviewPanel({ const refused: Array<{ name: string; label: string; reason: string; edit: OwdEdit }> = []; let saved = 0; const savedNames = new Set(); + // objectstack#36962 — rows whose publish met a newer published row and + // whose author chose to reload it: the row is read back, not kept as edited. + const reloadedNames = new Set(); + const unsavedOf = (rowsChanged: typeof changed) => + rowsChanged.filter((c) => !savedNames.has(c.row.name) && !reloadedNames.has(c.row.name)); // Read the rows back from the server, then put back the edits of the rows // this pass did not save. const reloadKeeping = async (unsaved: ReadonlyArray<{ row: { name: string }; edit: OwdEdit }>) => { - await load(); + await load(new Set(unsaved.map((u) => u.row.name))); if (unsaved.length > 0) { setEdits((prev) => ({ ...prev, ...Object.fromEntries(unsaved.map((u) => [u.row.name, u.edit])) })); } @@ -469,7 +530,7 @@ export function PackageOwdOverviewPanel({ // back before it asks, so it never shows a live row as unsaved, and // Cancel never puts back a value the server no longer holds. Every // row not yet saved keeps its edit for the confirm's second pass. - if (saved > 0) await reloadKeeping(changed.filter((c) => !savedNames.has(c.row.name))); + if (saved > 0 || reloadedNames.size > 0) await reloadKeeping(unsavedOf(changed)); setPrereq({ object: s.row.label, relations, @@ -484,7 +545,26 @@ export function PackageOwdOverviewPanel({ } // A draft that was pending before this save (a copilot's) is published // with it; a refused publish takes back only the draft this save wrote. - const live = await publishSavedItem(client, { type: 'object', name: s.row.name, packageId }, s.row.hasDraft); + // objectstack#36962 — pinned to the published row this row was loaded at, + // sent again here because an earlier row's reload in this pass cleared + // the guard's pins (and a re-read that failed sent none back). + recordPublished('object', s.row.name, packageId, editPinsRef.current.get(s.row.name) ?? null); + const live = await publishSavedItem( + client, + { type: 'object', name: s.row.name, packageId }, + s.row.hasDraft, + publishPinned, + ); + // The published row moved and the author chose to reload it: the row + // is read back once the pass ends, and the pass goes on with the other + // rows, each still pinned to its own load. The draft this Save wrote is + // taken back first, unless a draft was pending before the Save: that + // draft is kept, now carrying the edit, and its next publish stays + // pinned to the version the conflict was raised on (the guard's rule 9). + if (!live.ok && 'reloaded' in live) { + reloadedNames.add(s.row.name); + continue; + } if (!live.ok) { refused.push({ name: s.row.name, label: s.row.label, reason: formatMetadataError(live.error), edit: s.edit }); continue; @@ -495,8 +575,9 @@ export function PackageOwdOverviewPanel({ // A Save leaves no draft: the publish told the ledger behind Changes. // The reload puts every dial back at what the server holds: a refused // row's edit stays on the page, unsaved. - if (saved > 0) await reloadKeeping(changed.filter((c) => !savedNames.has(c.row.name))); - if (refused.length === 0) return { kind: 'saved' }; + if (saved > 0 || reloadedNames.size > 0) await reloadKeeping(unsavedOf(changed)); + // A row the author reloaded was not saved: the Save did not go through as asked. + if (refused.length === 0) return { kind: reloadedNames.size > 0 ? 'declined' : 'saved' }; const reason = refused .map((r) => tFormat('engine.studio.save.refusedLine', locale, { item: r.label, reason: r.reason })) .join('\n'); @@ -505,7 +586,7 @@ export function PackageOwdOverviewPanel({ } finally { setSaving(false); } - }, [rowState, client, packageId, load, locale]); + }, [rowState, client, packageId, load, locale, publishPinned, recordPublished]); // Cancel: every dial back at what the server holds, nothing sent. const doCancel = React.useCallback(() => { @@ -554,6 +635,8 @@ export function PackageOwdOverviewPanel({

{t('engine.studio.owd.description', locale)}

+ {conflictDialog} + !open && setPrereq(null)}>