From 34b440213fc35296d50085ccf03bad7b240ccc0c Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Tue, 11 Oct 2022 17:02:45 -0500 Subject: [PATCH] WIP _app.tsx for "global" state --- components/Page.tsx | 1 + fresh.gen.ts | 70 +++++++++++++++++++++++--------------------- import_map.json | 2 +- routes/_app.tsx | 25 ++++++++++++++++ routes/dashboard.tsx | 6 ++-- types.ts | 3 ++ 6 files changed, 69 insertions(+), 38 deletions(-) create mode 100644 routes/_app.tsx diff --git a/components/Page.tsx b/components/Page.tsx index cf9a0ec..8b7dec9 100644 --- a/components/Page.tsx +++ b/components/Page.tsx @@ -42,6 +42,7 @@ export interface PageProps extends JSX.HTMLAttributes { } export function Page(props: PageProps) { + console.log("PageProps:", props); return (
diff --git a/fresh.gen.ts b/fresh.gen.ts index 5924fff..b23516b 100644 --- a/fresh.gen.ts +++ b/fresh.gen.ts @@ -6,23 +6,24 @@ import config from "./deno.json" assert { type: "json" }; import * as $0 from "./routes/[name].tsx"; import * as $1 from "./routes/_404.tsx"; import * as $2 from "./routes/_500.tsx"; -import * as $3 from "./routes/_middleware.ts"; -import * as $4 from "./routes/about.tsx"; -import * as $5 from "./routes/api/joke.ts"; -import * as $6 from "./routes/api/random-uuid.ts"; -import * as $7 from "./routes/countdown.tsx"; -import * as $8 from "./routes/dashboard.tsx"; -import * as $9 from "./routes/github/[username].tsx"; -import * as $10 from "./routes/index.tsx"; -import * as $11 from "./routes/login.tsx"; -import * as $12 from "./routes/logout.tsx"; -import * as $13 from "./routes/note.tsx"; -import * as $14 from "./routes/note/[id].tsx"; -import * as $15 from "./routes/note/create.tsx"; -import * as $16 from "./routes/plain.ts"; -import * as $17 from "./routes/register.tsx"; -import * as $18 from "./routes/route-config-example.tsx"; -import * as $19 from "./routes/search.tsx"; +import * as $3 from "./routes/_app.tsx"; +import * as $4 from "./routes/_middleware.ts"; +import * as $5 from "./routes/about.tsx"; +import * as $6 from "./routes/api/joke.ts"; +import * as $7 from "./routes/api/random-uuid.ts"; +import * as $8 from "./routes/countdown.tsx"; +import * as $9 from "./routes/dashboard.tsx"; +import * as $10 from "./routes/github/[username].tsx"; +import * as $11 from "./routes/index.tsx"; +import * as $12 from "./routes/login.tsx"; +import * as $13 from "./routes/logout.tsx"; +import * as $14 from "./routes/note.tsx"; +import * as $15 from "./routes/note/[id].tsx"; +import * as $16 from "./routes/note/create.tsx"; +import * as $17 from "./routes/plain.ts"; +import * as $18 from "./routes/register.tsx"; +import * as $19 from "./routes/route-config-example.tsx"; +import * as $20 from "./routes/search.tsx"; import * as $$0 from "./islands/Countdown.tsx"; import * as $$1 from "./islands/Counter.tsx"; @@ -31,23 +32,24 @@ const manifest = { "./routes/[name].tsx": $0, "./routes/_404.tsx": $1, "./routes/_500.tsx": $2, - "./routes/_middleware.ts": $3, - "./routes/about.tsx": $4, - "./routes/api/joke.ts": $5, - "./routes/api/random-uuid.ts": $6, - "./routes/countdown.tsx": $7, - "./routes/dashboard.tsx": $8, - "./routes/github/[username].tsx": $9, - "./routes/index.tsx": $10, - "./routes/login.tsx": $11, - "./routes/logout.tsx": $12, - "./routes/note.tsx": $13, - "./routes/note/[id].tsx": $14, - "./routes/note/create.tsx": $15, - "./routes/plain.ts": $16, - "./routes/register.tsx": $17, - "./routes/route-config-example.tsx": $18, - "./routes/search.tsx": $19, + "./routes/_app.tsx": $3, + "./routes/_middleware.ts": $4, + "./routes/about.tsx": $5, + "./routes/api/joke.ts": $6, + "./routes/api/random-uuid.ts": $7, + "./routes/countdown.tsx": $8, + "./routes/dashboard.tsx": $9, + "./routes/github/[username].tsx": $10, + "./routes/index.tsx": $11, + "./routes/login.tsx": $12, + "./routes/logout.tsx": $13, + "./routes/note.tsx": $14, + "./routes/note/[id].tsx": $15, + "./routes/note/create.tsx": $16, + "./routes/plain.ts": $17, + "./routes/register.tsx": $18, + "./routes/route-config-example.tsx": $19, + "./routes/search.tsx": $20, }, islands: { "./islands/Countdown.tsx": $$0, diff --git a/import_map.json b/import_map.json index 432abd0..fc7a3bd 100644 --- a/import_map.json +++ b/import_map.json @@ -2,7 +2,7 @@ "imports": { "@/": "./", "$std/": "https://deno.land/std@0.158.0/", - "$fresh/": "https://deno.land/x/fresh@1.1.1/", + "$fresh/": "https://deno.land/x/fresh@1.1.2/", "preact": "https://esm.sh/preact@10.11.0", "preact/": "https://esm.sh/preact@10.11.0/", "preact-render-to-string": "https://esm.sh/*preact-render-to-string@5.2.4", diff --git a/routes/_app.tsx b/routes/_app.tsx new file mode 100644 index 0000000..2ada14d --- /dev/null +++ b/routes/_app.tsx @@ -0,0 +1,25 @@ +import { type AppProps, Handlers, PageProps } from "$fresh/server.ts"; +import { Page } from "@/components/Page.tsx"; +import { type PublicUser } from "@/types.ts"; +import { type ContextState } from "@/types.ts"; + +interface MyAppProps extends AppProps { + user?: PublicUser; +} + +export const handler: Handlers = { + async GET(_request: Request, context) { + const user: Partial = context.state.user; + if (user && "passwordDigest" in user) delete user.passwordDigest; + return await context.render(context.state.user); + }, +}; + +export default function App({ Component, ...props }: MyAppProps) { + console.log("AppProps:", props); + return ( + + + + ); +} diff --git a/routes/dashboard.tsx b/routes/dashboard.tsx index 41c2934..4d7b68a 100644 --- a/routes/dashboard.tsx +++ b/routes/dashboard.tsx @@ -2,12 +2,12 @@ import { Page } from "@/components/Page.tsx"; // import { getToken, getUser } from "@/db/mod.ts"; // import * as base64 from "$std/encoding/base64.ts"; import { Handlers, PageProps } from "$fresh/server.ts"; -import { type ContextState } from "@/types.ts"; +import { type ContextState, type PublicUser } from "@/types.ts"; export const handler: Handlers = { async GET(_request: Request, context) { - const user: Partial = context.state.user; - if (user && "passwordDigest" in user) delete user.passwordDigest; + const user: PublicUser | undefined = context.state.user; + if (user != undefined) delete user.passwordDigest; return await context.render(context.state.user); }, }; diff --git a/types.ts b/types.ts index 7f714c4..959386c 100644 --- a/types.ts +++ b/types.ts @@ -51,3 +51,6 @@ export type TokenDigest = string; export interface ContextState { user?: PublicUser; } + +function toPublicUser(user: User): PublicUser { +}