Skip to content

Commit 62ec614

Browse files
committed
feat: wasm loading error state
1 parent ee4da3a commit 62ec614

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

src/App.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ import { Editor } from "./Editor";
33
import { Logo } from "./components/Logo";
44
import { Preview } from "./Preview";
55
import { useStore } from "@/store";
6-
import { useEffect, type FC } from "react";
6+
import { useEffect } from "react";
77

88
// Glue code required to be able to run wasm compiled Go code.
99
import "@/utils/wasm_exec.js";
10-
import { LoaderIcon } from "lucide-react";
1110

1211
type GoPreviewDef = (v: unknown) => Promise<string>;
1312

@@ -32,8 +31,8 @@ declare class Go {
3231
}
3332

3433
export const App = () => {
35-
const $isWasmLoaded = useStore((state) => state.isWasmLoaded);
36-
const $setIsWasmLoaded = useStore((state) => state.setIsWasmLoaded);
34+
const $wasmState = useStore((state) => state.wasmState);
35+
const $setWasmState = useStore((state) => state.setWasmState);
3736

3837
// biome-ignore lint/correctness/useExhaustiveDependencies: <explanation>
3938
useEffect(() => {
@@ -46,13 +45,14 @@ export const App = () => {
4645
);
4746

4847
goWasm.run(result.instance);
49-
$setIsWasmLoaded(true);
48+
$setWasmState("loaded");
5049
} catch (e) {
50+
$setWasmState("error");
5151
console.error(e);
5252
}
5353
};
5454

55-
if (!$isWasmLoaded) {
55+
if ($wasmState !== "loaded") {
5656
initWasm();
5757
}
5858
}, []);
@@ -97,7 +97,7 @@ export const App = () => {
9797
</nav>
9898

9999
<ResizablePanelGroup
100-
aria-hidden={!$isWasmLoaded}
100+
aria-hidden={!$wasmState}
101101
direction={"horizontal"}
102102
>
103103
{/* EDITOR */}

src/Preview.tsx

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,20 @@ import type { ImperativePanelHandle } from "react-resizable-panels";
1010
import { useStore } from "@/store";
1111

1212
export const Preview: FC = () => {
13-
const $isWasmLoaded = useStore((state) => state.isWasmLoaded);
13+
const $wasmState = useStore((state) => state.wasmState);
1414

1515
return (
1616
<ResizablePanel className="relative">
17-
{!$isWasmLoaded ? (
17+
{$wasmState !== "loaded" ? (
1818
<div className="absolute top-0 left-0 z-10 flex h-full w-full items-center justify-center backdrop-blur-sm">
19-
<WasmLoading />
19+
{$wasmState === "loading" ? <WasmLoading /> : <WasmError />}
2020
</div>
2121
) : null}
2222

23-
<div aria-hidden={!$isWasmLoaded} className="flex h-full w-full flex-col items-start gap-6 p-8">
23+
<div
24+
aria-hidden={$wasmState !== "loaded"}
25+
className="flex h-full w-full flex-col items-start gap-6 p-8"
26+
>
2427
<div className="flex w-full items-center justify-between">
2528
<p className="font-semibold text-3xl text-content-primary">
2629
Parameters
@@ -122,7 +125,7 @@ const WasmLoading: FC = () => {
122125
<LoaderIcon className="animate-spin text-content-primary" />
123126
<div className="text-center">
124127
<p className="font-semibold text-content-primary text-xl">
125-
Loading Assets
128+
Loading assets
126129
</p>
127130
<p className="text-content-secondary text-sm">
128131
Add some copy here to explain that this will only take a few moments
@@ -131,3 +134,16 @@ const WasmLoading: FC = () => {
131134
</div>
132135
);
133136
};
137+
138+
const WasmError: FC = () => {
139+
return (
140+
<div className="flex w-full max-w-xs flex-col items-center justify-center gap-2 rounded-xl border border-border-destructive bg-surface-tertiary p-4 text-center">
141+
<p className="font-semibold text-content-primary text-xl">
142+
Unable to load assets{" "}
143+
</p>
144+
<p className="text-content-destructive text-sm">
145+
Add some copy here to explain that this will only take a few moments
146+
</p>
147+
</div>
148+
);
149+
};

src/store.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,21 @@ const defaultCode = `terraform {
88
}
99
}`;
1010

11+
type WasmState = "loaded" | "loading" | "error";
12+
1113
type State = {
1214
code: string;
13-
isWasmLoaded: boolean;
15+
wasmState: WasmState;
1416
error?: string;
1517
setCode: (code: string) => void;
1618
setError: (error: string) => void;
17-
setIsWasmLoaded: (isWasmLoaded: boolean) => void;
19+
setWasmState: (wasmState: WasmState) => void;
1820
};
1921

2022
export const useStore = create<State>()((set) => ({
2123
code: defaultCode,
22-
isWasmLoaded: false,
24+
wasmState: "loading",
2325
setCode: (code) => set((_) => ({ code })),
2426
setError: (error) => set((_) => ({ error })),
25-
setIsWasmLoaded: (isWasmLoaded) => set((_) => ({ isWasmLoaded })),
27+
setWasmState: (wasmState) => set((_) => ({ wasmState })),
2628
}));

0 commit comments

Comments
 (0)