diff --git a/src/components/Dashboard/Modals/SelectElement/Components/Shared/GenericElementType.tsx b/src/components/Dashboard/Modals/SelectElement/Components/Shared/GenericElementType.tsx index 7e07c9fb6..33d901dbd 100644 --- a/src/components/Dashboard/Modals/SelectElement/Components/Shared/GenericElementType.tsx +++ b/src/components/Dashboard/Modals/SelectElement/Components/Shared/GenericElementType.tsx @@ -6,6 +6,7 @@ import { useStyles } from './styles'; interface GenericAvailableElementTypeProps { name: string; + handleAddition: () => Promise; description?: string; image: string | TablerIcon; disabled?: boolean; @@ -16,6 +17,7 @@ export const GenericAvailableElementType = ({ description, image, disabled, + handleAddition, }: GenericAvailableElementTypeProps) => { const { classes } = useStyles(); @@ -39,7 +41,15 @@ export const GenericAvailableElementType = ({ )} - diff --git a/src/components/Dashboard/Modals/SelectElement/Components/WidgetsTab/AvailableWidgetsTab.tsx b/src/components/Dashboard/Modals/SelectElement/Components/WidgetsTab/AvailableWidgetsTab.tsx index c0821f395..e75559d67 100644 --- a/src/components/Dashboard/Modals/SelectElement/Components/WidgetsTab/AvailableWidgetsTab.tsx +++ b/src/components/Dashboard/Modals/SelectElement/Components/WidgetsTab/AvailableWidgetsTab.tsx @@ -1,14 +1,7 @@ import { Grid, Text } from '@mantine/core'; -import { - IconArrowsUpDown, - IconCalendarTime, - IconClock, - IconCloudRain, - IconFileDownload, -} from '@tabler/icons'; import { useTranslation } from 'next-i18next'; +import { useConfigContext } from '../../../../../../config/provider'; import widgets from '../../../../../../widgets'; -import { GenericAvailableElementType } from '../Shared/GenericElementType'; import { SelectorBackArrow } from '../Shared/SelectorBackArrow'; import { WidgetElementType } from './WidgetElementType'; @@ -20,6 +13,7 @@ export const AvailableIntegrationElements = ({ onClickBack, }: AvailableIntegrationElementsProps) => { const { t } = useTranslation('layout/element-selector/selector'); + const activeWidgets = useConfigContext().config?.widgets ?? []; return ( <> @@ -30,9 +24,11 @@ export const AvailableIntegrationElements = ({ - {Object.entries(widgets).map(([k, v]) => ( - - ))} + {Object.entries(widgets) + .filter(([widgetId]) => !activeWidgets.some((aw) => aw.id === widgetId)) + .map(([k, v]) => ( + + ))} ); diff --git a/src/components/Dashboard/Modals/SelectElement/Components/WidgetsTab/WidgetElementType.tsx b/src/components/Dashboard/Modals/SelectElement/Components/WidgetsTab/WidgetElementType.tsx index 978f55ab0..78aa40384 100644 --- a/src/components/Dashboard/Modals/SelectElement/Components/WidgetsTab/WidgetElementType.tsx +++ b/src/components/Dashboard/Modals/SelectElement/Components/WidgetsTab/WidgetElementType.tsx @@ -1,19 +1,63 @@ -import { Button, Card, Center, Grid, Stack, Text } from '@mantine/core'; +import { useModals } from '@mantine/modals'; import { TablerIcon } from '@tabler/icons'; import { useTranslation } from 'next-i18next'; -import Image from 'next/image'; -import React, { ReactNode } from 'react'; -import { AvailableElementTypes } from '../Overview/AvailableElementsOverview'; +import { useConfigContext } from '../../../../../../config/provider'; +import { useConfigStore } from '../../../../../../config/store'; +import { IWidget, IWidgetDefinition } from '../../../../../../widgets/widgets'; import { GenericAvailableElementType } from '../Shared/GenericElementType'; interface WidgetElementTypeProps { id: string; image: string | TablerIcon; disabled?: boolean; + widget: IWidgetDefinition; } -export const WidgetElementType = ({ id, image, disabled }: WidgetElementTypeProps) => { +export const WidgetElementType = ({ id, image, disabled, widget }: WidgetElementTypeProps) => { + const { closeModal } = useModals(); const { t } = useTranslation(`modules/${id}`); + const { name: configName, config } = useConfigContext(); + const updateConfig = useConfigStore((x) => x.updateConfig); + + if (!configName) return null; + + const getLowestWrapper = () => { + return config?.wrappers.sort((a, b) => a.position - b.position)[0]; + }; + + const handleAddition = async () => { + updateConfig(configName, (prev) => ({ + ...prev, + widgets: [ + ...prev.widgets.filter((w) => w.id !== widget.id), + { + id: widget.id, + properties: Object.entries(widget.options).reduce((prev, [k, v]) => { + prev[k] = v.defaultValue; + return prev; + }, {} as IWidget['properties']), + area: { + type: 'wrapper', + properties: { + id: getLowestWrapper()?.id ?? '', + }, + }, + shape: { + location: { + x: 0, + y: 0, + }, + size: { + width: widget.gridstack.minWidth, + height: widget.gridstack.minHeight, + }, + }, + }, + ], + })); + // TODO: safe to file system + closeModal('selectElement'); + }; return ( ); };