Skip to content

Commit 02abcfe

Browse files
committed
fix(playground): address follow-up json patch review
1 parent 0f0c40d commit 02abcfe

3 files changed

Lines changed: 48 additions & 1 deletion

File tree

sites/playground/web/src/components/genui-template/template-chat-utils/json-patch-format.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { JsonPatchOp } from 'jsondiffpatch/formatters/jsonpatch-apply';
33
import { t } from '../../../i18n';
44
import {
55
findComponentPath,
6+
getComponentItem,
67
getPositionRelativePath,
78
mergePath,
89
resolveJsonPointerAppendSentinel,
@@ -82,6 +83,32 @@ function finalizeAbsolutePath(templeSchema: any, item: IFormattedJsonPatchOperat
8283
}
8384
}
8485

86+
function regenerateCopiedNodeIds(templeSchema: any, item: IFormattedJsonPatchOperation): boolean {
87+
if (item.op !== 'copy' || typeof item.path !== 'string') {
88+
return true;
89+
}
90+
91+
const copiedNode = getComponentItem(templeSchema, item.path).node;
92+
if (!copiedNode) {
93+
return false;
94+
}
95+
96+
const resetIds = (node: any) => {
97+
if (!node || typeof node !== 'object') {
98+
return;
99+
}
100+
101+
delete node.id;
102+
if (Array.isArray(node.children)) {
103+
node.children.forEach(resetIds);
104+
}
105+
};
106+
107+
resetIds(copiedNode);
108+
generateIdForComponents(copiedNode);
109+
return true;
110+
}
111+
85112
export const formatJsonPatch = (
86113
currentSchema: any,
87114
value: any[],
@@ -116,6 +143,9 @@ export const formatJsonPatch = (
116143
}
117144

118145
jsonPatchFormatter.patch(templeSchema, [toStandardPatchOp(item)]);
146+
if (!regenerateCopiedNodeIds(templeSchema, item)) {
147+
item.idToPath = null;
148+
}
119149

120150
return item;
121151
});

sites/playground/web/src/components/genui-template/template-chat-utils/json-patch-validate.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ function validateOperation(operation: any): boolean {
2323
return false;
2424
}
2525

26+
if (
27+
operation.op === 'replace' &&
28+
operation.path === undefined &&
29+
(
30+
!operation.value ||
31+
typeof operation.value !== 'object' ||
32+
Array.isArray(operation.value) ||
33+
typeof operation.value.componentName !== 'string' ||
34+
operation.value.componentName.length === 0 ||
35+
typeof operation.value.id !== 'string' ||
36+
operation.value.id.length === 0
37+
)
38+
) {
39+
return false;
40+
}
41+
2642
return true;
2743
}
2844

sites/playground/web/src/components/genui-template/template-chat-utils/schema-path.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ export function getPositionRelativePath(
166166
}
167167
return `../${anchorIndex + 1}`;
168168
} else if (position === 'inside') {
169-
const children = schema ? getComponentItem(schema, componentPath).node?.children : [];
169+
const anchorNode = componentPath === '/' ? schema : getComponentItem(schema, componentPath).node;
170+
const children = anchorNode?.children;
170171
if (!Array.isArray(children)) {
171172
return undefined;
172173
}

0 commit comments

Comments
 (0)