Skip to content

Commit c7a7443

Browse files
committed
fix(plugin-detail): the record page's inline edit reads the stored row's field locks
record:details and record:highlights wrap their bodies in the record field projection scope, so a when-conditioned field lock disables a field only on the rows the server's write gate refuses. Claude-Session: https://claude.ai/code/session_01JQwxbZ6ALxKhRMeuJrvUeU Co-authored-by: Claude <noreply@anthropic.com>
1 parent b322b5d commit c7a7443

3 files changed

Lines changed: 129 additions & 15 deletions

File tree

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* ObjectUI
3+
* Copyright (c) 2024-present ObjectStack Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
/**
10+
* The record a detail body edits in place answers its own field locks
11+
* (objectstack#27146, the residual of objectstack#26594 ruling B).
12+
*
13+
* `DetailSection` and `HeaderHighlight` ask a field's in-place edit of the
14+
* `recordEdit` row (`resolveFieldAffordance`), which reads `usePermissions()`.
15+
* Under the page's provider alone that is `/auth/me/permissions`' answer, which
16+
* holds no row: a field permission whose `when` names only some rows is served
17+
* there as its lock, and the field was disabled on every row, the draft the
18+
* server lets the caller write included. Below `RecordFieldAccessScope`
19+
* (`@object-ui/permissions`) the same call answers from the server's
20+
* projection for THIS stored row (`DataSource.getRecordFieldAccess`, the
21+
* `GET /data/{object}/{id}/field-access` route) — the one source the record
22+
* form's edit dialog asks. Nothing here evaluates a `when`.
23+
*
24+
* Mounted by the two record blocks that edit the page's record in place,
25+
* `record:details` and `record:highlights`, around their bodies only. They are
26+
* sibling blocks of the page schema with no ancestor in this package, and a
27+
* mount above them (the page host) would also cover the page's action dialogs
28+
* and related lists, where a form editing ANOTHER row of the same object would
29+
* read this row's answer until its own arrives. So each block asks for itself:
30+
* a page drawing both asks twice.
31+
*
32+
* It asks only when the block's in-place edit is on: the page's inline-edit
33+
* session may edit (`canEdit`), the block offers it (`enabled`), the record is
34+
* stored (an id) and the data source carries the method. Otherwise the
35+
* row-blind answer stands, as it does for a non-ObjectStack backend.
36+
*
37+
* The answer belongs to the stored row, so it is asked again whenever the row
38+
* may have moved: a write the data source reports for this record (the inline
39+
* save bar's included) and a change on the data-invalidation bus for it (the
40+
* signal the page re-reads its record on). These are the triggers
41+
* `RecordFieldAccessForForm` (`@object-ui/plugin-form`) holds; that wrapper is
42+
* not exported and this package does not depend on that one, so the few lines
43+
* are repeated here rather than published as a new entry of either package.
44+
*/
45+
46+
import React, { useEffect, useState } from 'react';
47+
import { RecordFieldAccessScope } from '@object-ui/permissions';
48+
import { useDataInvalidation, useInlineEdit } from '@object-ui/react';
49+
import type { DataSource } from '@object-ui/types';
50+
51+
export interface RecordFieldAccessForRecordProps {
52+
objectName?: string | null;
53+
recordId?: string | number | null;
54+
dataSource?: DataSource | null;
55+
/** Whether the block draws in-place editors at all. Defaults to `true`. */
56+
enabled?: boolean;
57+
children: React.ReactNode;
58+
}
59+
60+
export function RecordFieldAccessForRecord({
61+
objectName,
62+
recordId,
63+
dataSource,
64+
enabled = true,
65+
children,
66+
}: RecordFieldAccessForRecordProps) {
67+
const inline = useInlineEdit();
68+
const load = dataSource?.getRecordFieldAccess;
69+
const asks = enabled
70+
&& !!inline?.canEdit
71+
&& !!objectName
72+
&& recordId !== undefined && recordId !== null && recordId !== ''
73+
&& typeof load === 'function';
74+
const id = asks ? String(recordId) : undefined;
75+
const busRevision = useDataInvalidation(asks ? (objectName as string) : undefined, id);
76+
const [writeRevision, setWriteRevision] = useState(0);
77+
useEffect(() => {
78+
if (!asks || typeof dataSource?.onMutation !== 'function') return;
79+
const object = (objectName as string).toLowerCase();
80+
return dataSource.onMutation((event) => {
81+
if ((event?.resource ?? '').toLowerCase() !== object) return;
82+
if (event.id != null && String(event.id) !== id) return;
83+
setWriteRevision((n) => n + 1);
84+
});
85+
}, [asks, dataSource, objectName, id]);
86+
if (!asks) {
87+
return <>{children}</>;
88+
}
89+
return (
90+
<RecordFieldAccessScope
91+
object={objectName as string}
92+
recordId={id as string}
93+
load={(object, recordKey) => (load as NonNullable<typeof load>).call(dataSource, object, recordKey)}
94+
revision={busRevision + writeRevision}
95+
>
96+
{children}
97+
</RecordFieldAccessScope>
98+
);
99+
}

