import {useForm, useFormContext} from 'react-hook-form'; import {useEffect} from 'react'; import {TuneIcon} from '../../../../icons/material/Tune'; import {Button} from '../../../../ui/buttons/button'; import {CssTheme} from '../../../../ui/themes/css-theme'; import {FormTextField} from '../../../../ui/forms/input-field/text-field/text-field'; import {FormSwitch} from '../../../../ui/forms/toggle/switch'; import {AppearanceValues} from '../../appearance-store'; import {DialogTrigger} from '../../../../ui/overlays/dialog/dialog-trigger'; import {DialogFooter} from '../../../../ui/overlays/dialog/dialog-footer'; import {useDialogContext} from '../../../../ui/overlays/dialog/dialog-context'; import {Dialog} from '../../../../ui/overlays/dialog/dialog'; import {DialogHeader} from '../../../../ui/overlays/dialog/dialog-header'; import {DialogBody} from '../../../../ui/overlays/dialog/dialog-body'; import {Trans} from '../../../../i18n/trans'; import {Form} from '../../../../ui/forms/form'; import {useParams} from 'react-router-dom'; export function ThemeSettingsDialogTrigger() { const {getValues, setValue} = useFormContext(); const {themeIndex} = useParams(); const theme = getValues(`appearance.themes.all.${+themeIndex!}`); return ( { if (!value) return; getValues('appearance.themes.all').forEach((currentTheme, index) => { // update changed theme if (currentTheme.id === value.id) { setValue(`appearance.themes.all.${index}`, value, { shouldDirty: true, }); return; } // unset "default_light" and "default_dark" on other themes if (value.default_light) { setValue( `appearance.themes.all.${index}`, {...currentTheme, default_light: false}, {shouldDirty: true} ); return; } if (value.default_dark) { setValue( `appearance.themes.all.${index}`, {...currentTheme, default_dark: false}, {shouldDirty: true} ); return; } }); }} > ); } interface SettingsDialogProps { theme: CssTheme; } function SettingsDialog({theme}: SettingsDialogProps) { const form = useForm({defaultValues: theme}); const {close, formId} = useDialogContext(); useEffect(() => { const subscription = form.watch((value, {name}) => { // theme can only be set as either light or dark default if (name === 'default_light' && value.default_light) { form.setValue('default_dark', false); } if (name === 'default_dark' && value.default_dark) { form.setValue('default_light', false); } }); return () => subscription.unsubscribe(); }, [form]); return (
{ close(values); }} > } className="mb-30" autoFocus /> } > } > } >
); }