Spaces:
Running

WorkPilot-AI / web /src /components /shell /app-gate.tsx
Aynkhn's picture
Deploy WorkPilot demo (part 3)
03608f2 verified
Raw History Blame Contribute Delete
1.48 kB
"use client";
import { Loader2Icon } from "lucide-react";
import { usePathname, useRouter } from "next/navigation";
import { useEffect, type ReactNode } from "react";
import { StepUpProvider } from "@/components/inbox/stepup";
import { AppShell } from "@/components/shell/app-shell";
import { useAuth } from "@/lib/auth";
import { useI18n } from "@/lib/i18n";
/** Everything behind sign-in. Tokens live in memory, so the gate waits for the silent
* refresh (httpOnly cookie) before deciding. */
export function AppGate({ children }: { children: ReactNode }) {
const { status } = useAuth();
const { t } = useI18n();
const router = useRouter();
const pathname = usePathname();
useEffect(() => {
if (status === "anonymous") router.replace(`/login?next=${encodeURIComponent(pathname)}`);
}, [status, router, pathname]);
if (status !== "authenticated") return <FullPageLoading label={t("common.loading")} />;
return (
<StepUpProvider>
<AppShell>{children}</AppShell>
</StepUpProvider>
);
}
export function FullPageLoading({ label }: { label?: string }) {
const { t } = useI18n();
return (
<div className="grid min-h-dvh place-items-center" role="status" aria-live="polite">
<span className="flex items-center gap-2 text-sm text-muted-foreground">
<Loader2Icon className="size-4 animate-spin" aria-hidden />
{label ?? t("common.loading")}
</span>
</div>
);
}