‎ui/packages/plugin-detail/src/renderers/record-details.tsx‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
import { DetailView } from '../DetailView';
3636
import { hasCellValue } from '../emptiness';
3737
import { RecordPageProvenanceContext } from '../recordPageProvenance';
38+
import { RecordFieldAccessForRecord } from '../recordFieldAccess';
3839
import { deriveFieldGroupDetailEntries, type FieldGroupDetailEntry } from '../synth/buildDefaultPageSchema';
3940
import { useRecordAriaProps } from './recordComponentAria';
4041
import { recordBindingBlock } from './recordBinding';
@@ -1187,15 +1188,24 @@ const RecordDetailsBody: React.FC<RecordDetailsRendererProps> = ({
11871188
return (
11881189
<div className={className} {...designer} {...ariaProps}>
11891190
<RecordPageProvenanceContext.Provider value={readsPageRecord}>
1190-
{/* objectui#12701 — the record is the context host's (the record
1191-
page's read, or the binding's row above); this body never reads it
1192-
again, it waits for `ctx.data`. */}
1193-
<DetailView
1194-
schema={synthesized}
1191+
{/* objectstack#27146 — the in-place edit's field locks are this
1192+
stored row's, asked of the server (`../recordFieldAccess`). */}
1193+
<RecordFieldAccessForRecord
1194+
objectName={ctx.objectName}
1195+
recordId={ctx.recordId}
11951196
dataSource={ctx.dataSource}
1196-
inlineEdit={inlineEditDefault}
1197-
recordFromHost
1198-
/>
1197+
enabled={inlineEditDefault}
1198+
>
1199+
{/* objectui#12701 — the record is the context host's (the record
1200+
page's read, or the binding's row above); this body never reads it
1201+
again, it waits for `ctx.data`. */}
1202+
<DetailView
1203+
schema={synthesized}
1204+
dataSource={ctx.dataSource}
1205+
inlineEdit={inlineEditDefault}
1206+
recordFromHost
1207+
/>
1208+
</RecordFieldAccessForRecord>
11991209
</RecordPageProvenanceContext.Provider>
12001210
</div>
12011211
);

‎ui/packages/plugin-detail/src/renderers/record-highlights.tsx‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { useRecordContext, useRegisterHighlightFields } from '@object-ui/react';
1515
import { useFieldPermissions, usePermissions } from '@object-ui/permissions';
1616
import type { RecordHighlightsComponentProps } from '@object-ui/types';
1717
import { HeaderHighlight } from '../HeaderHighlight';
18+
import { RecordFieldAccessForRecord } from '../recordFieldAccess';
1819
import { useRecordAriaProps } from './recordComponentAria';
1920
import { recordBindingBlock } from './recordBinding';
2021

@@ -180,13 +181,17 @@ const RecordHighlightsBody: React.FC<RecordHighlightsRendererProps> = ({
180181

181182
return (
182183
<div className={className} {...designer} {...ariaProps}>
183-
<HeaderHighlight
184-
fields={highlightFields as any}
185-
data={ctx?.data}
186-
objectName={ctx?.objectName}
187-
objectSchema={ctx?.objectSchema as any}
188-
dataSource={ctx?.dataSource}
189-
/>
184+
{/* objectstack#27146 — the in-place edit's field locks are this
185+
stored row's, asked of the server (`../recordFieldAccess`). */}
186+
<RecordFieldAccessForRecord objectName={ctx?.objectName} recordId={ctx?.recordId} dataSource={ctx?.dataSource}>
187+
<HeaderHighlight
188+
fields={highlightFields as any}
189+
data={ctx?.data}
190+
objectName={ctx?.objectName}
191+
objectSchema={ctx?.objectSchema as any}
192+
dataSource={ctx?.dataSource}
193+
/>
194+
</RecordFieldAccessForRecord>
190195
</div>
191196
);
192197
};

0 commit comments

Comments
 (0)