Skip to content

🎥 Make videos programmatically with React

WIP: Reset array in Zod

Jonny Burgerd7fa8cb29d96
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/SchemaLabel.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/SchemaLabel.tsx
index 4cd3f7aabd..aae21d029b 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/SchemaLabel.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/SchemaLabel.tsx
@@ -68,11 +68,14 @@ export const SchemaLabel: React.FC<{
export const SchemaFieldsetLabel: React.FC<{
jsonPath: JSONPath;
onRemove: null | (() => void);
-}> = ({jsonPath, onRemove}) => {
+ isDefaultValue: boolean;
+ onReset: () => void;
+}> = ({jsonPath, onRemove, isDefaultValue, onReset}) => {
return (
<legend style={fieldsetLabel}>
{getSchemaLabel(jsonPath)}
<Flex />
+ {isDefaultValue ? null : <SchemaResetButton onClick={onReset} />}
{onRemove ? (
<>
<Spacing x={1} /> <InlineRemoveButton onClick={onRemove} />
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodArrayEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodArrayEditor.tsx
index 2199fab6ef..c0471ab831 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodArrayEditor.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodArrayEditor.tsx
@@ -32,7 +32,6 @@ type LocalState = {
revision: number;
};
-// TODO: Ability to revert a change (e.g entry deletion )
export const ZodArrayEditor: React.FC<{
schema: z.ZodTypeAny;
jsonPath: JSONPath;
@@ -126,12 +125,25 @@ export const ZodArrayEditor: React.FC<{
}, true);
}, [def.type, onChange, z, zodTypes]);
+ const reset = useCallback(() => {
+ onChange(() => defaultValue, true);
+ }, [defaultValue, onChange]);
+
+ const isDefaultValue = useMemo(() => {
+ return deepEqual(localValue.value, defaultValue);
+ }, [defaultValue, localValue.value]);
+
return (
<div style={style}>
<div style={fullWidth}>
<Element style={fieldset}>
{isRoot ? null : (
- <SchemaFieldsetLabel jsonPath={jsonPath} onRemove={onRemove} />
+ <SchemaFieldsetLabel
+ onReset={reset}
+ isDefaultValue={isDefaultValue}
+ jsonPath={jsonPath}
+ onRemove={onRemove}
+ />
)}
<div style={isRoot ? undefined : container}>
{localValue.value.map((child, i) => {
@@ -170,3 +182,34 @@ export const ZodArrayEditor: React.FC<{
</div>
);
};
+
+// eslint-disable-next-line @typescript-eslint/no-explicit-any
+function deepEqual(a: any, b: any): boolean {
+ if (a === b) {
+ return true;
+ }
+
+ if (
+ typeof a !== 'object' ||
+ a === null ||
+ typeof b !== 'object' ||
+ b === null
+ ) {
+ return false;
+ }
+
+ const keysA = Object.keys(a);
+ const keysB = Object.keys(b);
+
+ if (keysA.length !== keysB.length) {
+ return false;
+ }
+
+ for (const key of keysA) {
+ if (!keysB.includes(key) || !deepEqual(a[key], b[key])) {
+ return false;
+ }
+ }
+
+ return true;
+}
diff --git a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodObjectEditor.tsx b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodObjectEditor.tsx
index 088fc9bd84..fc3b8c34d1 100644
--- a/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodObjectEditor.tsx
+++ b/packages/cli/src/editor/components/RenderModal/SchemaEditor/ZodObjectEditor.tsx
@@ -1,4 +1,4 @@
-import React, {useMemo} from 'react';
+import React, {useCallback, useMemo} from 'react';
import type {z} from 'zod';
import {INPUT_BORDER_COLOR_UNHOVERED} from '../../../helpers/colors';
import {useZodIfPossible} from '../../get-zod-if-possible';
@@ -76,12 +76,19 @@ export const ZodObjectEditor: React.FC<{
return {paddingTop};
}, [isRoot, paddingTop]);
+ const onRes = useCallback(() => undefined, []);
+
return (
<div style={style}>
<div style={fullWidth}>
<Element style={fieldset}>
{isRoot ? null : (
- <SchemaFieldsetLabel jsonPath={jsonPath} onRemove={onRemove} />
+ <SchemaFieldsetLabel
+ isDefaultValue
+ onReset={onRes}
+ jsonPath={jsonPath}
+ onRemove={onRemove}
+ />
)}
<div style={isRoot ? undefined : container}>
{keys.map((key) => {