Skip to content

Commit d6a06b9

Browse files
committed
refactor: simplify control JSON null filtering
1 parent 0b3b15e commit d6a06b9

2 files changed

Lines changed: 13 additions & 35 deletions

File tree

‎ui/src/core/page-components/agent-detail/modals/edit-control/edit-control-content.tsx‎

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@ import type {
4242
EditControlMode,
4343
} from './types';
4444
import { useEvaluatorConfigState } from './use-evaluator-config-state';
45-
import {
46-
applyApiErrorsToForms,
47-
omitNullProperties,
48-
stringifyJsonWithoutNulls,
49-
} from './utils';
45+
import { applyApiErrorsToForms, stringifyJsonWithoutNulls } from './utils';
5046

5147
const EVALUATOR_CONFIG_HEIGHT = 450;
5248
const JSON_EDITOR_HEIGHT = 520;
@@ -77,13 +73,9 @@ export const EditControlContent = ({
7773
}: EditControlContentProps) => {
7874
const { data: agentResponse } = useAgent(agentId);
7975
const steps = agentResponse?.steps ?? [];
80-
const sanitizedControlDefinition = useMemo(
81-
() => omitNullProperties(control.control) as ControlDefinition,
82-
[control.control]
83-
);
8476

8577
const [workingDefinition, setWorkingDefinition] = useState<ControlDefinition>(
86-
sanitizedControlDefinition
78+
control.control
8779
);
8880
const [editorMode, setEditorMode] =
8981
useState<ControlEditorMode>(initialEditorMode);
@@ -398,17 +390,17 @@ export const EditControlContent = ({
398390
}, [definitionForm.values.action_decision]);
399391

400392
useEffect(() => {
401-
setWorkingDefinition(sanitizedControlDefinition);
393+
setWorkingDefinition(control.control);
402394
setEditorMode(initialEditorMode);
403395
setDefinitionJsonText(
404396
initialEditorMode === 'json'
405-
? stringifyJsonWithoutNulls(sanitizedControlDefinition)
397+
? stringifyJsonWithoutNulls(control.control)
406398
: ''
407399
);
408400
setDefinitionJsonError(null);
409401
setDefinitionValidationError(null);
410402
setDefinitionValidationStatus('idle');
411-
}, [initialEditorMode, sanitizedControlDefinition]);
403+
}, [control.control, initialEditorMode]);
412404

413405
useEffect(() => {
414406
reset();

‎ui/src/core/page-components/agent-detail/modals/edit-control/utils.ts‎

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -156,31 +156,17 @@ export function sanitizeControlNamePart(s: string): string {
156156
return sanitized || 'control';
157157
}
158158

159-
/**
160-
* Recursively remove object properties whose values are null.
161-
*/
162-
export function omitNullProperties(value: unknown): unknown {
163-
if (Array.isArray(value)) {
164-
return value.map((item) => omitNullProperties(item));
165-
}
166-
167-
if (value !== null && typeof value === 'object') {
168-
return Object.fromEntries(
169-
Object.entries(value).flatMap(([key, currentValue]) =>
170-
currentValue === null
171-
? []
172-
: [[key, omitNullProperties(currentValue)]]
173-
)
174-
);
175-
}
176-
177-
return value;
178-
}
179-
180159
/**
181160
* Stringify JSON for editor display while omitting object properties whose
182161
* values are null. This keeps the editor aligned with persisted control data.
183162
*/
184163
export function stringifyJsonWithoutNulls(value: unknown): string {
185-
return JSON.stringify(omitNullProperties(value), null, 2) ?? '';
164+
return (
165+
JSON.stringify(
166+
value,
167+
(_key: string, currentValue: unknown) =>
168+
currentValue === null ? undefined : currentValue,
169+
2
170+
) ?? ''
171+
);
186172
}

0 commit comments

Comments
 (0)