From d12d7cb6276d99eae5acc1f75cb1998bf8408ee0 Mon Sep 17 00:00:00 2001 From: X4Lo Date: Mon, 29 Jun 2026 02:49:58 +0100 Subject: [PATCH] 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 --- react_frontend/src/pages/SearchPage.tsx | 412 +++++++++++++++++++----- 1 file changed, 337 insertions(+), 75 deletions(-) diff --git a/react_frontend/src/pages/SearchPage.tsx b/react_frontend/src/pages/SearchPage.tsx index 1f3c9b0..fd6eec4 100644 --- a/react_frontend/src/pages/SearchPage.tsx +++ b/react_frontend/src/pages/SearchPage.tsx @@ -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 { useTmdbSearch, useTmdbSearchPeople } from "@/hooks/useTmdb"; import { useLibrary } from "@/hooks/useEntries"; import { Input } from "@/components/ui/input"; -import { Search } from "lucide-react"; -import { EntryCard } from "@/components/entries/EntryCard"; +import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; 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() { const [searchParams, setSearchParams] = useSearchParams(); const initialQuery = searchParams.get("q") || ""; const [searchInput, setSearchInput] = useState(initialQuery); const [debouncedQuery, setDebouncedQuery] = useState(initialQuery); - const debounceRef = useRef>(); + const [activeTab, setActiveTab] = useState("entries"); + const debounceRef = useRef>(null); useEffect(() => { if (debounceRef.current) clearTimeout(debounceRef.current); @@ -36,90 +50,338 @@ export function SearchPage() { useLibrary(); - const searchResults = useTmdbSearch(debouncedQuery); - const peopleResults = useTmdbSearchPeople(debouncedQuery); + const { + data: searchData, + isLoading: searchLoading, + fetchNextPage: fetchNextSearch, + hasNextPage: hasNextSearch, + isFetchingNextPage: fetchingNextSearch, + } = useTmdbSearch(debouncedQuery); - const searchEntries = - searchResults.data?.pages.flatMap((p) => p.results) ?? []; - const searchPeople = - peopleResults.data?.pages.flatMap((p) => p.results) ?? []; + const peopleQuery = activeTab === "people" ? debouncedQuery : ""; + + const { + 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(); + 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(); + 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(null); + const peopleSentinelRef = useRef(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; return (
- {/* Search */} -
- - setSearchInput(e.target.value)} - className="pl-10" - autoFocus - /> + {/* Search + Tabs row */} +
+
+ + setSearchInput(e.target.value)} + className="pl-10" + autoFocus + /> +
+ {isSearching && ( + setActiveTab(v as SearchTab)} + > + + Movies & TV + People + + + )}
{isSearching ? ( -
- {/* People results */} - {searchPeople.length > 0 && ( -
-

People

-
- {searchPeople.slice(0, 10).map((person) => ( - + {/* Movies & TV tab */} + {activeTab === "entries" && ( + <> +

+ Results for “{debouncedQuery}” +

+ {searchEntries.length === 0 && !searchLoading ? ( +

No results found.

+ ) : ( + <> +
+ {searchEntries.map((entry, index) => ( +
+ +
+ ))} +
+ + {/* Sentinel / Load more */} +
- - - - {person.name - .split(" ") - .map((n) => n[0]) - .join("") - .slice(0, 2)} - - - {person.name} - {person.known_for_department && ( - - {person.known_for_department} + {fetchingNextSearch ? ( + + ) : entryLoadMore && hasNextSearch ? ( + + ) : hasNextSearch ? ( + + Scroll for more - )} - - ))} -
-
+ ) : searchEntries.length > 0 ? ( + + All results loaded + + ) : null} +
+ + )} + )} - {/* Entry results */} -

- Search Results for “{debouncedQuery}” -

- {searchEntries.length === 0 && !searchResults.isLoading ? ( -

No results found.

- ) : ( -
- {searchEntries.map((entry, index) => ( -
- + {/* People tab */} + {activeTab === "people" && ( + <> +

+ People matching “{debouncedQuery}” +

+ {peopleQuery && searchPeople.length === 0 && !peopleData ? ( +
+
- ))} -
+ ) : searchPeople.length === 0 && peopleData ? ( +

No people found.

+ ) : ( + <> +
+ {searchPeople.map((person, index) => ( + +
+ + + + {person.name + .split(" ") + .map((n) => n[0]) + .join("") + .slice(0, 2)} + + +
+ + {person.name} + + {person.known_for_department && ( + + {person.known_for_department} + + )} + + ))} +
+ + {/* Sentinel / Load more */} +
+ {fetchingNextPeople ? ( + + ) : peopleLoadMore && hasNextPeople ? ( + + ) : hasNextPeople ? ( + + Scroll for more + + ) : searchPeople.length > 0 ? ( + + All results loaded + + ) : null} +
+ + )} + )}
) : (