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
14 changes: 14 additions & 0 deletions .changeset/37231-grid-master-held-hide-delete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
'@object-ui/plugin-grid': patch
---

fix(plugin-grid): the row menu offers no Delete on a line whose master is held

Clause-②: no
Metadata-protocol: none

The grid row menu reads the batch approval-lock door (`POST /api/v1/approvals/records/:object/locks`) when a row's actions are reached for, and a row the door answers as locked offers no Edit. The door also says which approval holds the row, in `lockSource`: `'record'` (its own approval) or `'master'` (for a master-detail line, its master's approval). The menu read only the boolean, so a line whose master is held kept Delete, and the server refused that delete with `RECORD_LOCKED`.

The menu now reads `lockSource`. A row answered `lockSource: 'master'` offers neither Edit nor Delete. A row answered `lockSource: 'record'` offers no Edit and keeps Delete, as before: the server deletes such a record and cancels its request. The list page's single-row selection bar reads the same row props, so it follows. A 501, a `null` entry, a failed read or a read not yet asked leaves the menu as it was.

A grid whose rows offer Delete but no Edit now also reads the locks when a row's actions are reached for, so the rule holds there too. The read is still never sent on first paint.
14 changes: 8 additions & 6 deletions ui/packages/plugin-grid/src/ObjectGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1489,12 +1489,14 @@ export const ObjectGrid: React.FC<ObjectGridComponentProps> = ({
// objectstack#36911 — the row menu reads the same locks: a row an approval
// locks offers no Edit, as the record page offers none (objectstack#35593).
// Delete stays for a record held by its own approval: the server deletes it
// and cancels its request. (The batch read reports only that case today, not
// a line held because its master is held, which refuses delete too; #37041.) So the hook also runs on a grid whose rows offer Edit. It still
// and cancels its request. objectstack#37231 — a master-detail line held
// because its MASTER is held (the read's `lockSource: 'master'`) offers no
// Delete either: the detail-row lock hook refuses it with `RECORD_LOCKED`.
// So the hook also runs on a grid whose rows offer Edit or Delete. It still
// reads nothing until it is asked: by a cell being selected, or by a row's
// actions being reached for (`demandRowActions` below).
const rowMenuOffersEdit = canEdit && !!onEdit;
const approvalLocks = useRowApprovalLocks(hostCellSelection || rowMenuOffersEdit ? objectName : undefined);
const rowMenuReadsLocks = (canEdit && !!onEdit) || (canDelete && !!onDelete);
const approvalLocks = useRowApprovalLocks(hostCellSelection || rowMenuReadsLocks ? objectName : undefined);
/**
* objectstack#36911 — what reaching for a row's actions asks
* (`RowActionMenuProps.onActionsDemand`, objectstack#34208): the
Expand All @@ -1506,8 +1508,8 @@ export const ObjectGrid: React.FC<ObjectGridComponentProps> = ({
const probeApprovalLocks = approvalLocks.probe;
const demandRowActions = useCallback(() => {
requestRecordVerdicts();
if (rowMenuOffersEdit) probeApprovalLocks(pageRecordIds);
}, [requestRecordVerdicts, probeApprovalLocks, rowMenuOffersEdit, pageRecordIds]);
if (rowMenuReadsLocks) probeApprovalLocks(pageRecordIds);
}, [requestRecordVerdicts, probeApprovalLocks, rowMenuReadsLocks, pageRecordIds]);
// objectstack#35956 — a cell change that makes other fields required asks
// for them first, and saves them with it (`./objectGridRequiredPrompt`).
const fieldLabelOf = (field: string): string => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
* are reached for, never on first paint (objectstack#35795). Only an ANSWERED
* lock hides Edit: a 501, a `null` entry, a failed read or a read not yet
* asked leaves the menu as it was.
*
* objectstack#37231 — the read says which approval holds a locked row
* (`lockSource`). A master-detail line held because its MASTER is held
* (`'master'`) offers no Delete either: the detail-row lock hook refuses that
* delete with `RECORD_LOCKED`. A row held by its own approval (`'record'`)
* keeps Delete.
*/

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
Expand Down Expand Up @@ -60,10 +66,15 @@ const LOCKS_PATH = `/api/v1/approvals/records/${OBJECT}/locks`;
const LOCKED_ROW = { id: 'p_locked', name: 'Data Centre Build' };
/** Its sibling on the same page, with no approval running. */
const OPEN_ROW = { id: 'p_open', name: 'Office Refresh' };
/** A master-detail line whose MASTER is under a locking approval. */
const MASTER_HELD_ROW = { id: 'p_line', name: 'Rack Delivery' };

/** What the lock door answers, per id; `undefined` is the read's `null` entry. */
/**
* What the lock door answers, per id; `undefined` is the read's `null` entry.
* `sources` is the `lockSource` a locked entry carries.
*/
type LockDoor =
| { kind: 'answers'; locked: Record<string, boolean | null> }
| { kind: 'answers'; locked: Record<string, boolean | null>; sources?: Record<string, 'record' | 'master'> }
| { kind: 'status'; status: number }
| { kind: 'network' };

Expand All @@ -82,7 +93,11 @@ const server = {
reset() {
server.gate = null;
server.release = () => {};
server.door = { kind: 'answers', locked: { [LOCKED_ROW.id]: true, [OPEN_ROW.id]: false } };
server.door = {
kind: 'answers',
locked: { [LOCKED_ROW.id]: true, [OPEN_ROW.id]: false, [MASTER_HELD_ROW.id]: true },
sources: { [LOCKED_ROW.id]: 'record', [MASTER_HELD_ROW.id]: 'master' },
};
server.lockCalls = [];
server.responses = [];
},
Expand Down Expand Up @@ -110,7 +125,13 @@ const apiFetch = async (input: RequestInfo | URL, init?: RequestInit): Promise<R
const ids: string[] = body.recordIds ?? [];
return respond(200, {
success: true,
data: { records: ids.map((recordId) => ({ recordId, locked: door.locked[recordId] ?? null })) },
data: {
records: ids.map((recordId) => ({
recordId,
locked: door.locked[recordId] ?? null,
...(door.sources?.[recordId] ? { lockSource: door.sources[recordId] } : {}),
})),
},
});
}
// `security/explain`: every row may be written, so only the lock can take Edit away.
Expand All @@ -134,7 +155,7 @@ const PageBar: React.FC = () => {

function renderGrid(
rows: Array<Record<string, unknown>> = [LOCKED_ROW, OPEN_ROW],
{ listPage = false }: { listPage?: boolean } = {},
{ listPage = false, edit = true }: { listPage?: boolean; edit?: boolean } = {},
) {
const dataSource: any = {
getObjectSchema: async (name: string) => ({
Expand All @@ -155,7 +176,7 @@ function renderGrid(
<SchemaRendererProvider dataSource={dataSource} apiFetch={apiFetch}>
<div data-testid="page-column">
{listPage && <PageBar />}
<ObjectGrid schema={schema} dataSource={dataSource} onEdit={() => {}} onDelete={() => {}} />
<ObjectGrid schema={schema} dataSource={dataSource} onEdit={edit ? () => {} : undefined} onDelete={() => {}} />
</div>
</SchemaRendererProvider>
</ActionProvider>,
Expand Down Expand Up @@ -224,35 +245,42 @@ describe('objectstack#36911 — the row menu follows the approval lock', () => {

it('a 501 (no approvals service) leaves the menu as it was, even over an approval_status column', async () => {
server.door = { kind: 'status', status: 501 };
renderGrid([{ ...LOCKED_ROW, approval_status: 'pending' }, OPEN_ROW]);
renderGrid([{ ...LOCKED_ROW, approval_status: 'pending' }, OPEN_ROW, MASTER_HELD_ROW]);
await waitFor(() => expect(screen.getByText(LOCKED_ROW.name)).toBeInTheDocument());
await reachRowActions();
await waitFor(() => expect(server.lockCalls).toHaveLength(1));
await settleReads();
expect(await rowMenu(LOCKED_ROW.name)).toEqual({ edit: true, delete: true });
expect(await rowMenu(OPEN_ROW.name)).toEqual({ edit: true, delete: true });
expect(await rowMenu(MASTER_HELD_ROW.name)).toEqual({ edit: true, delete: true });
});

it('a null entry (a row the reader cannot read) leaves Edit where it was', async () => {
server.door = { kind: 'answers', locked: { [LOCKED_ROW.id]: null, [OPEN_ROW.id]: false } };
renderGrid();
server.door = {
kind: 'answers',
locked: { [LOCKED_ROW.id]: null, [OPEN_ROW.id]: false, [MASTER_HELD_ROW.id]: null },
};
renderGrid([LOCKED_ROW, OPEN_ROW, MASTER_HELD_ROW]);
await waitFor(() => expect(screen.getByText(LOCKED_ROW.name)).toBeInTheDocument());
await reachRowActions();
await waitFor(() => expect(server.lockCalls).toHaveLength(1));
await settleReads();
expect(await rowMenu(LOCKED_ROW.name)).toEqual({ edit: true, delete: true });
expect(await rowMenu(MASTER_HELD_ROW.name)).toEqual({ edit: true, delete: true });
});

it('a read that fails, or that never answered, leaves Edit where it was', async () => {
server.door = { kind: 'network' };
renderGrid();
renderGrid([LOCKED_ROW, OPEN_ROW, MASTER_HELD_ROW]);
await waitFor(() => expect(screen.getByText(LOCKED_ROW.name)).toBeInTheDocument());
// Not asked yet: the menu is the one it always was.
expect(server.lockCalls).toHaveLength(0);
expect(await rowMenu(MASTER_HELD_ROW.name)).toEqual({ edit: true, delete: true });
await reachRowActions();
await waitFor(() => expect(server.lockCalls).toHaveLength(1));
await settleReads();
expect(await rowMenu(LOCKED_ROW.name)).toEqual({ edit: true, delete: true });
expect(await rowMenu(MASTER_HELD_ROW.name)).toEqual({ edit: true, delete: true });
});
});

Expand Down Expand Up @@ -317,3 +345,39 @@ describe('objectstack#36911 — the read in flight', () => {
expect(screen.getAllByTestId('row-action-builtin-delete').length).toBeGreaterThan(0);
});
});

describe('objectstack#37231 — a line whose master is held offers no Delete', () => {
it('a master-held line offers neither Edit nor Delete; a record-held sibling offers no Edit and keeps Delete', async () => {
renderGrid([LOCKED_ROW, OPEN_ROW, MASTER_HELD_ROW]);
await waitFor(() => expect(screen.getByText(MASTER_HELD_ROW.name)).toBeInTheDocument());
await reachRowActions();
await waitFor(() => expect(server.lockCalls).toHaveLength(1));
await settleReads();
// CONTROL: the open sibling offers both, so the pins observe the lock.
expect(await rowMenu(OPEN_ROW.name)).toEqual({ edit: true, delete: true });
await waitFor(async () => expect(await rowMenu(MASTER_HELD_ROW.name)).toEqual({ edit: false, delete: false }));
expect(await rowMenu(LOCKED_ROW.name)).toEqual({ edit: false, delete: true });
});

it('a grid whose rows offer Delete but no Edit still reads the lock, and drops Delete on the master-held line', async () => {
renderGrid([OPEN_ROW, MASTER_HELD_ROW], { edit: false });
await waitFor(() => expect(screen.getByText(MASTER_HELD_ROW.name)).toBeInTheDocument());
expect(await rowMenu(OPEN_ROW.name)).toEqual({ edit: false, delete: true });
await waitFor(() => expect(server.lockCalls).toHaveLength(1));
await settleReads();
await waitFor(() => expect(screen.getByText(MASTER_HELD_ROW.name).closest('tr')
?.querySelector('[data-testid="row-action-trigger"]') ?? null).toBeNull());
expect(await rowMenu(MASTER_HELD_ROW.name)).toEqual({ edit: false, delete: false });
expect(await rowMenu(OPEN_ROW.name)).toEqual({ edit: false, delete: true });
});

it('the list page selection bar: a selected master-held line offers neither; a selected record-held sibling keeps Delete', async () => {
renderGrid([LOCKED_ROW, OPEN_ROW, MASTER_HELD_ROW], { listPage: true });
await waitFor(() => expect(screen.getByText(MASTER_HELD_ROW.name)).toBeInTheDocument());
expect(await selectionBarOffers(OPEN_ROW.name)).toEqual({ edit: true, delete: true });
expect(server.lockCalls).toHaveLength(1);
expect(await selectionBarOffers(MASTER_HELD_ROW.name)).toEqual({ edit: false, delete: false });
expect(await selectionBarOffers(LOCKED_ROW.name)).toEqual({ edit: false, delete: true });
expect(server.lockCalls).toHaveLength(1);
});
});
35 changes: 32 additions & 3 deletions ui/packages/plugin-grid/src/hooks/useRowApprovalLocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@
* grid already says of the selected cell (its `aria-readonly`) follows the
* lock without waiting for the reader to move: `isLocked`, and the value this
* hook returns, are new exactly when the answers it reads changed.
*
* The door also says WHICH approval holds a locked row (`lockSource`): its own
* (`'record'`), or, for a master-detail line, its master's (`'master'`, which
* wins when both hold). `heldByMaster` reads that bit for the row menu: the
* server deletes a record held by its own approval and cancels the request,
* but the detail-row lock hook refuses a line's delete while its master is
* held (objectstack#37231).
*/
import * as React from 'react';
import { APPROVAL_RECORD_LOCKS_MAX_RECORD_IDS } from '@objectstack/spec/contracts';
Expand All @@ -53,7 +60,10 @@ export function __resetApprovalsServiceLatch(): void {
* back to its `approval_status` column (fail open: the server still refuses a
* write to a locked record).
*/
type LockAnswer = { locked: boolean } | 'asking' | 'unanswered';
type LockAnswer = { locked: boolean; lockSource?: ApprovalLockSource } | 'asking' | 'unanswered';

/** Which approval holds a locked row: its own, or its master's (a master-detail line). */
export type ApprovalLockSource = 'record' | 'master';

export interface RowApprovalLocks {
/**
Expand All @@ -63,6 +73,13 @@ export interface RowApprovalLocks {
probe: (recordIds: readonly unknown[]) => void;
/** Whether the row is locked by an approval, from what has answered so far. */
isLocked: (recordId: unknown, row: any) => boolean;
/**
* Whether a read ANSWERED the row as held by its master's approval
* (`lockSource: 'master'`). Only an answer says so: a read refused (501),
* failed, answering `null`, or not yet asked answers `false`, and so does a
* row held by its own approval.
*/
heldByMaster: (recordId: unknown) => boolean;
/** The row's lock read while it is on the wire (it settles when it answers or fails), else `null`. */
settling: (recordId: unknown) => Promise<void> | null;
}
Expand Down Expand Up @@ -121,7 +138,10 @@ export function useRowApprovalLocks(objectName: string | undefined): RowApproval
chunk.forEach((id, index) => {
const entry = records[index];
if (entry && entry.recordId === id && typeof entry.locked === 'boolean') {
answered.set(id, { locked: entry.locked });
const lockSource = entry.locked && (entry.lockSource === 'record' || entry.lockSource === 'master')
? entry.lockSource as ApprovalLockSource
: undefined;
answered.set(id, lockSource ? { locked: entry.locked, lockSource } : { locked: entry.locked });
}
});
}
Expand Down Expand Up @@ -154,10 +174,19 @@ export function useRowApprovalLocks(objectName: string | undefined): RowApproval
}).locked;
}, [answerSeq]);

const heldByMaster = React.useCallback((recordId: unknown) => {
void answerSeq;
const answer = recordId == null ? undefined : answersRef.current.get(String(recordId));
return typeof answer === 'object' && answer.locked && answer.lockSource === 'master';
}, [answerSeq]);

const settling = React.useCallback(
(recordId: unknown) => (recordId == null ? null : wiresRef.current.get(String(recordId)) ?? null),
[],
);

return React.useMemo(() => ({ probe, isLocked, settling }), [probe, isLocked, settling]);
return React.useMemo(
() => ({ probe, isLocked, heldByMaster, settling }),
[probe, isLocked, heldByMaster, settling],
);
}
29 changes: 20 additions & 9 deletions ui/packages/plugin-grid/src/objectGridActionsColumn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ export function resolveObjectGridActionsColumn({
/**
* objectstack#36911 — the approval locks of the rows on screen, the read the
* select-first cells ask (`useRowApprovalLocks`). A row a read ANSWERED as
* locked offers no Edit. Delete is untouched, which is right only while the
* read answers for a record held by ITS OWN approval: the server deletes such
* a record and cancels its request. A line held because its MASTER is held
* refuses delete too; if the read ever reports those lines, it must tell
* the two apart before this menu reads it (#37041).
* locked offers no Edit. objectstack#37231 — the read says which approval
* holds it (`lockSource`): a record held by ITS OWN approval keeps Delete,
* because the server deletes such a record and cancels its request; a
* master-detail line held because its MASTER is held (`lockSource:
* 'master'`) offers no Delete either, because the detail-row lock hook
* refuses that delete with `RECORD_LOCKED`.
*/
approvalLocks: RowApprovalLocks;
/**
Expand Down Expand Up @@ -103,16 +104,23 @@ export function resolveObjectGridActionsColumn({
// NON-AUTHOR SURFACE — see the `maxInlineActions` prop below.
const maxInlineRowActions: number = foldInlineRowActions ? 0 : ((schema as any).maxInlineRowActions ?? 1);
/**
* objectstack#36911 — is this row held by its own locking approval, by a lock
* read that ANSWERED? (The batch read answers for the record's own approval
* only, not for a line under a held master; see `approvalLocks` above.) The row is handed to the hook as no row at all, so its
* objectstack#36911 — is this row held by a locking approval (its own, or,
* for a master-detail line, its master's), by a lock read that ANSWERED? The
* row is handed to the hook as no row at all, so its
* `approval_status` column does not stand in: a read refused (501), failed,
* answering `null` (a row the reader cannot read) or not yet asked leaves
* Edit where the menu had it. An answer arriving re-renders the grid. The
* select-first cells read the same hook with the column fallback; the menu
* offers what the server is known to refuse, never less on a guess.
*/
const approvalLockAnswered = (row: any): boolean => approvalLocks.isLocked(rowRecordId(row), null);
/**
* objectstack#37231 — and is the lock its MASTER's (`lockSource: 'master'`)?
* Then Delete goes too: the server refuses a line's delete while its master
* is held. A row held by its own approval keeps Delete; only an answer takes
* it away, as above.
*/
const heldByMasterAnswered = (row: any): boolean => approvalLocks.heldByMaster(rowRecordId(row));
const rowCrudVerdicts = (row: any) => {
// [objectui#8674] The CALLER's answer for this one row, ANDed in below.
const rowOps = rowOperations?.(row);
Expand All @@ -121,7 +129,10 @@ export function resolveObjectGridActionsColumn({
canEdit && rowOps?.update !== false && !approvalLockAnswered(row),
recordVerdict(rowRecordId(row), 'update'),
),
canDelete: resolveRowRecordCrudAffordance(canDelete && rowOps?.delete !== false, recordVerdict(rowRecordId(row), 'delete')),
canDelete: resolveRowRecordCrudAffordance(
canDelete && rowOps?.delete !== false && !heldByMasterAnswered(row),
recordVerdict(rowRecordId(row), 'delete'),
),
};
};
const rowOffersAction = (row: any): boolean => {
Expand Down
Loading