feat(frontend): add MarkSeasonWatchedDialog with scatter mode
- Create MarkSeasonWatchedDialog replacing two-step EpisodeSeenEditDialog+ConfirmDialog flow - Add scatter mode to distribute episodes across a date range with preview - Always show "Mark Season Watched" button on SeasonPage
This commit is contained in:
1 parent
ce01dc34fa
commit
6aa57cbe71
2 files changed
+321
-57
No files matched your search
@@ -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<SeenAccuracy>('EXACT');
|
||||||
|
|
||||||
|
// Single-date mode
|
||||||
|
const [singleDate, setSingleDate] = useState(today);
|
||||||
|
|
||||||
|
// Scatter mode
|
||||||
|
const [mode, setMode] = useState<Mode>('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<string, number>();
|
||||||
|
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 (
|
||||||
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||||
|
<DialogContent className="sm:max-w-md">
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle>Mark Season as Watched</DialogTitle>
|
||||||
|
<DialogDescription>
|
||||||
|
{unwatched.length} unwatched episode{unwatched.length !== 1 ? 's' : ''} will be marked as seen.
|
||||||
|
</DialogDescription>
|
||||||
|
</DialogHeader>
|
||||||
|
|
||||||
|
<div className="space-y-4 py-2">
|
||||||
|
{/* Mode toggle */}
|
||||||
|
<div className="flex items-center justify-between rounded-lg border border-border p-3">
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
{mode === 'single' ? (
|
||||||
|
<CalendarDays className="h-4 w-4 text-muted-foreground" />
|
||||||
|
) : (
|
||||||
|
<CalendarRange className="h-4 w-4 text-muted-foreground" />
|
||||||
|
)}
|
||||||
|
<span className="text-sm font-medium">
|
||||||
|
{mode === 'single' ? 'Single Date' : 'Scatter Mode'}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<Switch
|
||||||
|
checked={mode === 'scatter'}
|
||||||
|
onCheckedChange={(checked) =>
|
||||||
|
setMode(checked ? 'scatter' : 'single')
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Date inputs */}
|
||||||
|
{mode === 'single' ? (
|
||||||
|
<div className="space-y-1.5">
|
||||||
|
<Label htmlFor="single-date">Date Watched</Label>
|
||||||
|
<Input
|
||||||
|
id="single-date"
|
||||||
|
type="date"
|
||||||
|
value={singleDate}
|
||||||
|
onChange={(e) => setSingleDate(e.target.value)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="space-y-3">
|
||||||
|
<div className="grid grid-cols-2 gap-3">
|
||||||
|
<div className="space-y-1.5">
|
||||||
|
<Label htmlFor="scatter-start">Start Date</Label>
|
||||||
|
<Input
|
||||||
|
id="scatter-start"
|
||||||
|
type="date"
|
||||||
|
value={startDate}
|
||||||
|
onChange={(e) => setStartDate(e.target.value)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="space-y-1.5">
|
||||||
|
<Label htmlFor="scatter-end">End Date</Label>
|
||||||
|
<Input
|
||||||
|
id="scatter-end"
|
||||||
|
type="date"
|
||||||
|
value={endDate}
|
||||||
|
onChange={(e) => setEndDate(e.target.value)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Scatter preview */}
|
||||||
|
{scatterPreview && (
|
||||||
|
<div className="rounded-md bg-muted/50 px-3 py-2 text-xs text-muted-foreground space-y-0.5">
|
||||||
|
<p>
|
||||||
|
{unwatched.length} episodes across {scatterPreview.totalDays} day{scatterPreview.totalDays !== 1 ? 's' : ''}
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
~{scatterPreview.maxPerDay} episode{scatterPreview.maxPerDay !== 1 ? 's' : ''}/day max
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{startDate && endDate && new Date(endDate) < new Date(startDate) && (
|
||||||
|
<p className="text-xs text-destructive">
|
||||||
|
End date must be after start date.
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Accuracy */}
|
||||||
|
<div className="space-y-1.5">
|
||||||
|
<Label>Accuracy</Label>
|
||||||
|
<Select
|
||||||
|
value={accuracy}
|
||||||
|
onValueChange={(v) => setAccuracy(v as SeenAccuracy)}
|
||||||
|
>
|
||||||
|
<SelectTrigger className="w-full">
|
||||||
|
<SelectValue />
|
||||||
|
</SelectTrigger>
|
||||||
|
<SelectContent>
|
||||||
|
<SelectItem value="EXACT">Exact</SelectItem>
|
||||||
|
<SelectItem value="ESTIMATED">Estimated</SelectItem>
|
||||||
|
<SelectItem value="UNKNOWN">Unknown</SelectItem>
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Seen with */}
|
||||||
|
<div className="space-y-1.5">
|
||||||
|
<Label htmlFor="seen-with">Seen With</Label>
|
||||||
|
<Input
|
||||||
|
id="seen-with"
|
||||||
|
value={seenWith}
|
||||||
|
onChange={(e) => setSeenWith(e.target.value)}
|
||||||
|
placeholder="Optional"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<DialogFooter>
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
onClick={() => onOpenChange(false)}
|
||||||
|
disabled={isSubmitting}
|
||||||
|
>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
onClick={handleSubmit}
|
||||||
|
disabled={
|
||||||
|
isSubmitting ||
|
||||||
|
unwatched.length === 0 ||
|
||||||
|
(mode === 'single' && !singleDate) ||
|
||||||
|
(mode === 'scatter' &&
|
||||||
|
(!startDate ||
|
||||||
|
!endDate ||
|
||||||
|
new Date(endDate) < new Date(startDate)))
|
||||||
|
}
|
||||||
|
>
|
||||||
|
{isSubmitting && (
|
||||||
|
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
||||||
|
)}
|
||||||
|
{isSubmitting
|
||||||
|
? 'Marking...'
|
||||||
|
: `Mark ${unwatched.length} Episode${unwatched.length !== 1 ? 's' : ''}`}
|
||||||
|
</Button>
|
||||||
|
</DialogFooter>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,17 +1,16 @@
|
|||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { useParams, Link } from 'react-router-dom';
|
import { useParams, Link } from 'react-router-dom';
|
||||||
import { useSeason, useSeasonEpisodes } from '@/hooks/useSeasons';
|
import { useSeason, useSeasonEpisodes } from '@/hooks/useSeasons';
|
||||||
import { useCreateEpisodeSeen } from '@/hooks/useEpisodeSeen';
|
|
||||||
import { useLibraryStore } from '@/stores/useLibraryStore';
|
import { useLibraryStore } from '@/stores/useLibraryStore';
|
||||||
import { EpisodeSeenEditDialog } from '@/components/dialogs/EpisodeSeenEditDialog';
|
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 { Skeleton } from '@/components/ui/skeleton';
|
||||||
import { Progress } from '@/components/ui/progress';
|
import { Progress } from '@/components/ui/progress';
|
||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
import { Eye, CheckCircle2, ListChecks } from 'lucide-react';
|
import { Eye, CheckCircle2, ListChecks } from 'lucide-react';
|
||||||
import { getTmdbPosterUrl, getYear, formatDate } from '@/lib/media-utils';
|
import { getTmdbPosterUrl, getYear, formatDate } from '@/lib/media-utils';
|
||||||
import type { Episode } from '@/types';
|
import type { Episode } from '@/types';
|
||||||
import type { SeenAccuracy } from '@/types';
|
|
||||||
|
|
||||||
export function SeasonPage() {
|
export function SeasonPage() {
|
||||||
const { entryId, seasonId } = useParams<{
|
const { entryId, seasonId } = useParams<{
|
||||||
@@ -20,15 +19,9 @@ export function SeasonPage() {
|
|||||||
}>();
|
}>();
|
||||||
const { data: season, isLoading } = useSeason(parseInt(seasonId!, 10));
|
const { data: season, isLoading } = useSeason(parseInt(seasonId!, 10));
|
||||||
const { data: episodes = [] } = useSeasonEpisodes(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 entryTitle = useLibraryStore((s) => s.entries.get(parseInt(entryId!, 10))?.title);
|
||||||
const [dialogEpisode, setDialogEpisode] = useState<Episode | null>(null);
|
const [dialogEpisode, setDialogEpisode] = useState<Episode | null>(null);
|
||||||
const [markingAll, setMarkingAll] = useState(false);
|
|
||||||
const [showMarkAllDialog, setShowMarkAllDialog] = 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<SeenAccuracy>('EXACT');
|
|
||||||
const [showMarkAllConfirm, setShowMarkAllConfirm] = useState(false);
|
|
||||||
|
|
||||||
if (isLoading || !season) {
|
if (isLoading || !season) {
|
||||||
return (
|
return (
|
||||||
@@ -78,18 +71,15 @@ export function SeasonPage() {
|
|||||||
<div className="flex items-start justify-between gap-4">
|
<div className="flex items-start justify-between gap-4">
|
||||||
<h1 className="text-2xl font-bold">{season.name}</h1>
|
<h1 className="text-2xl font-bold">{season.name}</h1>
|
||||||
{/* Mark Season as Watched — top right */}
|
{/* Mark Season as Watched — top right */}
|
||||||
{watchedCount < totalEpisodes && (
|
|
||||||
<Button
|
<Button
|
||||||
size="sm"
|
size="sm"
|
||||||
variant="outline"
|
variant="outline"
|
||||||
className="shrink-0"
|
className="shrink-0"
|
||||||
disabled={markingAll}
|
|
||||||
onClick={() => setShowMarkAllDialog(true)}
|
onClick={() => setShowMarkAllDialog(true)}
|
||||||
>
|
>
|
||||||
<ListChecks className="mr-1.5 h-4 w-4" />
|
<ListChecks className="mr-1.5 h-4 w-4" />
|
||||||
{markingAll ? 'Marking...' : 'Mark Season Watched'}
|
Mark Season Watched
|
||||||
</Button>
|
</Button>
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
<div className="flex items-center gap-2 text-sm text-muted-foreground">
|
<div className="flex items-center gap-2 text-sm text-muted-foreground">
|
||||||
<span>Season {season.season_number}</span>
|
<span>Season {season.season_number}</span>
|
||||||
@@ -209,53 +199,13 @@ export function SeasonPage() {
|
|||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{/* Mark Season Watched — step 1: date/seen-with dialog */}
|
{/* Mark Season Watched dialog */}
|
||||||
{showMarkAllDialog && (
|
<MarkSeasonWatchedDialog
|
||||||
<EpisodeSeenEditDialog
|
|
||||||
open={showMarkAllDialog}
|
open={showMarkAllDialog}
|
||||||
onOpenChange={(open) => {
|
onOpenChange={setShowMarkAllDialog}
|
||||||
if (!open) setShowMarkAllDialog(false);
|
|
||||||
}}
|
|
||||||
mode="create"
|
|
||||||
episodeId={-1}
|
|
||||||
entryId={parseInt(entryId!, 10)}
|
entryId={parseInt(entryId!, 10)}
|
||||||
seasonId={parseInt(seasonId!, 10)}
|
seasonId={parseInt(seasonId!, 10)}
|
||||||
overrideSubmit={(date, seenWith, accuracy) => {
|
episodes={episodes}
|
||||||
setMarkAllDate(date);
|
|
||||||
setMarkAllSeenWith(seenWith);
|
|
||||||
setMarkAllAccuracy(accuracy);
|
|
||||||
setShowMarkAllDialog(false);
|
|
||||||
setShowMarkAllConfirm(true);
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{/* Mark Season Watched — step 2: confirmation */}
|
|
||||||
<ConfirmDialog
|
|
||||||
open={showMarkAllConfirm}
|
|
||||||
onOpenChange={setShowMarkAllConfirm}
|
|
||||||
title="Mark Season as Watched"
|
|
||||||
description={`This will mark all ${episodes.filter((ep) => !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);
|
|
||||||
}}
|
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in new issue
Block a user