fix: season episode dialog, mark season watched, episode 404 handling
Seasons Page: - Episode watch button now opens EpisodeSeenEditDialog instead of directly marking as watched, allowing date/seen-with input - Added "Mark Season as Watched" button that sequentially marks all unwatched episodes in the season Episode Page: - Fixed UI not updating after deleting the last episode seen record; the backend returns 404 when no records exist for an episode, which caused the query to error instead of resolving to an empty array. The API layer now catches 404 and returns [] gracefully.
This commit is contained in:
1 parent
984e2c8b50
commit
6bff2055b4
12 files changed
+337
-47
No files matched your search
@@ -54,10 +54,6 @@ class EntrySeenController {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async getEntrySeensByEntryId(req, res) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
async updateEntrySeen(req, res) {
|
async updateEntrySeen(req, res) {
|
||||||
const { date_seen, seen_with } = req.body;
|
const { date_seen, seen_with } = req.body;
|
||||||
const updated = await EntrySeen.update({ date_seen, seen_with }, { where: { id: req.params.id } });
|
const updated = await EntrySeen.update({ date_seen, seen_with }, { where: { id: req.params.id } });
|
||||||
|
|||||||
@@ -7,15 +7,15 @@ const entrySeenController = require('../controllers/entrySeenController');
|
|||||||
// * GET - ALL ENTRY SEEN
|
// * GET - ALL ENTRY SEEN
|
||||||
entrySeenRouter.get('/', entrySeenController.getEntrySeens);
|
entrySeenRouter.get('/', entrySeenController.getEntrySeens);
|
||||||
|
|
||||||
|
// * GET - ENTRY SEEN BY ENTRY ID (must be before /:id to avoid conflict)
|
||||||
|
entrySeenRouter.get('/by-entry/:entryId', entrySeenController.getEntrySeensByEntryId);
|
||||||
|
|
||||||
// * GET - ENTRY SEEN BY ID
|
// * GET - ENTRY SEEN BY ID
|
||||||
entrySeenRouter.get('/:id', entrySeenController.getEntrySeenById);
|
entrySeenRouter.get('/:id', entrySeenController.getEntrySeenById);
|
||||||
|
|
||||||
// * POST - CREATE ENTRY SEEN
|
// * POST - CREATE ENTRY SEEN
|
||||||
entrySeenRouter.post('/', entrySeenController.createEntrySeen);
|
entrySeenRouter.post('/', entrySeenController.createEntrySeen);
|
||||||
|
|
||||||
// * GET - ENTRY SEEN BY ENTRY ID
|
|
||||||
entrySeenRouter.get('/by-entry/:entryId', entrySeenController.getEntrySeensByEntryId);
|
|
||||||
|
|
||||||
// * PUT - UPDATE ENTRY SEEN
|
// * PUT - UPDATE ENTRY SEEN
|
||||||
entrySeenRouter.put('/:id', entrySeenController.updateEntrySeen);
|
entrySeenRouter.put('/:id', entrySeenController.updateEntrySeen);
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,13 @@ export const entrySeenApi = {
|
|||||||
api.get<EntrySeen>(`/entry-seen/${id}`).then((r) => r.data),
|
api.get<EntrySeen>(`/entry-seen/${id}`).then((r) => r.data),
|
||||||
|
|
||||||
getByEntry: (entryId: number) =>
|
getByEntry: (entryId: number) =>
|
||||||
api.get<EntrySeen[]>(`/entry-seen/by-entry/${entryId}`).then((r) => r.data),
|
api
|
||||||
|
.get<EntrySeen[]>(`/entry-seen/by-entry/${entryId}`)
|
||||||
|
.then((r) => r.data)
|
||||||
|
.catch((err) => {
|
||||||
|
if (err?.response?.status === 404) return [] as EntrySeen[];
|
||||||
|
throw err;
|
||||||
|
}),
|
||||||
|
|
||||||
create: (data: { entry_id: number; date_seen: string; seen_with?: string }) =>
|
create: (data: { entry_id: number; date_seen: string; seen_with?: string }) =>
|
||||||
api.post<EntrySeen>('/entry-seen', data).then((r) => r.data),
|
api.post<EntrySeen>('/entry-seen', data).then((r) => r.data),
|
||||||
|
|||||||
@@ -10,7 +10,11 @@ export const episodeSeenApi = {
|
|||||||
getByEpisode: (episodeId: number) =>
|
getByEpisode: (episodeId: number) =>
|
||||||
api
|
api
|
||||||
.get<EpisodeSeen[]>(`/episode-seen/by-episode/${episodeId}`)
|
.get<EpisodeSeen[]>(`/episode-seen/by-episode/${episodeId}`)
|
||||||
.then((r) => r.data),
|
.then((r) => r.data)
|
||||||
|
.catch((err) => {
|
||||||
|
if (err?.response?.status === 404) return [] as EpisodeSeen[];
|
||||||
|
throw err;
|
||||||
|
}),
|
||||||
|
|
||||||
create: (data: {
|
create: (data: {
|
||||||
entry_id: number;
|
entry_id: number;
|
||||||
|
|||||||
@@ -20,7 +20,10 @@ interface EpisodeSeenEditDialogProps {
|
|||||||
/** Required for create mode */
|
/** Required for create mode */
|
||||||
episodeId?: number;
|
episodeId?: number;
|
||||||
entryId: number;
|
entryId: number;
|
||||||
|
seasonId?: number;
|
||||||
mode?: 'create' | 'edit';
|
mode?: 'create' | 'edit';
|
||||||
|
/** If provided, called instead of the normal create/update mutation */
|
||||||
|
overrideSubmit?: (date: string, seenWith: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function EpisodeSeenEditDialog({
|
export function EpisodeSeenEditDialog({
|
||||||
@@ -29,7 +32,9 @@ export function EpisodeSeenEditDialog({
|
|||||||
record,
|
record,
|
||||||
episodeId,
|
episodeId,
|
||||||
entryId,
|
entryId,
|
||||||
|
seasonId,
|
||||||
mode = record ? 'edit' : 'create',
|
mode = record ? 'edit' : 'create',
|
||||||
|
overrideSubmit,
|
||||||
}: EpisodeSeenEditDialogProps) {
|
}: EpisodeSeenEditDialogProps) {
|
||||||
const today = new Date().toISOString().split('T')[0];
|
const today = new Date().toISOString().split('T')[0];
|
||||||
const [date, setDate] = useState(record?.date_seen?.split('T')[0] ?? today);
|
const [date, setDate] = useState(record?.date_seen?.split('T')[0] ?? today);
|
||||||
@@ -39,6 +44,10 @@ export function EpisodeSeenEditDialog({
|
|||||||
const createEpisodeSeen = useCreateEpisodeSeen();
|
const createEpisodeSeen = useCreateEpisodeSeen();
|
||||||
|
|
||||||
const handleSave = () => {
|
const handleSave = () => {
|
||||||
|
if (overrideSubmit) {
|
||||||
|
overrideSubmit(date, seenWith);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (mode === 'edit' && record) {
|
if (mode === 'edit' && record) {
|
||||||
updateEpisodeSeen.mutate({
|
updateEpisodeSeen.mutate({
|
||||||
id: record.id,
|
id: record.id,
|
||||||
@@ -57,6 +66,7 @@ export function EpisodeSeenEditDialog({
|
|||||||
episode_id: epId,
|
episode_id: epId,
|
||||||
date_seen: date,
|
date_seen: date,
|
||||||
seen_with: seenWith || undefined,
|
seen_with: seenWith || undefined,
|
||||||
|
season_id: seasonId,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
onOpenChange(false);
|
onOpenChange(false);
|
||||||
|
|||||||
@@ -0,0 +1,81 @@
|
|||||||
|
import { format } from 'date-fns';
|
||||||
|
import {
|
||||||
|
Dialog,
|
||||||
|
DialogContent,
|
||||||
|
DialogHeader,
|
||||||
|
DialogTitle,
|
||||||
|
} from '@/components/ui/dialog';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
import { Trash2 } from 'lucide-react';
|
||||||
|
import type { EntrySeen } from '@/types';
|
||||||
|
|
||||||
|
interface MovieWatchHistoryDialogProps {
|
||||||
|
open: boolean;
|
||||||
|
onOpenChange: (open: boolean) => void;
|
||||||
|
entryId: number;
|
||||||
|
watchHistory: EntrySeen[];
|
||||||
|
onDelete: (seenId: number) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function MovieWatchHistoryDialog({
|
||||||
|
open,
|
||||||
|
onOpenChange,
|
||||||
|
watchHistory,
|
||||||
|
onDelete,
|
||||||
|
}: MovieWatchHistoryDialogProps) {
|
||||||
|
return (
|
||||||
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||||
|
<DialogContent className="sm:max-w-md">
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle>Watch History</DialogTitle>
|
||||||
|
</DialogHeader>
|
||||||
|
{watchHistory.length === 0 ? (
|
||||||
|
<p className="text-sm text-muted-foreground py-4">
|
||||||
|
No watch history yet.
|
||||||
|
</p>
|
||||||
|
) : (
|
||||||
|
<div className="overflow-x-auto rounded-lg border border-border">
|
||||||
|
<table className="w-full text-sm">
|
||||||
|
<thead>
|
||||||
|
<tr className="border-b border-border bg-muted/50">
|
||||||
|
<th className="px-4 py-2 text-left font-medium">Date</th>
|
||||||
|
<th className="px-4 py-2 text-left font-medium">Seen With</th>
|
||||||
|
<th className="px-4 py-2 text-right font-medium">Actions</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{watchHistory.map((record) => (
|
||||||
|
<tr
|
||||||
|
key={record.id}
|
||||||
|
className="border-b border-border last:border-0"
|
||||||
|
>
|
||||||
|
<td className="px-4 py-2">
|
||||||
|
{record.date_seen
|
||||||
|
? format(new Date(record.date_seen), 'MMM d, yyyy')
|
||||||
|
: '-'}
|
||||||
|
</td>
|
||||||
|
<td className="px-4 py-2 text-muted-foreground">
|
||||||
|
{record.seen_with || '-'}
|
||||||
|
</td>
|
||||||
|
<td className="px-4 py-2">
|
||||||
|
<div className="flex justify-end">
|
||||||
|
<Button
|
||||||
|
size="icon"
|
||||||
|
variant="ghost"
|
||||||
|
className="h-7 w-7 text-destructive"
|
||||||
|
onClick={() => onDelete(record.id)}
|
||||||
|
>
|
||||||
|
<Trash2 className="h-3 w-3" />
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
))}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -3,8 +3,8 @@ import { Link } from 'react-router-dom';
|
|||||||
import { ChevronDown, ChevronUp, Play } from 'lucide-react';
|
import { ChevronDown, ChevronUp, Play } from 'lucide-react';
|
||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
import { Progress } from '@/components/ui/progress';
|
import { Progress } from '@/components/ui/progress';
|
||||||
|
import { EpisodeSeenEditDialog } from '@/components/dialogs/EpisodeSeenEditDialog';
|
||||||
import { getYear, getTmdbPosterUrl } from '@/lib/media-utils';
|
import { getYear, getTmdbPosterUrl } from '@/lib/media-utils';
|
||||||
import { useCreateEpisodeSeen } from '@/hooks/useEpisodeSeen';
|
|
||||||
import type { Season } from '@/types';
|
import type { Season } from '@/types';
|
||||||
|
|
||||||
interface SeasonsContainerProps {
|
interface SeasonsContainerProps {
|
||||||
@@ -65,7 +65,7 @@ function SeasonItem({
|
|||||||
season: Season;
|
season: Season;
|
||||||
entryId: number;
|
entryId: number;
|
||||||
}) {
|
}) {
|
||||||
const createEpisodeSeen = useCreateEpisodeSeen();
|
const [dialogOpen, setDialogOpen] = useState(false);
|
||||||
const watchedCount =
|
const watchedCount =
|
||||||
season.episodes?.filter(
|
season.episodes?.filter(
|
||||||
(ep) => ep.date_first_seen || (ep.episodeSeens && ep.episodeSeens.length > 0)
|
(ep) => ep.date_first_seen || (ep.episodeSeens && ep.episodeSeens.length > 0)
|
||||||
@@ -79,6 +79,7 @@ function SeasonItem({
|
|||||||
.find((ep) => !ep.date_first_seen && (!ep.episodeSeens || ep.episodeSeens.length === 0));
|
.find((ep) => !ep.date_first_seen && (!ep.episodeSeens || ep.episodeSeens.length === 0));
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<>
|
||||||
<Link
|
<Link
|
||||||
to={`/entry/${entryId}/season/${season.id}`}
|
to={`/entry/${entryId}/season/${season.id}`}
|
||||||
className="flex items-center gap-4 rounded-lg border border-border p-3 transition-colors hover:bg-accent/50"
|
className="flex items-center gap-4 rounded-lg border border-border p-3 transition-colors hover:bg-accent/50"
|
||||||
@@ -109,7 +110,7 @@ function SeasonItem({
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Quick watch button */}
|
{/* Quick watch button - opens dialog */}
|
||||||
{nextUnwatched && (
|
{nextUnwatched && (
|
||||||
<Button
|
<Button
|
||||||
size="icon"
|
size="icon"
|
||||||
@@ -118,17 +119,23 @@ function SeasonItem({
|
|||||||
onClick={(e) => {
|
onClick={(e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
createEpisodeSeen.mutate({
|
setDialogOpen(true);
|
||||||
entry_id: entryId,
|
|
||||||
episode_id: nextUnwatched.id,
|
|
||||||
date_seen: new Date().toISOString(),
|
|
||||||
});
|
|
||||||
}}
|
}}
|
||||||
disabled={createEpisodeSeen.isPending}
|
|
||||||
>
|
>
|
||||||
<Play className="h-4 w-4" />
|
<Play className="h-4 w-4" />
|
||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
</Link>
|
</Link>
|
||||||
|
|
||||||
|
{nextUnwatched && dialogOpen && (
|
||||||
|
<EpisodeSeenEditDialog
|
||||||
|
open={dialogOpen}
|
||||||
|
onOpenChange={setDialogOpen}
|
||||||
|
mode="create"
|
||||||
|
episodeId={nextUnwatched.id}
|
||||||
|
entryId={entryId}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -27,6 +27,7 @@ export function useCreateEpisodeSeen() {
|
|||||||
episode_id: number;
|
episode_id: number;
|
||||||
date_seen: string;
|
date_seen: string;
|
||||||
seen_with?: string;
|
seen_with?: string;
|
||||||
|
season_id?: number;
|
||||||
}) => episodeSeenApi.create(data),
|
}) => episodeSeenApi.create(data),
|
||||||
onSuccess: (_data, variables) => {
|
onSuccess: (_data, variables) => {
|
||||||
queryClient.invalidateQueries({
|
queryClient.invalidateQueries({
|
||||||
@@ -38,6 +39,11 @@ export function useCreateEpisodeSeen() {
|
|||||||
queryClient.invalidateQueries({
|
queryClient.invalidateQueries({
|
||||||
queryKey: entryKeys.seasons(variables.entry_id),
|
queryKey: entryKeys.seasons(variables.entry_id),
|
||||||
});
|
});
|
||||||
|
if (variables.season_id) {
|
||||||
|
queryClient.invalidateQueries({
|
||||||
|
queryKey: seasonKeys.episodes(variables.season_id),
|
||||||
|
});
|
||||||
|
}
|
||||||
toast.success('Episode marked as watched');
|
toast.success('Episode marked as watched');
|
||||||
},
|
},
|
||||||
onError: () => {
|
onError: () => {
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import {
|
|||||||
Settings,
|
Settings,
|
||||||
Star,
|
Star,
|
||||||
Loader2,
|
Loader2,
|
||||||
|
History,
|
||||||
} from 'lucide-react';
|
} from 'lucide-react';
|
||||||
import {
|
import {
|
||||||
Tooltip,
|
Tooltip,
|
||||||
@@ -40,6 +41,7 @@ import { EntrySeenDialog } from '@/components/dialogs/EntrySeenDialog';
|
|||||||
import { EntryMediaDialog } from '@/components/dialogs/EntryMediaDialog';
|
import { EntryMediaDialog } from '@/components/dialogs/EntryMediaDialog';
|
||||||
import { AddEntryToCollectionDialog } from '@/components/dialogs/AddEntryToCollectionDialog';
|
import { AddEntryToCollectionDialog } from '@/components/dialogs/AddEntryToCollectionDialog';
|
||||||
import { RatingDialog } from '@/components/dialogs/RatingDialog';
|
import { RatingDialog } from '@/components/dialogs/RatingDialog';
|
||||||
|
import { MovieWatchHistoryDialog } from '@/components/dialogs/MovieWatchHistoryDialog';
|
||||||
import { EntryDetailsSkeleton } from '@/components/skeletons/PageSkeletons';
|
import { EntryDetailsSkeleton } from '@/components/skeletons/PageSkeletons';
|
||||||
import {
|
import {
|
||||||
getPosterUrl,
|
getPosterUrl,
|
||||||
@@ -73,6 +75,7 @@ export function EntryDetailsPage() {
|
|||||||
const [showMediaDialog, setShowMediaDialog] = useState(false);
|
const [showMediaDialog, setShowMediaDialog] = useState(false);
|
||||||
const [showCollectionDialog, setShowCollectionDialog] = useState(false);
|
const [showCollectionDialog, setShowCollectionDialog] = useState(false);
|
||||||
const [showRatingDialog, setShowRatingDialog] = useState(false);
|
const [showRatingDialog, setShowRatingDialog] = useState(false);
|
||||||
|
const [showWatchHistoryDialog, setShowWatchHistoryDialog] = useState(false);
|
||||||
|
|
||||||
if (isLoading || !displayEntry) {
|
if (isLoading || !displayEntry) {
|
||||||
return <EntryDetailsSkeleton />;
|
return <EntryDetailsSkeleton />;
|
||||||
@@ -155,6 +158,12 @@ export function EntryDetailsPage() {
|
|||||||
<Star className="mr-2 h-4 w-4" />
|
<Star className="mr-2 h-4 w-4" />
|
||||||
Rate this
|
Rate this
|
||||||
</DropdownMenuItem>
|
</DropdownMenuItem>
|
||||||
|
{!e.is_tvseries && (
|
||||||
|
<DropdownMenuItem onClick={() => setShowWatchHistoryDialog(true)}>
|
||||||
|
<History className="mr-2 h-4 w-4" />
|
||||||
|
Watch History
|
||||||
|
</DropdownMenuItem>
|
||||||
|
)}
|
||||||
<DropdownMenuItem
|
<DropdownMenuItem
|
||||||
onClick={() =>
|
onClick={() =>
|
||||||
updateEntry.mutate({ id: e.id, data: { hidden: !e.hidden } })
|
updateEntry.mutate({ id: e.id, data: { hidden: !e.hidden } })
|
||||||
@@ -241,6 +250,31 @@ export function EntryDetailsPage() {
|
|||||||
<span>{formatRuntime(e.runtime)}</span>
|
<span>{formatRuntime(e.runtime)}</span>
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
|
{!e.is_tvseries && watchHistory.length > 0 && (
|
||||||
|
<>
|
||||||
|
<span>•</span>
|
||||||
|
{watchHistory.length === 1 ? (
|
||||||
|
<span>seen {formatDate(watchHistory[0].date_seen)}</span>
|
||||||
|
) : (
|
||||||
|
<TooltipProvider>
|
||||||
|
<Tooltip>
|
||||||
|
<TooltipTrigger asChild>
|
||||||
|
<span className="cursor-default underline decoration-dotted">
|
||||||
|
seen {watchHistory.length} times
|
||||||
|
</span>
|
||||||
|
</TooltipTrigger>
|
||||||
|
<TooltipContent>
|
||||||
|
<div className="space-y-0.5 text-xs">
|
||||||
|
{watchHistory.map((wh) => (
|
||||||
|
<p key={wh.id}>{formatDate(wh.date_seen)}</p>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</TooltipContent>
|
||||||
|
</Tooltip>
|
||||||
|
</TooltipProvider>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Genres */}
|
{/* Genres */}
|
||||||
@@ -372,6 +406,17 @@ export function EntryDetailsPage() {
|
|||||||
entry={e}
|
entry={e}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
{showWatchHistoryDialog && (
|
||||||
|
<MovieWatchHistoryDialog
|
||||||
|
open={showWatchHistoryDialog}
|
||||||
|
onOpenChange={setShowWatchHistoryDialog}
|
||||||
|
entryId={e.id}
|
||||||
|
watchHistory={watchHistory}
|
||||||
|
onDelete={(seenId) =>
|
||||||
|
deleteEntrySeen.mutate({ id: seenId, entryId: e.id })
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -172,6 +172,7 @@ export function EpisodePage() {
|
|||||||
mode="create"
|
mode="create"
|
||||||
episodeId={episode.id}
|
episodeId={episode.id}
|
||||||
entryId={entryIdNum}
|
entryId={entryIdNum}
|
||||||
|
seasonId={parseInt(seasonId!, 10)}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
@@ -183,6 +184,7 @@ export function EpisodePage() {
|
|||||||
mode="edit"
|
mode="edit"
|
||||||
record={editRecord}
|
record={editRecord}
|
||||||
entryId={entryIdNum}
|
entryId={entryIdNum}
|
||||||
|
seasonId={parseInt(seasonId!, 10)}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { useState, useMemo, useCallback } from 'react';
|
import { useState, useMemo, useCallback, useEffect, useRef } from 'react';
|
||||||
|
import { useSearchParams } from 'react-router-dom';
|
||||||
import { useLibrary } from '@/hooks/useEntries';
|
import { useLibrary } from '@/hooks/useEntries';
|
||||||
import { useLibraryEntries } from '@/stores/useLibraryStore';
|
import { useLibraryEntries } from '@/stores/useLibraryStore';
|
||||||
import { EntryLibraryGrid } from '@/components/entries/EntryLibraryGrid';
|
import { EntryLibraryGrid } from '@/components/entries/EntryLibraryGrid';
|
||||||
@@ -44,8 +45,8 @@ function sortEntries(entries: Entry[], field: SortField, dir: SortDir): Entry[]
|
|||||||
cmp = new Date(a.release_date || '1900').getTime() - new Date(b.release_date || '1900').getTime();
|
cmp = new Date(a.release_date || '1900').getTime() - new Date(b.release_date || '1900').getTime();
|
||||||
break;
|
break;
|
||||||
case 'rating':
|
case 'rating':
|
||||||
cmp = (a.entryRatings?.user_rating || a.entryRatings?.tmdb_rating || 0) -
|
cmp = (a.entryRatings?.user_rating ?? 0) -
|
||||||
(b.entryRatings?.user_rating || b.entryRatings?.tmdb_rating || 0);
|
(b.entryRatings?.user_rating ?? 0);
|
||||||
break;
|
break;
|
||||||
case 'date_last_seen':
|
case 'date_last_seen':
|
||||||
cmp = new Date(a.date_last_seen || '1900').getTime() - new Date(b.date_last_seen || '1900').getTime();
|
cmp = new Date(a.date_last_seen || '1900').getTime() - new Date(b.date_last_seen || '1900').getTime();
|
||||||
@@ -58,16 +59,56 @@ function sortEntries(entries: Entry[], field: SortField, dir: SortDir): Entry[]
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const PAGE_SIZE = 28;
|
||||||
|
|
||||||
export function LibraryPage() {
|
export function LibraryPage() {
|
||||||
const { isLoading } = useLibrary();
|
const { isLoading } = useLibrary();
|
||||||
|
const [searchParams, setSearchParams] = useSearchParams();
|
||||||
const [activeTab, setActiveTab] = useState<FilterTab>('all');
|
const [activeTab, setActiveTab] = useState<FilterTab>('all');
|
||||||
const [search, setSearch] = useState('');
|
const [search, setSearch] = useState('');
|
||||||
|
const [debouncedSearch, setDebouncedSearch] = useState('');
|
||||||
const [sortField, setSortField] = useState<SortField>('date_last_seen');
|
const [sortField, setSortField] = useState<SortField>('date_last_seen');
|
||||||
const [sortDir, setSortDir] = useState<SortDir>('desc');
|
const [sortDir, setSortDir] = useState<SortDir>('desc');
|
||||||
const [filters, setFilters] = useState<Filters>(defaultFilters);
|
const [filters, setFilters] = useState<Filters>(defaultFilters);
|
||||||
|
const [debouncedFilters, setDebouncedFilters] = useState<Filters>(defaultFilters);
|
||||||
|
const [visibleCount, setVisibleCount] = useState(PAGE_SIZE);
|
||||||
|
const filtersInitialized = useRef(false);
|
||||||
|
|
||||||
const allEntries = useLibraryEntries();
|
const allEntries = useLibraryEntries();
|
||||||
|
|
||||||
|
// Read URL query params once on mount
|
||||||
|
useEffect(() => {
|
||||||
|
if (filtersInitialized.current) return;
|
||||||
|
filtersInitialized.current = true;
|
||||||
|
const genre = searchParams.get('genre') || '';
|
||||||
|
const ratingMin = searchParams.get('ratingMin') || '';
|
||||||
|
const ratingMax = searchParams.get('ratingMax') || '';
|
||||||
|
if (genre || ratingMin || ratingMax) {
|
||||||
|
const newFilters = { ...defaultFilters, genre, ratingMin, ratingMax };
|
||||||
|
setFilters(newFilters);
|
||||||
|
setDebouncedFilters(newFilters);
|
||||||
|
// Clear URL params after applying
|
||||||
|
setSearchParams({}, { replace: true });
|
||||||
|
}
|
||||||
|
}, [searchParams, setSearchParams]);
|
||||||
|
|
||||||
|
// Debounce search
|
||||||
|
useEffect(() => {
|
||||||
|
const timer = setTimeout(() => setDebouncedSearch(search), 300);
|
||||||
|
return () => clearTimeout(timer);
|
||||||
|
}, [search]);
|
||||||
|
|
||||||
|
// Debounce filters
|
||||||
|
useEffect(() => {
|
||||||
|
const timer = setTimeout(() => setDebouncedFilters(filters), 300);
|
||||||
|
return () => clearTimeout(timer);
|
||||||
|
}, [filters]);
|
||||||
|
|
||||||
|
// Reset visible count when filters/search/tab/sort change
|
||||||
|
useEffect(() => {
|
||||||
|
setVisibleCount(PAGE_SIZE);
|
||||||
|
}, [activeTab, debouncedSearch, debouncedFilters, sortField, sortDir]);
|
||||||
|
|
||||||
// Gather unique genres from the library
|
// Gather unique genres from the library
|
||||||
const allGenres = useMemo(() => {
|
const allGenres = useMemo(() => {
|
||||||
const set = new Set<string>();
|
const set = new Set<string>();
|
||||||
@@ -95,33 +136,40 @@ export function LibraryPage() {
|
|||||||
const filteredEntries = useMemo(() => {
|
const filteredEntries = useMemo(() => {
|
||||||
let result = baseEntries;
|
let result = baseEntries;
|
||||||
|
|
||||||
if (search) {
|
if (debouncedSearch) {
|
||||||
const q = search.toLowerCase();
|
const q = debouncedSearch.toLowerCase();
|
||||||
result = result.filter((e) => e.title.toLowerCase().includes(q));
|
result = result.filter((e) => e.title.toLowerCase().includes(q));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (filters.genre) {
|
if (debouncedFilters.genre) {
|
||||||
result = result.filter((e) => e.genres?.some((g) => g.name === filters.genre));
|
result = result.filter((e) => e.genres?.some((g) => g.name === debouncedFilters.genre));
|
||||||
}
|
}
|
||||||
if (filters.ratingMin) {
|
if (debouncedFilters.ratingMin) {
|
||||||
const min = parseFloat(filters.ratingMin);
|
const min = parseFloat(debouncedFilters.ratingMin);
|
||||||
result = result.filter((e) => (e.entryRatings?.user_rating || e.entryRatings?.tmdb_rating || 0) >= min);
|
result = result.filter((e) => (e.entryRatings?.user_rating ?? 0) >= min);
|
||||||
}
|
}
|
||||||
if (filters.ratingMax) {
|
if (debouncedFilters.ratingMax) {
|
||||||
const max = parseFloat(filters.ratingMax);
|
const max = parseFloat(debouncedFilters.ratingMax);
|
||||||
result = result.filter((e) => (e.entryRatings?.user_rating || e.entryRatings?.tmdb_rating || 0) <= max);
|
result = result.filter((e) => (e.entryRatings?.user_rating ?? 0) <= max);
|
||||||
}
|
}
|
||||||
if (filters.yearMin) {
|
if (debouncedFilters.yearMin) {
|
||||||
const min = parseInt(filters.yearMin);
|
const min = parseInt(debouncedFilters.yearMin);
|
||||||
result = result.filter((e) => e.release_date && new Date(e.release_date).getFullYear() >= min);
|
result = result.filter((e) => e.release_date && new Date(e.release_date).getFullYear() >= min);
|
||||||
}
|
}
|
||||||
if (filters.yearMax) {
|
if (debouncedFilters.yearMax) {
|
||||||
const max = parseInt(filters.yearMax);
|
const max = parseInt(debouncedFilters.yearMax);
|
||||||
result = result.filter((e) => e.release_date && new Date(e.release_date).getFullYear() <= max);
|
result = result.filter((e) => e.release_date && new Date(e.release_date).getFullYear() <= max);
|
||||||
}
|
}
|
||||||
|
|
||||||
return sortEntries(result, sortField, sortDir);
|
return sortEntries(result, sortField, sortDir);
|
||||||
}, [baseEntries, search, filters, sortField, sortDir]);
|
}, [baseEntries, debouncedSearch, debouncedFilters, sortField, sortDir]);
|
||||||
|
|
||||||
|
const paginatedEntries = useMemo(
|
||||||
|
() => filteredEntries.slice(0, visibleCount),
|
||||||
|
[filteredEntries, visibleCount]
|
||||||
|
);
|
||||||
|
const hasMore = visibleCount < filteredEntries.length;
|
||||||
|
const loadMore = useCallback(() => setVisibleCount((c) => c + PAGE_SIZE), []);
|
||||||
|
|
||||||
const clearFilters = useCallback(() => setFilters(defaultFilters), []);
|
const clearFilters = useCallback(() => setFilters(defaultFilters), []);
|
||||||
|
|
||||||
@@ -214,7 +262,7 @@ export function LibraryPage() {
|
|||||||
<PopoverContent className="w-72 space-y-4" align="end">
|
<PopoverContent className="w-72 space-y-4" align="end">
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<label className="text-sm font-medium">Genre</label>
|
<label className="text-sm font-medium">Genre</label>
|
||||||
<Select value={filters.genre} onValueChange={(v) => setFilters((f) => ({ ...f, genre: v === '_all' ? '' : v }))}>
|
<Select value={filters.genre} onValueChange={(v) => setFilters((f) => ({ ...f, genre: v === '_all' ? '' : (v || '') }))}>
|
||||||
<SelectTrigger className="h-9"><SelectValue placeholder="All genres" /></SelectTrigger>
|
<SelectTrigger className="h-9"><SelectValue placeholder="All genres" /></SelectTrigger>
|
||||||
<SelectContent>
|
<SelectContent>
|
||||||
<SelectItem value="_all">All genres</SelectItem>
|
<SelectItem value="_all">All genres</SelectItem>
|
||||||
@@ -325,7 +373,7 @@ export function LibraryPage() {
|
|||||||
<PopoverContent className="w-72 space-y-4" align="end">
|
<PopoverContent className="w-72 space-y-4" align="end">
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<label className="text-sm font-medium">Genre</label>
|
<label className="text-sm font-medium">Genre</label>
|
||||||
<Select value={filters.genre} onValueChange={(v) => setFilters((f) => ({ ...f, genre: v === '_all' ? '' : v }))}>
|
<Select value={filters.genre} onValueChange={(v) => setFilters((f) => ({ ...f, genre: v === '_all' ? '' : (v || '') }))}>
|
||||||
<SelectTrigger className="h-9"><SelectValue placeholder="All genres" /></SelectTrigger>
|
<SelectTrigger className="h-9"><SelectValue placeholder="All genres" /></SelectTrigger>
|
||||||
<SelectContent>
|
<SelectContent>
|
||||||
<SelectItem value="_all">All genres</SelectItem>
|
<SelectItem value="_all">All genres</SelectItem>
|
||||||
@@ -367,7 +415,12 @@ export function LibraryPage() {
|
|||||||
</Tabs>
|
</Tabs>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<EntryLibraryGrid entries={filteredEntries} isLoading={isLoading} />
|
<EntryLibraryGrid
|
||||||
|
entries={paginatedEntries}
|
||||||
|
isLoading={isLoading}
|
||||||
|
hasMore={hasMore}
|
||||||
|
onLoadMore={loadMore}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,16 @@
|
|||||||
|
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 { useCreateEpisodeSeen } from '@/hooks/useEpisodeSeen';
|
||||||
|
import { EpisodeSeenEditDialog } from '@/components/dialogs/EpisodeSeenEditDialog';
|
||||||
|
import { ConfirmDialog } from '@/components/dialogs/ConfirmDialog';
|
||||||
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 { Badge } from '@/components/ui/badge';
|
import { Badge } from '@/components/ui/badge';
|
||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
import { Eye, CheckCircle2 } 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';
|
||||||
|
|
||||||
export function SeasonPage() {
|
export function SeasonPage() {
|
||||||
const { entryId, seasonId } = useParams<{
|
const { entryId, seasonId } = useParams<{
|
||||||
@@ -16,6 +20,12 @@ 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 createEpisodeSeen = useCreateEpisodeSeen();
|
||||||
|
const [dialogEpisode, setDialogEpisode] = useState<Episode | null>(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 [showMarkAllConfirm, setShowMarkAllConfirm] = useState(false);
|
||||||
|
|
||||||
if (isLoading || !season) {
|
if (isLoading || !season) {
|
||||||
return (
|
return (
|
||||||
@@ -49,8 +59,23 @@ export function SeasonPage() {
|
|||||||
alt={season.name}
|
alt={season.name}
|
||||||
className="h-56 w-36 shrink-0 rounded-lg object-cover shadow-lg"
|
className="h-56 w-36 shrink-0 rounded-lg object-cover shadow-lg"
|
||||||
/>
|
/>
|
||||||
<div className="flex flex-col justify-center space-y-3">
|
<div className="flex-1 flex flex-col justify-center space-y-3">
|
||||||
|
<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 */}
|
||||||
|
{watchedCount < totalEpisodes && (
|
||||||
|
<Button
|
||||||
|
size="sm"
|
||||||
|
variant="outline"
|
||||||
|
className="shrink-0"
|
||||||
|
disabled={markingAll}
|
||||||
|
onClick={() => setShowMarkAllDialog(true)}
|
||||||
|
>
|
||||||
|
<ListChecks className="mr-1.5 h-4 w-4" />
|
||||||
|
{markingAll ? 'Marking...' : 'Mark Season Watched'}
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</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>
|
||||||
{season.air_date && (
|
{season.air_date && (
|
||||||
@@ -144,14 +169,10 @@ export function SeasonPage() {
|
|||||||
ev.preventDefault();
|
ev.preventDefault();
|
||||||
ev.stopPropagation();
|
ev.stopPropagation();
|
||||||
if (!isWatched) {
|
if (!isWatched) {
|
||||||
createEpisodeSeen.mutate({
|
setDialogEpisode(episode);
|
||||||
entry_id: parseInt(entryId!, 10),
|
|
||||||
episode_id: episode.id,
|
|
||||||
date_seen: new Date().toISOString(),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
disabled={isWatched || createEpisodeSeen.isPending}
|
disabled={isWatched}
|
||||||
>
|
>
|
||||||
<Eye className="h-4 w-4" />
|
<Eye className="h-4 w-4" />
|
||||||
</Button>
|
</Button>
|
||||||
@@ -160,6 +181,65 @@ export function SeasonPage() {
|
|||||||
})}
|
})}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{/* Episode Seen Dialog */}
|
||||||
|
{dialogEpisode && (
|
||||||
|
<EpisodeSeenEditDialog
|
||||||
|
open={!!dialogEpisode}
|
||||||
|
onOpenChange={(open) => { if (!open) setDialogEpisode(null); }}
|
||||||
|
mode="create"
|
||||||
|
episodeId={dialogEpisode.id}
|
||||||
|
entryId={parseInt(entryId!, 10)}
|
||||||
|
seasonId={parseInt(seasonId!, 10)}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Mark Season Watched — step 1: date/seen-with dialog */}
|
||||||
|
{showMarkAllDialog && (
|
||||||
|
<EpisodeSeenEditDialog
|
||||||
|
open={showMarkAllDialog}
|
||||||
|
onOpenChange={(open) => {
|
||||||
|
if (!open) setShowMarkAllDialog(false);
|
||||||
|
}}
|
||||||
|
mode="create"
|
||||||
|
episodeId={-1}
|
||||||
|
entryId={parseInt(entryId!, 10)}
|
||||||
|
seasonId={parseInt(seasonId!, 10)}
|
||||||
|
overrideSubmit={(date, seenWith) => {
|
||||||
|
setMarkAllDate(date);
|
||||||
|
setMarkAllSeenWith(seenWith);
|
||||||
|
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,
|
||||||
|
season_id: parseInt(seasonId!, 10),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
setMarkingAll(false);
|
||||||
|
setShowMarkAllConfirm(false);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user