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
13 changes: 13 additions & 0 deletions .changeset/33987-header-predicates-pending-record.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
'@object-ui/components': patch
'@object-ui/console': patch
---

fix(components): record page header-action predicates wait for the record instead of reporting "No such key" for fields the object has

Clause-②: no
Metadata-protocol: none

A record page binds its record id at once and its row only when the read answers. Until then the `page:header` actions evaluated their `visible`, `hidden`, `disabled` and `pressed` predicates against an empty record, so every `record.<field>` predicate faulted, and the console said "A conditional predicate failed to evaluate … No such key: status … Check the field names and CEL" once per action on every page load, for a field the object has. Nothing was wrong with the metadata.

A record that is bound but not loaded is now its own state. Each predicate answers its key's safe default (hidden, not hidden, disabled, not pressed) and reports nothing; a `disabled` action does not claim it is "not available for this record" while there is no record yet. Once the record arrives the predicates run as before, and the warning still fires when a key is really missing from the loaded record. A header with no record bound at all evaluates as before.
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,103 @@ describe('page:header — fail-closed stays fail-closed, and says so once (#3521
}
});
});

describe('page:header — a record still loading is its own state, not an empty record (objectstack#33987)', () => {
// The record page binds its record id at once and hands `data: null` until
// the read answers. Evaluating then meant evaluating against an empty
// record: every `record.<field>` predicate faulted with "No such key", and
// the warn-once report told the author to check the field names of a field
// the object HAS — on every load, before the record arrived.
const faultsFor = (warn: ReturnType<typeof vi.spyOn>, name: string) =>
warn.mock.calls.filter(
(c: unknown[]) => String(c[0]).includes('failed to evaluate') && String(c[0]).includes(`"${name}"`),
);

function headerTree(action: Record<string, unknown>, recordId: string | null, data: any) {
return (
<PredicateScopeProvider scope={SCOPE}>
<ActionProvider>
<RecordContextProvider
objectName="showcase_field_zoo"
recordId={recordId}
data={data}
objectSchema={OBJECT_SCHEMA}
>
<PageHeader
schema={{
type: 'page:header',
title: 'Specimen',
actions: [{ locations: ['record_header'], label: 'Zoo Action', type: 'api', ...action }],
}}
/>
</RecordContextProvider>
</ActionProvider>
</PredicateScopeProvider>
);
}

let warn: ReturnType<typeof vi.spyOn>;
beforeEach(() => {
warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
return () => warn.mockRestore();
});

it('pending: no fault report; once the record arrives the predicate is honoured, still with no report', () => {
const action = { name: 'zoo_pending_visible_33987', visible: 'record.f_status == "open"' };
const { rerender } = render(headerTree(action, 'r1', null));
// Pending answers the key's own fallback (hidden), and says nothing.
expect(shown()).toBe(false);
expect(faultsFor(warn, 'zoo_pending_visible_33987')).toHaveLength(0);

rerender(headerTree(action, 'r1', RECORD));
expect(shown()).toBe(true);
expect(faultsFor(warn, 'zoo_pending_visible_33987')).toHaveLength(0);
});

it('the loaded verdict is the predicate’s own in the negative direction too', () => {
const action = { name: 'zoo_pending_negative_33987', visible: 'record.f_status == "open"' };
const { rerender } = render(headerTree(action, 'r1', undefined));
rerender(headerTree(action, 'r1', { ...RECORD, f_status: 'closed' }));
expect(shown()).toBe(false);
expect(faultsFor(warn, 'zoo_pending_negative_33987')).toHaveLength(0);
});

it('pending: `hidden`, `disabled` and `pressed` report nothing either, and `disabled` claims no reason about the record', () => {
const action = {
name: 'zoo_pending_legs_33987',
hidden: 'record.f_status == "closed"',
disabled: 'record.f_status == "closed"',
pressed: 'record.f_status == "open"',
};
const { rerender } = render(headerTree(action, 'r1', null));
// Each leg answers its own fallback: rendered, disabled, not pressed.
expect(shown()).toBe(true);
expect(button()).toBeDisabled();
expect(button()).toHaveAttribute('aria-pressed', 'false');
expect(button()).not.toHaveAttribute('aria-describedby');
expect(faultsFor(warn, 'zoo_pending_legs_33987')).toHaveLength(0);

rerender(headerTree(action, 'r1', RECORD));
expect(shown()).toBe(true);
expect(button()).not.toBeDisabled();
expect(button()).toHaveAttribute('aria-pressed', 'true');
expect(faultsFor(warn, 'zoo_pending_legs_33987')).toHaveLength(0);
});

it('CONTROL: a LOADED record really missing the key still faults, fails closed and says so once', () => {
const { f_status: _omitted, ...withoutStatus } = RECORD;
const action = { name: 'zoo_loaded_missing_33987', visible: 'record.f_status == "open"' };
render(headerTree(action, 'r1', withoutStatus));
expect(shown()).toBe(false);
const faults = faultsFor(warn, 'zoo_loaded_missing_33987');
expect(faults).toHaveLength(1);
expect(String(faults[0][0])).toContain('f_status');
});

it('CONTROL: a header with no record bound (no id) still evaluates, so a `record.*` predicate there faults and says so', () => {
const action = { name: 'zoo_unbound_33987', visible: 'record.f_status == "open"' };
render(headerTree(action, null, undefined));
expect(shown()).toBe(false);
expect(faultsFor(warn, 'zoo_unbound_33987')).toHaveLength(1);
});
});
38 changes: 30 additions & 8 deletions ui/packages/components/src/renderers/layout/containers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2053,6 +2053,15 @@ export const PageHeaderActions: React.FC<PageHeaderActionsProps> = ({
// `warnOnError: true`), so a broken predicate hides its button here exactly
// as it does on the other surfaces, and says so once.
const headerPredicateFields = headerObjectSchema?.fields;
// The record this header is ABOUT has not arrived yet: a record is bound (the
// host named its id) and its row is still absent — `RecordContextValue.data`
// is "undefined while loading", and the record page hands `null` until its
// read answers. That is its own state, not an empty record: an empty record
// makes every `record.<field>` predicate fault, which is an authoring error
// only when the key is missing from a record that DID load. A header with no
// record bound at all (no context, or no id) keeps evaluating as before, so a
// `record.*` predicate there still faults and says so.
const headerRecordPending = ctx != null && ctx.recordId != null && ctx.data == null;
const headerPredicateScope = React.useMemo(() => {
const scopeUser = (predicateScope as any)?.user;
// Relation fields bind as the FOREIGN KEY the server stores, not as the
Expand Down Expand Up @@ -2104,17 +2113,27 @@ export const PageHeaderActions: React.FC<PageHeaderActionsProps> = ({
* share `visible`'s `false`, which on this key means ENABLED — so
* `disabled: !current_user.can(…)` left the action pressable until the
* payload arrived, the one leg of this surface that failed open.
*
* While the record is PENDING (see `headerRecordPending`) nothing is
* evaluated: every predicate answers its `fallback` and reports nothing.
* Evaluating then meant evaluating against an empty record, where every
* `record.<field>` faults with "No such key" — and the fault report, which
* is warn-once per (label, predicate), told the author to "check the field
* names" of a field the object has, on every load, before the record it
* names had arrived. The predicate runs, and reports, once the record does.
*/
const evalHeaderPredicate = React.useCallback(
(pred: unknown, label: string, fallback: boolean): boolean =>
evalRowPredicate(pred as never, ctx?.data ?? {}, {
fallback,
scope: headerPredicateScope,
fields: headerPredicateFields,
warnOnError: true,
label,
}),
[ctx?.data, headerPredicateScope, headerPredicateFields],
headerRecordPending
? fallback
: evalRowPredicate(pred as never, ctx?.data ?? {}, {
fallback,
scope: headerPredicateScope,
fields: headerPredicateFields,
warnOnError: true,
label,
}),
[ctx?.data, headerRecordPending, headerPredicateScope, headerPredicateFields],
);

const headerActionPlan = React.useMemo<{ actions: any[]; authoredKeys: ReadonlySet<string> }>(() => {
Expand Down Expand Up @@ -2557,6 +2576,9 @@ export const PageHeaderActions: React.FC<PageHeaderActionsProps> = ({
const disabledReasonFor = (action: any): string | undefined => {
const key = (action?.name || action?.id || '') as string;
if (key === '' || !authoredHeaderActionKeys.has(key)) return undefined;
// A pending record answers `disabled`'s fallback (closed), which is not
// a fact about the record — so it is not explained as one.
if (headerRecordPending) return undefined;
if (!resolveDisabled(action?.disabled, action?.name ?? action?.id)) return undefined;
const declared = typeof action?.disabledReason === 'string' ? action.disabledReason : '';
return declared.trim() !== ''
Expand Down
Loading