"use client" /** * Campus-only greeting chrome — plain type vs the brand-tinted cover banner. * * Shell-global like `use-home-variant`: it is a preference about `/home/campus`, * not product-scoped state. */ import * as React from "react" import { getStorageItem, setStorageItem, subscribeToStorageKey, } from "@exxatdesignux/ui/lib/persisted-state" export type CampusBannerMode = "greeting" | "full" export const CAMPUS_BANNER_MODES: { id: CampusBannerMode label: string description: string }[] = [ { id: "greeting", label: "Greeting banner", description: "Greeting and school picker only", }, { id: "full", label: "Full banner", description: "Brand cover with greeting anchored at the foot", }, ] const STORAGE_KEY = "campus-banner-mode" const VERSION = 1 const DEFAULT_MODE: CampusBannerMode = "full" export function isCampusBannerMode(value: unknown): value is CampusBannerMode { return value === "greeting" || value === "full" } const listeners = new Set<() => void>() let snapshot: CampusBannerMode | null = null let hydrated = false function subscribe(onChange: () => void) { listeners.add(onChange) const unsubscribeStorage = subscribeToStorageKey(STORAGE_KEY, () => { hydrated = false onChange() }) return () => { listeners.delete(onChange) unsubscribeStorage() } } function read(): CampusBannerMode { if (hydrated && snapshot) return snapshot hydrated = true const raw = getStorageItem(STORAGE_KEY) if (raw) { try { const parsed: unknown = JSON.parse(raw) if ( typeof parsed === "object" && parsed !== null && (parsed as { v?: number }).v === VERSION && isCampusBannerMode((parsed as { d?: unknown }).d) ) { snapshot = (parsed as { d: CampusBannerMode }).d return snapshot } } catch { // Unparseable payload — fall through to the default. } } snapshot = DEFAULT_MODE return snapshot } export function setCampusBannerMode(next: CampusBannerMode) { snapshot = next hydrated = true setStorageItem(STORAGE_KEY, JSON.stringify({ v: VERSION, d: next })) for (const listener of listeners) listener() } export function useCampusBannerMode(): CampusBannerMode { return React.useSyncExternalStore(subscribe, read, () => DEFAULT_MODE) }