feat(people): unify person detail page for DB and TMDB sources
- Merge PersonDetailsPage and PersonTmdbRedirectPage into one component - /people/:id fetches from local DB with favorite toggle - /people/tmdb/:tmdbId fetches from TMDB with "Add to Library" button - Add TMDB external link in person metadata - Fix CastList links for TMDB preview cast (use /people/tmdb/:tmdbId) - Replace 11MB getAll() with server-side paginated infinite scroll - Add useLocalPeopleInfinite hook and PaginatedPeopleResponse type
This commit is contained in:
1 parent
6d676d803c
commit
db6da6782f
6 files changed
+231
-59
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import { useState, useCallback, useRef, useEffect, useMemo } from 'react';
|
||||
import { Link } from 'react-router-dom';
|
||||
import { usePopularPeople, useTmdbSearchPeople } from '@/hooks/useTmdb';
|
||||
import { useSearchLocalPeople } from '@/hooks/usePeople';
|
||||
import { useLocalPeopleInfinite } from '@/hooks/usePeople';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
|
||||
import { Skeleton } from '@/components/ui/skeleton';
|
||||
@@ -12,14 +12,12 @@ import { Search, Heart } from 'lucide-react';
|
||||
import { getProfileUrl } from '@/lib/media-utils';
|
||||
|
||||
type PeopleFilter = 'all' | 'favorite';
|
||||
const LOCAL_PAGE_SIZE = 48;
|
||||
|
||||
export function PeoplePage() {
|
||||
const [search, setSearch] = useState('');
|
||||
const [debouncedSearch, setDebouncedSearch] = useState('');
|
||||
const [filter, setFilter] = useState<PeopleFilter>('all');
|
||||
const [localOnly, setLocalOnly] = useState(false);
|
||||
const [localVisibleCount, setLocalVisibleCount] = useState(LOCAL_PAGE_SIZE);
|
||||
|
||||
// Debounce search input
|
||||
useEffect(() => {
|
||||
@@ -27,46 +25,40 @@ export function PeoplePage() {
|
||||
return () => clearTimeout(timer);
|
||||
}, [search]);
|
||||
|
||||
// Reset visible count when filter/search changes
|
||||
useEffect(() => {
|
||||
setLocalVisibleCount(LOCAL_PAGE_SIZE);
|
||||
}, [debouncedSearch, filter, localOnly]);
|
||||
|
||||
const isSearching = debouncedSearch.length > 0;
|
||||
|
||||
const popularQuery = usePopularPeople();
|
||||
const tmdbSearchQuery = useTmdbSearchPeople(localOnly ? '' : debouncedSearch);
|
||||
const { data: localSearchResults = [], isLoading: localSearchLoading } = useSearchLocalPeople(
|
||||
localOnly ? debouncedSearch : ''
|
||||
|
||||
// Paginated local people (handles both search and browse)
|
||||
const localPeopleQuery = useLocalPeopleInfinite(
|
||||
localOnly ? debouncedSearch : '',
|
||||
localOnly,
|
||||
);
|
||||
|
||||
// For TMDB mode, use infinite scroll queries
|
||||
const activeQuery = isSearching && !localOnly ? tmdbSearchQuery : !localOnly ? popularQuery : null;
|
||||
const tmdbPeople = activeQuery?.data?.pages.flatMap((p) => p.results) ?? [];
|
||||
|
||||
// For local mode with no search, fetch all local people
|
||||
const { data: allLocalPeople = [], isLoading: allLocalLoading } = useSearchLocalPeople(
|
||||
localOnly && !isSearching ? '*' : ''
|
||||
// Flatten paginated local results
|
||||
const allLocalPeople = useMemo(
|
||||
() => localPeopleQuery.data?.pages.flatMap((p) => p.data) ?? [],
|
||||
[localPeopleQuery.data],
|
||||
);
|
||||
|
||||
// Determine displayed people
|
||||
// Apply client-side favorite filter
|
||||
const filteredLocalPeople = useMemo(() => {
|
||||
if (!localOnly) return [];
|
||||
const people = isSearching ? localSearchResults : allLocalPeople;
|
||||
if (filter === 'favorite') {
|
||||
return people.filter((p: any) => p.favorite);
|
||||
return allLocalPeople.filter((p: any) => p.favorite);
|
||||
}
|
||||
return people;
|
||||
}, [localOnly, isSearching, localSearchResults, allLocalPeople, filter]);
|
||||
return allLocalPeople;
|
||||
}, [localOnly, allLocalPeople, filter]);
|
||||
|
||||
const displayPeople = useMemo(() => {
|
||||
if (localOnly) {
|
||||
return filteredLocalPeople.slice(0, localVisibleCount);
|
||||
}
|
||||
if (localOnly) return filteredLocalPeople;
|
||||
return tmdbPeople;
|
||||
}, [localOnly, filteredLocalPeople, localVisibleCount, tmdbPeople]);
|
||||
|
||||
const hasMoreLocal = localOnly && localVisibleCount < filteredLocalPeople.length;
|
||||
}, [localOnly, filteredLocalPeople, tmdbPeople]);
|
||||
|
||||
const observerRef = useRef<IntersectionObserver | null>(null);
|
||||
const lastRef = useCallback(
|
||||
@@ -76,14 +68,16 @@ export function PeoplePage() {
|
||||
observerRef.current = new IntersectionObserver((entries) => {
|
||||
if (!entries[0].isIntersecting) return;
|
||||
if (localOnly) {
|
||||
if (hasMoreLocal) setLocalVisibleCount((c) => c + LOCAL_PAGE_SIZE);
|
||||
if (localPeopleQuery.hasNextPage && !localPeopleQuery.isFetchingNextPage) {
|
||||
localPeopleQuery.fetchNextPage();
|
||||
}
|
||||
} else if (activeQuery && !activeQuery.isFetchingNextPage && activeQuery.hasNextPage) {
|
||||
activeQuery.fetchNextPage();
|
||||
}
|
||||
});
|
||||
observerRef.current.observe(node);
|
||||
},
|
||||
[localOnly, hasMoreLocal, activeQuery?.isFetchingNextPage, activeQuery?.hasNextPage, activeQuery?.fetchNextPage]
|
||||
[localOnly, localPeopleQuery.hasNextPage, localPeopleQuery.isFetchingNextPage, localPeopleQuery.fetchNextPage, activeQuery?.isFetchingNextPage, activeQuery?.hasNextPage, activeQuery?.fetchNextPage]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -91,7 +85,7 @@ export function PeoplePage() {
|
||||
}, []);
|
||||
|
||||
const isLoading = localOnly
|
||||
? (isSearching ? localSearchLoading : allLocalLoading)
|
||||
? localPeopleQuery.isLoading
|
||||
: activeQuery?.isLoading;
|
||||
|
||||
return (
|
||||
|
||||
Reference in new issue
Block a user