forked from AykutSarac/jsoncrack.com
-
Notifications
You must be signed in to change notification settings - Fork 338
Expand file tree
/
Copy pathindex.tsx
More file actions
202 lines (192 loc) · 6.82 KB
/
index.tsx
File metadata and controls
202 lines (192 loc) · 6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import React from "react";
import type { ModalProps } from "@mantine/core";
import {
Modal,
Stack,
Text,
ScrollArea,
Flex,
CloseButton,
Button,
Textarea,
Group,
} from "@mantine/core";
import { CodeHighlight } from "@mantine/code-highlight";
import { modify, applyEdits } from "jsonc-parser";
import useFile from "../../../store/useFile";
import useJson from "../../../store/useJson";
import type { NodeData } from "../../../types/graph";
import useGraph from "../../editor/views/GraphView/stores/useGraph";
// get the value from the full json string at the node path (JSONPath array)
const getValueAtPath = (jsonStr: string, path?: NodeData["path"]) => {
try {
const root = JSON.parse(jsonStr);
if (!path || path.length === 0) return root;
let cur: any = root;
for (const seg of path) {
if (typeof seg === "number") {
cur = cur?.[seg];
} else {
cur = cur?.[seg as string];
}
if (typeof cur === "undefined") return undefined;
}
return cur;
} catch (e) {
return undefined;
}
};
// fallback formatter: builds a small object from node rows for primitive-only nodes
const normalizeNodeData = (nodeRows: NodeData["text"]) => {
if (!nodeRows || nodeRows.length === 0) return "{}";
if (nodeRows.length === 1 && !nodeRows[0].key) return `${nodeRows[0].value}`;
const obj: Record<string, any> = {};
nodeRows?.forEach(row => {
if (row.type !== "array" && row.type !== "object") {
if (row.key) obj[row.key] = row.value;
}
});
return JSON.stringify(obj, null, 2);
};
// return json path in the format $["customer"]
const jsonPathToString = (path?: NodeData["path"]) => {
if (!path || path.length === 0) return "$";
const segments = path.map(seg => (typeof seg === "number" ? seg : `"${seg}"`));
return `$[${segments.join("][")}]`;
};
export const NodeModal = ({ opened, onClose }: ModalProps) => {
const nodeData = useGraph(state => state.selectedNode);
const json = useJson(state => state.json);
const [editing, setEditing] = React.useState(false);
const [value, setValue] = React.useState("");
React.useEffect(() => {
// reset editing state and editor value when modal opens or selection changes
setEditing(false);
// prefer the actual subtree from the current json so nested children are preserved
const subtree = getValueAtPath(json, nodeData?.path);
if (typeof subtree !== "undefined") {
try {
setValue(
typeof subtree === "string" ? JSON.stringify(subtree) : JSON.stringify(subtree, null, 2)
);
} catch (e) {
// fallback
setValue(normalizeNodeData(nodeData?.text ?? []));
}
} else {
setValue(normalizeNodeData(nodeData?.text ?? []));
}
}, [opened, nodeData]);
return (
<Modal size="auto" opened={opened} onClose={onClose} centered withCloseButton={false}>
<Stack pb="sm" gap="sm">
<Stack gap="xs">
<Flex justify="space-between" align="center">
<Text fz="xs" fw={500}>
Content
</Text>
<Flex align="center" gap="xs">
{!editing ? (
<Button size="xs" variant="outline" onClick={() => setEditing(true)}>
Edit
</Button>
) : (
<Group spacing="xs">
<Button
size="xs"
color="green"
onClick={() => {
// attempt to parse the edited value
let newValue: any = value;
try {
newValue = JSON.parse(value);
} catch (err) {
newValue = value;
}
try {
const path = nodeData?.path ?? [];
const edits = modify(json, path as any, newValue, {
formattingOptions: { insertSpaces: true, tabSize: 2 },
});
const newJson = applyEdits(json, edits);
useJson.getState().setJson(newJson);
// update the left-hand editor contents so sidebar reflects change
useFile.getState().setContents({ contents: newJson, hasChanges: true });
setEditing(false);
onClose?.();
} catch (e) {
console.error("Failed to apply edit", e);
}
}}
>
Save
</Button>
<Button
size="xs"
color="gray"
variant="outline"
onClick={() => {
setEditing(false);
// restore the actual subtree value from current json
const subtree = getValueAtPath(json, nodeData?.path);
if (typeof subtree !== "undefined") {
try {
setValue(
typeof subtree === "string"
? JSON.stringify(subtree)
: JSON.stringify(subtree, null, 2)
);
} catch (e) {
setValue(normalizeNodeData(nodeData?.text ?? []));
}
} else {
setValue(normalizeNodeData(nodeData?.text ?? []));
}
}}
>
Cancel
</Button>
</Group>
)}
<CloseButton onClick={onClose} />
</Flex>
</Flex>
<ScrollArea.Autosize mah={250} maw={600}>
{!editing ? (
<CodeHighlight
code={value || normalizeNodeData(nodeData?.text ?? [])}
miw={350}
maw={600}
language="json"
withCopyButton
/>
) : (
<Textarea
value={value}
onChange={e => setValue(e.currentTarget.value)}
autosize
minRows={4}
maxRows={20}
styles={{ input: { fontFamily: "monospace", fontSize: 12 } }}
/>
)}
</ScrollArea.Autosize>
</Stack>
<Text fz="xs" fw={500}>
JSON Path
</Text>
<ScrollArea.Autosize maw={600}>
<CodeHighlight
code={jsonPathToString(nodeData?.path)}
miw={350}
mah={250}
language="json"
copyLabel="Copy to clipboard"
copiedLabel="Copied to clipboard"
withCopyButton
/>
</ScrollArea.Autosize>
</Stack>
</Modal>
);
};