diff --git a/src/components/MonacoEditor/index.tsx b/src/components/MonacoEditor/index.tsx index e295b5b..48187a3 100644 --- a/src/components/MonacoEditor/index.tsx +++ b/src/components/MonacoEditor/index.tsx @@ -56,8 +56,7 @@ export const MonacoEditor = () => { (monaco: Monaco) => { monaco.editor.onDidChangeMarkers(([uri]) => { const markers = monaco.editor.getModelMarkers({ resource: uri }); - if (markers.length) setError(markers[0].message); - else setError(null); + setError(markers.length); }); }, [setError] @@ -87,7 +86,7 @@ export const MonacoEditor = () => { value={contents} theme={theme} options={editorOptions} - onChange={e => setContents({ contents: e, skipUpdate: true })} + onChange={contents => setContents({ contents, skipUpdate: true })} loading={} beforeMount={handleEditorWillMount} language="json" diff --git a/src/store/useFile.ts b/src/store/useFile.ts index ebb2a3e..61c5b85 100644 --- a/src/store/useFile.ts +++ b/src/store/useFile.ts @@ -16,7 +16,7 @@ type SetContents = { interface JsonActions { getContents: () => string; getHasChanges: () => boolean; - setError: (error: object | null) => void; + setError: (error: boolean) => void; setHasChanges: (hasChanges: boolean) => void; setContents: (data: SetContents) => void; saveToCloud: (isNew?: boolean) => void; @@ -39,7 +39,7 @@ export type File = { const initialStates = { fileData: null as File | null, contents: defaultJson, - error: null as any, + error: false, hasChanges: false, jsonSchema: null as object | null, }; @@ -49,10 +49,9 @@ export type FileStates = typeof initialStates; const isURL = /(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})/gi; -const debouncedUpdateJson = debounce( - (value: unknown) => useJson.getState().setJson(JSON.stringify(value, null, 2)), - 800 -); +const debouncedUpdateJson = debounce((value: unknown) => { + if (!useFile.getState().error) useJson.getState().setJson(JSON.stringify(value, null, 2)); +}, 800); const useFile = create()((set, get) => ({ ...initialStates, @@ -69,14 +68,9 @@ const useFile = create()((set, get) => ({ }, getContents: () => get().contents, getHasChanges: () => get().hasChanges, - setContents: async ({ contents, hasChanges = true }) => { - try { - set({ ...(contents && { contents }), error: null, hasChanges }); - debouncedUpdateJson(contents); - } catch (error: any) { - if (error?.mark?.snippet) return set({ error: error.mark.snippet }); - if (error?.message) set({ error: error.message }); - } + setContents: ({ contents, hasChanges = true }) => { + set({ ...(contents && { contents }), hasChanges }); + debouncedUpdateJson(contents); }, setError: error => set({ error }), setHasChanges: hasChanges => set({ hasChanges }),