Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: add setup for examples #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions src/client/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand Down Expand Up @@ -67,7 +68,9 @@ export const App = () => {
return "loading";
});
const loadedCode = useLoaderData<typeof loader>();
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<string, string>
Expand Down Expand Up @@ -203,14 +206,7 @@ export const App = () => {
>
Docs
</a>
<a
href="https://coder.com"
target="_blank"
rel="noreferrer"
className="font-light text-content-secondary text-sm hover:text-content-primary"
>
Support
</a>
<ExampleSelector />
<ThemeSelector />
</div>
</nav>
Expand Down Expand Up @@ -330,3 +326,31 @@ const ShareButton: FC<ShareButtonProps> = ({ code }) => {
</Tooltip>
);
};

const ExampleSelector: FC = () => {
return (
<DropdownMenu>
<DropdownMenuTrigger className="font-light text-content-secondary text-sm hover:text-content-primary">
Examples
</DropdownMenuTrigger>

<DropdownMenuPortal>
<DropdownMenuContent>
{examples.map(({ title, slug }) => {
const params = new URLSearchParams();
params.append("example", slug);

const href = `${window.location.origin}/parameters?${params.toString()}`;
return (
<DropdownMenuItem key={slug} asChild={true}>
<a href={href} target="_blank" rel="noreferrer">
{title}
</a>
</DropdownMenuItem>
);
})}
</DropdownMenuContent>
</DropdownMenuPortal>
</DropdownMenu>
);
};
23 changes: 23 additions & 0 deletions src/examples.ts
Original file line number Diff line number Diff line change
@@ -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-3",
code: "// Example 3",
},
];
16 changes: 16 additions & 0 deletions src/server/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { examples } from "@/examples";
import { api } from "@/server/api";
import { Hono } from "hono";
import { renderToString } from "react-dom/server";
Expand All @@ -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;
Expand Down Expand Up @@ -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(
[
"<!doctype html>",
Expand All @@ -64,6 +77,9 @@ app.get("*", (c) => {
</head>
<body>
<div id="root"></div>
{exampleCode ? (
<script type="module">{loadExampleCodeScript}</script>
) : null}
<script type="module" src={clientScriptPath}></script>
</body>
</html>,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ declare global {
// Loaded from wasm
go_preview?: GoPreviewDef;
Go: { new (): Go };
CODE?: string;
EXAMPLE_CODE?: string;
}
}

Expand Down