Files
Homarr/src/components/Dashboard/Tiles/Widgets/WidgetsMenu.tsx

91 lines
2.8 KiB
TypeScript
Raw Normal View History

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';
import WidgetsDefinitions from '../../../../widgets';
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';
import { WidgetEditModalInnerProps } from './WidgetsEditModal';
import { WidgetsRemoveModalInnerProps } from './WidgetsRemoveModal';
2022-12-10 22:14:31 +01:00
export type WidgetChangePositionModalInnerProps = {
2023-03-30 22:20:56 +02:00
widgetType: string;
widget: IWidget<string, any>;
2023-01-06 22:46:07 +01:00
wrapperColumnCount: number;
2022-12-17 00:28:46 +09:00
};
interface WidgetsMenuProps {
integration: string;
widget: IWidget<string, any> | undefined;
2022-12-10 22:14:31 +01:00
}
export const WidgetsMenu = ({ integration, widget }: WidgetsMenuProps) => {
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;
// 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);
// Then get the widget definition
const widgetDefinitionObject =
WidgetsDefinitions[widgetDefinition as keyof typeof WidgetsDefinitions];
2022-12-10 22:14:31 +01:00
const handleDeleteClick = () => {
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 = () => {
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 = () => {
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,
options: widget.properties,
// Cast as the right type for the correct widget
widgetOptions: widgetDefinitionObject.options as any,
2022-12-10 22:14:31 +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={
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
);
};