2022-12-11 00:00:11 +01:00
|
|
|
import { Title } from '@mantine/core';
|
2022-12-10 22:14:31 +01:00
|
|
|
import { useTranslation } from 'next-i18next';
|
|
|
|
|
import { openContextModalGeneric } from '../../../../tools/mantineModalManagerExtensions';
|
2023-02-05 21:41:47 +01:00
|
|
|
import WidgetsDefinitions from '../../../../widgets';
|
2022-12-18 22:58:00 +01:00
|
|
|
import { IWidget } from '../../../../widgets/widgets';
|
2023-01-06 22:46:07 +01:00
|
|
|
import { useWrapperColumnCount } from '../../Wrappers/gridstack/store';
|
2022-12-11 00:00:11 +01:00
|
|
|
import { GenericTileMenu } from '../GenericTileMenu';
|
2022-12-18 22:58:00 +01:00
|
|
|
import { WidgetEditModalInnerProps } from './WidgetsEditModal';
|
2022-12-16 21:01:06 +01:00
|
|
|
import { WidgetsRemoveModalInnerProps } from './WidgetsRemoveModal';
|
2022-12-10 22:14:31 +01:00
|
|
|
|
2022-12-16 21:01:06 +01:00
|
|
|
export type WidgetChangePositionModalInnerProps = {
|
2023-03-30 22:20:56 +02:00
|
|
|
widgetType: string;
|
2022-12-19 17:03:39 +01:00
|
|
|
widget: IWidget<string, any>;
|
2023-01-06 22:46:07 +01:00
|
|
|
wrapperColumnCount: number;
|
2022-12-17 00:28:46 +09:00
|
|
|
};
|
|
|
|
|
|
2022-12-18 22:58:00 +01:00
|
|
|
interface WidgetsMenuProps {
|
|
|
|
|
integration: string;
|
2022-12-19 17:03:39 +01:00
|
|
|
widget: IWidget<string, any> | undefined;
|
2022-12-10 22:14:31 +01:00
|
|
|
}
|
|
|
|
|
|
2022-12-19 17:03:39 +01:00
|
|
|
export const WidgetsMenu = ({ integration, widget }: WidgetsMenuProps) => {
|
2022-12-18 22:58:00 +01:00
|
|
|
const { t } = useTranslation(`modules/${integration}`);
|
2023-01-06 22:46:07 +01:00
|
|
|
const wrapperColumnCount = useWrapperColumnCount();
|
2022-12-10 22:14:31 +01:00
|
|
|
|
2023-01-06 22:46:07 +01:00
|
|
|
if (!widget || !wrapperColumnCount) return null;
|
2023-01-06 11:18:29 +09:00
|
|
|
// Match widget.id with WidgetsDefinitions
|
|
|
|
|
// First get the keys
|
|
|
|
|
const keys = Object.keys(WidgetsDefinitions);
|
2023-03-30 22:20:56 +02:00
|
|
|
// Then find the key that matches the widget.type
|
|
|
|
|
const widgetDefinition = keys.find((key) => key === widget.type);
|
2023-01-06 11:18:29 +09:00
|
|
|
// Then get the widget definition
|
|
|
|
|
const widgetDefinitionObject =
|
|
|
|
|
WidgetsDefinitions[widgetDefinition as keyof typeof WidgetsDefinitions];
|
2022-12-10 22:14:31 +01:00
|
|
|
|
|
|
|
|
const handleDeleteClick = () => {
|
2022-12-16 21:01:06 +01:00
|
|
|
openContextModalGeneric<WidgetsRemoveModalInnerProps>({
|
2022-12-10 22:14:31 +01:00
|
|
|
modal: 'integrationRemove',
|
2023-01-13 10:57:13 +09:00
|
|
|
title: <Title order={4}>{t('common:remove')}</Title>,
|
2022-12-10 22:14:31 +01:00
|
|
|
innerProps: {
|
2023-03-30 22:20:56 +02:00
|
|
|
widgetType: integration,
|
2022-12-10 22:14:31 +01:00
|
|
|
},
|
2023-02-05 22:23:07 +01:00
|
|
|
styles: {
|
|
|
|
|
inner: {
|
|
|
|
|
position: 'sticky',
|
|
|
|
|
top: 30,
|
|
|
|
|
},
|
|
|
|
|
},
|
2022-12-10 22:14:31 +01:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleChangeSizeClick = () => {
|
2022-12-16 21:01:06 +01:00
|
|
|
openContextModalGeneric<WidgetChangePositionModalInnerProps>({
|
2022-12-11 13:12:39 +01:00
|
|
|
modal: 'changeIntegrationPositionModal',
|
2022-12-10 22:14:31 +01:00
|
|
|
size: 'xl',
|
|
|
|
|
title: null,
|
|
|
|
|
innerProps: {
|
2023-03-30 22:20:56 +02:00
|
|
|
widgetType: integration,
|
2022-12-20 11:45:33 +09:00
|
|
|
widget,
|
2023-01-06 22:46:07 +01:00
|
|
|
wrapperColumnCount,
|
2022-12-10 22:14:31 +01:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleEditClick = () => {
|
2022-12-18 22:58:00 +01:00
|
|
|
openContextModalGeneric<WidgetEditModalInnerProps>({
|
2022-12-10 22:14:31 +01:00
|
|
|
modal: 'integrationOptions',
|
2023-03-30 22:20:56 +02:00
|
|
|
title: t('descriptor.settings.title'),
|
2022-12-10 22:14:31 +01:00
|
|
|
innerProps: {
|
2023-03-30 22:20:56 +02:00
|
|
|
widgetType: integration,
|
2022-12-19 17:03:39 +01:00
|
|
|
options: widget.properties,
|
2023-01-06 11:18:29 +09:00
|
|
|
// Cast as the right type for the correct widget
|
|
|
|
|
widgetOptions: widgetDefinitionObject.options as any,
|
2022-12-10 22:14:31 +01:00
|
|
|
},
|
2023-01-06 21:55:10 +01:00
|
|
|
zIndex: 5,
|
2022-12-10 22:14:31 +01:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2022-12-11 00:00:11 +01:00
|
|
|
<GenericTileMenu
|
|
|
|
|
handleClickEdit={handleEditClick}
|
|
|
|
|
handleClickChangePosition={handleChangeSizeClick}
|
|
|
|
|
handleClickDelete={handleDeleteClick}
|
2022-12-20 09:05:49 +01:00
|
|
|
displayEdit={
|
2023-02-05 21:41:47 +01:00
|
|
|
typeof widget.properties !== 'undefined' &&
|
|
|
|
|
Object.keys(widgetDefinitionObject?.options ?? {}).length !== 0
|
2022-12-20 09:05:49 +01:00
|
|
|
}
|
2022-12-11 00:00:11 +01:00
|
|
|
/>
|
2022-12-10 22:14:31 +01:00
|
|
|
);
|
|
|
|
|
};
|