Skip to content

Commit 14147bf

Browse files
committed
[maintainer] refactor(app-shell): move the Data pillar helpers out of StudioDesignSurface.tsx
Move-only (ruling #24586 A), seam row 6: the create dialog's OWD option keys, the added-field names, the system field names, the CEL panel stamps and formPreviewSource move byte for byte into views/studio-design/DataPillarHelpers.tsx. Only imports and `export` change. renderStudioGridList stays where it is: its eslint-disable line for react-refresh/only-export-components would be an unused directive (an error) in a module that exports no component, so moving it unchanged is not possible. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WGufzedfGwVcebpN5t3ETE
1 parent b0d9f35 commit 14147bf

2 files changed

Lines changed: 213 additions & 186 deletions

File tree

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* The Data pillar's helpers: the create dialog's OWD option keys, the added-field
5+
* names, the system field names, the CEL panel stamps and the form preview
6+
* source, moved out of `StudioDesignSurface.tsx` unchanged by objectstack#29988
7+
* (slice 4 of the move-only split).
8+
*/
9+
10+
import type { OwdCreateModel } from './owd-sharing.js';
11+
import { readFields, toFieldNameLoose } from '../metadata-admin/previews/object-fields-io.js';
12+
import { romanizeHan } from '../metadata-admin/romanizeHan.js';
13+
import { isStudioHiddenSystemField } from './studioHiddenSystemField.js';
14+
import { extensionFieldName, type ObjectExtensionBody } from './StudioCustomizeControls.js';
15+
16+
/**
17+
* The create dialog's OWD options reuse the SETTINGS tab's own label and gloss
18+
* strings verbatim (`7a90afdf9`). The complaint it answered was that the Settings
19+
* page "is excellent — four options, each with a plain-language gloss" and that
20+
* nothing routes the author there; copying the wording rather than writing a
21+
* second, shorter one is what keeps the two surfaces from drifting into two
22+
* descriptions of one security baseline.
23+
*/
24+
export const OWD_OPTION_LABEL_KEY: Record<OwdCreateModel, string> = {
25+
private: 'engine.studio.settings.sharingPrivate',
26+
public_read: 'engine.studio.settings.sharingPublicRead',
27+
public_read_write: 'engine.studio.settings.sharingPublicReadWrite',
28+
};
29+
export const OWD_OPTION_DESC_KEY: Record<OwdCreateModel, string> = {
30+
private: 'engine.studio.settings.sharingDescPrivate',
31+
public_read: 'engine.studio.settings.sharingDescPublicRead',
32+
public_read_write: 'engine.studio.settings.sharingDescPublicReadWrite',
33+
};
34+
35+
/** Next unused `field_N` name for a freshly-added field. */
36+
function nextFieldName(existing: string[]): string {
37+
let i = existing.length + 1;
38+
let name = `field_${i}`;
39+
while (existing.includes(name)) name = `field_${++i}`;
40+
return name;
41+
}
42+
43+
/**
44+
* The api name of a field added with `label` (objectui#12516): the name the
45+
* field inspector's Label derives from it (`toFieldNameLoose`, a Chinese label
46+
* through its pinyin as the popover previewed it — `romanizeHan`), or, as the
47+
* inspector keeps the default when that derivation is empty (a label with no
48+
* Latin letters) or names another field, the next free `field_<N>`. The Add
49+
* field popover asks for the name in those two cases (objectui#13389), so the
50+
* `field_<N>` fallback is left for a caller that hands no name.
51+
*/
52+
export function addedFieldName(label: string, existing: string[]): string {
53+
const derived = toFieldNameLoose(romanizeHan(label));
54+
return derived && !existing.includes(derived) ? derived : nextFieldName(existing);
55+
}
56+
57+
/**
58+
* objectstack#28575 — {@link addedFieldName} for a customer extension field:
59+
* the name the Add field popover shows, suffixed (`region__c`), or the next
60+
* free `field_<N>__c`.
61+
*/
62+
export function addedExtensionFieldName(label: string, existing: string[]): string {
63+
const derived = extensionFieldName(toFieldNameLoose(romanizeHan(label)).replace(/_+$/, ''));
64+
if (derived && !existing.includes(derived)) return derived;
65+
let i = existing.length + 1;
66+
let name = extensionFieldName(`field_${i}`);
67+
while (existing.includes(name)) name = extensionFieldName(`field_${++i}`);
68+
return name;
69+
}
70+
71+
/** objectstack#28575 — an extension body's `fields` map; `{}` when it declares none. */
72+
export function extensionFieldsOf(body: ObjectExtensionBody): Record<string, unknown> {
73+
const fields = body.fields;
74+
return fields && typeof fields === 'object' && !Array.isArray(fields) ? (fields as Record<string, unknown>) : {};
75+
}
76+
77+
/**
78+
* Data pillar — the package's objects: a records grid (Airtable parity) plus
79+
* table-based field management. Add a field, or click a column header's edit
80+
* affordance, to open ObjectFieldInspector (full type list + per-type config)
81+
* in the right panel; changes persist via the object draft → publish overlay.
82+
*/
83+
/**
84+
* Framework-managed/audit fields. They lead the raw metadata order but aren't
85+
* what a user manages in a data grid, so the Data pillar drops them from the
86+
* column set (mirrors ObjectGrid's regular-vs-system split) to open on the
87+
* meaningful fields first — the same way Airtable hides system columns.
88+
*
89+
* objectui#11780 — this list is no longer the only test. The columns the
90+
* platform injects AND hides (`system: true` + `hidden: true` on the served
91+
* definition: `__search`, `owning_business_unit_id`, `organization_id`) are
92+
* dropped by `isStudioHiddenSystemField`, read off each field's definition
93+
* beside this list. The list keeps its own job: the audit columns are
94+
* `system` but NOT `hidden` in the platform's own definitions
95+
* (`AUDIT_FIELD_DEFS`), so the marks alone would put them back.
96+
*/
97+
export const STUDIO_SYSTEM_FIELD_NAMES = new Set<string>([
98+
'_id', 'id', 'organization_id', 'org_id', 'space_id',
99+
'created_at', 'created_by', 'updated_at', 'updated_by',
100+
'modified_at', 'modified_by', 'created_time', 'modified_time', 'updated_time',
101+
'deleted_at', 'deleted_by',
102+
]);
103+
104+
/**
105+
* The form fields of an object definition's `fields`, in metadata order: the
106+
* platform's injected-and-hidden fields (objectui#11780) and the audit fields
107+
* dropped. Read for the draft (`formFields`) and the published baseline.
108+
*/
109+
export function formFieldNames(fields: unknown): string[] {
110+
return readFields(fields)
111+
.entries.filter((e) => !isStudioHiddenSystemField(e.def))
112+
.map((e) => e.name)
113+
.filter((n) => !STUDIO_SYSTEM_FIELD_NAMES.has(n));
114+
}
115+
116+
/**
117+
* objectstack#27485 — what the field inspector's CEL verdict on `field` was
118+
* judged from, as the object draft holds it: the field's type, its CEL texts
119+
* (`expression`, the legacy `formula`, `visibleWhen`, `readonlyWhen`,
120+
* `requiredWhen`) and the object's field names its lint resolves references
121+
* against. The same stamp is the same verdict. `null` when the field is gone.
122+
*/
123+
export function fieldCelStamp(draft: Record<string, unknown>, field: string): string | null {
124+
const { entries } = readFields(draft.fields);
125+
const def = entries.find((e) => e.name === field)?.def;
126+
if (!def) return null;
127+
const cel = [def.type, def.expression, def.formula, def.visibleWhen, def.readonlyWhen, def.requiredWhen];
128+
return JSON.stringify([cel.map((v) => v ?? null), entries.map((e) => e.name)]);
129+
}
130+
131+
/** objectstack#27629 — the Data pillar panels that mount a CEL editor and report per item. */
132+
export type CelPanelTab = 'rules' | 'actions';
133+
export const isCelPanelTab = (v: string): v is CelPanelTab => v === 'rules' || v === 'actions';
134+
135+
/** The rule (`rules`) or action (`actions`) named `item` in the object draft. */
136+
export function panelItemOf(draft: Record<string, unknown>, tab: CelPanelTab, item: string): Record<string, unknown> | undefined {
137+
const list = tab === 'rules' ? draft.validations : draft.actions;
138+
if (!Array.isArray(list)) return undefined;
139+
return list.find(
140+
(e): e is Record<string, unknown> => !!e && typeof e === 'object' && String((e as { name?: unknown }).name ?? '') === item,
141+
);
142+
}
143+
144+
/**
145+
* objectstack#27629 — what a panel's CEL verdict on `item` was judged from, as
146+
* the object draft holds it: the rule or action itself (its guard or
147+
* predicates among its keys) and the object's field names its lint resolves
148+
* references against. `fieldCelStamp`'s counterpart for the panel family; the
149+
* same stamp is the same verdict. `null` when the item is gone.
150+
*/
151+
export function panelItemCelStamp(draft: Record<string, unknown>, tab: CelPanelTab, item: string): string | null {
152+
const def = panelItemOf(draft, tab, item);
153+
if (!def) return null;
154+
return JSON.stringify([def, readFields(draft.fields).entries.map((e) => e.name)]);
155+
}
156+
157+
/** The data-source writes the Form tab's Preview refuses (objectui#12515). */
158+
const PREVIEW_REFUSED_WRITES = new Set<PropertyKey>(['create', 'update', 'delete', 'bulk', 'bulkUpdate', 'bulkDelete']);
159+
160+
/**
161+
* objectui#12515 — the data source the Form tab's Preview hands the runtime
162+
* `ObjectForm`, the same renderer the app's record form uses.
163+
*
164+
* The form reads the object's definition only through
165+
* `dataSource.getObjectSchema(name)`. With `draft` given, that read answers for
166+
* the open object from the pillar's buffer (the draft as served, plus the
167+
* edits not yet saved), so a draft-only object previews without a server read
168+
* and a published one previews its pending change. Every other read (another
169+
* object's definition, a lookup's options) goes to the real adapter.
170+
*
171+
* Preview never saves: the writes in `PREVIEW_REFUSED_WRITES` reject, in the
172+
* Draft and the Published preview alike.
173+
*/
174+
export function formPreviewSource(
175+
adapter: object | null,
176+
draft: { name: string; definition: Record<string, unknown> } | null,
177+
): object | null {
178+
// No adapter, no data source: the form says so, as it does today.
179+
if (!adapter) return null;
180+
return new Proxy(adapter, {
181+
get(target, prop) {
182+
if (PREVIEW_REFUSED_WRITES.has(prop)) {
183+
return () => Promise.reject(new Error('The form preview does not save.'));
184+
}
185+
if (draft && prop === 'getObjectSchema') {
186+
const read = Reflect.get(target, prop, target) as ((name: string) => Promise<unknown>) | undefined;
187+
return (name: string) =>
188+
name === draft.name
189+
? Promise.resolve(structuredClone({ ...draft.definition, name: draft.name }))
190+
: read
191+
? read.call(target, name)
192+
: Promise.reject(new Error(`No definition for "${name}".`));
193+
}
194+
const value = Reflect.get(target, prop, target);
195+
return typeof value === 'function' ? value.bind(target) : value;
196+
},
197+
});
198+
}

0 commit comments

Comments
 (0)