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
+332
-68
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>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user