From 4795ac4c77b4c7adedc1383763b1ea8560cb6c67 Mon Sep 17 00:00:00 2001 From: LASER-Yi Date: Sun, 22 Aug 2021 17:27:58 +0800 Subject: Move download subtitle action to background --- frontend/src/Movies/Detail/index.tsx | 5 +-- frontend/src/Series/Episodes/table.tsx | 5 +-- .../src/components/modals/ManualSearchModal.tsx | 48 +++++++++++++--------- frontend/src/utilites/index.ts | 2 +- 4 files changed, 33 insertions(+), 27 deletions(-) diff --git a/frontend/src/Movies/Detail/index.tsx b/frontend/src/Movies/Detail/index.tsx index d13b040e6..9c6ff0445 100644 --- a/frontend/src/Movies/Detail/index.tsx +++ b/frontend/src/Movies/Detail/index.tsx @@ -31,8 +31,7 @@ import { RouterEmptyPath } from "../../special-pages/404"; import { useOnLoadedOnce } from "../../utilites"; import Table from "./table"; -const download = (item: any, result: SearchResultType) => { - item = item as Item.Movie; +const download = (item: Item.Movie, result: SearchResultType) => { const { language, hearing_impaired, forced, provider, subtitle } = result; return ProvidersApi.downloadMovieSubtitle(item.radarrId, { language, @@ -182,7 +181,7 @@ const MovieDetailView: FunctionComponent = ({ match }) => { ); diff --git a/frontend/src/Series/Episodes/table.tsx b/frontend/src/Series/Episodes/table.tsx index ad6b5376e..a7dc02036 100644 --- a/frontend/src/Series/Episodes/table.tsx +++ b/frontend/src/Series/Episodes/table.tsx @@ -32,8 +32,7 @@ interface Props { profile?: Language.Profile; } -const download = (item: any, result: SearchResultType) => { - item = item as Item.Episode; +const download = (item: Item.Episode, result: SearchResultType) => { const { language, hearing_impaired, forced, provider, subtitle } = result; return ProvidersApi.downloadEpisodeSubtitle( item.sonarrSeriesId, @@ -226,7 +225,7 @@ const Table: FunctionComponent = ({ ); diff --git a/frontend/src/components/modals/ManualSearchModal.tsx b/frontend/src/components/modals/ManualSearchModal.tsx index 3c173a476..8a5259719 100644 --- a/frontend/src/components/modals/ManualSearchModal.tsx +++ b/frontend/src/components/modals/ManualSearchModal.tsx @@ -25,7 +25,6 @@ import { } from "react-bootstrap"; import { Column } from "react-table"; import { - AsyncButton, BaseModal, BaseModalProps, LanguageText, @@ -33,8 +32,10 @@ import { PageTable, useModalPayload, } from ".."; +import { dispatchTask } from "../../@modules/task"; +import { createTask } from "../../@modules/task/utilites"; import { ProvidersApi } from "../../apis"; -import { isMovie } from "../../utilites"; +import { GetItemId, isMovie } from "../../utilites"; import "./msmStyle.scss"; type SupportType = Item.Movie | Item.Episode; @@ -45,20 +46,19 @@ enum SearchState { Finished, } -interface Props { - onSelect: (item: SupportType, result: SearchResultType) => Promise; - onDownload?: () => void; +interface Props { + download: (item: T, result: SearchResultType) => Promise; } -export const ManualSearchModal: FunctionComponent = ( - props -) => { - const { onSelect, onDownload, ...modal } = props; +export function ManualSearchModal( + props: Props & BaseModalProps +) { + const { download, ...modal } = props; const [result, setResult] = useState([]); const [searchState, setSearchState] = useState(SearchState.Ready); - const item = useModalPayload(modal.modalKey); + const item = useModalPayload(modal.modalKey); const search = useCallback(async () => { if (item) { @@ -186,23 +186,32 @@ export const ManualSearchModal: FunctionComponent = ( }, { accessor: "subtitle", - Cell: ({ row, externalUpdate }) => { + Cell: ({ row }) => { const result = row.original; return ( - onSelect(item!, result)} - onSuccess={() => externalUpdate && externalUpdate(row)} + disabled={item === null} + onClick={() => { + if (!item) return; + + const id = GetItemId(item); + const task = createTask(item.title, id, download, item, result); + dispatchTask( + "Downloading subtitles...", + [task], + "Downloading..." + ); + }} > - + ); }, }, ], - [onSelect, item] + [download, item] ); const content = useMemo(() => { @@ -225,12 +234,11 @@ export const ManualSearchModal: FunctionComponent = ( emptyText="No Result" columns={columns} data={result} - externalUpdate={onDownload} > ); } - }, [searchState, columns, result, search, item?.path, onDownload]); + }, [searchState, columns, result, search, item?.path]); const footer = useMemo( () => ( @@ -271,7 +279,7 @@ export const ManualSearchModal: FunctionComponent = ( {content} ); -}; +} const StateIcon: FunctionComponent<{ matches: string[]; dont: string[] }> = ({ matches, diff --git a/frontend/src/utilites/index.ts b/frontend/src/utilites/index.ts index 3689bdfc1..ddd009684 100644 --- a/frontend/src/utilites/index.ts +++ b/frontend/src/utilites/index.ts @@ -40,7 +40,7 @@ export function submodProcessColor(s: string) { return `color(name=${s})`; } -export function GetItemId(item: any): number { +export function GetItemId(item: T): number { if (isMovie(item)) { return item.radarrId; } else if (isEpisode(item)) { -- cgit v1.2.3