forked from morpheus65535/bazarr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManualSearchModal.tsx
More file actions
258 lines (236 loc) · 6.8 KB
/
ManualSearchModal.tsx
File metadata and controls
258 lines (236 loc) · 6.8 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import React, { useCallback, useMemo, useState } from "react";
import {
Alert,
Anchor,
Badge,
Button,
Code,
Collapse,
Divider,
Stack,
Text,
} from "@mantine/core";
import {
faCaretDown,
faCloudDownloadAlt,
faDownload,
faInfoCircle,
} from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { UseQueryResult } from "@tanstack/react-query";
import { ColumnDef } from "@tanstack/react-table";
import { isString } from "lodash";
import { Action } from "@/components";
import Language from "@/components/bazarr/Language";
import StateIcon from "@/components/StateIcon";
import PageTable from "@/components/tables/PageTable";
import { withModal } from "@/modules/modals";
import { GetItemId } from "@/utilities";
type SupportType = Item.Movie | Item.Episode;
interface Props<T extends SupportType> {
download: (item: T, result: SearchResultType) => Promise<void>;
query: (id?: number) => UseQueryResult<SearchResultType[] | undefined>;
item: T;
}
function ManualSearchView<T extends SupportType>(props: Props<T>) {
const { download, query: useSearch, item } = props;
const [searchStarted, setSearchStarted] = useState(false);
const itemId = useMemo(() => GetItemId(item), [item]);
const results = useSearch(searchStarted ? itemId : undefined);
const haveResult = results.data !== undefined;
const search = useCallback(() => {
setSearchStarted(true);
void results.refetch();
}, [results]);
const ReleaseInfoCell = React.memo(
({ releaseInfo }: { releaseInfo: string[] }) => {
const [open, setOpen] = useState(false);
const items = useMemo(
() => releaseInfo.slice(1).map((v, idx) => <Text key={idx}>{v}</Text>),
[releaseInfo],
);
if (releaseInfo.length === 0) {
return <Text c="dimmed">Cannot get release info</Text>;
}
return (
<Stack gap={0} onClick={() => setOpen((o) => !o)}>
<Text className="table-primary" span>
{releaseInfo[0]}
{releaseInfo.length > 1 && (
<FontAwesomeIcon
icon={faCaretDown}
rotation={open ? 180 : undefined}
></FontAwesomeIcon>
)}
</Text>
<Collapse in={open}>
<>{items}</>
</Collapse>
</Stack>
);
},
);
const [Downloaded, setDownloaded] = useState({ id: "", state: false });
const columns = useMemo<ColumnDef<SearchResultType>[]>(
() => [
{
header: "Score",
accessorKey: "score",
cell: ({
row: {
original: { score },
},
}) => {
return <Text className="table-no-wrap">{score}%</Text>;
},
},
{
header: "Language",
accessorKey: "language",
cell: ({
row: {
original: { language, hearing_impaired: hi, forced },
},
}) => {
const lang: Language.Info = {
code2: language,
hi: hi === "True",
forced: forced === "True",
name: "",
};
return (
<Badge>
<Language.Text value={lang}></Language.Text>
</Badge>
);
},
},
{
header: "Provider",
accessorKey: "provider",
cell: ({
row: {
original: { provider, url },
},
}) => {
const value = provider;
if (url) {
return (
<Anchor
className="table-no-wrap"
href={url}
target="_blank"
rel="noopener noreferrer"
>
{value}
</Anchor>
);
} else {
return <Text>{value}</Text>;
}
},
},
{
header: "Release",
accessorKey: "release_info",
cell: ({
row: {
original: { release_info: releaseInfo },
},
}) => {
return <ReleaseInfoCell releaseInfo={releaseInfo} />;
},
},
{
header: "Uploader",
accessorKey: "uploader",
cell: ({
row: {
original: { uploader },
},
}) => {
return <Text className="table-no-wrap">{uploader ?? "-"}</Text>;
},
},
{
header: "Match",
accessorKey: "matches",
cell: (row) => {
const { matches, dont_matches: dont } = row.row.original;
return (
<StateIcon
matches={matches}
dont={dont}
isHistory={false}
></StateIcon>
);
},
},
{
header: "Get",
accessorKey: "subtitle",
cell: ({ row }) => {
const result = row.original;
const isDownloaded = Downloaded.id === row.id && Downloaded.state;
return (
<Action
label="Download"
icon={isDownloaded ? faCloudDownloadAlt : faDownload}
color={isDownloaded ? "brand" : "gray"}
disabled={item === null}
onClick={async () => {
if (!item) return;
setDownloaded({ id: row.id, state: true });
await download(item, result);
}}
></Action>
);
},
},
],
[download, item, ReleaseInfoCell, Downloaded],
);
const bSceneNameAvailable =
isString(item.sceneName) && item.sceneName.length !== 0;
const searchButtonText = useMemo(() => {
if (results.isFetching) {
return "Searching";
}
return searchStarted ? "Search Again" : "Search";
}, [results.isFetching, searchStarted]);
return (
<Stack>
<Alert
title="Resource"
color="gray"
icon={<FontAwesomeIcon icon={faInfoCircle}></FontAwesomeIcon>}
>
<Text size="sm">{item?.path}</Text>
<Divider hidden={!bSceneNameAvailable} my="xs"></Divider>
<Code hidden={!bSceneNameAvailable}>{item?.sceneName}</Code>
</Alert>
<Collapse in={haveResult && !results.isFetching}>
<PageTable
autoScroll={false}
tableStyles={{ emptyText: "No result", placeholder: 10 }}
columns={columns}
data={results.data ?? []}
></PageTable>
</Collapse>
<Divider></Divider>
<Button loading={results.isFetching} fullWidth onClick={search}>
{searchButtonText}
</Button>
</Stack>
);
}
export const MovieSearchModal = withModal<Props<Item.Movie>>(
ManualSearchView,
"movie-manual-search",
{ title: "Search Subtitles", size: "calc(100vw - 4rem)" },
);
export const EpisodeSearchModal = withModal<Props<Item.Episode>>(
ManualSearchView,
"episode-manual-search",
{ title: "Search Subtitles", size: "calc(100vw - 4rem)" },
);