import { dateFormat, locale, t } from '../i18n' import { Check, ChevronLeft, ChevronRight, Loader2, Plus, RefreshCw, X } from 'lucide-react' import { useCallback, useEffect, useMemo, useRef, useState } from 'react' import { loadAiring, refreshAiring, saveAiring } from '../lib/airing' import type { GlobalRelease, ReleaseCategory } from '../lib/catalogApi' import { releaseToInput } from '../lib/releaseAdd' import { franchiseKey } from '../lib/franchise' import { cachedMonth, isBrowsableMonth, loadMonth, RELEASE_CATEGORIES } from '../lib/globalReleases' import { calendarEvents, loadReleases, refreshReleases } from '../lib/releases' import { readStorage } from '../lib/security' import { useBackToClose } from '../lib/backNav' import { useEscape } from '../lib/escape' import { useScrollLock } from '../lib/scrollLock' import { cx, normalizeText, todayISO } from '../lib/utils' import { useMedia } from '../store' import { TYPE_BY_VALUE } from '../lib/constants' import type { MediaItem, MediaType } from '../types' import { TypeBadge } from './Badges' import Poster from './Poster' // Initiales des jours, du lundi au dimanche, dans la langue choisie (le 1er janvier 2024 est un lundi) const weekdays = () => Array.from({ length: 7 }, (_, k) => dateFormat({ weekday: 'narrow' }).format(new Date(2024, 0, 1 + k))) const PER_DAY = 20 const PREFS_KEY = 'azuucine:calendar-prefs' /** Jour local décalé de n jours (sans « + n × 24 h », faux aux changements d'heure). */ const plusDays = (n: number) => { const d = new Date() return new Date(d.getFullYear(), d.getMonth(), d.getDate() + n) } const iso = (d: Date) => `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}` const monthId = (d: Date) => iso(d).slice(0, 7) const niceDate = (d: string) => dateFormat({ weekday: 'long', day: 'numeric', month: 'long' }).format(new Date(d + 'T12:00:00')) type Mode = 'all' | 'mine' interface Entry { key: string date: string title: string poster?: string label: string type: MediaType item?: MediaItem release?: GlobalRelease pop: number } function loadPrefs(): { mode: Mode; cats: ReleaseCategory[] } { const raw = readStorage(PREFS_KEY, {}) as { mode?: unknown; cats?: unknown } const valid = RELEASE_CATEGORIES.map((c) => c.value) const cats = Array.isArray(raw?.cats) ? valid.filter((c) => (raw.cats as unknown[]).includes(c)) : valid return { mode: raw?.mode === 'mine' ? 'mine' : 'all', cats: cats.length ? cats : valid } } function EntryRow({ e, onOpen, onAdd, busy, added }: { e: Entry; onOpen: (i: MediaItem) => void; onAdd: (e: Entry) => void; busy: boolean; added: boolean }) { const mine = !!e.item const body = ( <>
{e.title}
{e.label}
{t('calendar.inMyListLegend')} {prefs.mode === 'all' && ( {t('calendar.others')} )}
{prefs.mode === 'all' && !hasKey && ({t('calendar.needKey')}
)} {prefs.mode === 'all' && !browsable && ({t('calendar.range')}
)} {failed &&{t('calendar.failed')}
} {/* Détail du jour choisi, ou liste jour par jour */} {selected ? ({t('calendar.nothingThatDay')}
}
{loading ? (
<>
{t('calendar.footer')}