From d233fa5597303b11eb5f53c2f540297f7fbe781b Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 01:21:07 +0000 Subject: [PATCH 1/6] fix(plugin-grid): ask row CRUD verdicts when the row actions are first used, not on first paint Claude-Session: https://claude.ai/code/session_01ADHkmRSP3ZjQXiwomqs6D6 Co-authored-by: Claude --- .../src/__tests__/askedRecordCrudVerdicts.ts | 33 ++++ .../__tests__/rowRecordCrudVerdict.test.tsx | 147 +++++++++++++++++- .../src/components/RowActionMenu.tsx | 50 +++++- ...ordCrudVerdicts.afterAction-13341.test.tsx | 6 +- .../useRecordCrudVerdicts.batchCap.test.tsx | 13 +- ...seRecordCrudVerdicts.invalidation.test.tsx | 6 +- ...RecordCrudVerdicts.principalScope.test.tsx | 7 +- .../src/hooks/useRecordCrudVerdicts.ts | 139 +++++++++++++---- .../src/objectGridActionsColumn.tsx | 11 ++ 9 files changed, 362 insertions(+), 50 deletions(-) create mode 100644 ui/packages/plugin-grid/src/__tests__/askedRecordCrudVerdicts.ts diff --git a/ui/packages/plugin-grid/src/__tests__/askedRecordCrudVerdicts.ts b/ui/packages/plugin-grid/src/__tests__/askedRecordCrudVerdicts.ts new file mode 100644 index 00000000000..8cbec3667a7 --- /dev/null +++ b/ui/packages/plugin-grid/src/__tests__/askedRecordCrudVerdicts.ts @@ -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[0], +): ReturnType { + 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; +} diff --git a/ui/packages/plugin-grid/src/__tests__/rowRecordCrudVerdict.test.tsx b/ui/packages/plugin-grid/src/__tests__/rowRecordCrudVerdict.test.tsx index 11aed818c43..8b7134027cf 100644 --- a/ui/packages/plugin-grid/src/__tests__/rowRecordCrudVerdict.test.tsx +++ b/ui/packages/plugin-grid/src/__tests__/rowRecordCrudVerdict.test.tsx @@ -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'; @@ -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 @@ -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(); @@ -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 }); }); @@ -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 }); }); @@ -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(); @@ -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 }); }); @@ -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. @@ -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 }); }); @@ -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 }); }); @@ -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 }); }); @@ -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. @@ -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); @@ -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); @@ -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']); @@ -519,3 +541,124 @@ 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('[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 () => { + renderGrid(); + await firstPaintSettled(); + expect(await rowKebab(OTHER_ROW.name)).toEqual({ edit: true, delete: true }); + 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(); + expect(await rowKebab(OTHER_ROW.name)).toEqual({ edit: true, delete: true }); + await settle(2); + 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}'); + }); +}); diff --git a/ui/packages/plugin-grid/src/components/RowActionMenu.tsx b/ui/packages/plugin-grid/src/components/RowActionMenu.tsx index 9976197a404..7768fa7300a 100644 --- a/ui/packages/plugin-grid/src/components/RowActionMenu.tsx +++ b/ui/packages/plugin-grid/src/components/RowActionMenu.tsx @@ -162,6 +162,21 @@ export interface RowActionMenuProps { * `record.owner == os.user.id` can be true (see `toPredicateRecord`). */ objectFields?: unknown; + /** + * This row's actions are about to be used (objectstack#34208): called when a + * pointer or the focus reaches the row's actions cell, when its "⋮" opens, + * and when any menu holding its items mounts (the row context menu among + * them). The grid asks the record-grained Edit / Delete verdicts only from + * here, never on first paint; a call after the first is free. + */ + onActionsDemand?: () => void; + /** + * The record-grained verdict for the built-in item is on its way + * (objectstack#34208). The item still shows — the object-level verdict + * stands until the answer lands — but inert, so it cannot be clicked into a + * write the answer is about to refuse. + */ + crudVerdictPending?: { edit?: boolean; delete?: boolean }; } // --------------------------------------------------------------------------- @@ -467,7 +482,9 @@ export const BuiltinRowActionItem: React.FC<{ className?: string; objectFields?: unknown; onSelect: (row: any) => void; -}> = ({ name, predicates, row, icon, label, className, objectFields, onSelect }) => { + /** The record-grained verdict is on its way: shown, but inert (objectstack#34208). */ + verdictPending?: boolean; +}> = ({ name, predicates, row, icon, label, className, objectFields, onSelect, verdictPending = false }) => { // `visibleWhen` is read through the shared `isBuiltinRowActionVisible`, the // same function the "⋮" guard counts with — see objectui#3562. const scope = usePredicateScope(); @@ -482,10 +499,11 @@ export const BuiltinRowActionItem: React.FC<{ fields: objectFields, }); if (!isVisible) return null; - const disabled = predicates?.disabledWhen != null && isDisabled; + const disabled = verdictPending || (predicates?.disabledWhen != null && isDisabled); return ( { if (!disabled) onSelect(row); }} data-testid={`row-action-builtin-${name}`} className={className} @@ -672,7 +690,17 @@ export const RowActionMenuItems: React.FC<{ props: RowActionMenuProps; }> = ({ plan, props }) => { const t = useRowActionTranslation(); - const { row, editPredicates, deletePredicates, objectFields, onEdit, onDelete, onAction, onActionDef } = props; + const { + row, editPredicates, deletePredicates, objectFields, onEdit, onDelete, onAction, onActionDef, crudVerdictPending, + } = props; + // [objectstack#34208] A menu showing this row's items is the row's actions + // in use — whichever menu hosts them. Read through a ref so the call is made + // once per mount, whatever identity the callback arrives with. + const demandRef = React.useRef(props.onActionsDemand); + demandRef.current = props.onActionsDemand; + React.useEffect(() => { + demandRef.current?.(); + }, []); return ( <> {/* Rendered off the PLAN, the same object the trigger's existence @@ -687,6 +715,7 @@ export const RowActionMenuItems: React.FC<{ icon={} label={t('grid.edit')} objectFields={objectFields} + verdictPending={!!crudVerdictPending?.edit} onSelect={(r) => onEdit?.(r)} /> )} @@ -698,6 +727,7 @@ export const RowActionMenuItems: React.FC<{ icon={} label={t('grid.delete')} objectFields={objectFields} + verdictPending={!!crudVerdictPending?.delete} onSelect={(r) => onDelete?.(r)} /> )} @@ -724,7 +754,7 @@ export const RowActionMenuItems: React.FC<{ }; export const RowActionMenu: React.FC = (props) => { - const { row, objectFields, onActionDef, maxInlineActions = 1 } = props; + const { row, objectFields, onActionDef, onActionsDemand, maxInlineActions = 1 } = props; const t = useRowActionTranslation(); // The one resolver both of this row's menus read (objectstack#28434). const plan = useRowActionPlan(props, maxInlineActions); @@ -735,7 +765,15 @@ export const RowActionMenu: React.FC = (props) => { // a grid with no actions at all has always produced. const hasMenu = plan.menuCount > 0; return ( -
e.stopPropagation()}> +
e.stopPropagation()} + // [objectstack#34208] Reaching the cell is the earliest sign the row's + // actions are wanted: asking here usually lands the answer before the + // "⋮" opens. Focus covers the keyboard, pointer covers mouse and touch. + onPointerEnter={onActionsDemand} + onFocus={onActionsDemand} + > {plan.inline.map(def => ( = (props) => { /> ))} {hasMenu && ( - + { if (open) onActionsDemand?.(); }}>