2022-02-10 22:27:57 +01:00
|
|
|
import { WatchedMediaCard } from "components/media/WatchedMediaCard";
|
|
|
|
import { SearchBarInput } from "components/SearchBar";
|
2022-02-17 21:48:35 +01:00
|
|
|
import {
|
|
|
|
MWMassProviderOutput,
|
|
|
|
MWMedia,
|
|
|
|
MWMediaType,
|
|
|
|
MWQuery,
|
|
|
|
SearchProviders,
|
|
|
|
} from "providers";
|
2022-02-13 19:29:25 +01:00
|
|
|
import { useEffect, useState } from "react";
|
2022-02-10 23:45:17 +01:00
|
|
|
import { ThinContainer } from "components/layout/ThinContainer";
|
|
|
|
import { SectionHeading } from "components/layout/SectionHeading";
|
|
|
|
import { Icons } from "components/Icon";
|
2022-02-13 18:49:03 +01:00
|
|
|
import { Loading } from "components/layout/Loading";
|
|
|
|
import { Tagline } from "components/Text/Tagline";
|
|
|
|
import { Title } from "components/Text/Title";
|
2022-02-13 19:29:25 +01:00
|
|
|
import { useDebounce } from "hooks/useDebounce";
|
2022-02-17 18:25:12 +01:00
|
|
|
import { useLoading } from "hooks/useLoading";
|
2022-02-18 14:36:32 +01:00
|
|
|
import { IconPatch } from "components/Buttons/IconPatch";
|
2022-02-07 23:22:35 +01:00
|
|
|
|
2022-02-17 18:25:12 +01:00
|
|
|
function SearchLoading() {
|
|
|
|
return <Loading className="my-12" text="Fetching your favourite shows..." />;
|
|
|
|
}
|
|
|
|
|
2022-02-17 21:48:35 +01:00
|
|
|
function SearchSuffix(props: {
|
|
|
|
fails: number;
|
|
|
|
total: number;
|
|
|
|
resultsSize: number;
|
|
|
|
}) {
|
2022-02-18 14:36:32 +01:00
|
|
|
const allFailed: boolean = props.fails === props.total;
|
|
|
|
const icon: Icons = allFailed ? Icons.WARNING : Icons.EYE_SLASH;
|
|
|
|
|
2022-02-17 21:48:35 +01:00
|
|
|
return (
|
2022-02-18 14:36:32 +01:00
|
|
|
<div className="my-24 flex flex-col items-center justify-center space-y-3 text-center">
|
|
|
|
<IconPatch
|
|
|
|
icon={icon}
|
|
|
|
className={`text-xl ${allFailed ? "text-red-400" : "text-bink-600"}`}
|
|
|
|
/>
|
|
|
|
|
|
|
|
{/* standard suffix */}
|
|
|
|
{!allFailed ? (
|
|
|
|
<div>
|
|
|
|
{props.fails > 0 ? (
|
|
|
|
<p className="text-red-400">
|
|
|
|
{props.fails}/{props.total} providers failed
|
|
|
|
</p>
|
|
|
|
) : null}
|
|
|
|
{props.resultsSize > 0 ? (
|
|
|
|
<p>That's all we have — sorry</p>
|
|
|
|
) : (
|
|
|
|
<p>We couldn't find anything — sorry</p>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
|
|
|
|
{/* Error result */}
|
|
|
|
{allFailed ? (
|
|
|
|
<div>
|
|
|
|
<p>All providers failed — whoops</p>
|
|
|
|
</div>
|
2022-02-17 21:48:35 +01:00
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-18 20:22:56 +01:00
|
|
|
function SearchResultsView({
|
|
|
|
searchQuery,
|
|
|
|
clear,
|
|
|
|
}: {
|
|
|
|
searchQuery: MWQuery;
|
|
|
|
clear: () => void;
|
|
|
|
}) {
|
2022-02-17 21:48:35 +01:00
|
|
|
const [results, setResults] = useState<MWMassProviderOutput | undefined>();
|
2022-02-17 18:25:12 +01:00
|
|
|
const [runSearchQuery, loading, error, success] = useLoading(
|
|
|
|
(query: MWQuery) => SearchProviders(query)
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (searchQuery.searchQuery !== "") runSearch(searchQuery);
|
|
|
|
}, [searchQuery]);
|
|
|
|
|
|
|
|
async function runSearch(query: MWQuery) {
|
|
|
|
const results = await runSearchQuery(query);
|
|
|
|
if (!results) return;
|
|
|
|
setResults(results);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{/* results */}
|
2022-02-17 21:48:35 +01:00
|
|
|
{success && results?.results.length ? (
|
2022-02-18 20:22:56 +01:00
|
|
|
<SectionHeading
|
|
|
|
title="Search results"
|
|
|
|
icon={Icons.SEARCH}
|
|
|
|
linkText="Back to home"
|
|
|
|
onClick={() => clear()}
|
|
|
|
>
|
2022-02-17 21:48:35 +01:00
|
|
|
{results.results.map((v) => (
|
2022-02-17 18:25:12 +01:00
|
|
|
<WatchedMediaCard
|
|
|
|
key={[v.mediaId, v.providerId].join("|")}
|
|
|
|
media={v}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</SectionHeading>
|
|
|
|
) : null}
|
|
|
|
|
2022-02-17 21:48:35 +01:00
|
|
|
{/* search suffix */}
|
|
|
|
{success && results ? (
|
|
|
|
<SearchSuffix
|
|
|
|
resultsSize={results.results.length}
|
|
|
|
fails={results.stats.failed}
|
|
|
|
total={results.stats.total}
|
|
|
|
/>
|
|
|
|
) : null}
|
2022-02-17 18:25:12 +01:00
|
|
|
|
|
|
|
{/* error */}
|
2022-02-18 14:36:32 +01:00
|
|
|
{error ? <SearchSuffix resultsSize={0} fails={1} total={1} /> : null}
|
2022-02-17 18:25:12 +01:00
|
|
|
|
|
|
|
{/* Loading icon */}
|
|
|
|
{loading ? <SearchLoading /> : null}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function SearchView() {
|
|
|
|
const [searching, setSearching] = useState<boolean>(false);
|
|
|
|
const [loading, setLoading] = useState<boolean>(false);
|
2022-02-13 19:29:25 +01:00
|
|
|
const [search, setSearch] = useState<MWQuery>({
|
|
|
|
searchQuery: "",
|
|
|
|
type: MWMediaType.MOVIE,
|
|
|
|
});
|
2022-02-07 23:22:35 +01:00
|
|
|
|
2022-02-13 19:29:25 +01:00
|
|
|
const debouncedSearch = useDebounce<MWQuery>(search, 2000);
|
|
|
|
useEffect(() => {
|
2022-02-17 18:25:12 +01:00
|
|
|
setSearching(search.searchQuery !== "");
|
|
|
|
setLoading(search.searchQuery !== "");
|
2022-02-13 19:29:25 +01:00
|
|
|
}, [search]);
|
2022-02-17 18:25:12 +01:00
|
|
|
useEffect(() => {
|
|
|
|
setLoading(false);
|
|
|
|
}, [debouncedSearch]);
|
2022-02-16 22:38:37 +01:00
|
|
|
|
2022-02-07 23:22:35 +01:00
|
|
|
return (
|
2022-02-10 23:45:17 +01:00
|
|
|
<ThinContainer>
|
2022-02-16 22:38:37 +01:00
|
|
|
{/* input section */}
|
2022-02-13 19:29:25 +01:00
|
|
|
<div className="mt-36 space-y-16 text-center">
|
2022-02-10 23:45:17 +01:00
|
|
|
<div className="space-y-4">
|
2022-02-13 18:49:03 +01:00
|
|
|
<Tagline>Because watching legally is boring</Tagline>
|
|
|
|
<Title>What movie do you want to watch?</Title>
|
2022-02-10 23:45:17 +01:00
|
|
|
</div>
|
|
|
|
<SearchBarInput
|
|
|
|
onChange={setSearch}
|
|
|
|
value={search}
|
|
|
|
placeholder="What movie do you want to watch?"
|
|
|
|
/>
|
|
|
|
</div>
|
2022-02-16 22:38:37 +01:00
|
|
|
|
2022-02-17 18:25:12 +01:00
|
|
|
{/* results view */}
|
|
|
|
{loading ? (
|
|
|
|
<SearchLoading />
|
|
|
|
) : searching ? (
|
2022-02-18 20:22:56 +01:00
|
|
|
<SearchResultsView
|
|
|
|
searchQuery={debouncedSearch}
|
|
|
|
clear={() => setSearch((v) => ({ searchQuery: "", type: v.type }))}
|
|
|
|
/>
|
2022-02-16 22:38:37 +01:00
|
|
|
) : null}
|
2022-02-10 23:45:17 +01:00
|
|
|
</ThinContainer>
|
|
|
|
);
|
2022-02-07 23:22:35 +01:00
|
|
|
}
|