Skip to content

Commit 3221b21

Browse files
committed
feat: share button
1 parent 4086338 commit 3221b21

File tree

5 files changed

+62
-17
lines changed

5 files changed

+62
-17
lines changed

src/client/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export const App = () => {
6666
fetch(
6767
import.meta.env.PROD
6868
? "/assets/build/preview.wasm"
69-
: "build/preview.wasm",
69+
: "/build/preview.wasm",
7070
),
7171
goWasm.importObject,
7272
);

src/client/Editor.tsx

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import {
88
} from "@/client/components/DropdownMenu";
99
import { ResizablePanel } from "@/client/components/Resizable";
1010
import * as Tabs from "@/client/components/Tabs";
11-
import { Tooltip, TooltipContent, TooltipTrigger } from "@/client/components/Tooltip";
11+
import {
12+
Tooltip,
13+
TooltipContent,
14+
TooltipTrigger,
15+
} from "@/client/components/Tooltip";
1216
import { useStore } from "@/client/store";
1317
import {
1418
BookIcon,
@@ -31,6 +35,8 @@ import { highlight, languages } from "prismjs/components/prism-core";
3135
import "prismjs/components/prism-hcl";
3236
import "prismjs/themes/prism.css";
3337
import { cn } from "@/utils/cn";
38+
import { rpc } from "@/utils/rpc";
39+
import { useParams } from "react-router";
3440

3541
// Adds line numbers to the highlight.
3642
const hightlightWithLineNumbers = (input: string, language: unknown) =>
@@ -43,6 +49,7 @@ const hightlightWithLineNumbers = (input: string, language: unknown) =>
4349
.join("\n");
4450

4551
export const Editor: FC = () => {
52+
const params = useParams();
4653
const $code = useStore((state) => state.code);
4754
const $setCode = useStore((state) => state.setCode);
4855

@@ -58,6 +65,28 @@ export const Editor: FC = () => {
5865
setCodeCopied(() => true);
5966
};
6067

68+
useEffect(() => {
69+
const loadCode = async () => {
70+
const { id } = params;
71+
if (!id) {
72+
return;
73+
}
74+
75+
try {
76+
const res = await rpc.parameters[":id"].$get({ param: { id } });
77+
if (res.ok) {
78+
const { code } = await res.json();
79+
$setCode(code);
80+
}
81+
} catch (e) {
82+
console.error(`Error loading playground: ${e}`);
83+
return;
84+
}
85+
};
86+
87+
loadCode();
88+
}, [params, $setCode]);
89+
6190
useEffect(() => {
6291
if (!codeCopied) {
6392
return;
@@ -136,7 +165,12 @@ export const Editor: FC = () => {
136165
tab !== "code" && "hidden",
137166
)}
138167
>
139-
<Button className="pointer-events-auto z-10" variant="subtle" size="sm" onClick={onCopy}>
168+
<Button
169+
className="pointer-events-auto z-10"
170+
variant="subtle"
171+
size="sm"
172+
onClick={onCopy}
173+
>
140174
{codeCopied ? <CheckIcon /> : <CopyIcon />} Copy
141175
</Button>
142176
</div>

src/client/index.tsx

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,35 @@ import { TooltipProvider } from "@/client/components/Tooltip";
22
import { ThemeProvider } from "@/client/contexts/theme.tsx";
33
import { StrictMode } from "react";
44
import { createRoot } from "react-dom/client";
5-
import { BrowserRouter } from "react-router";
5+
import { RouterProvider, createBrowserRouter, redirect } from "react-router";
66
import { App } from "./App.tsx";
77
import "@/client/index.css";
88

9+
const router = createBrowserRouter([
10+
{
11+
path: "/parameters/:id?",
12+
Component: App,
13+
},
14+
{
15+
path: "*",
16+
loader: () => {
17+
return redirect("/parameters");
18+
},
19+
},
20+
]);
21+
922
const root = document.getElementById("root");
1023

1124
if (!root) {
1225
console.error("An element with the id `root` does not exist");
1326
} else {
1427
createRoot(root).render(
1528
<StrictMode>
16-
<BrowserRouter>
17-
<ThemeProvider>
18-
<TooltipProvider>
19-
<App />
20-
</TooltipProvider>
21-
</ThemeProvider>
22-
</BrowserRouter>
29+
<ThemeProvider>
30+
<TooltipProvider>
31+
<RouterProvider router={router} />
32+
</TooltipProvider>
33+
</ThemeProvider>
2334
</StrictMode>,
2435
);
2536
}

src/server/api.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import * as v from "valibot";
21
import { vValidator } from "@hono/valibot-validator";
3-
import { nanoid } from "nanoid";
2+
import { head, put } from "@vercel/blob";
43
import { Hono } from "hono";
5-
import { put, head } from "@vercel/blob";
4+
import { nanoid } from "nanoid";
5+
import * as v from "valibot";
66

77
const BLOG_PATH = "parameters/share";
88

@@ -17,7 +17,7 @@ const parameters = new Hono()
1717
return c.json({ code });
1818
} catch (e) {
1919
console.error(`Failed to load playground with id ${id}: ${e}`);
20-
return c.notFound();
20+
return c.json({ code: "" }, 404);
2121
}
2222
})
2323
.post(

src/server/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ app.get("*", (c) => {
2828
// The production paths are hard coded based on the output of the build script.
2929
const cssPath = import.meta.env.PROD
3030
? "/assets/index.css"
31-
: "src/client/index.css";
31+
: "/src/client/index.css";
3232
const clientScriptPath = import.meta.env.PROD
3333
? "/assets/client.js"
34-
: "src/client/index.tsx";
34+
: "/src/client/index.tsx";
3535
const wasmExecScriptPath = import.meta.env.PROD
3636
? "/assets/wasm_exec.js"
3737
: "/wasm_exec.js";

0 commit comments

Comments
 (0)