Nuovi moduli src/lib/strapi-client.ts + rosa-base.ts/classifica-csi.ts/ storico-match.ts (hook TanStack Query, cache lunga, nessun polling). L'id stabile gN diventa il campo `codice` su Strapi, per non rompere le FK verso pagelle/cacche/mvp/palloni/presenze/azioni scout già chiavate su quella stringa. isAdmin ora prende il Giocatore invece di un id, così non dipende più dal roster. Rifattorizzati di conseguenza tutti i punti che importavano `giocatori`/`classifica`/`storicoMatch` come array sincroni (componenti, hook, due route server) per usare gli hook o un parametro esplicito. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
59 lines
1.5 KiB
TypeScript
59 lines
1.5 KiB
TypeScript
import { useSyncExternalStore } from "react";
|
|
import type { Giocatore } from "./crapp-data";
|
|
import { useRosaBase } from "./rosa-base";
|
|
import { conInfortuni, useInfortuniERitardi } from "./infortuni";
|
|
|
|
const KEY = "crapp-user-v1";
|
|
const listeners = new Set<() => void>();
|
|
let cache: string | null | undefined = undefined;
|
|
|
|
function read(): string | null {
|
|
if (cache !== undefined) return cache;
|
|
if (typeof window === "undefined") return null;
|
|
try {
|
|
cache = window.localStorage.getItem(KEY);
|
|
} catch {
|
|
cache = null;
|
|
}
|
|
return cache;
|
|
}
|
|
|
|
function write(next: string | null) {
|
|
cache = next;
|
|
try {
|
|
if (next) window.localStorage.setItem(KEY, next);
|
|
else window.localStorage.removeItem(KEY);
|
|
} catch {
|
|
/* storage non disponibile */
|
|
}
|
|
listeners.forEach((l) => l());
|
|
}
|
|
|
|
export function impostaGiocatore(id: string) {
|
|
write(id);
|
|
}
|
|
|
|
export function resetGiocatore() {
|
|
write(null);
|
|
}
|
|
|
|
/** Richiede un ancestor QueryClientProvider (la rosa base arriva dal CMS Strapi). */
|
|
export function useGiocatoreBase(): Giocatore | null {
|
|
const id = useSyncExternalStore(
|
|
(cb) => {
|
|
listeners.add(cb);
|
|
return () => listeners.delete(cb);
|
|
},
|
|
() => read(),
|
|
() => null,
|
|
);
|
|
const rosa = useRosaBase();
|
|
return id ? (rosa.find((x) => x.id === id) ?? null) : null;
|
|
}
|
|
|
|
export function useGiocatoreCorrente(): Giocatore | null {
|
|
const g = useGiocatoreBase();
|
|
const { infortuni, ritardi } = useInfortuniERitardi();
|
|
return g ? conInfortuni(g, infortuni, ritardi) : null;
|
|
}
|