import { locale, t } from '../i18n' import { Loader2, RefreshCw } from 'lucide-react' import { useCallback, useEffect, useMemo, useRef, useState } from 'react' import { loadAiring, needsCheck, novelties, refreshAiring, saveAiring, trackable, type AiringCache } from '../lib/airing' import { formatDate, todayISO } from '../lib/utils' import { useMedia } from '../store' import type { MediaItem } from '../types' import Poster from './Poster' import { SectionTitle } from './ui' const dayLabel = (iso: string) => { const d = new Date(iso + 'T12:00:00') const diff = Math.round((d.getTime() - new Date(todayISO() + 'T12:00:00').getTime()) / 86400000) if (diff <= 0) return t('time.today') if (diff === 1) return t('time.tomorrow') if (diff < 7) return d.toLocaleDateString(locale(), { weekday: 'long' }) return t('time.onDate', { date: formatDate(iso) }) } /** Section « Nouveautés » de l'accueil : nouveaux épisodes et nouvelles saisons. */ export default function NewEpisodes({ onOpen }: { onOpen: (item: MediaItem) => void }) { const { items, loading, settings, patchMany, update } = useMedia() const [cache, setCache] = useState(loadAiring) const [checking, setChecking] = useState(false) const started = useRef(false) const check = useCallback( async (force = false) => { setChecking(true) try { const next = await refreshAiring(items, loadAiring(), settings.tmdbKey, force) saveAiring(next) setCache(next) // Met à jour le nombre total d'épisodes des séries en cours si de nouveaux sont sortis const patches = trackable(items) .filter((i) => i.status !== 'termine' && next[i.id] && next[i.id].aired > (i.episodesTotal ?? 0)) .map((i) => ({ id: i.id, patch: { episodesTotal: next[i.id].aired } })) if (patches.length) await patchMany(patches) } finally { setChecking(false) } }, [items, settings.tmdbKey, patchMany], ) // Vérification automatique au plus toutes les 12 h, au lancement useEffect(() => { if (loading || started.current || !navigator.onLine) return started.current = true const c = loadAiring() if (trackable(items).some((i) => needsCheck(i, c))) void check() }, [loading, items, check]) const list = useMemo(() => novelties(items, cache), [items, cache]) const tracked = trackable(items).length const hasAniList = trackable(items).some((i) => i.externalId!.startsWith('anilist:')) if (!tracked) return null const resume = (item: MediaItem, aired: number) => update(item.id, { status: 'en_cours', episodesTotal: aired, startDate: item.startDate ?? todayISO(), endDate: undefined }) return ( <> check(true)} disabled={checking} className="flex items-center gap-1.5 text-xs font-medium text-ink-3"> {checking ? : } {checking ? t('settings.checking') : t('newEp.check')} } > {t('newEp.title')} {list.length === 0 ? (

{checking ? t('newEp.checking') : !settings.tmdbKey && !hasAniList ? t('newEp.needKey') : t('newEp.nothing', { count: tracked })} {!checking && !settings.tmdbKey && hasAniList && ` ${t('newEp.needKeyAlso')}`}

) : (
{list.map(({ item, info, unwatched, kind }) => (
{kind === 'new_season' ? t('newEp.newSeason') : kind === 'new_episodes' ? t('newEp.catchUp') : t('newEp.soon')} {kind === 'upcoming' ? t('newEp.epWhen', { ep: info.next!.episode, when: dayLabel(info.next!.date) }) : t('newEp.available', { count: unwatched })} {kind !== 'upcoming' && info.next && ( {t('newEp.next', { ep: info.next.episode, when: dayLabel(info.next.date) })} )} {kind === 'new_season' && ( )}
))}
)} ) }