2022-12-24 17:18:16 +09:00
|
|
|
import axios from 'axios';
|
|
|
|
|
import Consola from 'consola';
|
2023-01-13 20:08:09 +09:00
|
|
|
import { ActionIcon, Button, Group, Text, Title, Tooltip } from '@mantine/core';
|
2023-01-06 01:08:07 +09:00
|
|
|
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-30 21:41:16 +01:00
|
|
|
import { useHotkeys } from '@mantine/hooks';
|
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';
|
2023-01-13 20:08:09 +09:00
|
|
|
import { useNamedWrapperColumnCount } from '../../../../Dashboard/Wrappers/gridstack/store';
|
2023-01-19 10:39:39 +09:00
|
|
|
import { useCardStyles } from '../../../useCardStyles';
|
2022-12-10 17:58:01 +01:00
|
|
|
|
|
|
|
|
export const ToggleEditModeAction = () => {
|
2022-12-10 22:50:34 +01:00
|
|
|
const { enabled, toggleEditMode } = useEditModeStore();
|
2023-01-13 20:08:09 +09:00
|
|
|
const namedWrapperColumnCount = useNamedWrapperColumnCount();
|
2022-12-10 17:58:01 +01:00
|
|
|
const { t } = useTranslation('layout/header/actions/toggle-edit-mode');
|
2023-01-13 20:08:09 +09:00
|
|
|
const translatedSize = t(`screenSizes.${namedWrapperColumnCount}`);
|
2022-12-10 17:58:01 +01:00
|
|
|
|
2022-12-10 22:50:34 +01:00
|
|
|
const smallerThanSm = useScreenSmallerThan('sm');
|
2022-12-24 17:18:16 +09:00
|
|
|
const { config } = useConfigContext();
|
2023-01-22 23:10:05 +09:00
|
|
|
const { classes } = useCardStyles(true);
|
2022-12-24 17:18:16 +09:00
|
|
|
|
2023-01-30 21:41:16 +01:00
|
|
|
useHotkeys([['ctrl+E', toggleEditMode]]);
|
|
|
|
|
|
2022-12-14 10:27:02 +01:00
|
|
|
const toggleButtonClicked = () => {
|
|
|
|
|
toggleEditMode();
|
2023-01-07 23:25:13 +01:00
|
|
|
if (enabled || config === undefined || config?.schemaVersion === undefined) {
|
|
|
|
|
const configName = getCookie('config-name')?.toString() ?? 'default';
|
|
|
|
|
axios.put(`/api/configs/${configName}`, { ...config });
|
|
|
|
|
Consola.log('Saved config to server', configName);
|
|
|
|
|
hideNotification('toggle-edit-mode');
|
|
|
|
|
} else if (!enabled) {
|
2023-01-06 01:08:07 +09:00
|
|
|
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-13 20:08:09 +09:00
|
|
|
autoClose: 10000,
|
|
|
|
|
title: (
|
|
|
|
|
<Title order={4}>
|
|
|
|
|
<Trans
|
|
|
|
|
i18nKey="layout/header/actions/toggle-edit-mode:popover.title"
|
|
|
|
|
values={{ size: translatedSize }}
|
|
|
|
|
components={{
|
|
|
|
|
1: (
|
|
|
|
|
<Text
|
|
|
|
|
component="a"
|
|
|
|
|
style={{ color: 'inherit', textDecoration: 'underline' }}
|
|
|
|
|
href="https://homarr.dev/docs/customizations/layout"
|
|
|
|
|
target="_blank"
|
|
|
|
|
/>
|
|
|
|
|
),
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</Title>
|
|
|
|
|
),
|
2023-01-06 01:08:07 +09:00
|
|
|
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
|
2023-01-19 10:39:39 +09:00
|
|
|
className={classes.card}
|
2023-01-06 01:08:07 +09:00
|
|
|
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
|
2023-01-19 10:39:39 +09:00
|
|
|
className={classes.card}
|
2023-01-01 17:26:09 +01:00
|
|
|
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
|
|
|
);
|
|
|
|
|
};
|