Skip to content

Commit

Permalink
Added mass delete subtitle files (#2816)
Browse files Browse the repository at this point in the history
  • Loading branch information
phyzical authored Jan 10, 2025
1 parent 6a791b2 commit f146d97
Showing 1 changed file with 89 additions and 7 deletions.
96 changes: 89 additions & 7 deletions frontend/src/components/modals/SubtitleToolsModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,51 @@ import {
Text,
} from "@mantine/core";
import { ColumnDef } from "@tanstack/react-table";
import {
useEpisodeSubtitleModification,
useMovieSubtitleModification,
} from "@/apis/hooks";
import Language from "@/components/bazarr/Language";
import SubtitleToolsMenu from "@/components/SubtitleToolsMenu";
import SimpleTable from "@/components/tables/SimpleTable";
import { withModal } from "@/modules/modals";
import { isMovie } from "@/utilities";
import { useModals, withModal } from "@/modules/modals";
import { task, TaskGroup } from "@/modules/task";
import { fromPython, isMovie, toPython } from "@/utilities";

type SupportType = Item.Episode | Item.Movie;

type TableColumnType = FormType.ModifySubtitle & {
raw_language: Language.Info;
seriesId: number;
name: string;
isMovie: boolean;
};

type LocalisedType = {
id: number;
seriesId: number;
type: "movie" | "episode";
name: string;
isMovie: boolean;
};

function getIdAndType(item: SupportType): [number, "episode" | "movie"] {
function getLocalisedValues(item: SupportType): LocalisedType {
if (isMovie(item)) {
return [item.radarrId, "movie"];
return {
seriesId: 0,
id: item.radarrId,
type: "movie",
name: item.title,
isMovie: true,
};
} else {
return [item.sonarrEpisodeId, "episode"];
return {
seriesId: item.sonarrSeriesId,
id: item.sonarrEpisodeId,
type: "episode",
name: item.title,
isMovie: false,
};
}
}

Expand All @@ -41,6 +69,11 @@ const SubtitleToolView: FunctionComponent<SubtitleToolViewProps> = ({
payload,
}) => {
const [selections, setSelections] = useState<TableColumnType[]>([]);
const { remove: removeEpisode, download: downloadEpisode } =
useEpisodeSubtitleModification();
const { download: downloadMovie, remove: removeMovie } =
useMovieSubtitleModification();
const modals = useModals();

const columns = useMemo<ColumnDef<TableColumnType>[]>(
() => [
Expand Down Expand Up @@ -109,17 +142,22 @@ const SubtitleToolView: FunctionComponent<SubtitleToolViewProps> = ({
const data = useMemo<TableColumnType[]>(
() =>
payload.flatMap((item) => {
const [id, type] = getIdAndType(item);
const { seriesId, id, type, name, isMovie } = getLocalisedValues(item);
return item.subtitles.flatMap((v) => {
if (v.path) {
return [
{
id,
seriesId,
type,
language: v.code2,
path: v.path,
// eslint-disable-next-line camelcase
raw_language: v,
name,
hi: toPython(v.forced),
forced: toPython(v.hi),
isMovie,
},
];
} else {
Expand All @@ -143,7 +181,51 @@ const SubtitleToolView: FunctionComponent<SubtitleToolViewProps> = ({
></SimpleTable>
<Divider></Divider>
<Group>
<SubtitleToolsMenu selections={selections}>
<SubtitleToolsMenu
selections={selections}
onAction={(action) => {
selections.forEach((selection) => {
const actionPayload = {
form: {
language: selection.language,
hi: fromPython(selection.hi),
forced: fromPython(selection.forced),
path: selection.path,
},
radarrId: 0,
seriesId: 0,
episodeId: 0,
};
if (selection.isMovie) {
actionPayload.radarrId = selection.id;
} else {
actionPayload.seriesId = selection.seriesId;
actionPayload.episodeId = selection.id;
}
const download = selection.isMovie
? downloadMovie
: downloadEpisode;
const remove = selection.isMovie ? removeMovie : removeEpisode;

if (action === "search") {
task.create(
selection.name,
TaskGroup.SearchSubtitle,
download.mutateAsync,
actionPayload,
);
} else if (action === "delete" && selection.path) {
task.create(
selection.name,
TaskGroup.DeleteSubtitle,
remove.mutateAsync,
actionPayload,
);
}
});
modals.closeAll();
}}
>
<Button disabled={selections.length === 0} variant="light">
Select Action
</Button>
Expand Down

0 comments on commit f146d97

Please sign in to comment.