diff --git a/react_frontend/src/api/entries.ts b/react_frontend/src/api/entries.ts index 73a5c62..f372693 100644 --- a/react_frontend/src/api/entries.ts +++ b/react_frontend/src/api/entries.ts @@ -1,6 +1,6 @@ import { api } from '@/lib/api'; -import { normalizeEntry } from '@/lib/normalizers'; -import type { Entry, TmdbPreview } from '@/types'; +import { normalizeEntry, normalizeCredit } from '@/lib/normalizers'; +import type { Entry, Credit, TmdbPreview } from '@/types'; export const entriesApi = { getAll: () => @@ -42,6 +42,11 @@ export const entriesApi = { getSeasons: (id: number) => api.get(`/entries/${id}/seasons`).then((r) => Array.isArray(r.data) ? r.data : []), + getCredits: (id: number) => + api + .get(`/entries/${id}/credits`) + .then((r) => (Array.isArray(r.data) ? r.data.map(normalizeCredit) : [])), + syncSeries: () => api.get('/entries/sync-series').then((r) => r.data), previewTmdb: (tmdbId: number, type: 'movie' | 'tv') => diff --git a/react_frontend/src/hooks/useEntries.ts b/react_frontend/src/hooks/useEntries.ts index f4113d4..7022813 100644 --- a/react_frontend/src/hooks/useEntries.ts +++ b/react_frontend/src/hooks/useEntries.ts @@ -8,6 +8,7 @@ export const entryKeys = { all: ['entries'] as const, detail: (id: number) => ['entries', id] as const, seasons: (id: number) => ['entries', id, 'seasons'] as const, + credits: (id: number) => ['entries', id, 'credits'] as const, tmdbPreview: (tmdbId: number, type: string) => ['entries', 'tmdb-preview', type, tmdbId] as const, }; @@ -41,6 +42,15 @@ export function useEntrySeasons(id: number) { }); } +export function useEntryCredits(id: number) { + return useQuery({ + queryKey: entryKeys.credits(id), + queryFn: () => entriesApi.getCredits(id), + enabled: !!id, + staleTime: 5 * 60 * 1000, + }); +} + export function useCreateEntry() { const queryClient = useQueryClient(); const addEntry = useLibraryStore((s) => s.addEntry); diff --git a/react_frontend/src/lib/normalizers.ts b/react_frontend/src/lib/normalizers.ts index f1d25e8..db890c0 100644 --- a/react_frontend/src/lib/normalizers.ts +++ b/react_frontend/src/lib/normalizers.ts @@ -41,6 +41,7 @@ export function normalizeCredit(raw: any): Credit { department: raw.department, job: raw.job, order: raw.order, + episode_count: raw.episode_count ?? null, people: people, }; } diff --git a/react_frontend/src/pages/EntryCastsPage.tsx b/react_frontend/src/pages/EntryCastsPage.tsx index d5c088d..eec3147 100644 --- a/react_frontend/src/pages/EntryCastsPage.tsx +++ b/react_frontend/src/pages/EntryCastsPage.tsx @@ -1,6 +1,6 @@ -import { useState, useMemo } from 'react'; +import { useState, useMemo, useEffect, useRef } from 'react'; import { useParams, Link } from 'react-router-dom'; -import { useEntry } from '@/hooks/useEntries'; +import { useEntry, useEntryCredits } from '@/hooks/useEntries'; import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'; import { Skeleton } from '@/components/ui/skeleton'; import { Button } from '@/components/ui/button'; @@ -19,6 +19,23 @@ type SortField = 'order' | 'popularity' | 'name' | 'birthday' | 'gender' | 'epis type SortDir = 'asc' | 'desc'; type CastFilter = 'all' | 'cast' | 'crew'; +const PAGE_SIZE = 24; + +const SORT_LABELS: Record = { + order: 'Credit Order', + popularity: 'Popularity', + name: 'Name', + birthday: 'Birthday', + gender: 'Gender', + episode_count: 'Episodes', +}; + +const FILTER_LABELS: Record = { + all: 'All', + cast: 'Cast', + crew: 'Crew', +}; + function sortCredits(credits: Credit[], field: SortField, dir: SortDir): Credit[] { return [...credits].sort((a, b) => { let cmp = 0; @@ -53,21 +70,49 @@ export function EntryCastsPage() { const { id } = useParams<{ id: string }>(); const entryId = parseInt(id!, 10); const { data: entry, isLoading } = useEntry(entryId); + const { data: allCredits = [], isLoading: creditsLoading } = useEntryCredits(entryId); const [sortField, setSortField] = useState('order'); const [sortDir, setSortDir] = useState('asc'); const [filter, setFilter] = useState('all'); + const [visibleCount, setVisibleCount] = useState(PAGE_SIZE); const filtered = useMemo(() => { - if (!entry?.credits) return []; - if (filter === 'cast') return entry.credits.filter((c) => c.cast_type === 'cast'); - if (filter === 'crew') return entry.credits.filter((c) => c.cast_type === 'crew'); - return entry.credits; - }, [entry?.credits, filter]); + if (!allCredits) return []; + if (filter === 'cast') return allCredits.filter((c) => c.cast_type === 'cast'); + if (filter === 'crew') return allCredits.filter((c) => c.cast_type === 'crew'); + return allCredits; + }, [allCredits, filter]); const sorted = useMemo(() => sortCredits(filtered, sortField, sortDir), [filtered, sortField, sortDir]); - if (isLoading || !entry) { + // Reset visible window whenever filter/sort changes + useEffect(() => { + setVisibleCount(PAGE_SIZE); + }, [filter, sortField, sortDir]); + + const visible = useMemo(() => sorted.slice(0, visibleCount), [sorted, visibleCount]); + const hasMore = visibleCount < sorted.length; + + // IntersectionObserver-based lazy load on scroll + const sentinelRef = useRef(null); + useEffect(() => { + if (!hasMore) return; + const node = sentinelRef.current; + if (!node) return; + const observer = new IntersectionObserver( + (entries) => { + if (entries.some((e) => e.isIntersecting)) { + setVisibleCount((c) => Math.min(c + PAGE_SIZE, sorted.length)); + } + }, + { rootMargin: '400px 0px' } + ); + observer.observe(node); + return () => observer.disconnect(); + }, [hasMore, sorted.length]); + + if (isLoading || !entry || creditsLoading) { return (
@@ -103,7 +148,7 @@ export function EntryCastsPage() {
setSortField(v as SortField)}> - + {SORT_LABELS[sortField]} Credit Order @@ -144,11 +189,25 @@ export function EntryCastsPage() { {sorted.length === 0 ? (

No credits found.

) : ( -
- {sorted.map((credit) => ( - - ))} -
+ <> +
+ {visible.map((credit) => ( + + ))} +
+ {hasMore && ( +
+
+ {Array.from({ length: Math.min(PAGE_SIZE, sorted.length - visibleCount) }).map((_, i) => ( +
+ + +
+ ))} +
+
+ )} + )}
);