import { locale, t } from '../i18n' import { ChevronDown, ChevronUp, Search, X } from 'lucide-react' import { useMemo, useState } from 'react' import { TOP_CATEGORIES } from '../lib/constants' import { cx, formatRating, normalizeText } from '../lib/utils' import { GuestError, useMedia } from '../store' import { useToast } from './Toast' import type { TopCategory } from '../types' import { TypeBadge } from './Badges' import Poster from './Poster' import { useBackToClose } from '../lib/backNav' import { useEscape } from '../lib/escape' import { useScrollLock } from '../lib/scrollLock' interface Props { category: TopCategory onClose: () => void } /** Composer son Top 5 : 5 places, ajout depuis la bibliothèque, réordonnancement. */ export default function TopFiveEditor({ category: initialCategory, onClose }: Props) { const { items, settings, setTopList } = useMedia() const [category, setCategory] = useState(initialCategory) const currentIds = (cat: TopCategory) => items .filter((i) => i.top?.category === cat) .sort((a, b) => a.top!.rank - b.top!.rank) .map((i) => i.id) const [ids, setIds] = useState(() => currentIds(initialCategory)) const [query, setQuery] = useState('') const [allTypes, setAllTypes] = useState(false) const [saving, setSaving] = useState(false) const toast = useToast() const catInfo = TOP_CATEGORIES.find((c) => c.value === category)! const byId = useMemo(() => new Map(items.map((i) => [i.id, i])), [items]) useScrollLock() useEscape(onClose) useBackToClose(onClose) /** Changer de catégorie enregistre d'abord celle en cours. */ const switchCategory = async (cat: TopCategory) => { if (cat === category) return await setTopList(category, ids) setCategory(cat) setIds(currentIds(cat).filter((id) => !ids.includes(id))) setQuery('') } const candidates = useMemo(() => { const q = normalizeText(query) return items .filter((i) => !ids.includes(i.id)) .filter((i) => allTypes || q || i.type === category) .filter((i) => !q || normalizeText(`${i.title} ${i.originalTitle ?? ''}`).includes(q)) .sort((a, b) => Number(b.type === category) - Number(a.type === category) || (b.rating ?? -1) - (a.rating ?? -1) || a.title.localeCompare(b.title, locale())) .slice(0, 60) }, [items, ids, query, allTypes, category]) const move = (index: number, delta: number) => setIds((prev) => { const next = [...prev] const target = index + delta if (target < 0 || target >= next.length) return prev ;[next[index], next[target]] = [next[target], next[index]] return next }) const save = async () => { setSaving(true) // Échec (hors-ligne, mode découverte…) : le bouton ne reste pas bloqué et la fenêtre reste ouverte try { await setTopList(category, ids) onClose() } catch (e) { if (!(e instanceof GuestError)) toast.show((e as Error).message) } finally { setSaving(false) } } return (

Top 5 · {catInfo.plural}

{TOP_CATEGORIES.filter((c) => c.value === category || settings.topCategories.includes(c.value)).map((c) => ( ))}
{/* Les 5 places */}
    {[0, 1, 2, 3, 4].map((index) => { const item = ids[index] ? byId.get(ids[index]) : undefined return (
  1. {index + 1} {item ? ( <>

    {item.title}

    {[item.year, item.rating ? `${formatRating(item.rating)}/5` : null].filter(Boolean).join(' · ')}

    ) : ( {t('top.freeSlot')} )}
  2. ) })}
{/* Ajout depuis la bibliothèque */}

{t('top.addFromLibrary')}

setQuery(e.target.value)} placeholder={t('top.searchTitle')} className="field ps-10" />
{ids.length >= 5 &&

{t('top.full')}

}
    {candidates.map((item) => (
  • ))} {candidates.length === 0 &&
  • {t('top.noCandidates')}
  • }
) }