import { ActionIcon, Center, createStyles, Flex, Text, useMantineTheme } from '@mantine/core'; import { useDisclosure } from '@mantine/hooks'; import { showNotification } from '@mantine/notifications'; import { IconCheck, IconCopy, IconDownload, IconTrash, IconX } from '@tabler/icons'; import { useMutation } from '@tanstack/react-query'; import fileDownload from 'js-file-download'; import { useTranslation } from 'next-i18next'; import { useConfigContext } from '../../../../config/provider'; import Tip from '../../../layout/Tip'; import { CreateConfigCopyModal } from './CreateCopyModal'; export default function ConfigActions() { const { t } = useTranslation(['settings/general/config-changer', 'settings/common']); const [createCopyModalOpened, createCopyModal] = useDisclosure(false); const { config } = useConfigContext(); const { mutateAsync } = useDeleteConfigMutation(config?.configProperties.name ?? 'default'); if (!config) return null; const handleDownload = () => { // TODO: remove secrets fileDownload(JSON.stringify(config, null, '\t'), `${config?.configProperties.name}.json`); }; const handleDeletion = async () => { await mutateAsync(); }; const { classes } = useStyles(); const { colors } = useMantineTheme(); return ( <> {t('buttons.download')} {t('buttons.delete.text')} {t('buttons.saveCopy')}
{t('settings/common:tips.configTip')}
); } const useStyles = createStyles(() => ({ actionIcon: { width: 'auto', height: 'auto', maxWidth: 'auto', maxHeight: 'auto', flexGrow: 1, display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', textAlign: 'center', rowGap: 10, padding: 10, }, })); const useDeleteConfigMutation = (configName: string) => { const { t } = useTranslation(['settings/general/config-changer']); return useMutation({ mutationKey: ['config/delete', { configName }], mutationFn: () => fetchDeletion(configName), onSuccess() { showNotification({ title: t('buttons.delete.notifications.deleted.title'), icon: , color: 'green', autoClose: 1500, radius: 'md', message: t('buttons.delete.notifications.deleted.message'), }); // TODO: set config to default config and use fallback config if necessary }, onError() { showNotification({ title: t('buttons.delete.notifications.deleteFailed.title'), icon: , color: 'red', autoClose: 1500, radius: 'md', message: t('buttons.delete.notifications.deleteFailed.message'), }); }, }); }; const fetchDeletion = async (configName: string) => (await fetch(`/api/configs/${configName}`)).json();