/* * Copyright (C) Contributors to the Suwayomi project * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ import { Button, Dialog, DialogTitle, ListItemButton, ListItemText, Stack, Tooltip } from '@mui/material'; import { useEffect, useState } from 'react'; import DialogContent from '@mui/material/DialogContent'; import DialogActions from '@mui/material/DialogActions'; import { useTranslation } from 'react-i18next'; import List from '@mui/material/List'; import DeleteIcon from '@mui/icons-material/Delete'; import IconButton from '@mui/material/IconButton'; import DialogContentText from '@mui/material/DialogContentText'; import { TextSetting, TextSettingProps } from '@/components/settings/text/TextSetting.tsx'; import { TextSettingDialog } from '@/components/settings/text/TextSettingDialog.tsx'; import { makeToast } from '@/components/util/Toast.tsx'; const MutableListItem = ({ handleDelete, ...textSettingProps }: Omit & { handleDelete: () => void }) => { const { t } = useTranslation(); return ( ); }; type MutableListSettingProps = Pick & { values?: string[]; description?: string; addItemButtonTitle?: string; handleChange: (values: string[]) => void; allowDuplicates?: boolean; validateItem?: (value: string) => boolean; invalidItemError?: string; }; export const MutableListSetting = ({ settingName, description, values, handleChange, addItemButtonTitle, placeholder, allowDuplicates = false, validateItem = () => true, invalidItemError, }: MutableListSettingProps) => { const { t } = useTranslation(); const [isDialogOpen, setIsDialogOpen] = useState(false); const [dialogValues, setDialogValues] = useState(values ?? []); const [isAddItemDialogOpen, setIsAddItemDialogOpen] = useState(false); useEffect(() => { if (!values) { return; } setDialogValues(values); }, [values]); const closeDialog = (resetValue: boolean = true) => { if (resetValue) { setDialogValues(values ?? []); } setIsDialogOpen(false); }; const updateSetting = (index: number, newValue: string | undefined) => { const deleteValue = newValue === undefined; if (deleteValue) { setDialogValues(dialogValues.toSpliced(index, 1)); return; } const isDuplicate = !allowDuplicates && dialogValues.includes(newValue); if (isDuplicate) { return; } if (newValue === '') { return; } if (!validateItem(newValue)) { makeToast(invalidItemError ?? t('global.error.label.invalid_input'), 'error'); return; } setDialogValues(dialogValues.toSpliced(index, 1, newValue.trim())); }; const saveChanges = () => { closeDialog(true); handleChange(dialogValues.filter((dialogValue) => dialogValue !== '')); }; return ( <> setIsDialogOpen(true)}> closeDialog()} fullWidth> {settingName} {!!description && ( {description} )} {dialogValues.map((dialogValue, index) => ( updateSetting(index, newValue)} handleDelete={() => updateSetting(index, undefined)} value={dialogValue} /> ))} {isAddItemDialogOpen && ( updateSetting(dialogValues.length, newValue)} isDialogOpen={isAddItemDialogOpen} setIsDialogOpen={setIsAddItemDialogOpen} /> )} ); };