2022-12-24 17:18:16 +09:00
|
|
|
import axios from 'axios';
|
|
|
|
|
import Consola from 'consola';
|
2023-01-06 01:08:07 +09:00
|
|
|
import { ActionIcon, Button, Group, Title, Tooltip } from '@mantine/core';
|
|
|
|
|
import { IconEditCircle, IconEditCircleOff } from '@tabler/icons';
|
2022-12-24 17:18:16 +09:00
|
|
|
import { getCookie } from 'cookies-next';
|
2022-12-20 16:54:22 +09:00
|
|
|
import { Trans, useTranslation } from 'next-i18next';
|
2023-01-06 01:08:07 +09:00
|
|
|
import { useEffect } from 'react';
|
2023-01-06 01:10:16 +09:00
|
|
|
import { hideNotification, showNotification } from '@mantine/notifications';
|
2022-12-24 17:18:16 +09:00
|
|
|
import { useConfigContext } from '../../../../../config/provider';
|
2022-12-23 17:29:58 +01:00
|
|
|
import { useScreenSmallerThan } from '../../../../../hooks/useScreenSmallerThan';
|
2022-12-10 17:58:01 +01:00
|
|
|
|
|
|
|
|
import { useEditModeStore } from '../../../../Dashboard/Views/useEditModeStore';
|
2023-01-01 17:26:09 +01:00
|
|
|
import { AddElementAction } from '../AddElementAction/AddElementAction';
|
2022-12-10 17:58:01 +01:00
|
|
|
|
|
|
|
|
export const ToggleEditModeAction = () => {
|
2022-12-10 22:50:34 +01:00
|
|
|
const { enabled, toggleEditMode } = useEditModeStore();
|
|
|
|
|
|
2022-12-10 17:58:01 +01:00
|
|
|
const { t } = useTranslation('layout/header/actions/toggle-edit-mode');
|
|
|
|
|
|
2022-12-10 22:50:34 +01:00
|
|
|
const smallerThanSm = useScreenSmallerThan('sm');
|
2022-12-24 17:18:16 +09:00
|
|
|
const { config } = useConfigContext();
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (enabled || config === undefined || config?.schemaVersion === undefined) return;
|
|
|
|
|
const configName = getCookie('config-name')?.toString() ?? 'default';
|
|
|
|
|
axios.put(`/api/configs/${configName}`, { ...config });
|
|
|
|
|
Consola.log('Saved config to server', configName);
|
|
|
|
|
}, [enabled]);
|
2022-12-10 17:58:01 +01:00
|
|
|
|
2022-12-14 10:27:02 +01:00
|
|
|
const toggleButtonClicked = () => {
|
|
|
|
|
toggleEditMode();
|
2023-01-06 01:08:07 +09:00
|
|
|
if (!enabled) {
|
|
|
|
|
showNotification({
|
|
|
|
|
styles: (theme) => ({
|
|
|
|
|
root: {
|
|
|
|
|
backgroundColor: theme.colors.orange[7],
|
|
|
|
|
borderColor: theme.colors.orange[7],
|
2022-12-24 17:18:16 +09:00
|
|
|
|
2023-01-06 01:08:07 +09:00
|
|
|
'&::before': { backgroundColor: theme.white },
|
|
|
|
|
},
|
|
|
|
|
title: { color: theme.white },
|
|
|
|
|
description: { color: theme.white },
|
|
|
|
|
closeButton: {
|
|
|
|
|
color: theme.white,
|
|
|
|
|
'&:hover': { backgroundColor: theme.colors.orange[7] },
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
radius: 'md',
|
2023-01-06 01:10:16 +09:00
|
|
|
id: 'toggle-edit-mode',
|
2023-01-06 01:08:07 +09:00
|
|
|
autoClose: false,
|
|
|
|
|
title: <Title order={4}>{t('popover.title')}</Title>,
|
|
|
|
|
message: <Trans i18nKey="layout/header/actions/toggle-edit-mode:popover.text" />,
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2023-01-06 01:10:16 +09:00
|
|
|
hideNotification('toggle-edit-mode');
|
2023-01-06 01:08:07 +09:00
|
|
|
}
|
2022-12-14 10:27:02 +01:00
|
|
|
};
|
|
|
|
|
|
2023-01-01 17:26:09 +01:00
|
|
|
const ToggleButtonDesktop = () => (
|
2023-01-06 01:08:07 +09:00
|
|
|
<Tooltip label={enabled ? t('button.enabled') : t('button.disabled')}>
|
|
|
|
|
<Button
|
|
|
|
|
onClick={() => toggleButtonClicked()}
|
|
|
|
|
radius="md"
|
2023-01-06 11:18:47 +09:00
|
|
|
variant="default"
|
2023-01-06 01:08:07 +09:00
|
|
|
style={{ height: 43 }}
|
|
|
|
|
>
|
|
|
|
|
{enabled ? <IconEditCircleOff /> : <IconEditCircle />}
|
|
|
|
|
</Button>
|
|
|
|
|
</Tooltip>
|
2023-01-01 17:26:09 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const ToggleActionIconMobile = () => (
|
|
|
|
|
<ActionIcon
|
|
|
|
|
onClick={() => toggleButtonClicked()}
|
|
|
|
|
variant="default"
|
|
|
|
|
radius="md"
|
|
|
|
|
size="xl"
|
|
|
|
|
color="blue"
|
|
|
|
|
>
|
|
|
|
|
{enabled ? <IconEditCircleOff /> : <IconEditCircle />}
|
|
|
|
|
</ActionIcon>
|
|
|
|
|
);
|
|
|
|
|
|
2022-12-10 17:58:01 +01:00
|
|
|
return (
|
2023-01-06 01:08:07 +09:00
|
|
|
<>
|
|
|
|
|
{smallerThanSm ? (
|
|
|
|
|
enabled ? (
|
|
|
|
|
<Group style={{ flexWrap: 'nowrap' }}>
|
|
|
|
|
<AddElementAction type="action-icon" />
|
2023-01-02 02:52:12 +09:00
|
|
|
<ToggleActionIconMobile />
|
2023-01-06 01:08:07 +09:00
|
|
|
</Group>
|
2023-01-02 02:52:12 +09:00
|
|
|
) : (
|
2023-01-06 01:08:07 +09:00
|
|
|
<ToggleActionIconMobile />
|
|
|
|
|
)
|
|
|
|
|
) : enabled ? (
|
|
|
|
|
<Button.Group>
|
2023-01-02 02:52:12 +09:00
|
|
|
<ToggleButtonDesktop />
|
2023-01-06 01:08:07 +09:00
|
|
|
{enabled && <AddElementAction type="button" />}
|
|
|
|
|
</Button.Group>
|
|
|
|
|
) : (
|
|
|
|
|
<ToggleButtonDesktop />
|
|
|
|
|
)}
|
|
|
|
|
</>
|
2022-12-10 17:58:01 +01:00
|
|
|
);
|
|
|
|
|
};
|