import { ImageResponse } from "next/og"; import type { Fact } from "src/replicache"; import type { Attribute } from "src/replicache/attributes"; import { Database } from "../../supabase/database.types"; import { createServerClient } from "@supabase/ssr"; import { parseHSBToRGB } from "src/utils/parseHSB"; import { cookies } from "next/headers"; // Route segment config export const revalidate = 0; export const preferredRegion = ["sfo1"]; export const dynamic = "force-dynamic"; export const fetchCache = "force-no-store"; // Image metadata export const size = { width: 32, height: 32, }; export const contentType = "image/png"; // Image generation let supabase = createServerClient( process.env.NEXT_PUBLIC_SUPABASE_API_URL as string, process.env.SUPABASE_SERVICE_ROLE_KEY as string, { cookies: {} }, ); export default async function Icon() { let cookieStore = await cookies(); let identity = cookieStore.get("identity"); let rootEntity: string | null = null; if (identity) { let res = await supabase .from("identities") .select( `*, permission_tokens!identities_home_page_fkey(*, permission_token_rights(*)), permission_token_on_homepage( *, permission_tokens(*, permission_token_rights(*)) ) `, ) .eq("id", identity?.value) .single(); rootEntity = res.data?.permission_tokens?.root_entity || null; } let outlineColor, fillColor; if (rootEntity) { let { data } = await supabase.rpc("get_facts", { root: rootEntity, }); let initialFacts = (data as unknown as Fact[]) || []; let themePageBG = initialFacts.find( (f) => f.attribute === "theme/card-background", ) as Fact<"theme/card-background"> | undefined; let themePrimary = initialFacts.find( (f) => f.attribute === "theme/primary", ) as Fact<"theme/primary"> | undefined; outlineColor = parseHSBToRGB(`hsba(${themePageBG?.data.value})`); fillColor = parseHSBToRGB(`hsba(${themePrimary?.data.value})`); } return new ImageResponse( ( // ImageResponse JSX element
{/* outline */} {/* fill */}
), // ImageResponse options { // For convenience, we can re-use the exported icons size metadata // config to also set the ImageResponse's width and height. ...size, headers: { "Cache-Control": "no-cache", }, }, ); }