import { t } from '../i18n'
import { Loader2, RefreshCw } from 'lucide-react'
import { useMemo, useState } from 'react'
import { fetchPublicRatings, missingPublic } from '../lib/publicRatings'
import { cx, formatRating } from '../lib/utils'
import { useMedia } from '../store'
import type { MediaItem, RatingScale } from '../types'
import Poster from './Poster'
import { SectionTitle } from './ui'
/** Écart signé, arrondi au dixième dans l'échelle choisie (« 0 » plutôt que « +0,0 »). */
const signed = (v: number, scale: RatingScale) => {
const r = Math.round((scale === '5' ? v / 2 : v) * 10) / 10
if (r === 0) return '0'
return `${r > 0 ? '+' : '−'}${String(Math.abs(r)).replace('.', t('common.decimalSep'))}`
}
function Row({ item, scale }: { item: MediaItem; scale: RatingScale }) {
const gap = item.rating! - item.publicRating!
// barre centrée : moitié gauche = moins aimé, moitié droite = plus aimé (écart max affiché : 5 points /10)
const width = Math.min(50, (Math.abs(gap) / 5) * 50)
return (
{item.title}
{t('public.me')} {formatRating(item.rating, scale)} · {t('public.label')} {formatRating(item.publicRating, scale)}
= 0 ? 'bg-accent' : 'bg-ink-3')}
style={gap >= 0 ? { left: '50%', width: `${width}%` } : { right: '50%', width: `${width}%` }}
/>
= 0 ? 'text-accent' : 'text-ink-2')}>{signed(gap, scale)}
)
}
/** Stats « Moi vs le public » : écarts entre mes notes et la moyenne TMDB / AniList. */
export default function Disagreements({ scoped }: { scoped: MediaItem[] }) {
const { items, settings, patchMany } = useMedia()
const scale = settings.ratingScale
const [progress, setProgress] = useState<[number, number]>()
const [message, setMessage] = useState()
const compared = useMemo(() => scoped.filter((i) => i.rating != null && i.publicRating != null), [scoped])
const rated = useMemo(() => items.filter((i) => i.rating != null), [items])
const missing = useMemo(() => missingPublic(rated, settings.tmdbKey).length, [rated, settings.tmdbKey, progress])
const run = async () => {
if (!navigator.onLine) return setMessage(t('common.offline'))
setMessage(undefined)
setProgress([0, missing])
try {
const patches = await fetchPublicRatings(rated, settings.tmdbKey, (done, total) => setProgress([done, total]))
if (patches.length) await patchMany(patches)
setMessage(patches.length ? t('public.added', { count: patches.length }) : t('public.noneFound'))
} finally {
setProgress(undefined)
}
}
const gaps = compared.map((i) => i.rating! - i.publicRating!)
const avg = gaps.length ? gaps.reduce((a, b) => a + b, 0) / gaps.length : 0
const agree = gaps.length ? Math.round((gaps.filter((g) => Math.abs(g) <= 1).length / gaps.length) * 100) : 0
const sorted = [...compared].sort((a, b) => b.rating! - b.publicRating! - (a.rating! - a.publicRating!))
const loved = sorted.filter((i) => i.rating! - i.publicRating! >= 1).slice(0, 5)
const hated = sorted.filter((i) => i.rating! - i.publicRating! <= -1).reverse().slice(0, 5)
const fetchButton =
missing > 0 || progress ? (
) : undefined
return (
<>
{t('public.title')}
{message && {message}
}
{compared.length === 0 ? (
{missing > 0
? t('public.hintFetch')
: settings.tmdbKey
? t('public.hintNone')
: t('public.hintKey')}
) : (
<>
{t('public.avgGap')}
= 0.3 && 'text-accent')}>{signed(avg, scale)}
{Math.abs(avg) < 0.3 ? t('public.average') : avg > 0 ? t('public.generous') : t('public.harsh')}
{t('public.agreeTitle')}
{agree} %
{scale === '5' ? t('public.within5', { count: compared.length }) : t('public.within10', { count: compared.length })}
{loved.length > 0 && (
{t('public.lovedPre')}{t('public.lovedAccent')}{t('public.lovedPost')}
)}
{hated.length > 0 && (
{t('public.hatedPre')}{t('public.hatedAccent')}{t('public.hatedPost')}
)}
{!loved.length && !hated.length && {t('public.allAgree')}
}
>
)}
>
)
}