summaryrefslogtreecommitdiffhomepage
path: root/frontend/src/components/Search.tsx
blob: 9571d9f2bbeb3c004a1a88cd28986f02dbaf1d4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { FunctionComponent, useMemo, useState } from "react";
import { useNavigate } from "react-router-dom";
import {
  ComboboxItem,
  Image,
  OptionsFilter,
  Select,
  Text,
} from "@mantine/core";
import { faSearch } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { useServerSearch } from "@/apis/hooks";
import { useDebouncedValue } from "@/utilities";

type SearchResultItem = {
  value: string;
  label: string;
  link: string;
  poster: string;
  type: string;
};

function useSearch(query: string) {
  const debouncedQuery = useDebouncedValue(query, 500);
  const { data } = useServerSearch(debouncedQuery, debouncedQuery.length >= 0);

  return useMemo<SearchResultItem[]>(
    () =>
      data?.map((v) => {
        const { link, label, poster, type, value } = (() => {
          if (v.sonarrSeriesId) {
            return {
              poster: v.poster,
              link: `/series/${v.sonarrSeriesId}`,
              type: "show",
              label: `${v.title} (${v.year})`,
              value: `s-${v.sonarrSeriesId}`,
            };
          }

          if (v.radarrId) {
            return {
              poster: v.poster,
              link: `/movies/${v.radarrId}`,
              type: "movie",
              value: `m-${v.radarrId}`,
              label: `${v.title} (${v.year})`,
            };
          }

          throw new Error("Unknown search result");
        })();

        return {
          value: value,
          poster: poster,
          label: label,
          type: type,
          link: link,
        };
      }) ?? [],
    [data],
  );
}

const optionsFilter: OptionsFilter = ({ options, search }) => {
  const lowercaseSearch = search.toLowerCase();
  const trimmedSearch = search.trim();

  return (options as ComboboxItem[]).filter((option) => {
    return (
      option.label.toLowerCase().includes(lowercaseSearch) ||
      option.label
        .normalize("NFD")
        .replace(/[\u0300-\u036f]/g, "")
        .toLowerCase()
        .includes(trimmedSearch)
    );
  });
};

const Search: FunctionComponent = () => {
  const navigate = useNavigate();
  const [query, setQuery] = useState("");

  const results = useSearch(query);

  return (
    <Select
      placeholder="Search"
      withCheckIcon={false}
      leftSection={<FontAwesomeIcon icon={faSearch} />}
      rightSection={<></>}
      size="sm"
      searchable
      scrollAreaProps={{ type: "auto" }}
      maxDropdownHeight={400}
      data={results}
      value={query}
      onSearchChange={(a) => {
        setQuery(a);
      }}
      onBlur={() => setQuery("")}
      filter={optionsFilter}
      onOptionSubmit={(option) => {
        navigate(results.find((a) => a.value === option)?.link || "/");
      }}
      renderOption={(input) => {
        const result = results.find((r) => r.value === input.option.value);

        return (
          <>
            <Image src={result?.poster} w={35} h={50} />
            <Text p="xs">{result?.label}</Text>
          </>
        );
      }}
    />
  );
};

export default Search;