import AddIcon from '@mui/icons-material/Add' import CloseIcon from '@mui/icons-material/Close' import { Alert, AppBar, Backdrop, Box, Button, Dialog, Grid, IconButton, Paper, Slide, TextField, Toolbar, Typography } from '@mui/material' import { TransitionProps } from '@mui/material/transitions' import { matchW } from 'fp-ts/lib/Either' import { pipe } from 'fp-ts/lib/function' import { useAtomValue } from 'jotai' import { forwardRef, useEffect, useState, useTransition } from 'react' import { serverURL } from '../atoms/settings' import { useToast } from '../hooks/toast' import { useI18n } from '../hooks/useI18n' import { ffetch } from '../lib/httpClient' import { CustomTemplate } from '../types' import TemplateTextField from './TemplateTextField' const Transition = forwardRef(function Transition( props: TransitionProps & { children: React.ReactElement }, ref: React.Ref, ) { return }) interface Props extends React.HTMLAttributes { open: boolean onClose: () => void } const TemplatesEditor: React.FC = ({ open, onClose }) => { const [templateName, setTemplateName] = useState('') const [templateContent, setTemplateContent] = useState('') const serverAddr = useAtomValue(serverURL) const [isPending, startTransition] = useTransition() const [templates, setTemplates] = useState([]) const { i18n } = useI18n() const { pushMessage } = useToast() useEffect(() => { if (open) { fetchTemplates() } }, [open]) const fetchTemplates = async () => { const task = ffetch(`${serverAddr}/api/v1/template/all`) const either = await task() pipe( either, matchW( (l) => pushMessage(l), (r) => setTemplates(r) ) ) } const addTemplate = async () => { const task = ffetch(`${serverAddr}/api/v1/template`, { method: 'POST', body: JSON.stringify({ name: templateName, content: templateContent, }) }) const either = await task() pipe( either, matchW( (l) => pushMessage(l, 'warning'), () => { pushMessage('Added template') fetchTemplates() setTemplateName('') setTemplateContent('') } ) ) } const updateTemplate = async (template: CustomTemplate) => { const task = ffetch(`${serverAddr}/api/v1/template`, { method: 'PATCH', body: JSON.stringify(template) }) const either = await task() pipe( either, matchW( (l) => pushMessage(l, 'warning'), (r) => { pushMessage(`Updated template ${r.name}`) fetchTemplates() } ) ) } const deleteTemplate = async (id: string) => { const task = ffetch(`${serverAddr}/api/v1/template/${id}`, { method: 'DELETE', }) const either = await task() pipe( either, matchW( (l) => pushMessage(l, 'warning'), () => { pushMessage('Deleted template') fetchTemplates() } ) ) } return ( theme.zIndex.drawer + 1 }} open={isPending} /> {i18n.t('templatesEditor')} theme.palette.background.default, minHeight: (theme) => `calc(99vh - ${theme.mixins.toolbar.minHeight}px)` }}> {i18n.t('templatesReloadInfo')} setTemplateName(e.currentTarget.value)} value={templateName} /> setTemplateContent(e.currentTarget.value)} value={templateContent} InputProps={{ endAdornment: }} /> {templates.map(template => ( ))} ) } export default TemplatesEditor