2026-08-06 08:36:47 +02:00
|
|
|
import { useSyncExternalStore } from "react";
|
2026-09-03 10:16:52 +02:00
|
|
|
import type { Giocatore } from "./crapp-data";
|
2026-09-24 14:54:35 +02:00
|
|
|
import { isAllenatore, useGiocatoriSquadra, type GiocatoreSquadra } from "./giocatori-squadra";
|
2026-09-01 22:34:07 +02:00
|
|
|
import { useIo } from "./rosa";
|
2026-08-06 08:36:47 +02:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-01 22:34:07 +02:00
|
|
|
/** Id del giocatore collegato al dispositivo (localStorage). */
|
|
|
|
|
export function useGiocatoreId(): string | null {
|
|
|
|
|
return useSyncExternalStore(
|
2026-08-06 08:36:47 +02:00
|
|
|
(cb) => {
|
|
|
|
|
listeners.add(cb);
|
|
|
|
|
return () => listeners.delete(cb);
|
|
|
|
|
},
|
|
|
|
|
() => read(),
|
|
|
|
|
() => null,
|
|
|
|
|
);
|
2026-09-01 22:34:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Anagrafica base dal localStorage: le statistiche restano a zero finché non passa da `useIo`. */
|
2026-09-03 10:16:52 +02:00
|
|
|
export function useGiocatoreBase(): GiocatoreSquadra | null {
|
2026-09-01 22:34:07 +02:00
|
|
|
const id = useGiocatoreId();
|
2026-09-03 10:16:52 +02:00
|
|
|
const { righe } = useGiocatoriSquadra();
|
|
|
|
|
return id ? (righe.find((x) => x.id === id) ?? null) : null;
|
2026-08-06 08:36:47 +02:00
|
|
|
}
|
|
|
|
|
|
2026-09-24 14:54:35 +02:00
|
|
|
/**
|
|
|
|
|
* Chi usa l'app è un allenatore (DD-034): decide cosa si vede (niente badge, cacche,
|
|
|
|
|
* stagione, obiettivi). I permessi veri stanno in `user_roles` (`usePuoGestireEventi`).
|
|
|
|
|
*/
|
|
|
|
|
export function useSonoAllenatore(): boolean {
|
|
|
|
|
return isAllenatore(useGiocatoreBase());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Il proprio slot solo se è di un giocatore: per rispondere alle presenze, votare,
|
|
|
|
|
* dichiarare le cacche o portare i palloni. Per l'allenatore è `null`.
|
|
|
|
|
*/
|
|
|
|
|
export function useGiocatoreInCampo(): GiocatoreSquadra | null {
|
|
|
|
|
const g = useGiocatoreBase();
|
|
|
|
|
return g && !isAllenatore(g) ? g : null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-01 22:34:07 +02:00
|
|
|
/** Giocatore corrente con statistiche calcolate da dati reali (presenze, MVP, badge, …). */
|
2026-08-06 08:36:47 +02:00
|
|
|
export function useGiocatoreCorrente(): Giocatore | null {
|
2026-09-01 22:34:07 +02:00
|
|
|
return useIo();
|
2026-08-06 08:36:47 +02:00
|
|
|
}
|