forked from AykutSarac/jsoncrack.com
-
Notifications
You must be signed in to change notification settings - Fork 338
Expand file tree
/
Copy pathNodeContentModal.tsx
More file actions
194 lines (169 loc) · 4.67 KB
/
NodeContentModal.tsx
File metadata and controls
194 lines (169 loc) · 4.67 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
// src/features/modals/NodeContentModal.tsx
import React from "react";
import {
Modal,
Button,
Group,
TextInput,
Text,
Stack,
Code,
} from "@mantine/core";
interface NodeContentModalProps {
opened: boolean;
onClose: () => void;
// The value stored at that node (usually an object)
value: any | null;
// JSON path string like ${["fruits"][0]}
jsonPath: string | null;
// Called when the user saves changes
onSave: (updatedValue: any) => void;
}
const NodeContentModal: React.FC<NodeContentModalProps> = ({
opened,
onClose,
value,
jsonPath,
onSave,
}) => {
const [isEditing, setIsEditing] = React.useState(false);
const [fields, setFields] = React.useState<Record<string, string>>({});
// Whenever we open the modal or change node, reset local form state
React.useEffect(() => {
if (!opened) {
setIsEditing(false);
setFields({});
return;
}
if (value && typeof value === "object" && !Array.isArray(value)) {
const next: Record<string, string> = {};
Object.entries(value).forEach(([key, v]) => {
next[key] = v == null ? "" : String(v);
});
setFields(next);
} else {
// Primitive value: treat as single field called "value"
setFields({ value: value == null ? "" : String(value) });
}
}, [opened, value]);
const handleFieldChange = (key: string, newVal: string) => {
setFields((prev) => ({ ...prev, [key]: newVal }));
};
const handleCancelEdit = () => {
// Discard edits and go back to JSON view
setIsEditing(false);
if (value && typeof value === "object" && !Array.isArray(value)) {
const reset: Record<string, string> = {};
Object.entries(value).forEach(([key, v]) => {
reset[key] = v == null ? "" : String(v);
});
setFields(reset);
} else {
setFields({ value: value == null ? "" : String(value) });
}
};
const handleSave = () => {
let updated: any;
if (value && typeof value === "object" && !Array.isArray(value)) {
updated = { ...(value as any) };
Object.entries(fields).forEach(([key, text]) => {
const trimmed = text.trim();
let parsed: any;
if (trimmed === "") {
parsed = "";
} else {
// Try to parse as JSON literal, fall back to string
try {
parsed = JSON.parse(trimmed);
} catch {
parsed = text;
}
}
updated[key] = parsed;
});
} else {
const onlyKey = Object.keys(fields)[0];
const text = fields[onlyKey];
const trimmed = text.trim();
try {
updated = JSON.parse(trimmed);
} catch {
updated = text;
}
}
onSave(updated);
setIsEditing(false);
};
const prettyJson =
value !== undefined ? JSON.stringify(value, null, 2) : "";
return (
<Modal
opened={opened}
onClose={() => {
setIsEditing(false);
onClose();
}}
title="Content"
centered
size="lg"
>
{/* VIEW MODE: pretty JSON + Edit button */}
{!isEditing && (
<Stack gap="sm">
<Text fw={600}>Content</Text>
<pre
style={{
margin: 0,
padding: "12px 16px",
background: "#141414",
borderRadius: 8,
fontSize: 13,
maxHeight: 260,
overflow: "auto",
}}
>
<code>{prettyJson}</code>
</pre>
<Text fw={600} mt="sm">
JSON Path
</Text>
<Code>{jsonPath ?? "-"}</Code>
<Group justify="flex-end" mt="md">
<Button onClick={() => setIsEditing(true)}>Edit</Button>
</Group>
</Stack>
)}
{/* EDIT MODE: individual fields + Save / Cancel */}
{isEditing && (
<Stack gap="sm">
{Object.entries(fields).map(([key, val]) => (
<div key={key}>
<Text fw={600} mb={4}>
{key}
</Text>
<TextInput
value={val}
onChange={(e) =>
handleFieldChange(key, e.currentTarget.value)
}
/>
</div>
))}
<Text fw={600} mt="sm">
JSON Path
</Text>
<Code>{jsonPath ?? "-"}</Code>
<Group justify="flex-end" mt="md">
<Button variant="subtle" color="gray" onClick={handleCancelEdit}>
Cancel
</Button>
<Button color="green" onClick={handleSave}>
Save
</Button>
</Group>
</Stack>
)}
</Modal>
);
};
export default NodeContentModal;