bazarr/frontend/src/Movies/Detail/table.tsx

132 lines
3.8 KiB
TypeScript
Raw Normal View History

2021-03-25 14:22:43 +00:00
import { faSearch, faTrash } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { intersectionWith } from "lodash";
2021-03-25 14:22:43 +00:00
import React, { FunctionComponent, useMemo } from "react";
import { Badge } from "react-bootstrap";
import { Column } from "react-table";
import { useProfileItems } from "../../@redux/hooks";
import { useShowOnlyDesired } from "../../@redux/hooks/site";
2021-03-25 14:22:43 +00:00
import { MoviesApi } from "../../apis";
import { AsyncButton, LanguageText, SimpleTable } from "../../components";
const missingText = "Missing Subtitles";
interface Props {
movie: Item.Movie;
profile?: Profile.Languages;
2021-03-25 14:22:43 +00:00
}
const Table: FunctionComponent<Props> = ({ movie, profile }) => {
const onlyDesired = useShowOnlyDesired();
const profileItems = useProfileItems(profile);
2021-03-25 14:22:43 +00:00
const columns: Column<Subtitle>[] = useMemo<Column<Subtitle>[]>(
() => [
{
Header: "Subtitle Path",
accessor: "path",
Cell: (row) => {
if (row.value === null || row.value.length === 0) {
return "Video File Subtitle Track";
} else if (row.value === missingText) {
return <span className="text-muted">{row.value}</span>;
} else {
return row.value;
}
},
},
{
Header: "Language",
accessor: "name",
Cell: ({ row }) => {
if (row.original.path === missingText) {
return (
<Badge variant="primary">
<LanguageText text={row.original} long></LanguageText>
</Badge>
);
} else {
return (
<Badge variant="secondary">
<LanguageText text={row.original} long></LanguageText>
</Badge>
);
}
},
},
{
accessor: "code2",
Cell: (row) => {
const { original } = row.row;
if (original.path === null || original.path.length === 0) {
return null;
} else if (original.path === missingText) {
return (
<AsyncButton
promise={() =>
MoviesApi.downloadSubtitles(movie.radarrId, {
language: original.code2,
hi: original.hi,
forced: original.forced,
})
}
variant="light"
size="sm"
>
<FontAwesomeIcon icon={faSearch}></FontAwesomeIcon>
</AsyncButton>
);
} else {
return (
<AsyncButton
variant="light"
size="sm"
promise={() =>
MoviesApi.deleteSubtitles(movie.radarrId, {
language: original.code2,
hi: original.hi,
forced: original.forced,
path: original.path ?? "",
})
}
>
<FontAwesomeIcon icon={faTrash}></FontAwesomeIcon>
</AsyncButton>
);
}
},
},
],
[movie]
2021-03-25 14:22:43 +00:00
);
const data: Subtitle[] = useMemo(() => {
const missing = movie.missing_subtitles.map((item) => {
item.path = missingText;
return item;
});
let raw_subtitles = movie.subtitles;
if (onlyDesired) {
raw_subtitles = intersectionWith(
raw_subtitles,
profileItems,
(l, r) => l.code2 === r.code2
);
}
return [...raw_subtitles, ...missing];
}, [movie.missing_subtitles, movie.subtitles, onlyDesired, profileItems]);
2021-03-25 14:22:43 +00:00
return (
<SimpleTable
columns={columns}
data={data}
emptyText="No Subtitles Found For This Movie"
></SimpleTable>
);
};
export default Table;