import { faTrash } from "@fortawesome/free-solid-svg-icons"; import React, { FunctionComponent, useCallback, useEffect, useMemo, useState, } from "react"; import { Button, Form } from "react-bootstrap"; import { Column, TableUpdater } from "react-table"; import { useEnabledLanguagesContext } from "."; import { ActionButton, BaseModal, BaseModalProps, Chips, LanguageSelector, Selector, SimpleTable, useModalInformation, } from "../../components"; import { BuildKey } from "../../utilities"; import { Input, Message } from "../components"; import { cutoffOptions } from "./options"; interface Props { update: (profile: Language.Profile) => void; } function createDefaultProfile(): Language.Profile { return { profileId: -1, name: "", items: [], cutoff: null, mustContain: [], mustNotContain: [], }; } const LanguagesProfileModal: FunctionComponent = ( props ) => { const { update, ...modal } = props; const { payload: profile, closeModal } = useModalInformation(modal.modalKey); const languages = useEnabledLanguagesContext(); const [current, setProfile] = useState(createDefaultProfile); useEffect(() => { if (profile) { setProfile(profile); } else { setProfile(createDefaultProfile); } }, [profile]); const cutoff: SelectorOption[] = useMemo(() => { const options = [...cutoffOptions]; const newOptions = current.items.map>((v) => ({ label: `ID ${v.id} (${v.language})`, value: v.id, })); options.push(...newOptions); return options; }, [current.items]); const updateProfile = useCallback( (key: K, value: Language.Profile[K]) => { const newProfile = { ...current }; newProfile[key] = value; setProfile(newProfile); }, [current] ); const updateRow = useCallback>( (row, item: Language.ProfileItem) => { const list = [...current.items]; if (item) { list[row.index] = item; } else { list.splice(row.index, 1); } updateProfile("items", list); }, [current.items, updateProfile] ); const addItem = useCallback(() => { const id = 1 + current.items.reduce((val, item) => Math.max(item.id, val), 0); if (languages.length > 0) { const language = languages[0].code2; const item: Language.ProfileItem = { id, language, audio_exclude: "False", hi: "False", forced: "False", }; const list = [...current.items]; list.push(item); updateProfile("items", list); } }, [current.items, updateProfile, languages]); const canSave = current.name.length > 0 && current.items.length > 0; const footer = ( ); const columns = useMemo[]>( () => [ { Header: "ID", accessor: "id", }, { Header: "Language", accessor: "language", Cell: ({ value, row, update }) => { const code = value; const item = row.original; const lang = useMemo( () => languages.find((l) => l.code2 === code) ?? null, [code] ); return (
{ if (l) { item.language = l.code2; update && update(row, item); } }} >
); }, }, { Header: "Forced", accessor: "forced", Cell: ({ row, value, update }) => { const item = row.original; return ( { item.forced = v.target.checked ? "True" : "False"; update && update(row, item); }} > ); }, }, { Header: "HI", accessor: "hi", Cell: ({ row, value, update }) => { const item = row.original; return ( { item.hi = v.target.checked ? "True" : "False"; update && update(row, item); }} > ); }, }, { Header: "Exclude Audio", accessor: "audio_exclude", Cell: ({ row, value, update }) => { const item = row.original; return ( { item.audio_exclude = v.target.checked ? "True" : "False"; update && update(row, item); }} > ); }, }, { id: "action", accessor: "id", Cell: ({ row, update }) => { return ( update && update(row)} > ); }, }, ], [languages] ); return ( { updateProfile("name", v.target.value); }} > updateProfile("cutoff", num)} > Ignore others if existing updateProfile("mustContain", mc)} > Subtitles release info must include one of those words or they will be excluded from search results (regex supported). { updateProfile("mustNotContain", mnc); }} > Subtitles release info including one of those words (case insensitive) will be excluded from search results (regex supported). ); }; export default LanguagesProfileModal;