La percentuale di presenze degli ultimi 30 giorni decide le convocazioni, ma stava dentro due useMemo — scritta due volte, una per giocatore e una per la rosa — e nessun test la toccava. Le tre regole che la determinano non sono ovvie: la finestra è di 30 giorni, il ritardo conta come presenza, e convocati vuoto significa tutta la rosa attiva. presenze-mese-core.ts contiene ora il calcolo puro, presenze-mese.ts solo il collegamento agli hook. Comportamento invariato, inclusa la differenza fra i due percorsi: per giocatore si filtrano i convocati, per la rosa l'elenco vuoto si espande agli attivi. Il test fissa le tre regole e i casi che le rompono: nessun evento nella finestra dà 0 e non NaN, chi non è convocato resta fuori dal denominatore, chi non ha mai risposto ci resta dentro. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
37 lines
1.3 KiB
TypeScript
37 lines
1.3 KiB
TypeScript
import { useMemo } from "react";
|
|
import { useEventi } from "./eventi";
|
|
import { useRispostePresenze } from "./presenze";
|
|
import { useGiocatoriSquadra } from "./giocatori-squadra";
|
|
import {
|
|
presenzeUltimoMese,
|
|
presenzeUltimoMeseTutti,
|
|
type StatistichePresenza,
|
|
} from "./presenze-mese-core";
|
|
|
|
/**
|
|
* Percentuale di presenze dell'ultimo mese (30 giorni), utile per le convocazioni.
|
|
* Usa solo cache già in memoria: nessuna query aggiuntiva. Il calcolo sta in
|
|
* `presenze-mese-core.ts`, qui c'è solo il collegamento agli hook.
|
|
*/
|
|
export function usePresenzeUltimoMese(giocatoreId: string | undefined): StatistichePresenza {
|
|
const { eventi } = useEventi();
|
|
const { presenze } = useRispostePresenze();
|
|
|
|
return useMemo(
|
|
() => presenzeUltimoMese(giocatoreId, eventi, presenze),
|
|
[eventi, presenze, giocatoreId],
|
|
);
|
|
}
|
|
|
|
/** Percentuale presenze ultimi 30 giorni per tutti i giocatori (mappa per id). */
|
|
export function usePresenzeUltimoMeseTutti(): Record<string, StatistichePresenza> {
|
|
const { eventi } = useEventi();
|
|
const { presenze } = useRispostePresenze();
|
|
const { righe: squadra } = useGiocatoriSquadra();
|
|
|
|
return useMemo(() => {
|
|
const idRosa = squadra.filter((g) => g.attivo).map((g) => g.id);
|
|
return presenzeUltimoMeseTutti(eventi, presenze, idRosa);
|
|
}, [eventi, presenze, squadra]);
|
|
}
|