bazarr/frontend/src/pages/Settings/components/pathMapper.tsx

157 lines
3.7 KiB
TypeScript
Raw Normal View History

2022-05-31 15:49:04 +00:00
import { Action, FileBrowser, SimpleTable } from "@/components";
import { useArrayAction } from "@/utilities";
2021-03-25 14:22:43 +00:00
import { faArrowCircleRight, faTrash } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
2022-05-31 15:49:04 +00:00
import { Button } from "@mantine/core";
import { capitalize } from "lodash";
import { FunctionComponent, useCallback, useMemo } from "react";
import { Column } from "react-table";
2021-03-25 14:22:43 +00:00
import {
moviesEnabledKey,
pathMappingsKey,
pathMappingsMovieKey,
seriesEnabledKey,
} from "../keys";
2022-05-31 15:49:04 +00:00
import { useFormActions } from "../utilities/FormValues";
import { useSettingValue } from "../utilities/hooks";
2022-05-31 15:49:04 +00:00
import { Message } from "./Message";
2021-03-25 14:22:43 +00:00
type SupportType = "sonarr" | "radarr";
function getSupportKey(type: SupportType) {
if (type === "sonarr") {
return pathMappingsKey;
} else {
return pathMappingsMovieKey;
}
}
function getEnabledKey(type: SupportType) {
if (type === "sonarr") {
return seriesEnabledKey;
} else {
return moviesEnabledKey;
}
}
interface PathMappingItem {
from: string;
to: string;
}
interface TableProps {
type: SupportType;
}
export const PathMappingTable: FunctionComponent<TableProps> = ({ type }) => {
const key = getSupportKey(type);
2022-05-31 15:49:04 +00:00
const items = useSettingValue<[string, string][]>(key);
2021-03-25 14:22:43 +00:00
const enabledKey = getEnabledKey(type);
const enabled = useSettingValue<boolean>(enabledKey, { original: true });
2022-05-31 15:49:04 +00:00
const { setValue } = useFormActions();
2021-03-25 14:22:43 +00:00
const updateRow = useCallback(
(newItems: PathMappingItem[]) => {
2022-05-31 15:49:04 +00:00
setValue(
2021-03-25 14:22:43 +00:00
newItems.map((v) => [v.from, v.to]),
key
);
},
2022-05-31 15:49:04 +00:00
[key, setValue]
2021-03-25 14:22:43 +00:00
);
const addRow = useCallback(() => {
if (items) {
const newItems = [...items, ["", ""]];
2022-05-31 15:49:04 +00:00
setValue(newItems, key);
2021-03-25 14:22:43 +00:00
}
2022-05-31 15:49:04 +00:00
}, [items, key, setValue]);
2021-03-25 14:22:43 +00:00
const data = useMemo<PathMappingItem[]>(
() => items?.map((v) => ({ from: v[0], to: v[1] })) ?? [],
[items]
);
2022-05-31 15:49:04 +00:00
const action = useArrayAction<PathMappingItem>((fn) => {
updateRow(fn(data));
});
2021-03-25 14:22:43 +00:00
const columns = useMemo<Column<PathMappingItem>[]>(
() => [
{
Header: capitalize(type),
accessor: "from",
2022-05-31 15:49:04 +00:00
Cell: ({ value, row: { original, index } }) => {
return (
<FileBrowser
type={type}
defaultValue={value}
onChange={(path) => {
2022-05-31 15:49:04 +00:00
action.mutate(index, { ...original, from: path });
}}
></FileBrowser>
);
},
2021-03-25 14:22:43 +00:00
},
{
id: "arrow",
Cell: () => (
<FontAwesomeIcon icon={faArrowCircleRight}></FontAwesomeIcon>
),
},
{
Header: "Bazarr",
accessor: "to",
2022-05-31 15:49:04 +00:00
Cell: ({ value, row: { original, index } }) => {
return (
<FileBrowser
defaultValue={value}
type="bazarr"
onChange={(path) => {
2022-05-31 15:49:04 +00:00
action.mutate(index, { ...original, to: path });
}}
></FileBrowser>
);
},
2021-03-25 14:22:43 +00:00
},
{
id: "action",
accessor: "to",
2022-05-31 15:49:04 +00:00
Cell: ({ row: { index } }) => {
return (
2022-05-31 15:49:04 +00:00
<Action
2022-06-01 17:08:50 +00:00
label="Remove"
icon={faTrash}
2022-05-31 15:49:04 +00:00
onClick={() => action.remove(index)}
></Action>
);
},
2021-03-25 14:22:43 +00:00
},
],
2022-05-31 15:49:04 +00:00
[action, type]
2021-03-25 14:22:43 +00:00
);
if (enabled) {
return (
2022-05-31 15:49:04 +00:00
<>
2021-03-25 14:22:43 +00:00
<SimpleTable
2022-05-31 15:49:04 +00:00
tableStyles={{ emptyText: "No mapping" }}
2021-03-25 14:22:43 +00:00
columns={columns}
data={data}
></SimpleTable>
2022-05-31 15:49:04 +00:00
<Button fullWidth color="light" onClick={addRow}>
2021-03-25 14:22:43 +00:00
Add
</Button>
2022-05-31 15:49:04 +00:00
</>
2021-03-25 14:22:43 +00:00
);
} else {
return (
<Message>
Path Mappings will be available after staged changes are saved
</Message>
);
}
};