import Widgets from '../../../../widgets'; import { Button, Group, MultiSelect, Stack, Switch, TextInput } from '@mantine/core'; import { ContextModalProps } from '@mantine/modals'; import { useTranslation } from 'next-i18next'; import { useState } from 'react'; import { useConfigContext } from '../../../../config/provider'; import { useConfigStore } from '../../../../config/store'; import { IWidget } from '../../../../widgets/widgets'; export type WidgetEditModalInnerProps = { widgetId: string; options: IWidget['properties']; }; type IntegrationOptionsValueType = IWidget['properties'][string]; export const WidgetsEditModal = ({ context, id, innerProps, }: ContextModalProps) => { const { t } = useTranslation([`modules/${innerProps.widgetId}`, 'common']); const [moduleProperties, setModuleProperties] = useState(innerProps.options); const items = Object.entries(moduleProperties ?? {}) as [string, IntegrationOptionsValueType][]; const { name: configName } = useConfigContext(); const updateConfig = useConfigStore((x) => x.updateConfig); if (!configName || !innerProps.options) return null; const handleChange = (key: string, value: IntegrationOptionsValueType) => { setModuleProperties((prev) => { const copyOfPrev: any = { ...prev }; copyOfPrev[key] = value; return copyOfPrev; }); }; const getMutliselectData = (option: string) => { const currentWidgetDefinition = Widgets[innerProps.widgetId as keyof typeof Widgets]; if (!Widgets) return []; const options = currentWidgetDefinition.options as any; return options[option]?.data ?? []; }; const handleSave = () => { updateConfig( configName, (prev) => { let currentWidget = prev.widgets.find((x) => x.id === innerProps.widgetId); currentWidget!.properties = moduleProperties; return { ...prev, widgets: [...prev.widgets.filter((x) => x.id !== innerProps.widgetId), currentWidget!], }; }, true ); context.closeModal(id); }; return ( {items.map(([key, value]) => ( <> {typeof value === 'boolean' ? ( handleChange(key, ev.currentTarget.checked)} /> ) : null} {typeof value === 'string' ? ( handleChange(key, ev.currentTarget.value)} /> ) : null} {typeof value === 'object' && Array.isArray(value) ? ( handleChange(key, v)} /> ) : null} ))} ); };