2022-12-06 21:22:37 +01:00
|
|
|
import { ActionIcon, Center, createStyles, Flex, Text, useMantineTheme } from '@mantine/core';
|
2022-12-04 17:36:30 +01:00
|
|
|
import { useDisclosure } from '@mantine/hooks';
|
|
|
|
|
import { showNotification } from '@mantine/notifications';
|
2022-12-06 21:22:37 +01:00
|
|
|
import { IconCheck, IconCopy, IconDownload, IconTrash, IconX } from '@tabler/icons';
|
2022-12-04 17:36:30 +01:00
|
|
|
import { useMutation } from '@tanstack/react-query';
|
|
|
|
|
import fileDownload from 'js-file-download';
|
|
|
|
|
import { useTranslation } from 'next-i18next';
|
2022-12-06 21:22:37 +01:00
|
|
|
import { useConfigContext } from '../../../../config/provider';
|
|
|
|
|
import Tip from '../../../layout/Tip';
|
|
|
|
|
import { CreateConfigCopyModal } from './CreateCopyModal';
|
2022-12-04 17:36:30 +01:00
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-06 21:22:37 +01:00
|
|
|
const { classes } = useStyles();
|
|
|
|
|
const { colors } = useMantineTheme();
|
|
|
|
|
|
2022-12-04 17:36:30 +01:00
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<CreateConfigCopyModal
|
|
|
|
|
opened={createCopyModalOpened}
|
|
|
|
|
closeModal={createCopyModal.close}
|
|
|
|
|
initialConfigName={config.configProperties.name}
|
|
|
|
|
/>
|
2022-12-06 21:22:37 +01:00
|
|
|
<Flex gap="xs" justify="stretch">
|
|
|
|
|
<ActionIcon className={classes.actionIcon} onClick={handleDownload} variant="default">
|
|
|
|
|
<IconDownload size={20} />
|
|
|
|
|
<Text>{t('buttons.download')}</Text>
|
|
|
|
|
</ActionIcon>
|
|
|
|
|
<ActionIcon
|
|
|
|
|
className={classes.actionIcon}
|
2022-12-04 17:36:30 +01:00
|
|
|
onClick={handleDeletion}
|
2022-12-06 21:22:37 +01:00
|
|
|
color="red"
|
|
|
|
|
variant="light"
|
2022-12-04 17:36:30 +01:00
|
|
|
>
|
2022-12-06 21:22:37 +01:00
|
|
|
<IconTrash color={colors.red[2]} size={20} />
|
|
|
|
|
<Text>{t('buttons.delete.text')}</Text>
|
|
|
|
|
</ActionIcon>
|
|
|
|
|
<ActionIcon className={classes.actionIcon} onClick={createCopyModal.open} variant="default">
|
|
|
|
|
<IconCopy size={20} />
|
|
|
|
|
<Text>{t('buttons.saveCopy')}</Text>
|
|
|
|
|
</ActionIcon>
|
|
|
|
|
</Flex>
|
2022-12-04 17:36:30 +01:00
|
|
|
|
|
|
|
|
<Center>
|
|
|
|
|
<Tip>{t('settings/common:tips.configTip')}</Tip>
|
|
|
|
|
</Center>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-06 21:22:37 +01:00
|
|
|
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,
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
2022-12-04 17:36:30 +01:00
|
|
|
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: <IconCheck />,
|
|
|
|
|
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: <IconX />,
|
|
|
|
|
color: 'red',
|
|
|
|
|
autoClose: 1500,
|
|
|
|
|
radius: 'md',
|
|
|
|
|
message: t('buttons.delete.notifications.deleteFailed.message'),
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-17 00:28:46 +09:00
|
|
|
const fetchDeletion = async (configName: string) =>
|
|
|
|
|
(await fetch(`/api/configs/${configName}`)).json();
|