"use client"; import { useState, useEffect } from "react"; import { useSearchParams, useRouter } from "next/navigation"; import { SearchIcon } from "lucide-react"; import { useSearchData } from "@/features/search/hooks/useSearchData"; import { useSoulseekSearch } from "@/features/search/hooks/useSoulseekSearch"; import { SearchFilters } from "@/features/search/components/SearchFilters"; import { TopResult } from "@/features/search/components/TopResult"; import { EmptyState } from "@/features/search/components/EmptyState"; import { LibraryAlbumsGrid } from "@/features/search/components/LibraryAlbumsGrid"; import { LibraryPodcastsGrid } from "@/features/search/components/LibraryPodcastsGrid"; import { LibraryTracksList } from "@/features/search/components/LibraryTracksList"; import { SimilarArtistsGrid } from "@/features/search/components/SimilarArtistsGrid"; import { SoulseekSongsList } from "@/features/search/components/SoulseekSongsList"; import { TVSearchInput } from "@/features/search/components/TVSearchInput"; import type { FilterTab } from "@/features/search/types"; export default function SearchPage() { const searchParams = useSearchParams(); const router = useRouter(); const [filterTab, setFilterTab] = useState("all"); const [query, setQuery] = useState(""); // Custom hooks const { libraryResults, discoverResults, isLibrarySearching, isDiscoverSearching, hasSearched, } = useSearchData({ query }); const { soulseekResults, isSoulseekSearching, isSoulseekPolling, soulseekEnabled, downloadingFiles, handleDownload, } = useSoulseekSearch({ query }); // Read query from URL params on mount useEffect(() => { const q = searchParams.get("q"); if (q) { setQuery(q); } }, [searchParams]); // Derived state const topArtist = discoverResults.find((r) => r.type === "music"); const isLoading = isLibrarySearching || isDiscoverSearching || isSoulseekSearching || isSoulseekPolling; const showLibrary = filterTab === "all" || filterTab === "library"; const showDiscover = filterTab === "all" || filterTab === "discover"; const showSoulseek = filterTab === "all" || filterTab === "soulseek"; // Handle TV search const handleTVSearch = (searchQuery: string) => { setQuery(searchQuery); router.push(`/search?q=${encodeURIComponent(searchQuery)}`); }; return (
{/* TV Search Input - only visible in TV mode */}
{/* Loading spinner */} {hasSearched && (isLibrarySearching || isDiscoverSearching || isSoulseekSearching) && (!libraryResults || !libraryResults.artists?.length) && discoverResults.length === 0 && (

{isSoulseekSearching || isSoulseekPolling ? `Searching... (${soulseekResults.length} found)` : "Searching..."}

)} {/* Top Result */} {hasSearched && (showDiscover || showLibrary) && (libraryResults?.artists?.[0] || topArtist) && ( )} {/* Soulseek Songs */} {hasSearched && showSoulseek && soulseekResults.length > 0 && (

Songs

)} {/* Library Songs */} {hasSearched && showLibrary && libraryResults?.tracks?.length > 0 && (

Songs in Your Library

)} {/* Library Albums */} {hasSearched && showLibrary && libraryResults?.albums?.length > 0 && (

Your Albums

)} {/* Library Podcasts */} {hasSearched && showLibrary && libraryResults?.podcasts?.length > 0 && (

Podcasts

)} {/* Similar Artists */} {hasSearched && showDiscover && discoverResults.filter((r) => r.type === "music").length > 1 && ( )} {/* No Results */} {hasSearched && !isLoading && !topArtist && soulseekResults.length === 0 && (!libraryResults || (!libraryResults.artists?.length && !libraryResults.albums?.length && !libraryResults.tracks?.length)) && (

No results found

Try searching for something else

)}
); }