summaryrefslogtreecommitdiffhomepage
path: root/frontend/src/components/modals/SeriesUploadModal.tsx
blob: e91e7fb04663ef90c2dfe72c47bd1fc40e049937 (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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import {
  faCheck,
  faCircleNotch,
  faInfoCircle,
  faTrash,
} from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import React, {
  FunctionComponent,
  useCallback,
  useEffect,
  useMemo,
  useState,
} from "react";
import { Button, Container, Form } from "react-bootstrap";
import { Column, TableUpdater } from "react-table";
import { FileForm, LanguageSelector, MessageIcon, SimpleTable } from "..";
import { dispatchTask } from "../../@modules/task";
import { createTask } from "../../@modules/task/utilities";
import { useProfileBy, useProfileItemsToLanguages } from "../../@redux/hooks";
import { EpisodesApi, SubtitlesApi } from "../../apis";
import { Selector } from "../inputs";
import BaseModal, { BaseModalProps } from "./BaseModal";
import { useModalInformation } from "./hooks";

interface PendingSubtitle {
  file: File;
  fetching: boolean;
  instance?: Item.Episode;
}

type EpisodeMap = {
  [name: string]: Item.Episode;
};

interface SerieProps {
  episodes: readonly Item.Episode[];
}

export const TaskGroupName = "Uploading Subtitles...";

const SeriesUploadModal: FunctionComponent<SerieProps & BaseModalProps> = ({
  episodes,
  ...modal
}) => {
  const { payload, closeModal } = useModalInformation<Item.Series>(
    modal.modalKey
  );

  const [pending, setPending] = useState<PendingSubtitle[]>([]);

  const profile = useProfileBy(payload?.profileId);

  const avaliableLanguages = useProfileItemsToLanguages(profile);

  const [language, setLanguage] = useState<Language.Info | null>(null);

  useEffect(() => {
    if (avaliableLanguages.length > 0) {
      setLanguage(avaliableLanguages[0]);
    }
  }, [avaliableLanguages]);

  const filelist = useMemo(() => pending.map((v) => v.file), [pending]);

  const checkEpisodes = useCallback(
    async (list: PendingSubtitle[]) => {
      const names = list.map((v) => v.file.name);

      if (names.length > 0) {
        const results = await SubtitlesApi.info(names);

        const episodeMap = results.reduce<EpisodeMap>((prev, curr) => {
          const ep = episodes.find(
            (v) => v.season === curr.season && v.episode === curr.episode
          );
          if (ep) {
            prev[curr.filename] = ep;
          }
          return prev;
        }, {});

        setPending((pd) =>
          pd.map((v) => {
            const instance = episodeMap[v.file.name];
            return {
              ...v,
              instance,
              fetching: false,
            };
          })
        );
      }
    },
    [episodes]
  );

  const setFiles = useCallback(
    (files: File[]) => {
      // At lease 1 language is required
      const list: PendingSubtitle[] = files.map((f) => {
        return {
          file: f,
          didCheck: false,
          fetching: true,
        };
      });
      setPending(list);
      checkEpisodes(list);
    },
    [checkEpisodes]
  );

  const upload = useCallback(() => {
    if (payload === null || language === null) {
      return;
    }

    const { sonarrSeriesId: seriesid } = payload;
    const { code2, hi, forced } = language;

    const tasks = pending
      .filter((v) => v.instance !== undefined)
      .map((v) => {
        const { sonarrEpisodeId: episodeid } = v.instance!;

        const form: FormType.UploadSubtitle = {
          file: v.file,
          language: code2,
          hi: hi ?? false,
          forced: forced ?? false,
        };

        return createTask(
          v.file.name,
          episodeid,
          EpisodesApi.uploadSubtitles.bind(EpisodesApi),
          seriesid,
          episodeid,
          form
        );
      });

    dispatchTask(TaskGroupName, tasks, "Uploading subtitles...");
    setFiles([]);
    closeModal();
  }, [payload, pending, language, closeModal, setFiles]);

  const canUpload = useMemo(
    () =>
      pending.length > 0 &&
      pending.every((v) => v.instance !== undefined) &&
      language,
    [pending, language]
  );

  const showTable = pending.length > 0;

  const columns = useMemo<Column<PendingSubtitle>[]>(
    () => [
      {
        id: "Icon",
        accessor: "fetching",
        className: "text-center",
        Cell: ({ value: fetching, row: { original } }) => {
          let icon = faCircleNotch;
          let color: string | undefined = undefined;
          let spin = false;
          let msgs: string[] = [];

          const override = useMemo(
            () =>
              original.instance?.subtitles.find(
                (v) => v.code2 === language?.code2
              ) !== undefined,
            [original.instance?.subtitles]
          );

          if (fetching) {
            spin = true;
          } else if (override) {
            icon = faInfoCircle;
            color = "var(--warning)";
            msgs.push("Overwrite existing subtitle");
          } else if (original.instance) {
            icon = faCheck;
            color = "var(--success)";
          } else {
            icon = faInfoCircle;
            color = "var(--warning)";
            msgs.push("Season or episode info is missing");
          }

          return (
            <MessageIcon
              messages={msgs}
              color={color}
              icon={icon}
              spin={spin}
            ></MessageIcon>
          );
        },
      },
      {
        Header: "File",
        accessor: (d) => d.file.name,
      },
      {
        Header: "Episode",
        accessor: "instance",
        className: "vw-1",
        Cell: ({ value, row, update }) => {
          const options = episodes.map<SelectorOption<Item.Episode>>((ep) => ({
            label: `(${ep.season}x${ep.episode}) ${ep.title}`,
            value: ep,
          }));

          const change = useCallback(
            (ep: Nullable<Item.Episode>) => {
              if (ep) {
                const newInfo = { ...row.original };
                newInfo.instance = ep;
                update && update(row, newInfo);
              }
            },
            [row, update]
          );

          return (
            <Selector
              options={options}
              value={value ?? null}
              onChange={change}
            ></Selector>
          );
        },
      },
      {
        accessor: "file",
        Cell: ({ row, update }) => {
          return (
            <Button
              size="sm"
              variant="light"
              onClick={() => {
                update && update(row);
              }}
            >
              <FontAwesomeIcon icon={faTrash}></FontAwesomeIcon>
            </Button>
          );
        },
      },
    ],
    [language?.code2, episodes]
  );

  const updateItem = useCallback<TableUpdater<PendingSubtitle>>(
    (row, info?: PendingSubtitle) => {
      setPending((pd) => {
        const newPending = [...pd];
        if (info) {
          newPending[row.index] = info;
        } else {
          newPending.splice(row.index, 1);
        }
        return newPending;
      });
    },
    []
  );

  const footer = (
    <div className="d-flex flex-row flex-grow-1 justify-content-between">
      <div className="w-25">
        <LanguageSelector
          options={avaliableLanguages}
          value={language}
          onChange={(l) => {
            if (l) {
              setLanguage(l);
            }
          }}
        ></LanguageSelector>
      </div>
      <div>
        <Button
          disabled={pending.length === 0}
          variant="outline-secondary"
          className="mr-2"
          onClick={() => setFiles([])}
        >
          Clean
        </Button>
        <Button disabled={!canUpload} onClick={upload}>
          Upload
        </Button>
      </div>
    </div>
  );

  return (
    <BaseModal size="lg" title="Upload Subtitles" footer={footer} {...modal}>
      <Container fluid className="flex-column">
        <Form>
          <Form.Group>
            <FileForm
              emptyText="Select..."
              disabled={showTable || avaliableLanguages.length === 0}
              multiple
              value={filelist}
              onChange={setFiles}
            ></FileForm>
          </Form.Group>
        </Form>
        <div hidden={!showTable}>
          <SimpleTable
            columns={columns}
            data={pending}
            responsive={false}
            update={updateItem}
          ></SimpleTable>
        </div>
      </Container>
    </BaseModal>
  );
};

export default SeriesUploadModal;