Skip to content
14 changes: 14 additions & 0 deletions .changeset/34208-row-verdicts-on-first-use.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
'@object-ui/plugin-grid': patch
---

fix(plugin-grid): a list asks for its rows' Edit / Delete verdicts when the row actions are first used, not on first paint

Clause-②: no
Metadata-protocol: none

Every list view with rows sent two `POST /api/v1/security/explain` requests right after its data arrived, one for `update` and one for `delete`, to decide whether each row's menu offers Edit and Delete. Each costs the server tens of milliseconds and dozens of queries, and on a hosted page they were the last layer of the page load. The answer only matters to items a row shows inside its menus, so the grid now asks when a row's actions are first reached for: a pointer or the focus on the actions cell, the row's "⋮" opening, or the row context menu opening. That first use asks once for the whole page, one batch per operation. Opening another row's menu asks nothing new. Once asked, the list keeps the verdicts current as before, on a new page or after a data change.

Until the answer lands, every row keeps the object-level verdict, as it did before the answer arrived. A menu opened in that window shows Edit and Delete inert, so neither can be clicked into a write the answer is about to refuse. An item the answer refuses then goes. A failed or missing answer keeps the object-level verdict.

The exported `RowActionMenu` takes two new optional props: `onActionsDemand`, called when the row's actions are reached for, and `crudVerdictPending`, which shows the built-in Edit or Delete inert. A menu that passes neither behaves as before.
10 changes: 10 additions & 0 deletions content/docs/frontend/plugins/plugin-grid.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,16 @@ caller's **delete** or **update** grant. A closed row removes the button, and
with it the dialog, its undo and its retry. `custom` entries are unchanged.
With no permission provider mounted both read open.

Edit and Delete also fold the server's verdict on each row: `writeScope`,
sharing rules and row-level security can refuse a record the object grant
allows. The grid asks for that verdict (`POST /api/v1/security/explain`, one
batch per operation for the rows on the page) when a row's actions are first
reached for — a pointer or the focus on the actions cell, or a row menu opening
— and never on the list's first paint. Until the answer lands every row keeps
the object-level verdict, and an open menu shows Edit and Delete inert; an item
the answer refuses then goes. A failed or missing answer keeps the object-level
verdict.

### Row callbacks are component props

