Files
Homarr/src/components/layout/header/Actions/ToggleEditMode/ToggleEditMode.tsx

129 lines
4.2 KiB
TypeScript
Raw Normal View History

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';
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';
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';
import { useCardStyles } from '../../../useCardStyles';
2022-12-10 17:58:01 +01:00
export const ToggleEditModeAction = () => {
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
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
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) {
showNotification({
styles: (theme) => ({
root: {
backgroundColor: theme.colors.orange[7],
borderColor: theme.colors.orange[7],
2022-12-24 17:18:16 +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>
),
message: <Trans i18nKey="layout/header/actions/toggle-edit-mode:popover.text" />,
});
} else {
2023-01-06 01:10:16 +09:00
hideNotification('toggle-edit-mode');
}
2022-12-14 10:27:02 +01:00
};
2023-01-01 17:26:09 +01:00
const ToggleButtonDesktop = () => (
<Tooltip label={enabled ? t('button.enabled') : t('button.disabled')}>
<Button
className={classes.card}
onClick={() => toggleButtonClicked()}
radius="md"
2023-01-06 11:18:47 +09:00
variant="default"
style={{ height: 43 }}
>
{enabled ? <IconEditCircleOff /> : <IconEditCircle />}
</Button>
</Tooltip>
2023-01-01 17:26:09 +01:00
);
const ToggleActionIconMobile = () => (
<ActionIcon
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 (
<>
{smallerThanSm ? (
enabled ? (
<Group style={{ flexWrap: 'nowrap' }}>
<AddElementAction type="action-icon" />
2023-01-02 02:52:12 +09:00
<ToggleActionIconMobile />
</Group>
2023-01-02 02:52:12 +09:00
) : (
<ToggleActionIconMobile />
)
) : enabled ? (
<Button.Group>
2023-01-02 02:52:12 +09:00
<ToggleButtonDesktop />
{enabled && <AddElementAction type="button" />}
</Button.Group>
) : (
<ToggleButtonDesktop />
)}
</>
2022-12-10 17:58:01 +01:00
);
};