···11import { ImageResponse } from "next/og";
22import type { Fact } from "src/replicache";
33import type { Attribute } from "src/replicache/attributes";
44-import { Database } from "../../supabase/database.types";
44+import { Database } from "supabase/database.types";
55import { createServerClient } from "@supabase/ssr";
66import { parseHSBToRGB } from "src/utils/parseHSB";
77import { cookies } from "next/headers";
-124
app/home/page.tsx
···11-import { cookies } from "next/headers";
22-import { Fact, ReplicacheProvider, useEntity } from "src/replicache";
33-import type { Attribute } from "src/replicache/attributes";
44-import {
55- ThemeBackgroundProvider,
66- ThemeProvider,
77-} from "components/ThemeManager/ThemeProvider";
88-import { EntitySetProvider } from "components/EntitySetProvider";
99-import { createIdentity } from "actions/createIdentity";
1010-import { drizzle } from "drizzle-orm/node-postgres";
1111-import { IdentitySetter } from "./IdentitySetter";
1212-1313-import { getIdentityData } from "actions/getIdentityData";
1414-import { getFactsFromHomeLeaflets } from "app/api/rpc/[command]/getFactsFromHomeLeaflets";
1515-import { supabaseServerClient } from "supabase/serverClient";
1616-import { pool } from "supabase/pool";
1717-1818-import { NotFoundLayout } from "components/PageLayouts/NotFoundLayout";
1919-import { HomeLayout } from "./HomeLayout";
2020-2121-export default async function Home() {
2222- let cookieStore = await cookies();
2323- let auth_res = await getIdentityData();
2424- let identity: string | undefined;
2525- if (auth_res) identity = auth_res.id;
2626- else identity = cookieStore.get("identity")?.value;
2727- let needstosetcookie = false;
2828- if (!identity) {
2929- const client = await pool.connect();
3030- const db = drizzle(client);
3131- let newIdentity = await createIdentity(db);
3232- client.release();
3333- identity = newIdentity.id;
3434- needstosetcookie = true;
3535- }
3636-3737- async function setCookie() {
3838- "use server";
3939-4040- (await cookies()).set("identity", identity as string, {
4141- sameSite: "strict",
4242- });
4343- }
4444-4545- let permission_token = auth_res?.home_leaflet;
4646- if (!permission_token) {
4747- let res = await supabaseServerClient
4848- .from("identities")
4949- .select(
5050- `*,
5151- permission_tokens!identities_home_page_fkey(*, permission_token_rights(*))
5252- `,
5353- )
5454- .eq("id", identity)
5555- .single();
5656- permission_token = res.data?.permission_tokens;
5757- }
5858-5959- if (!permission_token)
6060- return (
6161- <NotFoundLayout>
6262- <p className="font-bold">Sorry, we can't find this home!</p>
6363- <p>
6464- This may be a glitch on our end. If the issue persists please{" "}
6565- <a href="mailto:contact@leaflet.pub">send us a note</a>.
6666- </p>
6767- </NotFoundLayout>
6868- );
6969- let [homeLeafletFacts, allLeafletFacts] = await Promise.all([
7070- supabaseServerClient.rpc("get_facts", {
7171- root: permission_token.root_entity,
7272- }),
7373- auth_res
7474- ? getFactsFromHomeLeaflets.handler(
7575- {
7676- tokens: auth_res.permission_token_on_homepage.map(
7777- (r) => r.permission_tokens.root_entity,
7878- ),
7979- },
8080- { supabase: supabaseServerClient },
8181- )
8282- : undefined,
8383- ]);
8484- let initialFacts =
8585- (homeLeafletFacts.data as unknown as Fact<Attribute>[]) || [];
8686-8787- let root_entity = permission_token.root_entity;
8888- let home_docs_initialFacts = allLeafletFacts?.result || {};
8989-9090- return (
9191- <ReplicacheProvider
9292- rootEntity={root_entity}
9393- token={permission_token}
9494- name={root_entity}
9595- initialFacts={initialFacts}
9696- >
9797- <IdentitySetter cb={setCookie} call={needstosetcookie} />
9898- <EntitySetProvider
9999- set={permission_token.permission_token_rights[0].entity_set}
100100- >
101101- <ThemeProvider entityID={root_entity}>
102102- <ThemeBackgroundProvider entityID={root_entity}>
103103- <HomeLayout
104104- titles={{
105105- ...home_docs_initialFacts.titles,
106106- ...auth_res?.permission_token_on_homepage.reduce(
107107- (acc, tok) => {
108108- let title =
109109- tok.permission_tokens.leaflets_in_publications[0]?.title;
110110- if (title) acc[tok.permission_tokens.root_entity] = title;
111111- return acc;
112112- },
113113- {} as { [k: string]: string },
114114- ),
115115- }}
116116- entityID={root_entity}
117117- initialFacts={home_docs_initialFacts.facts || {}}
118118- />
119119- </ThemeBackgroundProvider>
120120- </ThemeProvider>
121121- </EntitySetProvider>
122122- </ReplicacheProvider>
123123- );
124124-}