`onRowClick`, `onRowSelect`, `onCellChange`, `onRowSave`, `onBatchSave`,
Expand Down
10 changes: 10 additions & 0 deletions ui/packages/plugin-grid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,16 @@ const schema: ObjectGridBlockNode = {
};
```

Edit and Delete also fold the server's verdict on each row: `writeScope`,
sharing rules and row-level security can refuse a record the object grant
allows. The grid asks for that verdict (`POST /api/v1/security/explain`, one
batch per operation for the rows on the page) when a row's actions are first
reached for — a pointer or the focus on the actions cell, or a row menu opening
— and never on the list's first paint. Until the answer lands every row keeps
the object-level verdict, and an open menu shows Edit and Delete inert; an item
the answer refuses then goes. A failed or missing answer keeps the object-level
verdict.

### Row callbacks are component props

`onRowClick`, `onRowSelect`, `onCellChange`, `onRowSave`, `onBatchSave`, `onEdit`,
Expand Down
2 changes: 2 additions & 0 deletions ui/packages/plugin-grid/src/ObjectGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,8 @@ export const ObjectGrid: React.FC<ObjectGridComponentProps> = ({
recordIds: pageRecordIds,
update: canEdit || inlineEditable,
delete: canDelete,
// objectstack#34208 — inline cells read `update` while rendering, before any row action is used.
updateOnMount: inlineEditable,
});

// The projection keys and the filter, sort, search and server-paging state,
Expand Down
33 changes: 33 additions & 0 deletions ui/packages/plugin-grid/src/__tests__/askedRecordCrudVerdicts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* ObjectUI
* Copyright (c) 2024-present ObjectStack Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

/**
* `useRecordCrudVerdicts` as a list whose row actions have been reached for.
*
* The hook asks the explain engine nothing until a row-action surface calls
* `requestVerdicts` (objectstack#34208): the first paint of a list sends no
* explain request. The suites that pin what the hook does ONCE it asks — the
* batching, the cap, the cache, the re-checks after a change, the principal
* scope — render it through this, which asks on mount, so each of them keeps
* pinning the behaviour it was written for. What the hook does BEFORE anyone
* asks is pinned where the asking happens: `rowRecordCrudVerdict.test.tsx`.
*/
import * as React from 'react';
import { useRecordCrudVerdicts } from '../hooks/useRecordCrudVerdicts';

export function useAskedRecordCrudVerdicts(
opts: Parameters<typeof useRecordCrudVerdicts>[0],
): ReturnType<typeof useRecordCrudVerdicts> {
const verdicts = useRecordCrudVerdicts(opts);
const { requestVerdicts } = verdicts;
// A repeat call is free, so a new callback identity costs nothing here.
React.useEffect(() => {
requestVerdicts();
}, [requestVerdicts]);
return verdicts;
}
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,27 @@ describe('fail OPEN — no answer keeps the object-level behaviour (objectstack#
expect(await clickOpensEditor(OTHER_ROW.name)).toBe(false);
});
});

/**
* [objectstack#34208] A list asks the record verdicts only once its row actions
* are reached for — but the inline cells read `update` while they render, so an
* inline-editable grid still asks `update` when its rows arrive. `delete` is
* read only by the row menus, and waits for them.
*/
describe('[objectstack#34208] an inline-editable grid asks `update` on first render, `delete` on first use', () => {
it('the first paint asks `update` for the page and nothing else; opening a kebab then asks `delete` once', async () => {
renderGrid();
await settle();
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 50));
});
expect(server.calls.map((call) => call.operation)).toEqual(['update']);
expect([...(server.calls[0].recordIds ?? [])].sort()).toEqual([OTHER_ROW.id, OWN_ROW.id].sort());

expect(await kebabOffersEdit(OWN_ROW.name)).toBe(true);
await act(async () => {
await Promise.allSettled(server.responses);
});
expect(server.calls.map((call) => call.operation)).toEqual(['update', 'delete']);
});
});
151 changes: 149 additions & 2 deletions ui/packages/plugin-grid/src/__tests__/rowRecordCrudVerdict.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { EXPLAIN_BATCH_MAX_RECORD_IDS } from '@objectstack/spec/security';
import { render, screen, waitFor, cleanup, act } from '@testing-library/react';
import { render, screen, waitFor, cleanup, act, fireEvent } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import '@testing-library/jest-dom';
import React from 'react';
Expand Down Expand Up @@ -72,7 +72,7 @@ vi.mock('@object-ui/permissions', async (importOriginal) => {
import { ObjectGrid } from '../ObjectGrid';
import { __clearRecordCrudVerdictCache } from '../hooks/useRecordCrudVerdicts';
import { registerAllFields } from '@object-ui/fields';
import { ActionProvider, SchemaRendererProvider } from '@object-ui/react';
import { ActionProvider, SchemaRendererProvider, notifyDataChanged } from '@object-ui/react';
// READ-ONLY reference to the OTHER surface's real implementation. The agreement
// pin is worth nothing against a local re-statement of the detail header's
// logic: it has to consume the same hook `RecordDetailView` consumes, unchanged
Expand Down Expand Up @@ -257,6 +257,16 @@ async function rowKebab(label: string): Promise<{ edit: boolean; delete: boolean
return answer;
}

/**
* [objectstack#34208] Reach for a row's actions the way a pointer does —
* hover the "⋮" — which is when the grid asks for the record verdicts. The
* first paint asks nothing; every pin below that reads a verdict reaches first.
*/
async function reachRowActions() {
const trigger = screen.getAllByTestId('row-action-trigger')[0];
await userEvent.hover(trigger);
}

beforeEach(() => {
server.reset();
__clearRecordCrudVerdictCache();
Expand All @@ -273,6 +283,7 @@ describe('[#4296] the row kebab folds the record-level verdict', () => {
// Pre-fix this returned `{ edit: true, delete: true }` — the whole card.
renderGrid();
await waitFor(() => expect(screen.getByText(OTHER_ROW.name)).toBeInTheDocument());
await reachRowActions();
await settle(2);
expect(await rowKebab(OTHER_ROW.name)).toEqual({ edit: false, delete: false });
});
Expand All @@ -283,6 +294,7 @@ describe('[#4296] the row kebab folds the record-level verdict', () => {
// wearing a bug fix's clothes.
renderGrid();
await waitFor(() => expect(screen.getByText(OWN_ROW.name)).toBeInTheDocument());
await reachRowActions();
await settle(2);
expect(await rowKebab(OWN_ROW.name)).toEqual({ edit: true, delete: true });
});
Expand All @@ -293,6 +305,7 @@ describe('[#4296] the row kebab folds the record-level verdict', () => {
// ONE decision, so a row with nothing left must not sprout an empty menu.
renderGrid();
await waitFor(() => expect(screen.getByText(OTHER_ROW.name)).toBeInTheDocument());
await reachRowActions();
await settle(2);
const deniedRow = screen.getByText(OTHER_ROW.name).closest('tr');
expect(deniedRow?.querySelector('[data-testid="row-action-trigger"]')).toBeNull();
Expand All @@ -306,6 +319,7 @@ describe('[#4296] the row kebab folds the record-level verdict', () => {
server.verdicts.set(OTHER_ROW.id, { update: false, delete: true });
renderGrid();
await waitFor(() => expect(screen.getByText(OTHER_ROW.name)).toBeInTheDocument());
await reachRowActions();
await settle(2);
expect(await rowKebab(OTHER_ROW.name)).toEqual({ edit: false, delete: true });
});
Expand All @@ -328,6 +342,7 @@ describe('[#4296] kebab answer == detail-header answer, for the same record and
});
await waitFor(() => expect(screen.getByText(OTHER_ROW.name)).toBeInTheDocument());
// 2 batch calls (one per operation, for the page) + 4 singular probes.
await reachRowActions();
await settle(6);

// The detail header's own verdicts, measured first and independently.
Expand Down Expand Up @@ -371,6 +386,7 @@ describe('[#4296] degradation — an unanswered row keeps the object-level verdi
server.ok = false;
renderGrid();
await waitFor(() => expect(screen.getByText(OTHER_ROW.name)).toBeInTheDocument());
await reachRowActions();
await settle(2);
expect(await rowKebab(OTHER_ROW.name)).toEqual({ edit: true, delete: true });
});
Expand All @@ -379,6 +395,7 @@ describe('[#4296] degradation — an unanswered row keeps the object-level verdi
server.network = false;
renderGrid();
await waitFor(() => expect(screen.getByText(OTHER_ROW.name)).toBeInTheDocument());
await reachRowActions();
await settle(2);
expect(await rowKebab(OTHER_ROW.name)).toEqual({ edit: true, delete: true });
});
Expand All @@ -387,6 +404,7 @@ describe('[#4296] degradation — an unanswered row keeps the object-level verdi
server.answersBatch = false;
renderGrid();
await waitFor(() => expect(screen.getByText(OTHER_ROW.name)).toBeInTheDocument());
await reachRowActions();
await settle(2);
expect(await rowKebab(OTHER_ROW.name)).toEqual({ edit: true, delete: true });
});
Expand All @@ -408,6 +426,7 @@ describe('[#4296] degradation — an unanswered row keeps the object-level verdi
server.hold();
renderGrid();
await waitFor(() => expect(screen.getByText(OTHER_ROW.name)).toBeInTheDocument());
await reachRowActions();
await waitFor(() => expect(server.calls.length).toBe(2));
expect(await rowKebab(OTHER_ROW.name)).toEqual({ edit: true, delete: true });
// …and once the verdict lands, the entries go.
Expand Down Expand Up @@ -435,6 +454,7 @@ describe('[#4296] one batched call per (object, operation) per page', () => {
// text is no readiness signal: on this many rows the settled re-render
// outran the wait. The rows being on screen is: count them.
await waitFor(() => expect(container.querySelectorAll('tbody tr')).toHaveLength(rows.length));
await reachRowActions();
await settle(2);

expect(server.calls.length).toBe(2);
Expand Down Expand Up @@ -470,6 +490,7 @@ describe('[#4296] one batched call per (object, operation) per page', () => {
// text is no readiness signal: on this many rows the settled re-render
// outran the wait. The rows being on screen is: count them.
await waitFor(() => expect(container.querySelectorAll('tbody tr')).toHaveLength(rows.length));
await reachRowActions();
await settle(4);

expect(server.calls.length).toBe(4);
Expand Down Expand Up @@ -506,6 +527,7 @@ describe('[#4296] the record layer intersects, never unions', () => {
server.verdicts.set(OTHER_ROW.id, { update: true, delete: true });
renderGrid({ permDelete: false });
await waitFor(() => expect(screen.getByText(OTHER_ROW.name)).toBeInTheDocument());
await reachRowActions();
await settle(1);
expect(await rowKebab(OTHER_ROW.name)).toEqual({ edit: true, delete: false });
expect(server.calls.map((c) => c.operation)).toEqual(['update']);
Expand All @@ -519,3 +541,128 @@ describe('[#4296] the record layer intersects, never unions', () => {
expect(await rowKebab(OTHER_ROW.name)).toEqual({ edit: false, delete: false });
});
});

/**
* [objectstack#34208] The verdicts are asked when a row's actions are reached
* for, never on first paint. Each batch costs the server tens of milliseconds
* and dozens of queries, and the answer only matters to items a row shows
* inside its menus — so the list paints without asking, and the first reach
* for a row's actions asks once for the whole page.
*/
describe('[objectstack#34208] verdicts are asked on first use of the row actions, not on first paint', () => {
/** Let every effect the first paint scheduled run and every read it started land. */
async function firstPaintSettled() {
await waitFor(() => expect(screen.getByText(OTHER_ROW.name)).toBeInTheDocument());
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 50));
});
}

const triggerOf = (label: string) =>
screen.getByText(label).closest('tr')!.querySelector<HTMLElement>('[data-testid="row-action-trigger"]')!;

it('the first paint of a list with rows sends no explain request, and rows keep the object-level verdict', async () => {
renderGrid();
await firstPaintSettled();
expect(server.calls).toEqual([]);
// Fail open meanwhile: the server would refuse this row, nobody asked yet.
const deniedRow = screen.getByText(OTHER_ROW.name).closest('tr');
expect(deniedRow?.querySelector('[data-testid="row-action-trigger"]')).not.toBeNull();
});

it('opening a row\'s "⋮" asks once for the page — one batch per operation — and the open menu narrows to the answer', async () => {
server.hold();
renderGrid();
await firstPaintSettled();
expect(server.calls).toEqual([]);

// Open the menu without a hover first (a keyboard press on the trigger).
triggerOf(OTHER_ROW.name).focus();
await userEvent.keyboard('{Enter}');
await waitFor(() => expect(server.calls).toHaveLength(2));
expect(server.calls.map((c) => c.operation).sort()).toEqual(['delete', 'update']);
for (const call of server.calls) expect(call.recordIds).toEqual(ROWS.map((r) => r.id));

// While the answer is on the wire the items show, inert: neither can be
// clicked into a write the answer is about to refuse.
const edit = screen.getByTestId('row-action-builtin-edit');
const del = screen.getByTestId('row-action-builtin-delete');
expect(edit).toHaveAttribute('aria-disabled', 'true');
expect(del).toHaveAttribute('aria-disabled', 'true');
expect(edit).toHaveAttribute('aria-busy', 'true');

server.release();
await settle(2);
// The answer refuses both, so the open menu has nothing left and the row
// grows no "⋮" (#3562's invariant, unchanged).
await waitFor(() => expect(screen.queryByTestId('row-action-builtin-edit')).toBeNull());
expect(screen.queryByTestId('row-action-builtin-delete')).toBeNull();
expect(screen.getByText(OTHER_ROW.name).closest('tr')?.querySelector('[data-testid="row-action-trigger"]')).toBeNull();
});

it('a second row\'s menu sends nothing new, and a permitted row\'s items are live once answered', async () => {
server.hold();
renderGrid();
await firstPaintSettled();
// The first row's menu, opened while the answer is on the wire.
expect(await rowKebab(OTHER_ROW.name)).toEqual({ edit: true, delete: true });
server.release();
await settle(2);

const ownTrigger = triggerOf(OWN_ROW.name);
await userEvent.click(ownTrigger);
const edit = screen.getByTestId('row-action-builtin-edit');
expect(edit).not.toHaveAttribute('aria-disabled');
expect(edit).not.toHaveAttribute('aria-busy');
await userEvent.keyboard('{Escape}');
expect(server.calls).toHaveLength(2);
});

it('a pointer reaching the actions cell asks before the menu opens, so the answer can be in hand by the click', async () => {
renderGrid();
await firstPaintSettled();
await userEvent.hover(triggerOf(OWN_ROW.name));
await settle(2);
// Answered before any menu opened: the denied row's "⋮" is already gone.
expect(screen.getByText(OTHER_ROW.name).closest('tr')?.querySelector('[data-testid="row-action-trigger"]')).toBeNull();
});

it('the row context menu is a use of the row actions too', async () => {
renderGrid();
await firstPaintSettled();
const cell = screen.getByText(OWN_ROW.name).closest('td')!;
act(() => {
fireEvent.contextMenu(cell, { button: 2, clientX: 40, clientY: 60 });
});
await settle(2);
});

it('a data change before any use asks nothing; after the first use it re-asks the changed row', async () => {
renderGrid();
await firstPaintSettled();
act(() => notifyDataChanged({ objectName: OBJECT, recordId: OTHER_ROW.id }));
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 50));
});
expect(server.calls).toEqual([]);

await reachRowActions();
await settle(2);
act(() => notifyDataChanged({ objectName: OBJECT, recordId: OWN_ROW.id }));
await settle(4);
expect(server.calls.slice(2).map((c) => c.recordIds)).toEqual([[OWN_ROW.id], [OWN_ROW.id]]);
});

it('CONTROL: a non-OK answer to the first use keeps the object-level items, live (fail open)', async () => {
server.ok = false;
renderGrid();
await firstPaintSettled();
await reachRowActions();
await settle(2);
expect(await rowKebab(OTHER_ROW.name)).toEqual({ edit: true, delete: true });
await userEvent.click(triggerOf(OTHER_ROW.name));
expect(screen.getByTestId('row-action-builtin-edit')).not.toHaveAttribute('aria-disabled');
expect(screen.getByTestId('row-action-builtin-delete')).not.toHaveAttribute('aria-disabled');
await userEvent.keyboard('{Escape}');
});
});
Loading
Loading