import "server-only"; import { cookies, headers } from "next/headers"; import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { listSiteIds } from "yt-dlp-transcript-common/lib/site"; import { activeSiteCookieName, resolveActiveSiteFrom, type ResolvedActiveSite, } from "./activeSite"; // THE ONE READ of the active site on the server (see ./activeSite.ts). The root // layout calls it with no param (a layout has no searchParams) and hands the // result to SiteScopeProvider; Dashboard and Channels call it with their // `searchParams.site`, which wins for that request when it names a site or // "__all__". Nothing is written here: a server component cannot set a cookie, // and only setActiveSiteAction does. export type ActiveSiteRead = ResolvedActiveSite & { // The cookie's raw value, or null when the request carried none — which is // what tells the provider a visitor may still have the old localStorage key. stored: string | null; }; // The request's own cookie name (per port; see activeSiteCookieName). export async function activeSiteCookieForRequest(): Promise { return activeSiteCookieName((await headers()).get("host")); } export async function readActiveSite( param?: string | null, siteIds: string[] = listSiteIds(getPaths()), ): Promise { const name = await activeSiteCookieForRequest(); const stored = (await cookies()).get(name)?.value ?? null; return { ...resolveActiveSiteFrom([param, stored], siteIds), stored }; }