import { genreLabel, subtypeLabel } from '../lib/genres' import { locale, t } from '../i18n' import { Heart, Search, SlidersHorizontal, X } from 'lucide-react' import { useMemo, useState } from 'react' import { StatusDot } from '../components/Badges' import JournalView from '../components/JournalView' import ListsView from '../components/ListsView' import { MediaCard } from '../components/MediaCard' import { EmptyState, PageHeader } from '../components/ui' import { MEDIA_TYPES, STATUSES } from '../lib/constants' import { cx, normalizeText } from '../lib/utils' import { useMedia } from '../store' import type { MediaItem, MediaType, WatchStatus } from '../types' type SortKey = 'recent' | 'rating' | 'title' | 'watched' const SORTS: { value: SortKey; readonly label: string }[] = [ { value: 'recent', get label() { return t('catalog.sortRecent') } }, { value: 'watched', get label() { return t('catalog.sortWatched') } }, { value: 'rating', get label() { return t('catalog.sortRating') } }, { value: 'title', get label() { return t('catalog.sortTitle') } }, ] interface Props { onOpen: (item: MediaItem) => void onAdd: () => void } export default function CatalogPage({ onOpen, onAdd }: Props) { const { items, settings, lists } = useMedia() const [view, setView] = useState<'titles' | 'journal' | 'lists'>('titles') const [query, setQuery] = useState('') const [types, setTypes] = useState>(new Set()) const [status, setStatus] = useState('') const [genre, setGenre] = useState('') const [minRating, setMinRating] = useState(0) const [favOnly, setFavOnly] = useState(false) const [sort, setSort] = useState('recent') const [showFilters, setShowFilters] = useState(false) const allGenres = useMemo( () => [...new Set(items.flatMap((i) => i.genres))].sort((a, b) => a.localeCompare(b, locale())), [items], ) const toggleType = (t: MediaType) => setTypes((prev) => { const next = new Set(prev) if (next.has(t)) next.delete(t) else next.add(t) return next }) const results = useMemo(() => { const q = normalizeText(query) const filtered = items.filter((i) => { if (types.size && !types.has(i.type)) return false if (status && i.status !== status) return false if (genre && !i.genres.includes(genre)) return false if (minRating && (i.rating ?? 0) < minRating) return false if (favOnly && !i.favorite) return false if (q) { const haystack = normalizeText([i.title, i.originalTitle, i.subtype, i.subtype && subtypeLabel(i.subtype), i.platform, ...i.genres, ...i.genres.map(genreLabel)].filter(Boolean).join(' ')) if (!haystack.includes(q)) return false } return true }) const sorted = [...filtered] if (sort === 'rating') sorted.sort((a, b) => (b.rating ?? -1) - (a.rating ?? -1)) else if (sort === 'title') sorted.sort((a, b) => a.title.localeCompare(b.title, locale())) else if (sort === 'watched') sorted.sort((a, b) => (b.endDate ?? b.startDate ?? '').localeCompare(a.endDate ?? a.startDate ?? '')) return sorted }, [items, query, types, status, genre, minRating, favOnly, sort]) const activeAdvanced = (genre ? 1 : 0) + (minRating ? 1 : 0) + (favOnly ? 1 : 0) const anyFilter = query || types.size || status || activeAdvanced const resetAll = () => { setQuery('') setTypes(new Set()) setStatus('') setGenre('') setMinRating(0) setFavOnly(false) } // Options de note minimale selon l'échelle affichée (valeurs internes sur 10) const ratingOptions = settings.ratingScale === '5' ? [2, 4, 6, 8, 9, 10] : [5, 6, 7, 8, 9, 10] return ( <>
{(['titles', 'journal', 'lists'] as const).map((v) => ( ))}
{view === 'journal' ? ( ) : view === 'lists' ? ( ) : ( <> {/* Recherche + filtres (collants en haut) */}
setQuery(e.target.value)} placeholder={t('catalog.searchPh')} className="field ps-10" enterKeyHint="search" />
{MEDIA_TYPES.map((mt) => ( ))}
{STATUSES.map((s) => ( ))}
{showFilters && (
)}
{results.length === 0 ? ( {t('catalog.clearFilters')} ) : ( ) } /> ) : (
{results.map((item) => ( ))}
)} )} ) }