Skip to content

Commit 8c8d967

Browse files
committed
fix(app-shell): Studio's action inspector keeps a new field-value row and stores a script action's type
Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QsR2jVG55iUTTWRd5tR6Di
1 parent e9e344a commit 8c8d967

1 file changed

Lines changed: 65 additions & 14 deletions

File tree

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

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
* `aiDescription` keys, which the spec refuses by name, so one tick refused
2929
* every later save of the object (objectstack#26728). The two flat keys are
3030
* cleared by the AI control's next write, and never read.
31+
*
32+
* The Type control shows the `type` the draft holds, and every write that
33+
* authors a script writes `type: 'script'` with it (objectstack#26734). The
34+
* field-value editor holds a row that names no field yet, and writes it to
35+
* `patch` once it does (objectstack#26735).
3136
*/
3237

3338
import * as React from 'react';
@@ -95,6 +100,16 @@ const ACTION_TYPES: KeyedOption[] = [
95100
{ value: 'url', labelKey: 'engine.inspector.action.type.url' },
96101
];
97102

103+
/**
104+
* objectstack#26734 — the route `@objectstack/spec` gives an action that names
105+
* no `type` (`ActionSchema.type` defaults to it). The Behavior panels follow it,
106+
* so a draft without `type` still shows the editor for what it will run. It is
107+
* never shown in the Type control as if stored: the console reads the stored
108+
* row, not its parse, and runs nothing for an action without `type`. The first
109+
* script write stores it instead.
110+
*/
111+
const DEFAULT_ACTION_TYPE = 'script';
112+
98113
const VARIANT_OPTS: KeyedOption[] = [
99114
{ value: 'primary', labelKey: 'engine.inspector.action.variant.primary' },
100115
{ value: 'secondary', labelKey: 'engine.inspector.action.variant.secondary' },
@@ -402,6 +417,19 @@ const BODY_SHAPE_OPTS: KeyedOption[] = [
402417
];
403418

404419
/** What a script action DOES — `operation` is authored beside `type`, not in it. */
420+
/** One field-value row of an update action: the field, and the value written to it. */
421+
type PatchRow = [string, unknown];
422+
423+
/** The rows `patch` stores: those that name a field, under the trimmed name. */
424+
function namedPatchRows(rows: ReadonlyArray<PatchRow>): PatchRow[] {
425+
return rows.filter(([key]) => key.trim() !== '').map(([key, value]) => [key.trim(), value]);
426+
}
427+
428+
function samePatchRows(a: ReadonlyArray<PatchRow>, b: ReadonlyArray<PatchRow>): boolean {
429+
return a.length === b.length
430+
&& a.every(([key, value], i) => key === b[i][0] && JSON.stringify(value) === JSON.stringify(b[i][1]));
431+
}
432+
405433
const SCRIPT_OPERATIONS: KeyedOption[] = [
406434
{ value: '', labelKey: 'engine.inspector.action.scriptOp.run' },
407435
{ value: 'update', labelKey: 'engine.inspector.action.scriptOp.update' },
@@ -626,7 +654,13 @@ export function ActionDefaultInspector({
626654
const iconFieldId = React.useId();
627655

628656
const str = (k: string): string => (typeof draft[k] === 'string' ? (draft[k] as string) : '');
629-
const type = str('type') || 'script';
657+
// objectstack#26734 — `storedType` is what the Type control shows ("—" when
658+
// the draft holds none); `type` is the route the panels below follow.
659+
const storedType = str('type');
660+
const type = storedType || DEFAULT_ACTION_TYPE;
661+
/** A script write, with the `type` a draft that holds none is stored with. */
662+
const withScriptType = (change: Record<string, unknown>): Record<string, unknown> =>
663+
storedType === '' ? { type: 'script', ...change } : change;
630664
const objectName = str('objectName');
631665
const body = (draft.body && typeof draft.body === 'object' ? draft.body : {}) as Record<string, unknown>;
632666
const params: ActionParam[] = Array.isArray(draft.params) ? (draft.params as ActionParam[]) : [];
@@ -674,15 +708,30 @@ export function ActionDefaultInspector({
674708
onPatch(cleared);
675709
return;
676710
}
677-
const out: Record<string, unknown> = { operation: undefined, patch: undefined };
711+
const out: Record<string, unknown> = withScriptType({ operation: undefined, patch: undefined });
678712
if (!str('target')) out.body = writeScriptBody(body, {});
679713
onPatch(out);
680714
};
681715

682-
const commitPatch = (rows: Array<[string, unknown]>) => {
683-
const next: Record<string, unknown> = {};
684-
for (const [key, value] of rows) if (key.trim() !== '') next[key.trim()] = value;
685-
onPatch({ patch: next });
716+
/* ─── objectstack#26735 — a field-value row waits here for its field ──────
717+
*
718+
* `patch` is a record, so a row that names no field has no key to be stored
719+
* under. "Add field value" used to commit `['', '']`, the write dropped the
720+
* blank key, and no row ever appeared. The rows on screen are now this list:
721+
* a row is written to `patch` once it names a field, and one that names none
722+
* (just added, or its field cleared) is held here, so a blank row never
723+
* reaches the draft. Stamped with the action, like the Wrap choice below, and
724+
* read only while its named rows are exactly the draft's `patch`: any other
725+
* write to `patch` shows the draft's rows instead. */
726+
const [heldRows, setHeldRows] = React.useState<{ action: string; rows: PatchRow[] } | null>(null);
727+
const fieldValueRows: PatchRow[] =
728+
heldRows && heldRows.action === str('name') && samePatchRows(namedPatchRows(heldRows.rows), patchRows)
729+
? heldRows.rows
730+
: patchRows;
731+
const commitFieldValueRows = (rows: PatchRow[]) => {
732+
setHeldRows({ action: str('name'), rows });
733+
const named = namedPatchRows(rows);
734+
if (!samePatchRows(named, patchRows)) onPatch(withScriptType({ patch: Object.fromEntries(named) }));
686735
};
687736

688737
/* ─── objectui#7234 — the OTHER way a correctly-placed action never appears ──
@@ -818,7 +867,7 @@ export function ActionDefaultInspector({
818867
}, [blockingIssues]);
819868

820869
// objectui#11921 — every body write goes through the one writer.
821-
const patchBody = (p: Record<string, unknown>) => onPatch({ body: writeScriptBody(body, p) });
870+
const patchBody = (p: Record<string, unknown>) => onPatch(withScriptType({ body: writeScriptBody(body, p) }));
822871
const patchParam = (i: number, p: Partial<ActionParam>) =>
823872
onPatch({ params: params.map((it, j) => (j === i ? { ...it, ...p } : it)) });
824873

@@ -869,7 +918,7 @@ export function ActionDefaultInspector({
869918
{/* 2 ─ Behavior (type-first) */}
870919
<div className="border-t pt-3 space-y-3">
871920
<SectionHeader title={tr('engine.inspector.action.behavior')} hint={tr('engine.inspector.action.behaviorHint')} />
872-
<InspectorSelectField label={tr('engine.inspector.action.type')} value={type} options={localizeOptions(ACTION_TYPES, locale)} onCommit={commitType} disabled={readOnly || isUpdateOperation} />
921+
<InspectorSelectField label={tr('engine.inspector.action.type')} value={storedType || undefined} options={localizeOptions(ACTION_TYPES, locale)} onCommit={commitType} disabled={readOnly || isUpdateOperation} />
873922
{isUpdateOperation && (
874923
<div className="text-[11px] text-muted-foreground/70">
875924
{withSlots(tr('engine.inspector.action.pinnedScript'), { script: <code>script</code> })}
@@ -888,18 +937,20 @@ export function ActionDefaultInspector({
888937
{isUpdateOperation ? (
889938
<div className="space-y-2">
890939
<Label className="text-xs text-muted-foreground">{tr('engine.inspector.action.fieldValues')}</Label>
891-
{patchRows.length === 0 ? (
940+
{fieldValueRows.length === 0 ? (
892941
<p className="rounded-md border border-dashed bg-muted/30 px-3 py-2.5 text-center text-[11px] text-muted-foreground">
893942
{tr('engine.inspector.action.fieldValuesEmpty')}
894943
</p>
895944
) : (
896-
patchRows.map(([key, value], i) => (
945+
// Keyed by position, so a held row that gains its field keeps
946+
// its inputs (and the focus) as it is written.
947+
fieldValueRows.map(([key, value], i) => (
897948
<div key={i} className="flex items-end gap-2">
898949
<div className="flex-1">
899950
<InspectorTextField
900951
label={tr('engine.inspector.action.fieldValueField')}
901952
value={key}
902-
onCommit={(v) => commitPatch(patchRows.map((row, j) => (j === i ? [v, row[1]] : row)))}
953+
onCommit={(v) => commitFieldValueRows(fieldValueRows.map((row, j) => (j === i ? [v, row[1]] : row)))}
903954
placeholder="status"
904955
disabled={readOnly}
905956
mono
@@ -909,7 +960,7 @@ export function ActionDefaultInspector({
909960
<InspectorTextField
910961
label={tr('engine.inspector.action.fieldValueValue')}
911962
value={typeof value === 'string' ? value : JSON.stringify(value ?? '')}
912-
onCommit={(v) => commitPatch(patchRows.map((row, j) => (j === i ? [row[0], v] : row)))}
963+
onCommit={(v) => commitFieldValueRows(fieldValueRows.map((row, j) => (j === i ? [row[0], v] : row)))}
913964
placeholder="done"
914965
disabled={readOnly}
915966
mono
@@ -922,7 +973,7 @@ export function ActionDefaultInspector({
922973
className="h-8 w-8 shrink-0"
923974
disabled={readOnly}
924975
aria-label={tr('engine.inspector.action.removeFieldValue')}
925-
onClick={() => commitPatch(patchRows.filter((_, j) => j !== i))}
976+
onClick={() => commitFieldValueRows(fieldValueRows.filter((_, j) => j !== i))}
926977
>
927978
<Trash2 className="h-3.5 w-3.5" />
928979
</Button>
@@ -935,7 +986,7 @@ export function ActionDefaultInspector({
935986
size="sm"
936987
className="h-7 text-xs"
937988
disabled={readOnly}
938-
onClick={() => commitPatch([...patchRows, ['', '']])}
989+
onClick={() => commitFieldValueRows([...fieldValueRows, ['', '']])}
939990
>
940991
<Plus className="mr-1 h-3 w-3" /> {tr('engine.inspector.action.addFieldValue')}
941992
</Button>

0 commit comments

Comments
 (0)