"use server"; import { cookies } from "next/headers"; import { ACTIVE_SITE_COOKIE_MAX_AGE, isStorableActiveSite, } from "./activeSite"; import { activeSiteCookieForRequest } from "./activeSiteServer"; // THE ONE WRITER of the active-site cookie (see ./activeSite.ts). Called by the // picker's onChange, by SiteScopeProvider when a site's own page is visited, and // once by its localStorage migration. Setting a cookie in a server action makes // Next re-render the current page and its layouts with the new value, so the // root layout hands the provider the new selection and Dashboard or Channels // re-scope, with no refresh of our own. // // The value is only SHAPE-checked (a site id or "__all__"): whether it names a // configured site is decided at every read, so a site deleted later resolves as // no selection. Returns false, writing nothing, for anything else. export async function setActiveSiteAction(value: string): Promise { if (!isStorableActiveSite(value)) return false; const name = await activeSiteCookieForRequest(); (await cookies()).set(name, value, { path: "/", sameSite: "lax", maxAge: ACTIVE_SITE_COOKIE_MAX_AGE, // Only the server reads it: the page gets it through the layout. httpOnly: true, }); return true; }