Skip to content

Commit

Permalink
feat: Add search poster and categories
Browse files Browse the repository at this point in the history
  • Loading branch information
anderson-oki committed Oct 21, 2024
1 parent 5934d1b commit 2894d5e
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 40 deletions.
7 changes: 6 additions & 1 deletion bazarr/api/system/searches.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from flask_restx import Resource, Namespace, reqparse
from unidecode import unidecode

from app.config import settings
from app.config import base_url, settings
from app.database import TableShows, TableMovies, database, select

from ..utils import authenticate
Expand Down Expand Up @@ -34,6 +34,7 @@ def get(self):
search_list += database.execute(
select(TableShows.title,
TableShows.sonarrSeriesId,
TableShows.poster,
TableShows.year)
.order_by(TableShows.title)) \
.all()
Expand All @@ -43,6 +44,7 @@ def get(self):
search_list += database.execute(
select(TableMovies.title,
TableMovies.radarrId,
TableMovies.poster,
TableMovies.year)
.order_by(TableMovies.title)) \
.all()
Expand All @@ -58,8 +60,11 @@ def get(self):

if hasattr(x, 'sonarrSeriesId'):
result['sonarrSeriesId'] = x.sonarrSeriesId
result['poster'] = f"{base_url}/images/series{x.poster}" if x.poster else None

else:
result['radarrId'] = x.radarrId
result['poster'] = f"{base_url}/images/movies{x.poster}" if x.poster else None

results.append(result)

Expand Down
9 changes: 9 additions & 0 deletions frontend/src/components/Search.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, it } from "vitest";
import { Search } from "@/components/index";
import { render } from "@/tests";

describe("Search Bar", () => {
it.skip("should render the closed empty state", () => {
render(<Search />);
});
});
132 changes: 93 additions & 39 deletions frontend/src/components/Search.tsx
Original file line number Diff line number Diff line change
@@ -1,77 +1,92 @@
import { FunctionComponent, useMemo, useState } from "react";
import { useNavigate } from "react-router-dom";
import { Autocomplete, ComboboxItem, OptionsFilter, Text } from "@mantine/core";
import {
ComboboxItem,
Image,
OptionsFilter,
Select,
Text,
} from "@mantine/core";
import { ComboboxItemGroup } from "@mantine/core/lib/components/Combobox/Combobox.types";
import { faSearch } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { chain, includes } from "lodash";
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);

const duplicates = chain(data)
.groupBy((item) => `${item.title} (${item.year})`)
.filter((group) => group.length > 1)
.map((group) => `${group[0].title} (${group[0].year})`)
.value();

return useMemo<SearchResultItem[]>(
() =>
data?.map((v) => {
const { link, displayName } = (() => {
const hasDuplicate = includes(duplicates, `${v.title} (${v.year})`);

const { link, label, poster, type, value } = (() => {
if (v.sonarrSeriesId) {
return {
poster: v.poster,
link: `/series/${v.sonarrSeriesId}`,
displayName: hasDuplicate
? `${v.title} (${v.year}) (S)`
: `${v.title} (${v.year})`,
type: "show",
label: `${v.title} (${v.year})`,
value: `s-${v.sonarrSeriesId}`,
};
}

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

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

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

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

return (options as ComboboxItem[]).filter((option) => {
return (
option.value.toLowerCase().includes(lowercaseSearch) ||
option.value
.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "")
.toLowerCase()
.includes(trimmedSearch)
);
(options as ComboboxItemGroup[]).map((c) => {
return {
group: c.group,
items: c.items.filter((option) => {
const o = option as ComboboxItem;

return (
o.label.toLowerCase().includes(lowercaseSearch) ||
o.label
.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "")
.toLowerCase()
.includes(trimmedSearch)
);
}),
};
});

return options;
};

const Search: FunctionComponent = () => {
Expand All @@ -80,23 +95,62 @@ const Search: FunctionComponent = () => {

const results = useSearch(query);

const groups = [
{
group: "Series",
items: results
.filter((r) => r.type === "show")
.map((r) => {
return {
label: r.label,
value: r.value,
};
}),
},
{
group: "Movies",
items: results
.filter((r) => r.type === "movie")
.map((r) => {
return {
label: r.label,
value: r.value,
};
}),
},
];

return (
<Autocomplete
leftSection={<FontAwesomeIcon icon={faSearch} />}
renderOption={(input) => <Text p="xs">{input.option.value}</Text>}
<Select
placeholder="Search"
withCheckIcon={false}
leftSection={<FontAwesomeIcon icon={faSearch} />}
rightSection={<></>}
size="sm"
data={results}
value={query}
searchable
scrollAreaProps={{ type: "auto" }}
maxDropdownHeight={400}
onChange={setQuery}
data={groups}
value={query}
onSearchChange={(a) => {
setQuery(a);
}}
onBlur={() => setQuery("")}
filter={optionsFilter}
onOptionSubmit={(option) =>
navigate(results.find((a) => a.value === option)?.link || "/")
}
></Autocomplete>
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>
</>
);
}}
/>
);
};

Expand Down
1 change: 1 addition & 0 deletions frontend/src/types/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ type ItemSearchResult = Partial<SeriesIdType> &
Partial<MovieIdType> & {
title: string;
year: string;
poster: string;
};

type BackendError = {
Expand Down

0 comments on commit 2894d5e

Please sign in to comment.