diff --git a/src/components/EpisodeList.tsx b/src/components/EpisodeList.tsx index d8d38cd..6187660 100644 --- a/src/components/EpisodeList.tsx +++ b/src/components/EpisodeList.tsx @@ -1,4 +1,4 @@ -import { Check, Loader2 } from 'lucide-react' +import { Check, Loader2, Lock } from 'lucide-react' import { useEffect, useLayoutEffect, useRef, useState } from 'react' import { locale, t } from '../i18n' import { getTmdbSeasonEpisodes, type EpisodeInfo } from '../lib/catalogApi' @@ -9,6 +9,8 @@ import type { MediaInput } from '../types' interface Props { form: Pick + /** Épisodes déjà sortis (toutes saisons), si on le sait : les suivants ne se cochent pas */ + aired?: number /** Nouveau nombre d'épisodes vus (et saison choisie) */ onChange: (episodesWatched: number, season?: number) => void } @@ -19,7 +21,7 @@ const MAX_GENERIC = 500 * Liste des épisodes, saison par saison (façon plateforme de streaming). * Toucher un épisode le marque comme vu, ainsi que tous ceux d'avant ; retoucher le dernier vu l'annule. */ -export default function EpisodeList({ form, onChange }: Props) { +export default function EpisodeList({ form, aired, onChange }: Props) { const { settings } = useMedia() const seasons = form.seasons const pos = seasonPosition(form) @@ -105,15 +107,19 @@ export default function EpisodeList({ form, onChange }: Props) {
{rows.map((e) => { const seen = e.number <= watchedHere - const upcoming = !!e.date && e.date > today + // Pas encore sorti : date à venir (TMDB), ou au-delà des épisodes diffusés + const upcoming = e.date ? e.date > today : aired != null && before + e.number > aired + // Un épisode à venir ne se coche pas (mais on peut décocher une erreur passée) + const locked = upcoming && !seen return ( ) diff --git a/src/components/HomeHero.tsx b/src/components/HomeHero.tsx index e0d9f59..0aa6cbb 100644 --- a/src/components/HomeHero.tsx +++ b/src/components/HomeHero.tsx @@ -2,22 +2,29 @@ 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 } from '../lib/utils' +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) return undefined - const cap = episodeCap(item) - if (cap && item.episodesWatched >= cap) return 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() @@ -104,7 +111,7 @@ export default function HomeHero({ items, onOpen }: { items: MediaItem[]; onOpen ) : episodic ? ( - {t('hero.allSeen')} + {waitingLabel(item)} ) : null}
) diff --git a/src/components/MediaCard.tsx b/src/components/MediaCard.tsx index 9db075d..75882ed 100644 --- a/src/components/MediaCard.tsx +++ b/src/components/MediaCard.tsx @@ -1,6 +1,7 @@ import { t } from '../i18n' import { Heart, Plus } from 'lucide-react' import { TYPE_BY_VALUE } from '../lib/constants' +import { canWatchMore } from '../lib/airing' import { episodeCap, seasonPosition } from '../lib/franchise' import { useMedia } from '../store' import type { MediaItem } from '../types' @@ -40,7 +41,7 @@ export function MediaCard({ item, onOpen }: { item: MediaItem; onOpen: (item: Me const canIncrement = TYPE_BY_VALUE[item.type].episodic && item.status === 'en_cours' && - (!episodeCap(item) || item.episodesWatched < episodeCap(item)!) + canWatchMore(item) return (
diff --git a/src/components/MediaForm.tsx b/src/components/MediaForm.tsx index 7dbdaf1..ebf551d 100644 --- a/src/components/MediaForm.tsx +++ b/src/components/MediaForm.tsx @@ -8,7 +8,8 @@ import { getTmdbCredits, getTmdbExtras } from '../lib/catalogApi' import { episodeCap, seasonPosition } from '../lib/franchise' import { renderItemCard, slug } from '../lib/shareCard' import { useScrollLock } from '../lib/scrollLock' -import { cx, formatRating, todayISO } from '../lib/utils' +import { cx, formatDate, formatRating, todayISO } from '../lib/utils' +import { airedCount, nextAirDate, useAiringCache } from '../lib/airing' import { useMedia } from '../store' import type { MediaInput, MediaItem, RatingScale, WatchStatus } from '../types' import { StatusDot } from './Badges' @@ -203,10 +204,18 @@ export default function MediaForm({ item, onClose, onGoToSettings, onOpenItem }: } /** Bouton principal de l'en-tête : épisode suivant (séries) ou « vu » (films). */ + // Épisodes déjà sortis (fiche suivie) : on ne coche pas un épisode à venir + const airingCache = useAiringCache() + const aired = item ? airedCount({ id: item.id, type: form.type, externalId: form.externalId }, airingCache) : undefined + const primary: HeroAction = (() => { if (typeInfo.episodic) { const cap = episodeCap(form) if (cap && form.episodesWatched >= cap) return { label: t('hero.allSeen'), done: true } + if (aired != null && form.episodesWatched >= aired) { + const date = item ? nextAirDate({ id: item.id, type: form.type, externalId: form.externalId }, airingCache) : undefined + return { label: date ? t('hero.nextOn', { date: formatDate(date) }) : t('hero.upToDate'), done: true } + } const nextPos = seasonPosition({ ...form, episodesWatched: form.episodesWatched + 1, season: undefined }) const ep = t('episodes.short', { n: nextPos ? nextPos.episode : form.episodesWatched + 1 }) const label = t('hero.seenEp', { ep: nextPos ? `${t('episodes.seasonShort', { n: nextPos.season })} · ${ep}` : ep }) @@ -465,7 +474,7 @@ export default function MediaForm({ item, onClose, onGoToSettings, onOpenItem }:
{/* Liste d'épisodes pour une fiche liée (TMDB / AniList) ; compteur + total modifiable pour une fiche manuelle */} {form.seasons || (form.externalId && form.episodesTotal) ? ( - setForm((f) => withEpisodes(f, n, season))} /> + setForm((f) => withEpisodes(f, n, season))} /> ) : (
diff --git a/src/i18n/locales/de.ts b/src/i18n/locales/de.ts index 714b9db..2d0cd7b 100644 --- a/src/i18n/locales/de.ts +++ b/src/i18n/locales/de.ts @@ -737,6 +737,8 @@ const dict: Translations = { 'form.startShort': "Begonnen", 'form.endShort': "Beendet", 'hero.allSeen': "Alles gesehen", + 'hero.nextOn': 'Nächste Folge am {date}', + 'hero.upToDate': 'Auf dem neuesten Stand', 'hero.seenEp': "{ep} gesehen", 'hero.seen': "Gesehen", 'hero.markSeen': "Als gesehen markieren", diff --git a/src/i18n/locales/en.ts b/src/i18n/locales/en.ts index 274b4ed..e0fc377 100644 --- a/src/i18n/locales/en.ts +++ b/src/i18n/locales/en.ts @@ -737,6 +737,8 @@ const dict: Translations = { 'form.startShort': "Started", 'form.endShort': "Finished", 'hero.allSeen': "All watched", + 'hero.nextOn': 'Next episode on {date}', + 'hero.upToDate': 'Up to date', 'hero.seenEp': "Watched {ep}", 'hero.seen': "Watched", 'hero.markSeen': "Mark as watched", diff --git a/src/i18n/locales/es.ts b/src/i18n/locales/es.ts index 6d0c4e4..c112ea7 100644 --- a/src/i18n/locales/es.ts +++ b/src/i18n/locales/es.ts @@ -737,6 +737,8 @@ const dict: Translations = { 'form.startShort': "Empezado", 'form.endShort': "Terminado", 'hero.allSeen': "Todo visto", + 'hero.nextOn': 'Próximo episodio el {date}', + 'hero.upToDate': 'Al día', 'hero.seenEp': "He visto {ep}", 'hero.seen': "Visto", 'hero.markSeen': "Marcar como visto", diff --git a/src/i18n/locales/fr.ts b/src/i18n/locales/fr.ts index 9e0fffa..8d3e3d6 100644 --- a/src/i18n/locales/fr.ts +++ b/src/i18n/locales/fr.ts @@ -739,6 +739,8 @@ const dict = { 'form.startShort': "Commencé le", 'form.endShort': "Fini le", 'hero.allSeen': "Tout vu", + 'hero.nextOn': 'Prochain épisode le {date}', + 'hero.upToDate': 'À jour', 'hero.seenEp': "J'ai vu {ep}", 'hero.seen': "Vu", 'hero.markSeen': "Marquer comme vu", diff --git a/src/i18n/locales/it.ts b/src/i18n/locales/it.ts index 9d1277d..a50db68 100644 --- a/src/i18n/locales/it.ts +++ b/src/i18n/locales/it.ts @@ -737,6 +737,8 @@ const dict: Translations = { 'form.startShort': "Iniziato", 'form.endShort': "Finito", 'hero.allSeen': "Tutto visto", + 'hero.nextOn': 'Prossimo episodio il {date}', + 'hero.upToDate': 'In pari', 'hero.seenEp': "Ho visto {ep}", 'hero.seen': "Visto", 'hero.markSeen': "Segna come visto", diff --git a/src/i18n/locales/ja.ts b/src/i18n/locales/ja.ts index e6c9cb6..0331368 100644 --- a/src/i18n/locales/ja.ts +++ b/src/i18n/locales/ja.ts @@ -737,6 +737,8 @@ const dict: Translations = { 'form.startShort': "開始日", 'form.endShort': "完了日", 'hero.allSeen': "すべて視聴済み", + 'hero.nextOn': '次のエピソード:{date}', + 'hero.upToDate': '最新話まで視聴済み', 'hero.seenEp': "{ep}を見た", 'hero.seen': "視聴済み", 'hero.markSeen': "視聴済みにする", diff --git a/src/i18n/locales/ko.ts b/src/i18n/locales/ko.ts index 7c27fd7..c100766 100644 --- a/src/i18n/locales/ko.ts +++ b/src/i18n/locales/ko.ts @@ -737,6 +737,8 @@ const dict: Translations = { 'form.startShort': "시작일", 'form.endShort': "완료일", 'hero.allSeen': "모두 시청함", + 'hero.nextOn': '다음 에피소드: {date}', + 'hero.upToDate': '최신 화까지 봤어요', 'hero.seenEp': "{ep} 봤어요", 'hero.seen': "봤어요", 'hero.markSeen': "본 것으로 표시", diff --git a/src/i18n/locales/pt.ts b/src/i18n/locales/pt.ts index 529430c..333a9de 100644 --- a/src/i18n/locales/pt.ts +++ b/src/i18n/locales/pt.ts @@ -737,6 +737,8 @@ const dict: Translations = { 'form.startShort': "Começou", 'form.endShort': "Terminou", 'hero.allSeen': "Tudo visto", + 'hero.nextOn': 'Próximo episódio em {date}', + 'hero.upToDate': 'Em dia', 'hero.seenEp': "Vi {ep}", 'hero.seen': "Visto", 'hero.markSeen': "Marcar como visto", diff --git a/src/i18n/locales/zh.ts b/src/i18n/locales/zh.ts index 591ff46..ce74e28 100644 --- a/src/i18n/locales/zh.ts +++ b/src/i18n/locales/zh.ts @@ -737,6 +737,8 @@ const dict: Translations = { 'form.startShort': "开始于", 'form.endShort': "看完于", 'hero.allSeen': "全部看完", + 'hero.nextOn': '下一集:{date}', + 'hero.upToDate': '已追平', 'hero.seenEp': "已看 {ep}", 'hero.seen': "已看", 'hero.markSeen': "标记为已看", diff --git a/src/lib/airing.ts b/src/lib/airing.ts index 905349e..81d324f 100644 --- a/src/lib/airing.ts +++ b/src/lib/airing.ts @@ -1,3 +1,4 @@ +import { useEffect, useState } from 'react' import { tmdbLanguage } from '../i18n' import type { MediaItem } from '../types' import { CLOUD_TMDB, isPlausibleTmdbKey } from './catalogApi' @@ -38,8 +39,24 @@ const SEASONS_SINCE = '2026-10-04T10:00:00.000Z' const n0 = (v: unknown, max = 100000) => (typeof v === 'number' && Number.isFinite(v) && v >= 0 ? Math.min(max, Math.round(v)) : 0) +// Dernière lecture (le cache est relu par chaque carte : on évite de le réanalyser à chaque fois) +let memo: { raw: string | null; cache: AiringCache } | undefined + /** Relit le cache en ne gardant que des entrées bien formées. */ export function loadAiring(): AiringCache { + let text: string | null = null + try { + text = localStorage.getItem(KEY) + } catch { + /* stockage indisponible */ + } + if (memo && memo.raw === text) return memo.cache + const cache = parseAiring() + memo = { raw: text, cache } + return cache +} + +function parseAiring(): AiringCache { const raw = readStorage(KEY, {}) const out: AiringCache = {} if (!isPlainObject(raw)) return out @@ -87,17 +104,55 @@ export function needsCheck(item: MediaItem, cache: AiringCache, force = false): return age > (info.ended ? RECHECK_ENDED_MS : RECHECK_MS) } +type AiringItem = Pick + +const followed = (item: AiringItem) => + TYPE_BY_VALUE[item.type]?.episodic && isSafeExternalId(item.externalId) && (item.externalId.startsWith('tmdb:tv:') || item.externalId.startsWith('anilist:')) + +/** + * Nombre d'épisodes déjà sortis (toutes saisons), ou undefined si on ne sait pas (fiche manuelle, pas encore vérifiée). + * Un épisode annoncé dont la date est passée compte comme sorti, même avant la prochaine vérification. + */ +export function airedCount(item: AiringItem, cache: AiringCache = loadAiring()): number | undefined { + const info = cache[item.id] + if (!info || !info.aired || !followed(item)) return undefined + return info.next && info.next.date <= localDay() ? info.aired + 1 : info.aired +} + +/** Date du prochain épisode s'il n'est pas encore sorti. */ +export function nextAirDate(item: AiringItem, cache: AiringCache = loadAiring()): string | undefined { + const d = cache[item.id]?.next?.date + return d && d > localDay() ? d : undefined +} + +/** Peut-on cocher un épisode de plus ? (non si le suivant n'est pas encore sorti) */ +export function canWatchMore(item: MediaItem, cache: AiringCache = loadAiring()): boolean { + const cap = episodeCap(item) + if (cap && item.episodesWatched >= cap) return false + const aired = airedCount(item, cache) + return aired == null || item.episodesWatched < aired +} + /** * À jour : j'ai vu tous les épisodes sortis et la série continue. * La fiche quitte « Continuer », puis revient dès que le prochain épisode sort. */ export function isCaughtUp(item: MediaItem, cache: AiringCache): boolean { - const info = cache[item.id] - if (!info || !info.aired || !trackable([item]).length) return false - // Épisode annoncé dont la date est passée : il est sorti, même si on n'a pas encore revérifié - const aired = info.next && info.next.date <= localDay() ? info.aired + 1 : info.aired + const aired = airedCount(item, cache) + if (aired == null || !trackable([item]).length) return false const cap = episodeCap(item) - return item.episodesWatched >= aired && !(cap && item.episodesWatched >= cap && info.ended) + return item.episodesWatched >= aired && !(cap && item.episodesWatched >= cap && cache[item.id]?.ended) +} + +/** Cache des sorties, relu quand il change (pour l'affichage). */ +export function useAiringCache(): AiringCache { + const [cache, setCache] = useState(loadAiring) + useEffect(() => { + const reload = () => setCache(loadAiring()) + window.addEventListener(CACHE_EVENT, reload) + return () => window.removeEventListener(CACHE_EVENT, reload) + }, []) + return cache } // ───────── TMDB ───────── diff --git a/src/store.tsx b/src/store.tsx index 8f488fa..086a27b 100644 --- a/src/store.tsx +++ b/src/store.tsx @@ -14,6 +14,7 @@ import { isPlainObject, LIMITS, readStorage } from './lib/security' import { mediaDB, requestPersistentStorage } from './lib/db' import { todayISO, uid } from './lib/utils' import { episodeCap, seasonPosition } from './lib/franchise' +import { airedCount } from './lib/airing' // Clés propres à l'espace actif (compte connecté, ou appareil sans compte) const settingsKey = () => scopedKey('settings') @@ -255,6 +256,9 @@ export function MediaProvider({ children, cloudUser }: { children: ReactNode; cl let next = Math.max(0, item.episodesWatched + delta) const cap = episodeCap(item) if (cap) next = Math.min(next, cap) + // On ne peut pas cocher un épisode qui n'est pas encore sorti + const aired = airedCount(item) + if (delta > 0 && aired != null) next = Math.min(next, Math.max(aired, item.episodesWatched)) if (next === item.episodesWatched) return const patch: Partial = { episodesWatched: next } // Série en plusieurs saisons : la saison suit les épisodes vus