Skip to content

Commit f99c7eb

Browse files
committed
chore: add zustand store
1 parent 16291cb commit f99c7eb

File tree

8 files changed

+92
-33
lines changed

8 files changed

+92
-33
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"react-resizable-panels": "^3.0.2",
2424
"react-simple-code-editor": "^0.14.1",
2525
"tailwind-merge": "^3.3.0",
26-
"tailwindcss-animate": "^1.0.7"
26+
"tailwindcss-animate": "^1.0.7",
27+
"zustand": "^5.0.5"
2728
},
2829
"devDependencies": {
2930
"@eslint/js": "^9.25.0",

pnpm-lock.yaml

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/build/preview.wasm

78.3 MB
Binary file not shown.

src/Editor.tsx

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
import { ResizablePanel } from "@/components/Resizable";
21
import { Button } from "@/components/Button";
2+
import { ResizablePanel } from "@/components/Resizable";
33
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/Tooltip";
4-
// @ts-expect-error TODO: figure this out and fix
5-
import { highlight, languages } from "prismjs/components/prism-core";
64
import { type FC, useEffect, useRef, useState } from "react";
75
import CodeEditor from "react-simple-code-editor";
6+
import { useStore } from "@/store";
7+
import { CheckIcon, CopyIcon, FileJsonIcon, SettingsIcon } from "lucide-react";
8+
9+
// The following imports can't be re-ordered otherwise things break
10+
import { highlight, languages } from "prismjs/components/prism-core";
811
import "prismjs/components/prism-hcl";
912
import "prismjs/themes/prism.css";
10-
import { CheckIcon, CopyIcon, FileJsonIcon, SettingsIcon } from "lucide-react";
1113

1214
// Adds line numbers to the highlight.
1315
const hightlightWithLineNumbers = (input: string, language: unknown) =>
@@ -20,15 +22,16 @@ const hightlightWithLineNumbers = (input: string, language: unknown) =>
2022
.join("\n");
2123

2224
export const Editor: FC = () => {
23-
const [code, setCode] = useState(() => defaultCode);
25+
const $code = useStore((state) => state.code);
26+
const $setCode = useStore((state) => state.setCode);
2427

2528
const [codeCopied, setCodeCopied] = useState(() => false);
2629
const copyTimeoutId = useRef<ReturnType<typeof setTimeout> | undefined>(
2730
undefined,
2831
);
2932

3033
const onCopy = () => {
31-
navigator.clipboard.writeText(code);
34+
navigator.clipboard.writeText($code);
3235
setCodeCopied(() => true);
3336
};
3437

@@ -78,8 +81,8 @@ export const Editor: FC = () => {
7881
{/* CODE EDITOR */}
7982
<div className="h-full w-full overflow-y-scroll bg-surface-secondary font-mono">
8083
<CodeEditor
81-
value={code}
82-
onValueChange={(code) => setCode(() => code)}
84+
value={$code}
85+
onValueChange={(code) => $setCode(code)}
8386
highlight={(code) => hightlightWithLineNumbers(code, languages.hcl)}
8487
textareaId="codeArea"
8588
className="editor pt-3"
@@ -88,11 +91,3 @@ export const Editor: FC = () => {
8891
</ResizablePanel>
8992
);
9093
};
91-
92-
const defaultCode = `terraform {
93-
required_providers {
94-
coder = {
95-
source = "coder/coder"
96-
}
97-
}
98-
}`;

src/Preview.tsx

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,11 @@ import {
77
import { ActivityIcon, ExternalLinkIcon } from "lucide-react";
88
import { useRef, useState, type FC } from "react";
99
import type { ImperativePanelHandle } from "react-resizable-panels";
10+
import { useStore } from "@/store";
1011

1112
export const Preview: FC = () => {
12-
const [errorPanelSize, setErrorPanelSize] = useState(() => 50);
13-
const errorPanelRef = useRef<ImperativePanelHandle>(null);
14-
15-
const onCollapseError = () => {
16-
errorPanelRef.current?.collapse();
17-
};
18-
1913
return (
2014
<ResizablePanel className="relative flex flex-col items-start gap-6 p-8">
21-
<div
22-
className="pointer-events-none absolute top-0 left-0 h-full w-full bg-black"
23-
style={{ opacity: errorPanelSize / 100 }}
24-
>
25-
{/* OVERLAY */}
26-
</div>
27-
2815
<div className="flex w-full items-center justify-between">
2916
<p className="font-semibold text-3xl text-content-primary">
3017
Parameters
@@ -65,6 +52,34 @@ export const Preview: FC = () => {
6552
</div>
6653
</div>
6754

55+
<ErrorPane />
56+
</ResizablePanel>
57+
);
58+
};
59+
60+
const ErrorPane = () => {
61+
const $error = useStore((state) => state.error);
62+
63+
const [errorPanelSize, setErrorPanelSize] = useState(() => 50);
64+
const errorPanelRef = useRef<ImperativePanelHandle>(null);
65+
66+
const onCollapseError = () => {
67+
errorPanelRef.current?.collapse();
68+
};
69+
70+
if (!$error) {
71+
return null;
72+
}
73+
74+
return (
75+
<>
76+
<div
77+
className="pointer-events-none absolute top-0 left-0 h-full w-full bg-black"
78+
style={{ opacity: errorPanelSize / 100 }}
79+
>
80+
{/* OVERLAY */}
81+
</div>
82+
6883
<ResizablePanelGroup
6984
direction="vertical"
7085
className="pointer-events-none absolute top-0 left-0"
@@ -87,6 +102,6 @@ export const Preview: FC = () => {
87102
}}
88103
></ResizablePanel>
89104
</ResizablePanelGroup>
90-
</ResizablePanel>
105+
</>
91106
);
92107
};

src/main.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import "@fontsource-variable/inter";
2-
32
import { TooltipProvider } from "@/components/Tooltip";
43
import { StrictMode } from "react";
54
import { createRoot } from "react-dom/client";

src/store.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { create } from "zustand";
2+
3+
const defaultCode = `terraform {
4+
required_providers {
5+
coder = {
6+
source = "coder/coder"
7+
}
8+
}
9+
}`;
10+
11+
type State = {
12+
code: string;
13+
error?: string;
14+
setCode: (code: string) => void;
15+
setError: (error: string) => void;
16+
};
17+
18+
export const useStore = create<State>()((set) => ({
19+
code: defaultCode,
20+
setCode: (code) => set((_) => ({ code })),
21+
setError: (error) => set((_) => ({ error })),
22+
}));
23+
File renamed without changes.

0 commit comments

Comments
 (0)