fix: parsing with error

This commit is contained in:
AykutSarac 2023-05-15 11:59:35 +03:00
parent 1400f755b0
commit d8cefd5cd5
No known key found for this signature in database
2 changed files with 10 additions and 17 deletions

View File

@ -56,8 +56,7 @@ export const MonacoEditor = () => {
(monaco: Monaco) => { (monaco: Monaco) => {
monaco.editor.onDidChangeMarkers(([uri]) => { monaco.editor.onDidChangeMarkers(([uri]) => {
const markers = monaco.editor.getModelMarkers({ resource: uri }); const markers = monaco.editor.getModelMarkers({ resource: uri });
if (markers.length) setError(markers[0].message); setError(markers.length);
else setError(null);
}); });
}, },
[setError] [setError]
@ -87,7 +86,7 @@ export const MonacoEditor = () => {
value={contents} value={contents}
theme={theme} theme={theme}
options={editorOptions} options={editorOptions}
onChange={e => setContents({ contents: e, skipUpdate: true })} onChange={contents => setContents({ contents, skipUpdate: true })}
loading={<Loading message="Loading Editor..." />} loading={<Loading message="Loading Editor..." />}
beforeMount={handleEditorWillMount} beforeMount={handleEditorWillMount}
language="json" language="json"

View File

@ -16,7 +16,7 @@ type SetContents = {
interface JsonActions { interface JsonActions {
getContents: () => string; getContents: () => string;
getHasChanges: () => boolean; getHasChanges: () => boolean;
setError: (error: object | null) => void; setError: (error: boolean) => void;
setHasChanges: (hasChanges: boolean) => void; setHasChanges: (hasChanges: boolean) => void;
setContents: (data: SetContents) => void; setContents: (data: SetContents) => void;
saveToCloud: (isNew?: boolean) => void; saveToCloud: (isNew?: boolean) => void;
@ -39,7 +39,7 @@ export type File = {
const initialStates = { const initialStates = {
fileData: null as File | null, fileData: null as File | null,
contents: defaultJson, contents: defaultJson,
error: null as any, error: false,
hasChanges: false, hasChanges: false,
jsonSchema: null as object | null, jsonSchema: null as object | null,
}; };
@ -49,10 +49,9 @@ export type FileStates = typeof initialStates;
const isURL = 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; /(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( const debouncedUpdateJson = debounce((value: unknown) => {
(value: unknown) => useJson.getState().setJson(JSON.stringify(value, null, 2)), if (!useFile.getState().error) useJson.getState().setJson(JSON.stringify(value, null, 2));
800 }, 800);
);
const useFile = create<FileStates & JsonActions>()((set, get) => ({ const useFile = create<FileStates & JsonActions>()((set, get) => ({
...initialStates, ...initialStates,
@ -69,14 +68,9 @@ const useFile = create<FileStates & JsonActions>()((set, get) => ({
}, },
getContents: () => get().contents, getContents: () => get().contents,
getHasChanges: () => get().hasChanges, getHasChanges: () => get().hasChanges,
setContents: async ({ contents, hasChanges = true }) => { setContents: ({ contents, hasChanges = true }) => {
try { set({ ...(contents && { contents }), hasChanges });
set({ ...(contents && { contents }), error: null, hasChanges }); debouncedUpdateJson(contents);
debouncedUpdateJson(contents);
} catch (error: any) {
if (error?.mark?.snippet) return set({ error: error.mark.snippet });
if (error?.message) set({ error: error.message });
}
}, },
setError: error => set({ error }), setError: error => set({ error }),
setHasChanges: hasChanges => set({ hasChanges }), setHasChanges: hasChanges => set({ hasChanges }),