import { Check, Info, Plus } from 'lucide-react' import { useEffect, useRef, useState } from 'react' import { t } from '../i18n' import { TYPE_BY_VALUE } from '../lib/constants' import { canWatchMore, nextAirDate } from '../lib/airing' import { episodeCap, seasonPosition } from '../lib/franchise' import { genreLabel } from '../lib/genres' import { cx, formatDate } from '../lib/utils' import { useMedia } from '../store' import type { MediaItem } from '../types' /** Où j'en suis : « S2 · É3 » (épisode suivant à voir) */ export function nextEpisodeLabel(item: MediaItem): string | undefined { if (!TYPE_BY_VALUE[item.type].episodic || !canWatchMore(item)) return undefined const pos = seasonPosition({ ...item, episodesWatched: item.episodesWatched + 1, season: undefined }) const ep = t('episodes.short', { n: pos ? pos.episode : item.episodesWatched + 1 }) return pos ? `${t('episodes.seasonShort', { n: pos.season })} · ${ep}` : ep } /** Plus rien à cocher : « Tout vu », ou « Prochain épisode le … » si la suite n'est pas encore sortie. */ export function waitingLabel(item: MediaItem): string { const cap = episodeCap(item) if (cap && item.episodesWatched >= cap) return t('hero.allSeen') const date = nextAirDate(item) return date ? t('hero.nextOn', { date: formatDate(date) }) : t('hero.upToDate') } /** Grande bannière de l'accueil : défile entre les titres que je regarde en ce moment (ou le prochain à voir). */ export default function HomeHero({ items, onOpen }: { items: MediaItem[]; onOpen: (item: MediaItem) => void }) { const { incrementEpisode } = useMedia() // On retient le titre affiché (et pas sa position) : la liste se réordonne quand on avance un épisode const [currentId, setCurrentId] = useState() const [pausedAt, setPausedAt] = useState(0) const touchX = useRef(null) const count = items.length const current = Math.max(0, items.findIndex((i) => i.id === currentId)) const item = items[current] const latest = useRef(items) latest.current = items // Défilement automatique (toutes les 7 s), relancé après chaque action manuelle useEffect(() => { if (count < 2) return const timer = setInterval(() => { if (document.visibilityState !== 'visible') return setCurrentId((id) => { const list = latest.current const k = Math.max(0, list.findIndex((i) => i.id === id)) return list[(k + 1) % list.length]?.id }) }, 7000) return () => clearInterval(timer) }, [count, pausedAt]) const go = (k: number) => { setCurrentId(items[((k % count) + count) % count]?.id) setPausedAt(Date.now()) } if (!item) return null const next = nextEpisodeLabel(item) const episodic = TYPE_BY_VALUE[item.type].episodic const meta = [TYPE_BY_VALUE[item.type].label, item.year, ...item.genres.slice(0, 2).map(genreLabel)].filter(Boolean).join(' · ') return (
(touchX.current = e.touches[0].clientX)} onTouchEnd={(e) => { const start = touchX.current touchX.current = null if (start == null || count < 2) return const dx = e.changedTouches[0].clientX - start if (Math.abs(dx) > 45) go(current + (dx < 0 ? 1 : -1)) }} >

{item.status === 'en_cours' ? t('homeHero.continue') : t('homeHero.next')}

{item.title}

{meta &&

{meta}

}
{episodic && next ? ( ) : episodic ? ( {waitingLabel(item)} ) : null} {count > 1 && ( {items.map((it, k) => (
) } /** Carte horizontale « Reprendre » : image, barre de progression, prochain épisode. */ export function ContinueCard({ item, onOpen }: { item: MediaItem; onOpen: (item: MediaItem) => void }) { const { incrementEpisode } = useMedia() const image = item.backdrop ?? item.poster const next = nextEpisodeLabel(item) return (
{next && ( )}
) }