Spaces:
Running

File size: 1,480 Bytes
03608f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"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>
  );
}