Files
CRAPP/src/routes/__root.tsx
T

211 lines
7.3 KiB
TypeScript
Raw Normal View History

2026-08-06 08:36:47 +02:00
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import {
Outlet,
Link,
createRootRouteWithContext,
useRouter,
useNavigate,
useLocation,
HeadContent,
Scripts,
} from "@tanstack/react-router";
import { useEffect, useState, type ReactNode } from "react";
import appCss from "../styles.css?url";
import { reportLovableError } from "../lib/lovable-error-reporting";
import { BottomNav } from "../components/crapp/BottomNav";
import { CelebrazioneBadge } from "../components/crapp/CelebrazioneBadge";
import { Toaster } from "../components/ui/sonner";
import { TeamLogo } from "../components/crapp/ui-bits";
import { useGiocatoreBase } from "../lib/user-store";
2026-08-31 18:21:34 +02:00
import { useSessione } from "../lib/auth";
import { mantieniWorkerPushAggiornato } from "../lib/push-client";
2026-08-06 08:36:47 +02:00
function NotFoundComponent() {
return (
<div className="flex min-h-dvh items-center justify-center bg-background px-4">
2026-08-06 08:36:47 +02:00
<div className="max-w-md text-center">
<h1 className="font-display-lg text-7xl text-foreground">404</h1>
<h2 className="mt-4 text-xl font-semibold text-foreground">Pagina non trovata</h2>
2026-08-06 08:36:47 +02:00
<p className="mt-2 text-sm text-muted-foreground">
La pagina che cerchi non esiste o è stata spostata.
2026-08-06 08:36:47 +02:00
</p>
<div className="mt-6">
<Link
to="/"
className="premi inline-flex min-h-11 items-center justify-center rounded-2xl bg-primary px-4 py-2 text-sm font-semibold text-primary-foreground"
2026-08-06 08:36:47 +02:00
>
Torna alla home
2026-08-06 08:36:47 +02:00
</Link>
</div>
</div>
</div>
);
}
function ErrorComponent({ error, reset }: { error: Error; reset: () => void }) {
console.error(error);
const router = useRouter();
useEffect(() => {
reportLovableError(error, { boundary: "tanstack_root_error_component" });
}, [error]);
return (
<div className="flex min-h-dvh items-center justify-center bg-background px-4">
2026-08-06 08:36:47 +02:00
<div className="max-w-md text-center">
<h1 className="text-xl font-semibold tracking-tight text-foreground">
Questa pagina non si è caricata
2026-08-06 08:36:47 +02:00
</h1>
<p className="mt-2 text-sm text-muted-foreground">
Qualcosa è andato storto da parte nostra. Puoi riprovare o tornare alla home.
2026-08-06 08:36:47 +02:00
</p>
<div className="mt-6 flex flex-wrap justify-center gap-2">
<button
type="button"
2026-08-06 08:36:47 +02:00
onClick={() => {
router.invalidate();
reset();
}}
className="premi inline-flex min-h-11 items-center justify-center rounded-2xl bg-primary px-4 py-2 text-sm font-semibold text-primary-foreground"
2026-08-06 08:36:47 +02:00
>
Riprova
2026-08-06 08:36:47 +02:00
</button>
<a
href="/"
className="premi inline-flex min-h-11 items-center justify-center rounded-2xl border border-border bg-card px-4 py-2 text-sm font-semibold text-foreground"
2026-08-06 08:36:47 +02:00
>
Torna alla home
2026-08-06 08:36:47 +02:00
</a>
</div>
</div>
</div>
);
}
export const Route = createRootRouteWithContext<{ queryClient: QueryClient }>()({
head: () => ({
meta: [
{ charSet: "utf-8" },
// `viewport-fit=cover` è obbligatorio perché env(safe-area-inset-*) sia
// diverso da 0 su iOS: senza, la BottomNav finisce sotto la home bar.
{
name: "viewport",
content: "width=device-width, initial-scale=1, viewport-fit=cover",
},
2026-08-06 08:36:47 +02:00
{ title: "CrAPP — L'app del CRAP Volley" },
{
name: "description",
content:
"Convocazioni, presenze, statistiche e classifica del CRAP Volley in un'unica app mobile.",
2026-08-06 08:36:47 +02:00
},
{ name: "author", content: "CRAP Volley" },
// Deve combaciare con --background, altrimenti la barra di stato resta
// nera sopra un'interfaccia chiara.
{ name: "theme-color", content: "#e4e8ed" },
2026-08-06 08:36:47 +02:00
{ property: "og:title", content: "CrAPP — L'app del CRAP Volley" },
{
property: "og:description",
content:
"Convocazioni, presenze, statistiche e classifica del CRAP Volley in un'unica app mobile.",
2026-08-06 08:36:47 +02:00
},
{ property: "og:type", content: "website" },
// `summary` e non `summary_large_image`: l'unica immagine è l'icona
// quadrata della squadra, non una copertina 2:1.
{ name: "twitter:card", content: "summary" },
2026-08-06 08:36:47 +02:00
{ name: "twitter:title", content: "CrAPP — L'app del CRAP Volley" },
{
name: "twitter:description",
content:
"Convocazioni, presenze, statistiche e classifica del CRAP Volley in un'unica app mobile.",
},
{ property: "og:image", content: "/icon-512.png" },
{ name: "twitter:image", content: "/icon-512.png" },
2026-08-06 08:36:47 +02:00
],
links: [
{
rel: "stylesheet",
href: appCss,
},
{ rel: "preconnect", href: "https://fonts.googleapis.com" },
{ rel: "preconnect", href: "https://fonts.gstatic.com", crossOrigin: "anonymous" },
{
rel: "stylesheet",
href: "https://fonts.googleapis.com/css2?family=Bebas+Neue&family=Barlow:wght@400;500;600;700;800&display=swap",
},
{ rel: "icon", href: "/favicon.png", type: "image/png" },
{ rel: "apple-touch-icon", href: "/icon-192.png", sizes: "192x192" },
{ rel: "manifest", href: "/manifest.webmanifest" },
],
}),
shellComponent: RootShell,
component: RootComponent,
notFoundComponent: NotFoundComponent,
errorComponent: ErrorComponent,
});
function RootShell({ children }: { children: ReactNode }) {
return (
<html lang="it">
2026-08-06 08:36:47 +02:00
<head>
<HeadContent />
</head>
<body>
{children}
<Scripts />
</body>
</html>
);
}
function RootComponent() {
const { queryClient } = Route.useRouteContext();
// `AppShell` legge la rosa da `giocatori_squadra` (useGiocatoreBase → DD-015): serve stare
// dentro il QueryClientProvider, quindi il provider avvolge tutto fin da qui.
return (
<QueryClientProvider client={queryClient}>
<AppShell />
</QueryClientProvider>
);
}
function AppShell() {
2026-08-06 08:36:47 +02:00
const navigate = useNavigate();
const location = useLocation();
const giocatore = useGiocatoreBase();
2026-08-31 18:21:34 +02:00
const { pronta, utenteId } = useSessione();
2026-08-06 08:36:47 +02:00
const [mounted, setMounted] = useState(false);
const isBenvenuto = location.pathname === "/benvenuto";
useEffect(mantieniWorkerPushAggiornato, []);
2026-08-31 18:21:34 +02:00
// Senza sessione Google non si entra: l'identità la dà il login, non la scelta del nome
// (DD-011). Si aspetta `pronta`, altrimenti il primo render sloggato rimbalzerebbe fuori
// chi ha già la sessione in localStorage.
2026-08-06 08:36:47 +02:00
useEffect(() => {
setMounted(true);
2026-08-31 18:21:34 +02:00
if (pronta && (!giocatore || !utenteId) && !isBenvenuto) {
2026-08-06 08:36:47 +02:00
navigate({ to: "/benvenuto" });
}
2026-08-31 18:21:34 +02:00
}, [giocatore, utenteId, pronta, isBenvenuto, navigate]);
2026-08-06 08:36:47 +02:00
2026-08-31 18:21:34 +02:00
if (!mounted || !pronta) {
2026-08-06 08:36:47 +02:00
return (
<div className="grid min-h-dvh place-items-center bg-background">
2026-08-06 08:36:47 +02:00
<TeamLogo className="h-16 w-16 animate-pulse" />
</div>
);
}
return (
<>
<div className="spazio-nav mx-auto min-h-dvh max-w-md bg-background">
2026-08-06 08:36:47 +02:00
{/* Required: nested routes render here. Removing <Outlet /> breaks all child routes. */}
<Outlet />
</div>
{!isBenvenuto && <BottomNav />}
{!isBenvenuto && <CelebrazioneBadge />}
<Toaster position="top-center" duration={3500} closeButton />
</>
2026-08-06 08:36:47 +02:00
);
}