From d22dc31a32b5f7a4abd11ab68a8cab9f563a4615 Mon Sep 17 00:00:00 2001 From: Brett Kolodny Date: Wed, 18 Jun 2025 15:16:20 +0000 Subject: [PATCH 1/2] feat: add setup for examples --- src/client/App.tsx | 44 ++++++++++++++++++++++++++++++++++---------- src/examples.ts | 23 +++++++++++++++++++++++ src/server/index.tsx | 16 ++++++++++++++++ src/utils/wasm.ts | 2 +- 4 files changed, 74 insertions(+), 11 deletions(-) create mode 100644 src/examples.ts diff --git a/src/client/App.tsx b/src/client/App.tsx index 27c3ee0..5396358 100644 --- a/src/client/App.tsx +++ b/src/client/App.tsx @@ -20,11 +20,13 @@ import { } from "@/client/components/Tooltip"; import { useTheme } from "@/client/contexts/theme"; import { defaultCode } from "@/client/snippets"; +import { examples } from "@/examples"; import type { ParameterWithSource, PreviewOutput, WorkspaceOwner, } from "@/gen/types"; +import { mockUsers } from "@/owner"; import { rpc } from "@/utils/rpc"; import { type WasmLoadState, @@ -36,7 +38,6 @@ import { MoonIcon, ShareIcon, SunIcon, SunMoonIcon } from "lucide-react"; import { type FC, useEffect, useMemo, useRef, useState } from "react"; import { type LoaderFunctionArgs, useLoaderData } from "react-router"; import { useDebouncedValue } from "./hooks/debounce"; -import { mockUsers } from "@/owner"; /** * Load the shared code if present. @@ -67,7 +68,9 @@ export const App = () => { return "loading"; }); const loadedCode = useLoaderData(); - const [code, setCode] = useState(loadedCode ?? defaultCode); + const [code, setCode] = useState( + loadedCode ?? window.EXAMPLE_CODE ?? defaultCode, + ); const [debouncedCode, isDebouncing] = useDebouncedValue(code, 1000); const [parameterValues, setParameterValues] = useState< Record @@ -203,14 +206,7 @@ export const App = () => { > Docs - - Support - + @@ -330,3 +326,31 @@ const ShareButton: FC = ({ code }) => { ); }; + +const ExampleSelector: FC = () => { + return ( + + + Examples + + + + + {examples.map(({ title, slug }) => { + const params = new URLSearchParams(); + params.append("example", slug); + + const href = `${window.location.origin}/parameters?${params.toString()}`; + return ( + + + {title} + + + ); + })} + + + + ); +}; diff --git a/src/examples.ts b/src/examples.ts new file mode 100644 index 0000000..fdf9511 --- /dev/null +++ b/src/examples.ts @@ -0,0 +1,23 @@ +type Example = { + title: string; + slug: string; + code: string; +}; + +export const examples: Example[] = [ + { + title: "Example 1", + slug: "example-1", + code: "// Example 1", + }, + { + title: "Example 2", + slug: "example-2", + code: "// Example 2", + }, + { + title: "Example 3", + slug: "example-4", + code: "// Example 3", + }, +]; diff --git a/src/server/index.tsx b/src/server/index.tsx index 0ea6bd5..057fdd1 100644 --- a/src/server/index.tsx +++ b/src/server/index.tsx @@ -1,3 +1,4 @@ +import { examples } from "@/examples"; import { api } from "@/server/api"; import { Hono } from "hono"; import { renderToString } from "react-dom/server"; @@ -17,6 +18,15 @@ app.route("/api", api); // Serves the main web application. This must come after the API route. app.get("*", (c) => { + const getExampleCode = () => { + const { example } = c.req.query(); + if (!example) { + return; + } + + return examples.find((e) => e.slug === example)?.code; + }; + // Along with the vite React plugin this enables HMR within react while // running the dev server. const { url } = c.req; @@ -45,6 +55,9 @@ app.get("*", (c) => { : "/wasm_exec.js"; const iconPath = import.meta.env.PROD ? "/assets/logo.svg" : "/logo.svg"; + const exampleCode = getExampleCode(); + const loadExampleCodeScript = `window.EXAMPLE_CODE = "${exampleCode}"`; + return c.html( [ "", @@ -64,6 +77,9 @@ app.get("*", (c) => {
+ {exampleCode ? ( + + ) : null} , diff --git a/src/utils/wasm.ts b/src/utils/wasm.ts index 8166218..46397d0 100644 --- a/src/utils/wasm.ts +++ b/src/utils/wasm.ts @@ -20,7 +20,7 @@ declare global { // Loaded from wasm go_preview?: GoPreviewDef; Go: { new (): Go }; - CODE?: string; + EXAMPLE_CODE?: string; } } From d83d0bd080ce4b106799f2fa45ac48c3b93db1d7 Mon Sep 17 00:00:00 2001 From: Brett Kolodny Date: Wed, 18 Jun 2025 15:18:20 +0000 Subject: [PATCH 2/2] fix: example title --- src/examples.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/examples.ts b/src/examples.ts index fdf9511..308dae7 100644 --- a/src/examples.ts +++ b/src/examples.ts @@ -17,7 +17,7 @@ export const examples: Example[] = [ }, { title: "Example 3", - slug: "example-4", + slug: "example-3", code: "// Example 3", }, ];