Rework SearchPage with tabs, infinite scroll, and anti-spam
- Add Movies & TV / People tabs next to search input - Filter person results from movie/TV grid - People tab with larger avatar cards, name always visible - Infinite scroll with IntersectionObserver sentinel per tab - Stop auto-scroll after 4 consecutive empty pages, show Load More button instead
This commit is contained in:
1 parent
c09f70054c
commit
d12d7cb627
1 file changed
+308
-46
@@ -1,19 +1,33 @@
|
|||||||
import { useState, useEffect, useRef } from "react";
|
import {
|
||||||
|
useState,
|
||||||
|
useEffect,
|
||||||
|
useRef,
|
||||||
|
useCallback,
|
||||||
|
useMemo,
|
||||||
|
useReducer,
|
||||||
|
} from "react";
|
||||||
import { useSearchParams, Link } from "react-router-dom";
|
import { useSearchParams, Link } from "react-router-dom";
|
||||||
import { useTmdbSearch, useTmdbSearchPeople } from "@/hooks/useTmdb";
|
import { useTmdbSearch, useTmdbSearchPeople } from "@/hooks/useTmdb";
|
||||||
import { useLibrary } from "@/hooks/useEntries";
|
import { useLibrary } from "@/hooks/useEntries";
|
||||||
import { Input } from "@/components/ui/input";
|
import { Input } from "@/components/ui/input";
|
||||||
import { Search } from "lucide-react";
|
import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs";
|
||||||
import { EntryCard } from "@/components/entries/EntryCard";
|
|
||||||
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
|
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
|
||||||
import { getTmdbImageUrl } from "@/lib/api";
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { Search, Loader2 } from "lucide-react";
|
||||||
|
import { EntryCard } from "@/components/entries/EntryCard";
|
||||||
|
import { getProfileUrl } from "@/lib/media-utils";
|
||||||
|
|
||||||
|
type SearchTab = "entries" | "people";
|
||||||
|
|
||||||
|
const MAX_EMPTY_PAGES = 4;
|
||||||
|
|
||||||
export function SearchPage() {
|
export function SearchPage() {
|
||||||
const [searchParams, setSearchParams] = useSearchParams();
|
const [searchParams, setSearchParams] = useSearchParams();
|
||||||
const initialQuery = searchParams.get("q") || "";
|
const initialQuery = searchParams.get("q") || "";
|
||||||
const [searchInput, setSearchInput] = useState(initialQuery);
|
const [searchInput, setSearchInput] = useState(initialQuery);
|
||||||
const [debouncedQuery, setDebouncedQuery] = useState(initialQuery);
|
const [debouncedQuery, setDebouncedQuery] = useState(initialQuery);
|
||||||
const debounceRef = useRef<ReturnType<typeof setTimeout>>();
|
const [activeTab, setActiveTab] = useState<SearchTab>("entries");
|
||||||
|
const debounceRef = useRef<ReturnType<typeof setTimeout>>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (debounceRef.current) clearTimeout(debounceRef.current);
|
if (debounceRef.current) clearTimeout(debounceRef.current);
|
||||||
@@ -36,20 +50,175 @@ export function SearchPage() {
|
|||||||
|
|
||||||
useLibrary();
|
useLibrary();
|
||||||
|
|
||||||
const searchResults = useTmdbSearch(debouncedQuery);
|
const {
|
||||||
const peopleResults = useTmdbSearchPeople(debouncedQuery);
|
data: searchData,
|
||||||
|
isLoading: searchLoading,
|
||||||
|
fetchNextPage: fetchNextSearch,
|
||||||
|
hasNextPage: hasNextSearch,
|
||||||
|
isFetchingNextPage: fetchingNextSearch,
|
||||||
|
} = useTmdbSearch(debouncedQuery);
|
||||||
|
|
||||||
const searchEntries =
|
const peopleQuery = activeTab === "people" ? debouncedQuery : "";
|
||||||
searchResults.data?.pages.flatMap((p) => p.results) ?? [];
|
|
||||||
const searchPeople =
|
const {
|
||||||
peopleResults.data?.pages.flatMap((p) => p.results) ?? [];
|
data: peopleData,
|
||||||
|
fetchNextPage: fetchNextPeople,
|
||||||
|
hasNextPage: hasNextPeople,
|
||||||
|
isFetchingNextPage: fetchingNextPeople,
|
||||||
|
} = useTmdbSearchPeople(peopleQuery);
|
||||||
|
|
||||||
|
// Filter entries: only movies & TV (TMDB multi-search sometimes returns "person" results)
|
||||||
|
const searchEntries = useMemo(() => {
|
||||||
|
const seen = new Set<string>();
|
||||||
|
const all = searchData?.pages.flatMap((p) => p.results) ?? [];
|
||||||
|
return all
|
||||||
|
.filter((entry) => entry.media_type !== "person")
|
||||||
|
.filter((entry) => {
|
||||||
|
const key = `${entry.id}-${entry.media_type}`;
|
||||||
|
if (seen.has(key)) return false;
|
||||||
|
seen.add(key);
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
}, [searchData]);
|
||||||
|
|
||||||
|
const searchPeople = useMemo(() => {
|
||||||
|
const seen = new Set<number>();
|
||||||
|
const all = peopleData?.pages.flatMap((p) => p.results) ?? [];
|
||||||
|
return all.filter((person) => {
|
||||||
|
if (seen.has(person.id)) return false;
|
||||||
|
seen.add(person.id);
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
}, [peopleData]);
|
||||||
|
|
||||||
|
type SpamAction =
|
||||||
|
| { type: "entry_empty" }
|
||||||
|
| { type: "entry_found" }
|
||||||
|
| { type: "people_empty" }
|
||||||
|
| { type: "people_found" }
|
||||||
|
| { type: "reset_entry" }
|
||||||
|
| { type: "reset_people" }
|
||||||
|
| { type: "reset_all" };
|
||||||
|
|
||||||
|
// ── Anti-spam: stop auto-scroll after N consecutive empty pages ──
|
||||||
|
const prevEntryCountRef = useRef(0);
|
||||||
|
const prevPeopleCountRef = useRef(0);
|
||||||
|
|
||||||
|
const [spam, spamDispatch] = useReducer(
|
||||||
|
(state: { entry: number; people: number }, action: SpamAction) => {
|
||||||
|
switch (action.type) {
|
||||||
|
case "entry_empty":
|
||||||
|
return { ...state, entry: state.entry + 1 };
|
||||||
|
case "entry_found":
|
||||||
|
return { ...state, entry: 0 };
|
||||||
|
case "people_empty":
|
||||||
|
return { ...state, people: state.people + 1 };
|
||||||
|
case "people_found":
|
||||||
|
return { ...state, people: 0 };
|
||||||
|
case "reset_entry":
|
||||||
|
return { ...state, entry: 0 };
|
||||||
|
case "reset_people":
|
||||||
|
return { ...state, people: 0 };
|
||||||
|
case "reset_all":
|
||||||
|
return { entry: 0, people: 0 };
|
||||||
|
default:
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ entry: 0, people: 0 },
|
||||||
|
);
|
||||||
|
|
||||||
|
const entryLoadMore = spam.entry >= MAX_EMPTY_PAGES;
|
||||||
|
const peopleLoadMore = spam.people >= MAX_EMPTY_PAGES;
|
||||||
|
|
||||||
|
// Reset streak on new query
|
||||||
|
useEffect(() => {
|
||||||
|
prevEntryCountRef.current = 0;
|
||||||
|
prevPeopleCountRef.current = 0;
|
||||||
|
spamDispatch({ type: "reset_all" });
|
||||||
|
}, [debouncedQuery]);
|
||||||
|
|
||||||
|
// Detect empty entry fetches
|
||||||
|
useEffect(() => {
|
||||||
|
if (fetchingNextSearch || searchEntries.length === 0) return;
|
||||||
|
if (searchEntries.length === prevEntryCountRef.current) {
|
||||||
|
spamDispatch({ type: "entry_empty" });
|
||||||
|
} else {
|
||||||
|
spamDispatch({ type: "entry_found" });
|
||||||
|
}
|
||||||
|
prevEntryCountRef.current = searchEntries.length;
|
||||||
|
}, [fetchingNextSearch, searchEntries.length]);
|
||||||
|
|
||||||
|
// Detect empty people fetches
|
||||||
|
useEffect(() => {
|
||||||
|
if (fetchingNextPeople || searchPeople.length === 0) return;
|
||||||
|
if (searchPeople.length === prevPeopleCountRef.current) {
|
||||||
|
spamDispatch({ type: "people_empty" });
|
||||||
|
} else {
|
||||||
|
spamDispatch({ type: "people_found" });
|
||||||
|
}
|
||||||
|
prevPeopleCountRef.current = searchPeople.length;
|
||||||
|
}, [fetchingNextPeople, searchPeople.length]);
|
||||||
|
|
||||||
|
// ── Infinite scroll sentinels ──
|
||||||
|
const entrySentinelRef = useRef<HTMLDivElement>(null);
|
||||||
|
const peopleSentinelRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
|
const handleEntryIntersect = useCallback(
|
||||||
|
(entries: IntersectionObserverEntry[]) => {
|
||||||
|
if (
|
||||||
|
entries[0]?.isIntersecting &&
|
||||||
|
hasNextSearch &&
|
||||||
|
!fetchingNextSearch &&
|
||||||
|
!entryLoadMore
|
||||||
|
) {
|
||||||
|
fetchNextSearch();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[hasNextSearch, fetchingNextSearch, fetchNextSearch, entryLoadMore],
|
||||||
|
);
|
||||||
|
|
||||||
|
const handlePeopleIntersect = useCallback(
|
||||||
|
(entries: IntersectionObserverEntry[]) => {
|
||||||
|
if (
|
||||||
|
entries[0]?.isIntersecting &&
|
||||||
|
hasNextPeople &&
|
||||||
|
!fetchingNextPeople &&
|
||||||
|
!peopleLoadMore
|
||||||
|
) {
|
||||||
|
fetchNextPeople();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[hasNextPeople, fetchingNextPeople, fetchNextPeople, peopleLoadMore],
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const sentinel = entrySentinelRef.current;
|
||||||
|
if (!sentinel) return;
|
||||||
|
const observer = new IntersectionObserver(handleEntryIntersect, {
|
||||||
|
rootMargin: "200px",
|
||||||
|
});
|
||||||
|
observer.observe(sentinel);
|
||||||
|
return () => observer.disconnect();
|
||||||
|
}, [handleEntryIntersect]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const sentinel = peopleSentinelRef.current;
|
||||||
|
if (!sentinel || activeTab !== "people") return;
|
||||||
|
const observer = new IntersectionObserver(handlePeopleIntersect, {
|
||||||
|
rootMargin: "200px",
|
||||||
|
});
|
||||||
|
observer.observe(sentinel);
|
||||||
|
return () => observer.disconnect();
|
||||||
|
}, [handlePeopleIntersect, activeTab]);
|
||||||
|
|
||||||
const isSearching = debouncedQuery.length > 0;
|
const isSearching = debouncedQuery.length > 0;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="space-y-6 animate-page-enter">
|
<div className="space-y-6 animate-page-enter">
|
||||||
{/* Search */}
|
{/* Search + Tabs row */}
|
||||||
<div className="relative max-w-md">
|
<div className="flex items-center justify-between gap-3">
|
||||||
|
<div className="relative flex-1 max-w-xs">
|
||||||
<Search className="absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground" />
|
<Search className="absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground" />
|
||||||
<Input
|
<Input
|
||||||
placeholder="Search movies, TV shows & people..."
|
placeholder="Search movies, TV shows & people..."
|
||||||
@@ -59,30 +228,109 @@ export function SearchPage() {
|
|||||||
autoFocus
|
autoFocus
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
{isSearching && (
|
||||||
|
<Tabs
|
||||||
|
value={activeTab}
|
||||||
|
onValueChange={(v) => setActiveTab(v as SearchTab)}
|
||||||
|
>
|
||||||
|
<TabsList>
|
||||||
|
<TabsTrigger value="entries">Movies & TV</TabsTrigger>
|
||||||
|
<TabsTrigger value="people">People</TabsTrigger>
|
||||||
|
</TabsList>
|
||||||
|
</Tabs>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
{isSearching ? (
|
{isSearching ? (
|
||||||
<div className="space-y-6">
|
<div className="space-y-4">
|
||||||
{/* People results */}
|
{/* Movies & TV tab */}
|
||||||
{searchPeople.length > 0 && (
|
{activeTab === "entries" && (
|
||||||
<div className="space-y-3">
|
<>
|
||||||
<h2 className="text-lg font-semibold">People</h2>
|
<h2 className="text-xl font-semibold">
|
||||||
<div className="flex flex-wrap gap-3">
|
Results for “{debouncedQuery}”
|
||||||
{searchPeople.slice(0, 10).map((person) => (
|
</h2>
|
||||||
|
{searchEntries.length === 0 && !searchLoading ? (
|
||||||
|
<p className="text-muted-foreground">No results found.</p>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<div className="grid grid-cols-2 gap-3 sm:gap-4 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-5 xl:grid-cols-6 2xl:grid-cols-7">
|
||||||
|
{searchEntries.map((entry, index) => (
|
||||||
|
<div
|
||||||
|
key={`${entry.id}-${entry.media_type}`}
|
||||||
|
className="animate-card-enter"
|
||||||
|
style={{
|
||||||
|
animationDelay: `${Math.min(index * 30, 300)}ms`,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<EntryCard tmdbEntry={entry} showTypeBadge />
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Sentinel / Load more */}
|
||||||
|
<div
|
||||||
|
ref={entrySentinelRef}
|
||||||
|
className="flex h-20 items-center justify-center"
|
||||||
|
>
|
||||||
|
{fetchingNextSearch ? (
|
||||||
|
<Loader2 className="h-6 w-6 animate-spin text-muted-foreground" />
|
||||||
|
) : entryLoadMore && hasNextSearch ? (
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
size="sm"
|
||||||
|
onClick={() => {
|
||||||
|
spamDispatch({ type: "reset_entry" });
|
||||||
|
fetchNextSearch();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Load more
|
||||||
|
</Button>
|
||||||
|
) : hasNextSearch ? (
|
||||||
|
<span className="text-sm text-muted-foreground">
|
||||||
|
Scroll for more
|
||||||
|
</span>
|
||||||
|
) : searchEntries.length > 0 ? (
|
||||||
|
<span className="text-sm text-muted-foreground">
|
||||||
|
All results loaded
|
||||||
|
</span>
|
||||||
|
) : null}
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* People tab */}
|
||||||
|
{activeTab === "people" && (
|
||||||
|
<>
|
||||||
|
<h2 className="text-xl font-semibold">
|
||||||
|
People matching “{debouncedQuery}”
|
||||||
|
</h2>
|
||||||
|
{peopleQuery && searchPeople.length === 0 && !peopleData ? (
|
||||||
|
<div className="flex h-40 items-center justify-center">
|
||||||
|
<Loader2 className="h-6 w-6 animate-spin text-muted-foreground" />
|
||||||
|
</div>
|
||||||
|
) : searchPeople.length === 0 && peopleData ? (
|
||||||
|
<p className="text-muted-foreground">No people found.</p>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<div className="grid grid-cols-3 gap-4 sm:grid-cols-4 md:grid-cols-5 lg:grid-cols-6 xl:grid-cols-8">
|
||||||
|
{searchPeople.map((person, index) => (
|
||||||
<Link
|
<Link
|
||||||
key={person.id}
|
key={person.id}
|
||||||
to={`/people/tmdb/${person.id}`}
|
to={`/people/tmdb/${person.id}`}
|
||||||
className="flex items-center gap-2 rounded-full bg-muted px-3 py-1.5 transition-colors hover:bg-accent"
|
className="group flex flex-col items-center gap-2 text-center animate-card-enter"
|
||||||
|
style={{
|
||||||
|
animationDelay: `${Math.min(index * 20, 300)}ms`,
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
<Avatar className="h-7 w-7">
|
<div className="relative">
|
||||||
|
<Avatar className="h-28 w-28 transition-transform group-hover:scale-105 sm:h-32 sm:w-32">
|
||||||
<AvatarImage
|
<AvatarImage
|
||||||
src={
|
src={getProfileUrl(person.profile_path)}
|
||||||
person.profile_path
|
|
||||||
? getTmdbImageUrl(person.profile_path, "w92")
|
|
||||||
: undefined
|
|
||||||
}
|
|
||||||
alt={person.name}
|
alt={person.name}
|
||||||
/>
|
/>
|
||||||
<AvatarFallback className="text-xs">
|
<AvatarFallback className="text-lg">
|
||||||
{person.name
|
{person.name
|
||||||
.split(" ")
|
.split(" ")
|
||||||
.map((n) => n[0])
|
.map((n) => n[0])
|
||||||
@@ -90,7 +338,10 @@ export function SearchPage() {
|
|||||||
.slice(0, 2)}
|
.slice(0, 2)}
|
||||||
</AvatarFallback>
|
</AvatarFallback>
|
||||||
</Avatar>
|
</Avatar>
|
||||||
<span className="text-sm font-medium">{person.name}</span>
|
</div>
|
||||||
|
<span className="text-sm font-medium leading-tight line-clamp-2">
|
||||||
|
{person.name}
|
||||||
|
</span>
|
||||||
{person.known_for_department && (
|
{person.known_for_department && (
|
||||||
<span className="text-xs text-muted-foreground">
|
<span className="text-xs text-muted-foreground">
|
||||||
{person.known_for_department}
|
{person.known_for_department}
|
||||||
@@ -99,27 +350,38 @@ export function SearchPage() {
|
|||||||
</Link>
|
</Link>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{/* Entry results */}
|
{/* Sentinel / Load more */}
|
||||||
<h2 className="text-xl font-semibold">
|
|
||||||
Search Results for “{debouncedQuery}”
|
|
||||||
</h2>
|
|
||||||
{searchEntries.length === 0 && !searchResults.isLoading ? (
|
|
||||||
<p className="text-muted-foreground">No results found.</p>
|
|
||||||
) : (
|
|
||||||
<div className="grid grid-cols-2 gap-3 sm:gap-4 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-5 xl:grid-cols-6 2xl:grid-cols-7">
|
|
||||||
{searchEntries.map((entry, index) => (
|
|
||||||
<div
|
<div
|
||||||
key={entry.id}
|
ref={peopleSentinelRef}
|
||||||
className="animate-card-enter"
|
className="flex h-20 items-center justify-center"
|
||||||
style={{ animationDelay: `${Math.min(index * 30, 300)}ms` }}
|
|
||||||
>
|
>
|
||||||
<EntryCard tmdbEntry={entry} showTypeBadge />
|
{fetchingNextPeople ? (
|
||||||
</div>
|
<Loader2 className="h-6 w-6 animate-spin text-muted-foreground" />
|
||||||
))}
|
) : peopleLoadMore && hasNextPeople ? (
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
size="sm"
|
||||||
|
onClick={() => {
|
||||||
|
spamDispatch({ type: "reset_people" });
|
||||||
|
fetchNextPeople();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Load more
|
||||||
|
</Button>
|
||||||
|
) : hasNextPeople ? (
|
||||||
|
<span className="text-sm text-muted-foreground">
|
||||||
|
Scroll for more
|
||||||
|
</span>
|
||||||
|
) : searchPeople.length > 0 ? (
|
||||||
|
<span className="text-sm text-muted-foreground">
|
||||||
|
All results loaded
|
||||||
|
</span>
|
||||||
|
) : null}
|
||||||
</div>
|
</div>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
|
|||||||
Reference in new issue
Block a user