Skip to content

Commit 5002d07

Browse files
committed
wip: studio pending edit survives selection change (#27511, #27485)
1 parent 6ca1a47 commit 5002d07

5 files changed

Lines changed: 155 additions & 27 deletions

File tree

‎ui/packages/app-shell/src/views/metadata-admin/inspectors/ObjectFieldInspector.tsx‎

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ import {
113113
useExternalWriteState,
114114
} from '../previews/external-object.js';
115115
import { CelPredicateField } from '../CelPredicateField.js';
116-
import type { CelLintIssue } from '../celAuthoring.js';
116+
import type { CelLintIssue, CelValueType } from '../celAuthoring.js';
117117
import { t, tFormat, type SupportedLocale } from '../i18n.js';
118118
import { FilterConditionField } from './FilterConditionField.js';
119119
import type { FilterCondition } from './datasetFilterCondition.js';
@@ -627,6 +627,20 @@ export function ObjectFieldInspector({
627627
React.useEffect(() => {
628628
formulaEdited.current = false;
629629
}, [name]);
630+
// objectstack#27485 — the formula editor's two verdicts on one source, its
631+
// lint and its inferred type, which arrive separately and in either order.
632+
// The stamp is settled once both are in for the source the draft holds: a
633+
// source with a lint error proves no type, so the stored `returnType` stays
634+
// (it was cleared on every keystroke that broke the formula, and the broken
635+
// body then reached the server without it); a source that lints clean
636+
// stamps the proven type, or clears it, as before.
637+
const formulaVerdictRef = React.useRef<{
638+
field: string;
639+
source: string;
640+
errors?: number;
641+
typed?: boolean;
642+
inferred?: CelValueType | null;
643+
}>({ field: '', source: '' });
630644

631645
/* ─── Who owns the API name (objectui#7615) ──────────────────────────────
632646
*
@@ -793,6 +807,20 @@ export function ObjectFieldInspector({
793807
writeView({ shape: view.shape, entries: nextEntries });
794808
};
795809

810+
/** objectstack#27485 — records one of the formula's verdicts and settles `returnType` (see `formulaVerdictRef`). */
811+
const settleFormulaType = (verdict: { errors: number } | { typed: true; inferred: CelValueType | null }) => {
812+
const source = readPredicate(def.expression ?? def.formula);
813+
const was = formulaVerdictRef.current;
814+
const same = was.field === entry.name && was.source === source;
815+
const now = { ...(same ? was : { field: entry.name, source }), ...verdict };
816+
formulaVerdictRef.current = now;
817+
if (!formulaEdited.current || now.errors === undefined || !now.typed || now.errors > 0) return;
818+
// Spec: `returnType` carries only a PROVEN concrete type —
819+
// an ambiguous (`unknown`) or unavailable verdict clears it.
820+
const next = now.inferred && now.inferred !== 'unknown' ? now.inferred : undefined;
821+
if (def.returnType !== next) patchDef({ returnType: next });
822+
};
823+
796824
/**
797825
* objectui#10202 — "use picklist". Naming a list sets `picklist` and removes
798826
* `options` outright (the key is ABSENT, as `unsetDefKey` leaves it): the
@@ -1264,14 +1292,11 @@ export function ObjectFieldInspector({
12641292
...(v.trim() ? {} : { returnType: undefined }),
12651293
});
12661294
}}
1267-
onLintChange={(issues) => reportCel('formula', issues)}
1268-
onInferredTypeChange={(inferred) => {
1269-
if (!formulaEdited.current) return;
1270-
// Spec: `returnType` carries only a PROVEN concrete type —
1271-
// an ambiguous (`unknown`) or unavailable verdict clears it.
1272-
const next = inferred && inferred !== 'unknown' ? inferred : undefined;
1273-
if (def.returnType !== next) patchDef({ returnType: next });
1295+
onLintChange={(issues) => {
1296+
reportCel('formula', issues);
1297+
settleFormulaType({ errors: issues.filter((i) => i.severity === 'error').length });
12741298
}}
1299+
onInferredTypeChange={(inferred) => settleFormulaType({ typed: true, inferred })}
12751300
disabled={readOnly}
12761301
placeholder="record.amount * 0.2"
12771302
objectName={typeof (draft as any).name === 'string' ? ((draft as any).name as string) : undefined}

‎ui/packages/app-shell/src/views/studio-design/StudioDesignSurface.tsx‎

Lines changed: 114 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6415,13 +6415,54 @@ export function DataPillar({
64156415
// that does not parse must not be saveable, let alone publishable as the live
64166416
// field definition (objectui#4306).
64176417
//
6418-
// The count is STAMPED with the selection it came from, so it expires by
6419-
// construction when the selection changes or the panel closes: an unmounted
6420-
// inspector can never retract its last verdict, and a host that waited for
6421-
// one would leave Save wedged shut with nothing on screen to fix.
6418+
// The report is STAMPED with the selection it came from: it is the open
6419+
// inspector's live verdict, and never reads as another selection's.
64226420
const [blockingReport, setBlockingReport] = React.useState({ key: '', count: 0 });
64236421
const fieldSelKey = fieldSel ? `${current?.name ?? ''}:${fieldSel.kind}:${fieldSel.id}` : '';
6424-
const inspectorBlocking = blockingReport.key === fieldSelKey ? blockingReport.count : 0;
6422+
// objectstack#27485 — the verdict belongs to the draft, not to the selection.
6423+
// Expiring it with the selection released the hold the moment the inspector
6424+
// closed (Escape, Close, another field, another tab) while the broken text
6425+
// stayed in the draft, and the autosave sent it. So each field's last live
6426+
// verdict is kept with the stamp of what it judged (`fieldCelStamp`, taken
6427+
// while the inspector is open on it) and holds, with the inspector closed,
6428+
// while the draft still carries that stamp: an edit to the text, the field's
6429+
// removal or the object's next load ends it. Nothing is left shut with
6430+
// nothing to fix: the held line names the field and its "Show me" opens it,
6431+
// and the open inspector's live verdict always wins.
6432+
const [celVerdicts, setCelVerdicts] = React.useState<Record<string, { field: string; count: number; stamp: string | null }>>({});
6433+
const liveFieldReport = fieldSel?.kind === 'field' && blockingReport.key === fieldSelKey;
6434+
const openFieldStamp = fieldSel?.kind === 'field' ? fieldCelStamp(objDraft, fieldSel.id) : null;
6435+
React.useEffect(() => {
6436+
if (!liveFieldReport || fieldSel === null) return;
6437+
const kept = { field: fieldSel.id, count: blockingReport.count, stamp: openFieldStamp };
6438+
setCelVerdicts((prev) => {
6439+
const was = prev[fieldSelKey];
6440+
if (was && was.count === kept.count && was.stamp === kept.stamp) return prev;
6441+
return { ...prev, [fieldSelKey]: kept };
6442+
});
6443+
}, [liveFieldReport, fieldSel, fieldSelKey, blockingReport.count, openFieldStamp]);
6444+
// The open object's fields whose verdict holds, and their summed count.
6445+
const celHeldFields: string[] = [];
6446+
let inspectorBlocking = 0;
6447+
const celFieldPrefix = `${current?.name ?? ''}:field:`;
6448+
for (const [key, kept] of Object.entries(celVerdicts)) {
6449+
if (!key.startsWith(celFieldPrefix) || key === fieldSelKey) continue;
6450+
if (kept.count > 0 && kept.stamp === fieldCelStamp(objDraft, kept.field)) {
6451+
celHeldFields.push(kept.field);
6452+
inspectorBlocking += kept.count;
6453+
}
6454+
}
6455+
if (blockingReport.key === fieldSelKey && blockingReport.count > 0) {
6456+
if (fieldSel?.kind === 'field') celHeldFields.push(fieldSel.id);
6457+
inspectorBlocking += blockingReport.count;
6458+
} else if (fieldSelKey !== '' && !liveFieldReport) {
6459+
// Opened, and its inspector has not reported yet: its kept verdict stands.
6460+
const kept = celVerdicts[fieldSelKey];
6461+
if (kept && kept.count > 0 && kept.stamp === openFieldStamp) {
6462+
celHeldFields.push(kept.field);
6463+
inspectorBlocking += kept.count;
6464+
}
6465+
}
64256466
// The pillar's PANEL family reports separately (objectui#4527): the
64266467
// validations, actions and settings panels all write through this same draft
64276468
// and own no Save of their own, so their CEL faults have to reach this
@@ -6691,6 +6732,8 @@ export function DataPillar({
66916732
setLoading(true);
66926733
setError(null);
66936734
setFieldSel(null);
6735+
// objectstack#27485 — kept CEL verdicts judged the buffer this load replaces.
6736+
setCelVerdicts({});
66946737
setDirty(false);
66956738
(async () => {
66966739
try {
@@ -6958,7 +7001,7 @@ export function DataPillar({
69587001

69597002
// objectui#5813 — auto-save replaces the Save draft button; the blocked guard
69607003
// is the button's old disabled-condition verbatim, plus the hold above.
6961-
const { sending: sendingObjDraft, loaded: objLoaded } = useDraftAutoSave({
7004+
const { flush: flushObjSave, sending: sendingObjDraft, loaded: objLoaded } = useDraftAutoSave({
69627005
// objectui#11232 — the object `doSave` addresses.
69637006
target: `object:${current?.name ?? ''}`,
69647007
loadedFor: objDraftFor,
@@ -6967,8 +7010,27 @@ export function DataPillar({
69677010
snapshot: objDraft,
69687011
save: doSave,
69697012
});
7013+
// objectstack#27485 — an unsent edit the CEL verdict holds is named like an
7014+
// incomplete one: the held line, the surface's Publish and the rail's
7015+
// question all read it, so it outlives the inspector without going unseen.
7016+
const celHeldField = celHeldFields[0] ?? null;
7017+
const celHeldLabel = React.useMemo(() => {
7018+
if (celHeldField === null) return null;
7019+
const label = readFields(objDraft.fields).entries.find((e) => e.name === celHeldField)?.def.label;
7020+
return typeof label === 'string' && label.trim() !== '' ? label.trim() : celHeldField;
7021+
}, [celHeldField, objDraft.fields]);
7022+
const objCelHeld = React.useMemo<StudioHeld | null>(
7023+
() =>
7024+
dirty && celHeldField !== null && celHeldLabel !== null
7025+
? {
7026+
clause: tFormat('engine.studio.held.celInvalid', locale, { field: celHeldLabel }),
7027+
target: { kind: 'field', id: celHeldField },
7028+
}
7029+
: null,
7030+
[dirty, celHeldField, celHeldLabel, locale],
7031+
);
69707032
// Only the open object's own buffer is held (objectui#11272).
6971-
const objHeld = objLoaded ? objIncomplete : null;
7033+
const objHeld = objLoaded ? (objIncomplete ?? objCelHeld) : null;
69727034
// objectstack#26917 — the refusal's way out: the object holding the
69737035
// identifier is on this rail, so it opens instead of a second one being
69747036
// made. The New button already asked about a held edit (objectstack#26845).
@@ -6985,19 +7047,56 @@ export function DataPillar({
69857047
const fieldAgentMark =
69867048
objLoaded && fieldSel?.kind === 'field' ? agentDraftMarkOf(objDraftChanges, ['fields', fieldSel.id]) : null;
69877049
useHeldEditReport(onHeldEditChange, objHeld?.clause ?? null);
7050+
// objectstack#27511 — opening another object replaces the one buffer the
7051+
// open object's edits live in, and the autosave's timer sends nothing once
7052+
// the open object has changed (objectui#11232). So an edit still inside the
7053+
// debounce was dropped by the switch with nothing on screen. The switch now
7054+
// sends it first with the autosave's own `flush` and opens the object once
7055+
// the buffer is clean, as the Interfaces nav's "Done" closes (objectui#11189);
7056+
// a save already in flight is waited out the same way. An edit that cannot
7057+
// be sent (held, or its save refused with the refusal on screen) is the
7058+
// author's to drop: the switch asks (objectstack#26705).
7059+
const [switchTo, setSwitchTo] = React.useState<Surface | null>(null);
7060+
const openObject = React.useCallback(
7061+
(next: Surface): boolean => {
7062+
if (next.name === current?.name) {
7063+
setSwitchTo(null);
7064+
setCurrent(next);
7065+
return true;
7066+
}
7067+
if (saving || (dirty && objHeld === null && flushObjSave())) {
7068+
setSwitchTo(next);
7069+
return true;
7070+
}
7071+
if (!confirmDropUnsavedWork(dirty, locale)) return false;
7072+
setSwitchTo(null);
7073+
setCurrent(next);
7074+
return true;
7075+
},
7076+
[current, saving, dirty, objHeld, flushObjSave, locale],
7077+
);
7078+
React.useEffect(() => {
7079+
if (!switchTo || saving) return;
7080+
if (dirty && objHeld === null && flushObjSave()) return;
7081+
setSwitchTo(null);
7082+
// Still dirty: the save was refused (on screen) or an edit taken meanwhile
7083+
// is held. The author stays; the next switch asks.
7084+
if (dirty) return;
7085+
setCurrent(switchTo);
7086+
}, [switchTo, saving, dirty, objHeld, flushObjSave]);
69887087
// The live request's apply (see `requestedSurface` above). objectstack#26845
69897088
// — another object's load replaces the buffer a held edit lives in, so it
6990-
// asks first, as the rail does; declined, the request is spent and the held
7089+
// goes through the rail's own switch (`openObject`): an unsent edit is sent
7090+
// first, or the author is asked; declined, the request is spent and the
69917091
// edit stays. A request for the open object drops nothing and asks nothing.
69927092
React.useEffect(() => {
69937093
if (!requestedSurface || !objectsLoaded) return;
69947094
if (requestedSurface.id === appliedRequestRef.current) return;
69957095
appliedRequestRef.current = requestedSurface.id;
69967096
const match = resolveSurfaceDeepLink(objects, requestedSurface.target, 'object');
69977097
if (!match) return;
6998-
if (match.name !== current?.name && !confirmDropUnsavedWork(objHeld !== null, locale)) return;
6999-
setCurrent(match);
7000-
}, [requestedSurface, objects, objectsLoaded, current, objHeld, locale]);
7098+
openObject(match);
7099+
}, [requestedSurface, objects, objectsLoaded, openObject]);
70017100
// objectstack#26845 — a created object opens at once, and its load replaces
70027101
// the buffer a held edit lives in: asked before the dialog opens, so Cancel
70037102
// costs nothing and the create itself asks nothing.
@@ -7283,11 +7382,10 @@ export function DataPillar({
72837382
key={o.name}
72847383
active={current?.name === o.name}
72857384
onClick={() => {
7286-
// objectstack#26705 — another object's load replaces the
7287-
// buffer the held edit lives in; the open one keeps it.
7288-
if (current?.name !== o.name && !confirmDropUnsavedWork(objHeld !== null, locale)) return;
7289-
setCurrent(o);
7290-
if (isMobile) setRailOpen(false);
7385+
// objectstack#26705, objectstack#27511 — another object's load
7386+
// replaces the buffer the open one's edits live in: they are
7387+
// sent first, or the author is asked (`openObject`).
7388+
if (openObject(o) && isMobile) setRailOpen(false);
72917389
}}
72927390
icon={<Icon className="h-4 w-4 shrink-0" />}
72937391
>

‎ui/packages/app-shell/src/views/studio-design/useDraftAutoSave.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ import * as React from 'react';
4343
// load installs its own; so a period that began on another item is never
4444
// sent, by the timer or by `flush`, and it ends only when the caller's dirty
4545
// flag falls.
46-
// The previous item's pending edit is dropped: what the Data pillar's switch
47-
// has always done, and what every pillar did whenever the new load landed
48-
// inside the debounce. A claim also reads moved once the target has
46+
// The previous item's pending edit is dropped by the hook: what every
47+
// pillar did whenever the new load landed inside the debounce. A caller
48+
// that must not lose it sends it with `flush` BEFORE it switches, and
49+
// switches once its save has landed: the Data pillar's object rail does
50+
// (objectstack#27511). A claim also reads moved once the target has
4951
// changed, so a save that lands after a switch never clears the dirty flag
5052
// of the item opened since.
5153
// - objectui#11272 — the buffer belongs to the item it was loaded for. The

‎ui/packages/i18n/src/namespaces/studio/en.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2999,6 +2999,8 @@ const STUDIO_EN: Record<string, string> = {
29992999
// objectui#11910 — a dashboard widget held until it is bound; `input` is the
30003000
// widget inspector's own label for it (Dataset, Values (measures)).
30013001
'engine.studio.held.widgetNeedsInput': '{input} on the widget “{widget}” needs a value',
3002+
// objectstack#27485 — a field CEL expression the inspector judged broken, held after it closes.
3003+
'engine.studio.held.celInvalid': 'an expression on the field “{field}” has an error to fix',
30023004
'engine.studio.held.line': 'Not saved yet: {clause}. Your changes are kept here and saved once it is filled in.',
30033005
'engine.studio.held.publish': 'Not published: {clause}, so that change is not saved yet. Finish it, then publish.',
30043006
'engine.studio.held.inputHint': 'Required. Changes are saved once this has a value.',

‎ui/packages/i18n/src/namespaces/studio/zh.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2668,6 +2668,7 @@ const STUDIO_ZH: Record<string, string> = {
26682668
'engine.studio.held.needsRuleField': '规则「{rule}」需要选择一个字段',
26692669
'engine.studio.held.needsRuleMessage': '规则「{rule}」需要一条错误消息',
26702670
'engine.studio.held.widgetNeedsInput': '组件「{widget}」的「{input}」需要填写',
2671+
'engine.studio.held.celInvalid': '字段「{field}」的表达式有错误需要修正',
26712672
'engine.studio.held.line': '尚未保存:{clause}。更改已保留在此处,补全后会自动保存。',
26722673
'engine.studio.held.publish': '未发布:{clause},该更改尚未保存。请先补全,再发布。',
26732674
'engine.studio.held.inputHint': '必填。填写后才会保存更改。',

0 commit comments

Comments
 (0)