import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { searchForMedia } from "@/backend/metadata/search"; import { MWMediaMeta, MWQuery } from "@/backend/metadata/types/mw"; import { IconPatch } from "@/components/buttons/IconPatch"; import { Icons } from "@/components/Icon"; import { SectionHeading } from "@/components/layout/SectionHeading"; import { MediaGrid } from "@/components/media/MediaGrid"; import { WatchedMediaCard } from "@/components/media/WatchedMediaCard"; import { useLoading } from "@/hooks/useLoading"; import { SearchLoadingPart } from "@/views/parts/search/SearchLoadingPart"; function SearchSuffix(props: { failed?: boolean; results?: number }) { const { t } = useTranslation(); const icon: Icons = props.failed ? Icons.WARNING : Icons.EYE_SLASH; return (
{/* standard suffix */} {!props.failed ? (
{(props.results ?? 0) > 0 ? (

{t("search.allResults")}

) : (

{t("search.noResults")}

)}
) : null} {/* Error result */} {props.failed ? (

{t("search.allFailed")}

) : null}
); } export function SearchResultsView({ searchQuery }: { searchQuery: MWQuery }) { const { t } = useTranslation(); const [results, setResults] = useState([]); const [runSearchQuery, loading, error] = useLoading((query: MWQuery) => searchForMedia(query) ); useEffect(() => { async function runSearch(query: MWQuery) { const searchResults = await runSearchQuery(query); if (!searchResults) return; setResults(searchResults); } if (searchQuery.searchQuery !== "") runSearch(searchQuery); }, [searchQuery, runSearchQuery]); if (loading) return ; if (error) return ; if (!results) return null; return (
{results.length > 0 ? (
{results.map((v) => ( ))}
) : null}
); }