11import { examples } from "@/examples/code" ;
2- import { api } from "@/server/api" ;
2+ import { api } from "@/server/routes/ api" ;
33import { Hono } from "hono" ;
44import { renderToString } from "react-dom/server" ;
5+ import { getShareData } from "./blob" ;
6+ import { trimTrailingSlash } from "hono/trailing-slash" ;
57
68// This must be exported for the dev server to work
79export const app = new Hono ( ) ;
@@ -16,12 +18,21 @@ app.use("*", async (ctx, next) => {
1618} ) ;
1719app . route ( "/api" , api ) ;
1820
21+ app . use ( trimTrailingSlash ( ) ) ;
22+
1923// Serves the main web application. This must come after the API route.
20- app . get ( "*" , ( c ) => {
21- const getExampleCode = ( ) => {
24+ app . get ( "/parameters/:shareId?" , async ( c ) => {
25+ const getExampleCode = async ( ) : Promise < string | null > => {
26+ const { shareId } = c . req . param ( ) ;
2227const { example } = c . req . query ( ) ;
28+
29+ if ( shareId ) {
30+ const shareData = await getShareData ( shareId ) ;
31+ return shareData ?. code ?? null ;
32+ }
33+
2334if ( ! example ) {
24- return ;
35+ return null ;
2536}
2637
2738return examples [ example ] ;
@@ -54,7 +65,7 @@ app.get("*", (c) => {
5465? "/assets/wasm_exec.js"
5566: "/wasm_exec.js" ;
5667const iconPath = import . meta. env . PROD ? "/assets/logo.svg" : "/logo.svg" ;
57- const exampleCode = getExampleCode ( ) ;
68+ const exampleCode = await getExampleCode ( ) ;
5869
5970return c . html (
6071[
@@ -76,7 +87,7 @@ app.get("*", (c) => {
7687< body >
7788< div id = "root" > </ div >
7889{ exampleCode ? (
79- < script type = "module" > { `window.EXAMPLE_CODE = ${ JSON . stringify ( exampleCode ) } ` } </ script >
90+ < script type = "module" > { `window.CODE = ${ JSON . stringify ( exampleCode ) } ` } </ script >
8091) : null }
8192< script type = "module" src = { clientScriptPath } > </ script >
8293</ body >
@@ -85,3 +96,5 @@ app.get("*", (c) => {
8596] . join ( "\n" ) ,
8697) ;
8798} ) ;
99+
100+ app . get ( "*" , ( c ) => c . redirect ( "/parameters" ) ) ;
0 commit comments