import { Settings2 } from 'lucide-react' import { useMemo, useState } from 'react' import { t } from '../i18n' import { BarList, Columns, Panel, StatTile } from '../components/stats/Charts' import Poster from '../components/Poster' import StatsSettings from '../components/stats/StatsSettings' import { EmptyState, PageHeader } from '../components/ui' import { STATUSES, TYPE_BY_VALUE } from '../lib/constants' import { genreLabel } from '../lib/genres' import { availableYears, computeStats, countries, itemsForPeriod, monthly, records, watchMinutes } from '../lib/stats' import { cx, formatDuration, formatRating } from '../lib/utils' import { useMedia } from '../store' import type { MediaItem, MediaType } from '../types' /** Onglets : tout, puis un par grande famille (les dramas coréens / chinois vont avec les séries). */ type Scope = 'all' | 'film' | 'series' | 'anime' | 'other' const SCOPE_TYPES: Record, MediaType[]> = { film: ['film'], series: ['serie', 'kdrama', 'cdrama'], anime: ['anime'], other: ['autre'], } const scopeLabel = (s: Scope) => s === 'all' ? t('stats.tabAll') : s === 'film' ? t('typePlural.film') : s === 'series' ? t('typePlural.serie') : s === 'anime' ? t('typePlural.anime') : t('stats.tabOther') /** Statistiques : vue générale, puis films, séries, animes et autres formats. */ export default function StatsPage({ onOpen }: { onOpen: (item: MediaItem) => void }) { const { items, settings } = useMedia() const [scope, setScope] = useState('all') const [year, setYear] = useState('') const [settingsOpen, setSettingsOpen] = useState(false) const scoped = useMemo(() => (scope === 'all' ? items : items.filter((i) => SCOPE_TYPES[scope].includes(i.type))), [items, scope]) const years = useMemo(() => availableYears(scoped), [scoped]) const period = year && years.includes(year) ? year : '' const list = useMemo(() => itemsForPeriod(scoped, period), [scoped, period]) // Onglets : « Général », « Films », « Séries » toujours ; les autres seulement s'ils ont des titres const scopes: Scope[] = (['all', 'film', 'series', 'anime', 'other'] as Scope[]).filter( (s) => s === 'all' || s === 'film' || s === 'series' || items.some((i) => SCOPE_TYPES[s].includes(i.type)), ) return ( <> {/* Filtres : une seule rangée (catégorie + période) */}
{scopes.map((s) => ( ))}
{list.length === 0 ? ( ) : ( setSettingsOpen(true)} /> )} {settingsOpen && setSettingsOpen(false)} />} ) } function StatsBody({ scope, items, year, hiddenMonths, onOpen, onSettings, }: { scope: Scope items: MediaItem[] year: string /** Mois masqués dans la courbe (paramètres des stats) */ hiddenMonths?: string[] onOpen: (item: MediaItem) => void onSettings: () => void }) { const hidden = useMemo(() => new Set(hiddenMonths ?? []), [hiddenMonths]) const s = useMemo(() => computeStats(items), [items]) const months = useMemo(() => monthly(items, year), [items, year]) const rec = useMemo(() => records(items), [items]) const geo = useMemo(() => countries(items), [items]) const isFilm = scope === 'film' const episodic = scope !== 'all' && scope !== 'film' const films = useMemo(() => items.filter((i) => i.type === 'film' && i.status === 'termine'), [items]) const avgFilm = films.length ? films.reduce((n, f) => n + watchMinutes(f) / (1 + (f.rewatchDates?.length ?? 0)), 0) / films.length : 0 const rewatches = items.reduce((n, i) => n + (i.rewatchDates?.length ?? 0), 0) // Décennies de sortie des titres vus const decades = useMemo(() => { const m = new Map() for (const i of items) if (i.status !== 'a_voir' && i.year && i.year > 1870) m.set(Math.floor(i.year / 10) * 10, (m.get(Math.floor(i.year / 10) * 10) ?? 0) + 1) return [...m.entries()].sort((a, b) => b[0] - a[0]) }, [items]) const days = Math.floor(s.totalMinutes / 1440) const timeHint = days > 0 ? `${t('stats.nonStopPre')}${t('stats.days', { count: days })}${t('stats.nonStopPost')}` : undefined // Mois le plus chargé : seulement s'il sort vraiment du lot (pas d'égalité en tête) const ranked = months.filter((m) => !hidden.has(m.key)).sort((a, b) => b.count - a.count) const busiest = ranked[0] && ranked[0].count > (ranked[1]?.count ?? 0) ? ranked[0] : undefined // Répartition des notes : 10 tranches sur /10 (½ étoile par tranche en échelle /5) const ratingBars = s.ratingBuckets.map((n, k) => { const v = k + 1 const shown = formatRating(v) return { key: String(v), axis: v % 2 === 0 ? shown : '', value: n, tip: `${t('stats.ratedAs', { count: n, rating: shown })} : ${n}` } }) const typeRows = s.byType .filter((x) => x.value > 0 && (scope === 'all' || SCOPE_TYPES[scope as Exclude].includes(x.type))) .sort((a, b) => b.minutes - a.minutes) .map((x) => ({ key: x.type, label: TYPE_BY_VALUE[x.type].label, value: x.minutes, sub: t('stats.finishedN', { count: x.completed }) })) return (
{/* Les chiffres clés */}
{isFilm ? ( ) : ( )}
{/* Activité mois par mois */} } > ({ key: m.key, axis: m.initial, value: m.count, highlight: m === busiest, hidden: hidden.has(m.key), tip: hidden.has(m.key) ? `${m.label} : ${t('stats.monthHidden')}` : `${m.label} : ${t('stats.monthFinished', { count: m.count })}${m.minutes ? ` · ${formatDuration(m.minutes)}` : ''}`, }))} /> {busiest && busiest.count > 0 && (

{t('stats.busiest')} {busiest.label} · {t('stats.monthFinished', { count: busiest.count })}

)}
{/* Temps par type (vue générale, et séries quand il y a plusieurs formats) */} {typeRows.length > 1 && ( )} {s.ratedCount ? :

{t('stats.noRatings')}

}
{s.topGenres.length ? ( ({ key: g.label, label: genreLabel(g.label), value: g.value }))} /> ) : (

{t('stats.noGenres')}

)}
{geo.list.length > 0 && ( ({ key: c.code, label: ( <> {c.name} ), value: c.count, }))} /> )} {decades.length > 1 && ( ({ key: String(d), label: `${d} – ${d + 9}`, value: n }))} /> )} x.value > 0).map((x) => ({ key: x.status, label: x.label, value: x.value }))} /> {s.topPlatforms.length > 0 && ( ({ key: p.label, label: p.label, value: p.value }))} /> )} {/* Records */} {(rec.marathon && (episodic || scope === 'all')) || (rec.longestFilm && (isFilm || scope === 'all')) ? (
{rec.marathon && (episodic || scope === 'all') && ( )} {rec.longestFilm && (isFilm || scope === 'all') && ( )}
) : null}
{/* Mes mieux notés */} {rec.bestRated && rec.bestRated.length > 0 && (
    {rec.bestRated.map((i) => (
  • ))}
)}

{t('stats.footnote')}

) } function RecordRow({ label, item, value, onOpen }: { label: string; item: MediaItem; value: string; onOpen: (item: MediaItem) => void }) { return ( ) }