Files
bobinoo/src/components/TopFiveEditor.tsx
T
Claude ad211b8112 Notes en étoiles uniquement : plus d'échelle sur 10
- Réglage « Notation » retiré, ainsi que le curseur sur 10 et l'affichage « /10 »
  (fiche, cartes, Top 5, stats, filtres, carte de partage) : tout s'affiche sur 5 étoiles
- Les notes restent stockées comme avant (sur 10 en interne) : rien ne change pour les notes existantes
- Réglage ratingScale retiré des réglages synchronisés et des sauvegardes ; textes inutiles retirés (18 langues)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YVo1qxKoBQCaMd7Ue8PGbe
2026-11-27 16:22:07 +00:00

194 lines
8.9 KiB
TypeScript

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<string[]>(() => 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 (
<div className="sheet sheet-in" role="dialog" aria-modal="true" aria-label={t('top.editLabel')}>
<header className="safe-top border-b border-line">
<div className="mx-auto flex max-w-2xl items-center gap-2 px-3 py-2.5">
<button onClick={onClose} className="grid size-10 place-items-center rounded-full text-ink-2" aria-label={t('common.close')}>
<X size={22} />
</button>
<h2 className="flex-1 text-center text-base font-semibold">
Top <span className="text-accent">5</span> · {catInfo.plural}
</h2>
<button onClick={save} disabled={saving} className="btn btn-light px-4 py-2 text-sm">
{t('common.save')}
</button>
</div>
</header>
<div className="sheet-scroll">
<div className="safe-bottom mx-auto max-w-2xl space-y-6 px-4 py-5 pb-16">
<div className="no-scrollbar -mx-4 flex gap-2 overflow-x-auto px-4">
{TOP_CATEGORIES.filter((c) => c.value === category || settings.topCategories.includes(c.value)).map((c) => (
<button key={c.value} onClick={() => switchCategory(c.value)} className={cx('chip py-1! text-[13px]', category === c.value && 'chip-on')}>
{c.plural}
</button>
))}
</div>
{/* Les 5 places */}
<ol className="space-y-2">
{[0, 1, 2, 3, 4].map((index) => {
const item = ids[index] ? byId.get(ids[index]) : undefined
return (
<li key={index} className={cx('flex items-center gap-3 rounded-2xl border p-2.5', item ? 'border-line bg-surface' : 'border-dashed border-line-strong')}>
<span className={cx('w-8 text-center text-3xl font-black', index === 0 ? 'text-accent' : 'text-ink-3')}>{index + 1}</span>
{item ? (
<>
<div className="w-10 shrink-0">
<Poster src={item.poster} title={item.title} />
</div>
<div className="min-w-0 flex-1">
<p className="truncate text-sm font-semibold">{item.title}</p>
<p className="text-xs text-ink-3">
{[item.year, item.rating ? `${formatRating(item.rating)}/5` : null].filter(Boolean).join(' · ')}
</p>
</div>
<div className="flex shrink-0 items-center">
<button onClick={() => move(index, -1)} disabled={index === 0} className="grid size-9 place-items-center text-ink-2 disabled:opacity-20" aria-label={t('common.moveUp')}>
<ChevronUp size={18} />
</button>
<button onClick={() => move(index, 1)} disabled={index >= ids.length - 1} className="grid size-9 place-items-center text-ink-2 disabled:opacity-20" aria-label={t('common.moveDown')}>
<ChevronDown size={18} />
</button>
<button onClick={() => setIds((p) => p.filter((id) => id !== item.id))} className="grid size-9 place-items-center text-ink-3" aria-label={t('common.removeX', { name: item.title })}>
<X size={17} />
</button>
</div>
</>
) : (
<span className="py-3 text-sm text-ink-3">{t('top.freeSlot')}</span>
)}
</li>
)
})}
</ol>
{/* Ajout depuis la bibliothèque */}
<div>
<div className="mb-3 flex items-center justify-between">
<h3 className="eyebrow text-ink-2">{t('top.addFromLibrary')}</h3>
<button onClick={() => setAllTypes((v) => !v)} className={cx('chip py-0.5! text-xs', allTypes && 'chip-on')}>
{t('top.allTypes')}
</button>
</div>
<div className="relative mb-2">
<Search size={17} className="pointer-events-none absolute start-3.5 top-1/2 -translate-y-1/2 text-ink-3" />
<input type="search" value={query} onChange={(e) => setQuery(e.target.value)} placeholder={t('top.searchTitle')} className="field ps-10" />
</div>
{ids.length >= 5 && <p className="py-2 text-xs text-ink-3">{t('top.full')}</p>}
<ul className="divide-y divide-line">
{candidates.map((item) => (
<li key={item.id}>
<button
onClick={() => setIds((p) => (p.length < 5 ? [...p, item.id] : p))}
disabled={ids.length >= 5}
className="flex w-full items-center gap-3 py-2.5 text-start disabled:opacity-40"
>
<div className="w-9 shrink-0">
<Poster src={item.poster} title={item.title} />
</div>
<div className="min-w-0 flex-1">
<p className="truncate text-sm font-medium">{item.title}</p>
<div className="mt-1 flex items-center gap-2 text-xs text-ink-3">
<TypeBadge item={item} />
{item.rating ? `${formatRating(item.rating)}/5` : ''}
{item.top && item.top.category !== category && (
<span className="text-accent">
{t('top.number', { rank: item.top.rank })} {TOP_CATEGORIES.find((c) => c.value === item.top!.category)?.plural}
</span>
)}
</div>
</div>
<span className="text-accent">+</span>
</button>
</li>
))}
{candidates.length === 0 && <li className="py-4 text-sm text-ink-3">{t('top.noCandidates')}</li>}
</ul>
</div>
</div>
</div>
</div>
)
}