diff --git a/react_frontend/src/components/dialogs/MarkSeasonWatchedDialog.tsx b/react_frontend/src/components/dialogs/MarkSeasonWatchedDialog.tsx new file mode 100644 index 0000000..1570169 --- /dev/null +++ b/react_frontend/src/components/dialogs/MarkSeasonWatchedDialog.tsx @@ -0,0 +1,314 @@ +import { useState, useMemo } from 'react'; +import { + Dialog, + DialogContent, + DialogFooter, + DialogHeader, + DialogTitle, + DialogDescription, +} from '@/components/ui/dialog'; +import { Button } from '@/components/ui/button'; +import { Input } from '@/components/ui/input'; +import { Label } from '@/components/ui/label'; +import { Switch } from '@/components/ui/switch'; +import { + Select, + SelectContent, + SelectItem, + SelectTrigger, + SelectValue, +} from '@/components/ui/select'; +import { Loader2, CalendarRange, CalendarDays } from 'lucide-react'; +import { useCreateEpisodeSeen } from '@/hooks/useEpisodeSeen'; +import type { Episode, SeenAccuracy } from '@/types'; + +interface MarkSeasonWatchedDialogProps { + open: boolean; + onOpenChange: (open: boolean) => void; + entryId: number; + seasonId: number; + episodes: Episode[]; +} + +type Mode = 'single' | 'scatter'; + +function addDays(date: Date, days: number): Date { + const result = new Date(date); + result.setDate(result.getDate() + days); + return result; +} + +function toDateStr(date: Date): string { + return date.toISOString().split('T')[0]; +} + +/** + * Distribute `count` episodes across the days between `start` and `end` (inclusive). + * Returns an array of date strings, one per episode. + * + * Strategy: + * - If days >= count → 1 episode per day, spread evenly + * - If days >= count/2 → 2 episodes per day where needed + * - Otherwise → distribute as evenly as possible + */ +function scatterDates(start: Date, end: Date, count: number): string[] { + if (count === 0) return []; + + const totalDays = + Math.round((end.getTime() - start.getTime()) / (1000 * 60 * 60 * 24)) + 1; + + if (totalDays <= 0 || count === 1) { + return Array(count).fill(toDateStr(start)); + } + + const dates: string[] = []; + + // Evenly space episodes across the available days + for (let i = 0; i < count; i++) { + const dayOffset = Math.round((i * (totalDays - 1)) / (count - 1)); + dates.push(toDateStr(addDays(start, dayOffset))); + } + + return dates; +} + +export function MarkSeasonWatchedDialog({ + open, + onOpenChange, + entryId, + seasonId, + episodes, +}: MarkSeasonWatchedDialogProps) { + const today = new Date().toISOString().split('T')[0]; + const createEpisodeSeen = useCreateEpisodeSeen(); + + // Shared fields + const [seenWith, setSeenWith] = useState(''); + const [accuracy, setAccuracy] = useState('EXACT'); + + // Single-date mode + const [singleDate, setSingleDate] = useState(today); + + // Scatter mode + const [mode, setMode] = useState('single'); + const [startDate, setStartDate] = useState(today); + const [endDate, setEndDate] = useState(today); + + const [isSubmitting, setIsSubmitting] = useState(false); + + const unwatched = useMemo( + () => + episodes.filter( + (ep) => + !ep.date_first_seen && + (!ep.episodeSeens || ep.episodeSeens.length === 0), + ), + [episodes], + ); + + const scatterPreview = useMemo(() => { + if (mode !== 'scatter' || !startDate || !endDate) return null; + const start = new Date(startDate); + const end = new Date(endDate); + if (end < start) return null; + + const dates = scatterDates(start, end, unwatched.length); + const totalDays = + Math.round((end.getTime() - start.getTime()) / (1000 * 60 * 60 * 24)) + 1; + + // Group by date to show per-day count + const grouped = new Map(); + for (const d of dates) { + grouped.set(d, (grouped.get(d) || 0) + 1); + } + const maxPerDay = Math.max(...grouped.values(), 0); + + return { totalDays, maxPerDay, dates }; + }, [mode, startDate, endDate, unwatched.length]); + + const handleSubmit = async () => { + if (unwatched.length === 0) return; + setIsSubmitting(true); + + try { + if (mode === 'single') { + for (const ep of unwatched) { + await createEpisodeSeen.mutateAsync({ + entry_id: entryId, + episode_id: ep.id, + date_seen: singleDate, + seen_with: seenWith || undefined, + accuracy, + season_id: seasonId, + }); + } + } else { + const dates = scatterDates( + new Date(startDate), + new Date(endDate), + unwatched.length, + ); + for (let i = 0; i < unwatched.length; i++) { + await createEpisodeSeen.mutateAsync({ + entry_id: entryId, + episode_id: unwatched[i].id, + date_seen: dates[i], + seen_with: seenWith || undefined, + accuracy, + season_id: seasonId, + }); + } + } + onOpenChange(false); + } finally { + setIsSubmitting(false); + } + }; + + return ( + + + + Mark Season as Watched + + {unwatched.length} unwatched episode{unwatched.length !== 1 ? 's' : ''} will be marked as seen. + + + +
+ {/* Mode toggle */} +
+
+ {mode === 'single' ? ( + + ) : ( + + )} + + {mode === 'single' ? 'Single Date' : 'Scatter Mode'} + +
+ + setMode(checked ? 'scatter' : 'single') + } + /> +
+ + {/* Date inputs */} + {mode === 'single' ? ( +
+ + setSingleDate(e.target.value)} + /> +
+ ) : ( +
+
+
+ + setStartDate(e.target.value)} + /> +
+
+ + setEndDate(e.target.value)} + /> +
+
+ + {/* Scatter preview */} + {scatterPreview && ( +
+

+ {unwatched.length} episodes across {scatterPreview.totalDays} day{scatterPreview.totalDays !== 1 ? 's' : ''} +

+

+ ~{scatterPreview.maxPerDay} episode{scatterPreview.maxPerDay !== 1 ? 's' : ''}/day max +

+
+ )} + + {startDate && endDate && new Date(endDate) < new Date(startDate) && ( +

+ End date must be after start date. +

+ )} +
+ )} + + {/* Accuracy */} +
+ + +
+ + {/* Seen with */} +
+ + setSeenWith(e.target.value)} + placeholder="Optional" + /> +
+
+ + + + + +
+
+ ); +} diff --git a/react_frontend/src/pages/SeasonPage.tsx b/react_frontend/src/pages/SeasonPage.tsx index 482c38f..4f62b86 100644 --- a/react_frontend/src/pages/SeasonPage.tsx +++ b/react_frontend/src/pages/SeasonPage.tsx @@ -1,17 +1,16 @@ import { useState } from 'react'; import { useParams, Link } from 'react-router-dom'; import { useSeason, useSeasonEpisodes } from '@/hooks/useSeasons'; -import { useCreateEpisodeSeen } from '@/hooks/useEpisodeSeen'; + import { useLibraryStore } from '@/stores/useLibraryStore'; import { EpisodeSeenEditDialog } from '@/components/dialogs/EpisodeSeenEditDialog'; -import { ConfirmDialog } from '@/components/dialogs/ConfirmDialog'; +import { MarkSeasonWatchedDialog } from '@/components/dialogs/MarkSeasonWatchedDialog'; import { Skeleton } from '@/components/ui/skeleton'; import { Progress } from '@/components/ui/progress'; import { Button } from '@/components/ui/button'; import { Eye, CheckCircle2, ListChecks } from 'lucide-react'; import { getTmdbPosterUrl, getYear, formatDate } from '@/lib/media-utils'; import type { Episode } from '@/types'; -import type { SeenAccuracy } from '@/types'; export function SeasonPage() { const { entryId, seasonId } = useParams<{ @@ -20,15 +19,9 @@ export function SeasonPage() { }>(); const { data: season, isLoading } = useSeason(parseInt(seasonId!, 10)); const { data: episodes = [] } = useSeasonEpisodes(parseInt(seasonId!, 10)); - const createEpisodeSeen = useCreateEpisodeSeen(); const entryTitle = useLibraryStore((s) => s.entries.get(parseInt(entryId!, 10))?.title); const [dialogEpisode, setDialogEpisode] = useState(null); - const [markingAll, setMarkingAll] = useState(false); const [showMarkAllDialog, setShowMarkAllDialog] = useState(false); - const [markAllDate, setMarkAllDate] = useState(new Date().toISOString().split('T')[0]); - const [markAllSeenWith, setMarkAllSeenWith] = useState(''); - const [markAllAccuracy, setMarkAllAccuracy] = useState('EXACT'); - const [showMarkAllConfirm, setShowMarkAllConfirm] = useState(false); if (isLoading || !season) { return ( @@ -78,18 +71,15 @@ export function SeasonPage() {

{season.name}

{/* Mark Season as Watched — top right */} - {watchedCount < totalEpisodes && ( - - )} +
Season {season.season_number} @@ -209,53 +199,13 @@ export function SeasonPage() { /> )} - {/* Mark Season Watched — step 1: date/seen-with dialog */} - {showMarkAllDialog && ( - { - if (!open) setShowMarkAllDialog(false); - }} - mode="create" - episodeId={-1} - entryId={parseInt(entryId!, 10)} - seasonId={parseInt(seasonId!, 10)} - overrideSubmit={(date, seenWith, accuracy) => { - setMarkAllDate(date); - setMarkAllSeenWith(seenWith); - setMarkAllAccuracy(accuracy); - setShowMarkAllDialog(false); - setShowMarkAllConfirm(true); - }} - /> - )} - - {/* Mark Season Watched — step 2: confirmation */} - !ep.date_first_seen && (!ep.episodeSeens || ep.episodeSeens.length === 0)).length} unwatched episodes as seen on ${markAllDate}. Continue?`} - confirmLabel="Mark All" - isLoading={markingAll} - onConfirm={async () => { - setMarkingAll(true); - const unwatched = episodes.filter( - (ep) => !ep.date_first_seen && (!ep.episodeSeens || ep.episodeSeens.length === 0) - ); - for (const ep of unwatched) { - await createEpisodeSeen.mutateAsync({ - entry_id: parseInt(entryId!, 10), - episode_id: ep.id, - date_seen: markAllDate, - seen_with: markAllSeenWith || undefined, - accuracy: markAllAccuracy, - season_id: parseInt(seasonId!, 10), - }); - } - setMarkingAll(false); - setShowMarkAllConfirm(false); - }} + {/* Mark Season Watched dialog */} +
);