import { useState, useMemo } from 'react'; import { Link } from 'react-router-dom'; import { format, startOfMonth, endOfMonth, eachDayOfInterval, getDay, addMonths, subMonths, isSameDay } from 'date-fns'; import { ChevronLeft, ChevronRight } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { useEpisodesByMonth } from '@/hooks/useEpisodes'; import { Tooltip, TooltipContent, TooltipTrigger, TooltipProvider } from '@/components/ui/tooltip'; import { getTmdbPosterUrl } from '@/lib/media-utils'; import type { Episode } from '@/types'; const DAYS = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; export function CalendarPage() { const [currentDate, setCurrentDate] = useState(new Date()); const monthStart = startOfMonth(currentDate); const monthEnd = endOfMonth(currentDate); const startDateStr = format(monthStart, 'yyyy-MM-dd'); const endDateStr = format(monthEnd, 'yyyy-MM-dd'); const { data: episodes = [] } = useEpisodesByMonth(startDateStr, endDateStr); const days = useMemo(() => eachDayOfInterval({ start: monthStart, end: monthEnd }), [monthStart, monthEnd]); const startDayOfWeek = getDay(monthStart); // Group episodes by date const episodesByDate = useMemo(() => { const map = new Map(); episodes.forEach((ep) => { if (!ep.air_date) return; const key = format(new Date(ep.air_date), 'yyyy-MM-dd'); const existing = map.get(key) || []; existing.push(ep); map.set(key, existing); }); return map; }, [episodes]); return (

Calendar

{format(currentDate, 'MMMM yyyy')}
{/* Calendar Grid — fills remaining height */}
{/* Day headers */}
{DAYS.map((day) => (
{day}
))}
{/* Day cells */}
{/* Empty cells for offset */} {Array.from({ length: startDayOfWeek }).map((_, i) => (
))} {days.map((day) => { const key = format(day, 'yyyy-MM-dd'); const dayEpisodes = episodesByDate.get(key) || []; const isToday = isSameDay(day, new Date()); return (
{format(day, 'd')}
{dayEpisodes.slice(0, 4).map((ep) => ( ))} {dayEpisodes.length > 4 && ( +{dayEpisodes.length - 4} more )}
); })}
); } function EpisodeCalendarItem({ episode }: { episode: Episode }) { type EpisodeWithEntry = Episode & { entry?: { id: number; title?: string; poster_path?: string | null }; }; const ep = episode as EpisodeWithEntry; const entry = ep.entry; const entryId = entry?.id; const entryTitle = entry?.title ?? ''; const seasonId = episode.season_id; const seasonNumber = episode.season?.season_number ?? null; const seasonLabel = seasonNumber != null ? `S${String(seasonNumber).padStart(2, '0')}` : ''; const episodeLabel = `E${String(episode.episode_number).padStart(2, '0')}`; const thumbSrc = episode.still_path ? getTmdbPosterUrl(episode.still_path, 'w92') : entry?.poster_path ? getTmdbPosterUrl(entry.poster_path, 'w92') : undefined; const content = (
{thumbSrc && ( )}

{entryTitle || 'Unknown'}

{seasonLabel} {episodeLabel}

); return ( {entryId ? ( {content} ) : (
{content}
)}

{entryTitle}

{episode.name && (

{episode.name}

)}

{seasonLabel} {episodeLabel}

); }