import { Action, FileBrowser, SimpleTable } from "@/components"; import { useArrayAction } from "@/utilities"; import { faArrowCircleRight, faTrash } from "@fortawesome/free-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { Button } from "@mantine/core"; import { capitalize } from "lodash"; import { FunctionComponent, useCallback, useMemo } from "react"; import { Column } from "react-table"; import { moviesEnabledKey, pathMappingsKey, pathMappingsMovieKey, seriesEnabledKey, } from "../keys"; import { useFormActions } from "../utilities/FormValues"; import { useSettingValue } from "../utilities/hooks"; import { Message } from "./Message"; 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 = ({ type }) => { const key = getSupportKey(type); const items = useSettingValue<[string, string][]>(key); const enabledKey = getEnabledKey(type); const enabled = useSettingValue(enabledKey, { original: true }); const { setValue } = useFormActions(); const updateRow = useCallback( (newItems: PathMappingItem[]) => { setValue( newItems.map((v) => [v.from, v.to]), key ); }, [key, setValue] ); const addRow = useCallback(() => { if (items) { const newItems = [...items, ["", ""]]; setValue(newItems, key); } }, [items, key, setValue]); const data = useMemo( () => items?.map((v) => ({ from: v[0], to: v[1] })) ?? [], [items] ); const action = useArrayAction((fn) => { updateRow(fn(data)); }); const columns = useMemo[]>( () => [ { Header: capitalize(type), accessor: "from", Cell: ({ value, row: { original, index } }) => { return ( { action.mutate(index, { ...original, from: path }); }} > ); }, }, { id: "arrow", Cell: () => ( ), }, { Header: "Bazarr", accessor: "to", Cell: ({ value, row: { original, index } }) => { return ( { action.mutate(index, { ...original, to: path }); }} > ); }, }, { id: "action", accessor: "to", Cell: ({ row: { index } }) => { return ( action.remove(index)} > ); }, }, ], [action, type] ); if (enabled) { return ( <> ); } else { return ( Path Mappings will be available after staged changes are saved ); } };