"use client"; import { useEffect } from "react"; import { usePathname, useRouter } from "next/navigation"; import { useAuth } from "@/components/AuthProvider"; import { Nav } from "@/components/Nav"; import { MarketingHeader } from "@/components/marketing/MarketingHeader"; import { MarketingFooter } from "@/components/marketing/MarketingFooter"; // Public marketing surface — exact paths, not prefixes, so e.g. /features/x // (if it ever exists) doesn't accidentally get pulled into marketing chrome. // Keep in sync with PUBLIC_PATHS in middleware.ts (that one decides whether // to redirect at all; this one just picks page chrome). const MARKETING_PATHS = new Set(["/", "/features", "/pricing", "/faq", "/terms", "/privacy"]); // Pages a signed-in user with no org_id claim must still be able to reach — // otherwise the redirect below would loop against itself, or lock someone // out of the one screen (/onboarding) that fixes their account. const NO_ORG_ALLOWED_PATHS = new Set(["/onboarding", "/login", "/signup", "/profile"]); /** * Picks page chrome by route: the public marketing pages get a full-bleed * layout with their own header/footer, everything else (the authenticated * app, including /login and /settings) keeps the existing app Nav + padded * main container. * * Also carries AuthProvider's no-claim guard (SAAS_PLAN.md B3): a signed-in * Firebase user with no org_id claim is a real session that is nonetheless * provisioned into nothing — AuthProvider already refuses to set the * drb_session cookie for them, so middleware.ts's redirect only covers * "not signed in at all". This effect covers the other case: signed in, no * org, anywhere in the app — send them to /onboarding rather than letting * every page's data hooks fail open or silently return nothing. */ export function ChromeSwitcher({ children }: { children: React.ReactNode }) { const pathname = usePathname(); const { user, loading, orgId } = useAuth(); const router = useRouter(); useEffect(() => { if (loading) return; if (!user) return; // not signed in — middleware.ts already routes this to /login if (orgId) return; if (MARKETING_PATHS.has(pathname) || NO_ORG_ALLOWED_PATHS.has(pathname)) return; router.replace("/onboarding"); }, [loading, user, orgId, pathname, router]); if (MARKETING_PATHS.has(pathname)) { return ( <> {children} ); } return ( <>