Skip to content

✨ Innovative and open-source visualization application that transforms various data formats, such as JSON, YAML, XML and CSV into interactive graphs.

fix: prevent double parsing

AykutSarac1840496620bf
diff --git a/src/components/MonacoEditor/index.tsx b/src/components/MonacoEditor/index.tsx
index 9e5e8c47..626c66b1 100644
--- a/src/components/MonacoEditor/index.tsx
+++ b/src/components/MonacoEditor/index.tsx
@@ -69,6 +69,15 @@ export const MonacoEditor = () => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [debouncedSetJson, hasError, value]);
+ const handleChange = React.useCallback((value?: string) => {
+ try {
+ const parsedJson = JSON.stringify(JSON.parse(value!), null, 2);
+ setValue(parsedJson);
+ } catch (error) {
+ setValue(value);
+ }
+ }, []);
+
React.useEffect(() => {
const beforeunload = (e: BeforeUnloadEvent) => {
if (getHasChanges()) {
@@ -93,7 +102,7 @@ export const MonacoEditor = () => {
value={json}
theme={lightmode}
options={editorOptions}
- onChange={setValue}
+ onChange={handleChange}
loading={<Loading message="Loading Editor..." />}
beforeMount={handleEditorWillMount}
defaultLanguage="json"
diff --git a/src/store/useJson.tsx b/src/store/useJson.tsx
index aa0310b3..2f89f3ba 100644
--- a/src/store/useJson.tsx
+++ b/src/store/useJson.tsx
@@ -85,7 +85,7 @@ const useJson = create<JsonStates & JsonActions>()((set, get) => ({
},
setJson: json => {
useGraph.getState().setGraph(json);
- set({ json: JSON.stringify(JSON.parse(json), null, 2), hasChanges: true });
+ set({ json, hasChanges: true });
},
saveJson: async (isNew = true) => {
try {