Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
71 lines
2.9 KiB
TypeScript
71 lines
2.9 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect } from "react";
|
|
import { usePathname, useRouter } from "next/navigation";
|
|
import { useAdminAuth } from "@/app/lib/AdminAuthContext";
|
|
import { isPublicPath } from "@/app/lib/routeGuard";
|
|
import { CompanyScopeProvider } from "@/app/lib/CompanyScope";
|
|
import Header from "./Header";
|
|
import Sidebar from "./Sidebar";
|
|
import PoweredBy from "./PoweredBy";
|
|
import { SessionKeepAlive } from "@/app/components/app/SessionKeepAlive";
|
|
|
|
// The authenticated app shell: composes the sidebar (from useAclSidebar via
|
|
// Sidebar) and header (user menu + logout, via Header) around every page.
|
|
//
|
|
// Ruling R3: `middleware.ts` only checks that the colite_admin cookie is present
|
|
// and decodable — it cannot tell an expired/invalid token from a good one
|
|
// without calling the mobile-api. AdminAuthContext's mount-time hydration
|
|
// (GET /api/auth/session -> /api/admin/me) is what actually validates the
|
|
// token, so THIS is the layer that must redirect to /login once hydration
|
|
// finishes and comes back empty — closing the expired-but-present-cookie
|
|
// case the middleware cannot catch.
|
|
export default function AppShell({ children }: { children: React.ReactNode }) {
|
|
const pathname = usePathname();
|
|
const router = useRouter();
|
|
const { name, loading } = useAdminAuth();
|
|
const publicPage = isPublicPath(pathname);
|
|
|
|
useEffect(() => {
|
|
if (!publicPage && !loading && !name) {
|
|
router.replace("/login");
|
|
}
|
|
}, [publicPage, loading, name, router]);
|
|
|
|
// /login and /reset-password render standalone, with no authenticated
|
|
// chrome and no redirect logic (they are the redirect target).
|
|
if (publicPage) {
|
|
return <>{children}</>;
|
|
}
|
|
|
|
// While hydrating, or after hydration comes back empty (redirect above is
|
|
// in flight), render nothing rather than flashing the authenticated shell
|
|
// or its content.
|
|
if (loading || !name) {
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<CompanyScopeProvider>
|
|
<div className="flex min-h-svh bg-background">
|
|
<SessionKeepAlive />
|
|
<Sidebar />
|
|
{/* min-w-0 on both the content column and <main>: without it a flex item
|
|
defaults to min-width:auto and refuses to shrink below its content's
|
|
intrinsic width, so any page whose content is wider than the viewport
|
|
(e.g. a wide genealogy chart) stretches the whole shell and scrolls
|
|
the page horizontally instead of scrolling inside its own
|
|
overflow-x-auto container. */}
|
|
<div className="flex min-h-svh min-w-0 flex-1 flex-col">
|
|
<Header />
|
|
<main className="mx-auto w-full max-w-[var(--content-max-width)] min-w-0 flex-1 p-4">{children}</main>
|
|
{/* The sidebar (and its credit) is hidden below lg, so repeat the credit under the content there. */}
|
|
<div data-testid="mobile-credit" className="px-4 pb-4 text-center lg:hidden print:hidden">
|
|
<PoweredBy />
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</CompanyScopeProvider>
|
|
);
|
|
}
|