Skip to content

Commit 9d1e71e

Browse files
committed
chore: move errors over from zustand
1 parent f87dd05 commit 9d1e71e

File tree

3 files changed

+33
-61
lines changed

3 files changed

+33
-61
lines changed

src/client/Editor.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
} from "@/client/components/Tooltip";
1616
import { useTheme } from "@/client/contexts/theme";
1717
import { multiSelect, radio, switchInput, textInput } from "@/client/snippets";
18-
import { useStore } from "@/client/store";
1918
import type { ParameterFormType } from "@/gen/types";
2019
import { cn } from "@/utils/cn";
2120
import { Editor as MonacoEditor } from "@monaco-editor/react";
@@ -41,8 +40,6 @@ type EditorProps = {
4140
export const Editor: FC<EditorProps> = ({ code, setCode }) => {
4241
const { appliedTheme } = useTheme();
4342

44-
const $setEditor = useStore((state) => state.setEditor);
45-
4643
const [codeCopied, setCodeCopied] = useState(() => false);
4744
const copyTimeoutId = useRef<ReturnType<typeof setTimeout> | undefined>(
4845
undefined,
@@ -161,7 +158,9 @@ export const Editor: FC<EditorProps> = ({ code, setCode }) => {
161158
<div className="h-full w-full bg-surface-secondary font-mono">
162159
<MonacoEditor
163160
value={code}
164-
onMount={(editor) => $setEditor(editor)}
161+
onMount={(editor) => {
162+
// $setEditor(editor);
163+
}}
165164
onChange={(code) => {
166165
if (code !== undefined) {
167166
setCode(code);

src/client/Preview.tsx

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ import {
1717
} from "@/client/components/Select";
1818
import * as Tabs from "@/client/components/Tabs";
1919
import { useTheme } from "@/client/contexts/theme";
20-
import type { Diagnostic } from "@/client/diagnostics";
21-
import { useStore } from "@/client/store";
20+
import { outputToDiagnostics, type Diagnostic } from "@/client/diagnostics";
2221
import type {
2322
ParameterWithSource,
2423
ParserLog,
@@ -70,10 +69,12 @@ export const Preview: FC<PreviewProps> = ({
7069
onReset,
7170
setOwner,
7271
}) => {
73-
const $errors = useStore((state) => state.errors);
7472
const [params] = useSearchParams();
7573
const isDebug = params.has("debug");
7674
const [tab, setTab] = useState(() => "preview");
75+
const [showErrors, setShowErrors] = useState(true);
76+
77+
const errors = output ? outputToDiagnostics(output) : [];
7778

7879
const onDownloadOutput = () => {
7980
const blob = new Blob([JSON.stringify(output, null, 2)], {
@@ -204,13 +205,12 @@ export const Preview: FC<PreviewProps> = ({
204205
<Tabs.Content value="preview" asChild={true}>
205206
<div
206207
aria-hidden={
207-
wasmLoadState !== "loaded" ||
208-
($errors.show && $errors.diagnostics.length > 0)
208+
wasmLoadState !== "loaded" || (showErrors && errors.length > 0)
209209
}
210210
className={cn(
211211
"flex h-full w-full flex-col items-start gap-4 p-5 ",
212212
(wasmLoadState !== "loaded" ||
213-
($errors.show && $errors.diagnostics.length > 0)) &&
213+
(showErrors && errors.length > 0)) &&
214214
"pointer-events-none",
215215
isDebug && "max-h-[calc(100%-48px)]",
216216
)}
@@ -267,7 +267,11 @@ export const Preview: FC<PreviewProps> = ({
267267
<Debugger output={output} />
268268
</Tabs.Content>
269269

270-
<ErrorPane />
270+
<ErrorPane
271+
errors={errors}
272+
setShowErrors={setShowErrors}
273+
showErrors={showErrors}
274+
/>
271275
</ResizablePanel>
272276
</Tabs.Root>
273277
);
@@ -303,16 +307,22 @@ const PreviewEmptyState = () => {
303307
);
304308
};
305309

306-
const ErrorPane = () => {
307-
const $errors = useStore((state) => state.errors);
308-
const $toggleShowError = useStore((state) => state.toggleShowError);
309-
310-
const hasErrors = useMemo(() => $errors.diagnostics.length > 0, [$errors]);
310+
type ErrorPaneProps = {
311+
errors: Diagnostic[];
312+
showErrors: boolean;
313+
setShowErrors: React.Dispatch<React.SetStateAction<boolean>>;
314+
};
315+
const ErrorPane: FC<ErrorPaneProps> = ({
316+
errors,
317+
setShowErrors,
318+
showErrors,
319+
}) => {
320+
const hasErrors = errors.length > 0;
311321

312322
return (
313323
<>
314324
<AnimatePresence propagate={true}>
315-
{$errors.show && hasErrors ? (
325+
{showErrors && hasErrors ? (
316326
// lint/a11y/useKeyWithClickEvents: key events don't seem to
317327
// work for divs, and I'm otherwise not sure how to make this element
318328
// more accesible. But I think it's fine since the functionality is able to
@@ -324,7 +334,7 @@ const ErrorPane = () => {
324334
aria-hidden={true}
325335
className="absolute top-0 left-0 h-full w-full cursor-pointer bg-black/10 dark:bg-black/50"
326336
onClick={() => {
327-
$toggleShowError(false);
337+
setShowErrors(false);
328338
}}
329339
>
330340
{/* OVERLAY */}
@@ -342,21 +352,21 @@ const ErrorPane = () => {
342352
exit={{ opacity: 0 }}
343353
className={cn(
344354
"absolute bottom-0 left-0 flex max-h-[60%] w-full flex-col justify-start",
345-
$errors.show && "h-auto",
355+
showErrors && "h-auto",
346356
)}
347357
>
348358
<motion.button
349359
className="flex h-4 min-h-4 w-full items-center justify-center rounded-t-xl bg-border-destructive"
350-
onClick={() => $toggleShowError()}
360+
onClick={() => setShowErrors((curr) => !curr)}
351361
aria-label={
352-
$errors.show ? "Hide error dialog" : "Show error dialog"
362+
showErrors ? "Hide error dialog" : "Show error dialog"
353363
}
354364
>
355365
<div className="h-0.5 w-2/3 max-w-32 rounded-full bg-white/40"></div>
356366
</motion.button>
357367

358368
<AnimatePresence propagate={true}>
359-
{$errors.show ? (
369+
{showErrors ? (
360370
<motion.div
361371
initial={{ height: 0 }}
362372
animate={{
@@ -366,7 +376,7 @@ const ErrorPane = () => {
366376
className="flex flex-col gap-6 overflow-y-scroll bg-surface-secondary"
367377
>
368378
<div className="flex w-full flex-col gap-3 p-6">
369-
{$errors.diagnostics.map((diagnostic, index) => (
379+
{errors.map((diagnostic, index) => (
370380
<ErrorBlock diagnostic={diagnostic} key={index} />
371381
))}
372382
</div>

src/client/store.tsx

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,10 @@
1-
import type { Diagnostic } from "@/client/diagnostics";
21
import type { editor } from "monaco-editor";
32
import { create } from "zustand";
4-
import { defaultCode } from "./snippets";
5-
import { mockUsers } from "@/owner";
6-
7-
type ErrorsState = {
8-
diagnostics: Diagnostic[];
9-
show: boolean;
10-
};
11-
const defaultErrorsState: ErrorsState = {
12-
diagnostics: [],
13-
show: true,
14-
};
153

164
type State = {
175
editor: editor.IStandaloneCodeEditor | null;
18-
errors: ErrorsState;
19-
setError: (diagnostics: Diagnostic[]) => void;
20-
toggleShowError: (open?: boolean) => void;
21-
setEditor: (editor: editor.IStandaloneCodeEditor) => void;
226
};
237

24-
export const useStore = create<State>()((set) => ({
25-
code: window.CODE ?? defaultCode,
8+
export const useStore = create<State>()(() => ({
269
editor: null,
27-
owner: mockUsers.admin,
28-
errors: defaultErrorsState,
29-
setError: (data) =>
30-
set((state) => {
31-
const errors = state.errors ?? defaultErrorsState;
32-
return {
33-
errors: { ...errors, diagnostics: data },
34-
};
35-
}),
36-
toggleShowError: (open) =>
37-
set((state) => {
38-
const errors = state.errors ?? defaultErrorsState;
39-
return {
40-
errors: {
41-
...errors,
42-
show: open !== undefined ? open : !errors.show,
43-
},
44-
};
45-
}),
46-
setEditor: (editor) => set(() => ({ editor })),
4710
}));

0 commit comments

Comments
 (0)