2023-01-05 22:43:56 +09:00
|
|
|
import {
|
|
|
|
|
Alert,
|
|
|
|
|
Button,
|
|
|
|
|
Group,
|
|
|
|
|
MultiSelect,
|
|
|
|
|
Stack,
|
|
|
|
|
Switch,
|
|
|
|
|
TextInput,
|
|
|
|
|
Text,
|
|
|
|
|
NumberInput,
|
|
|
|
|
Slider,
|
|
|
|
|
} from '@mantine/core';
|
2022-12-10 22:14:31 +01:00
|
|
|
import { ContextModalProps } from '@mantine/modals';
|
2022-12-31 17:48:46 +01:00
|
|
|
import { IconAlertTriangle } from '@tabler/icons';
|
|
|
|
|
import { Trans, useTranslation } from 'next-i18next';
|
2022-12-10 22:14:31 +01:00
|
|
|
import { useState } from 'react';
|
2022-12-20 11:45:33 +09:00
|
|
|
import Widgets from '../../../../widgets';
|
2022-12-24 17:18:16 +09:00
|
|
|
import type { IWidgetOptionValue } from '../../../../widgets/widgets';
|
2022-12-16 21:01:06 +01:00
|
|
|
import { useConfigContext } from '../../../../config/provider';
|
|
|
|
|
import { useConfigStore } from '../../../../config/store';
|
2022-12-18 22:58:00 +01:00
|
|
|
import { IWidget } from '../../../../widgets/widgets';
|
2023-01-05 22:43:56 +09:00
|
|
|
import { useColorTheme } from '../../../../tools/color';
|
2022-12-10 22:14:31 +01:00
|
|
|
|
2022-12-18 22:58:00 +01:00
|
|
|
export type WidgetEditModalInnerProps = {
|
2022-12-19 19:01:29 +01:00
|
|
|
widgetId: string;
|
2022-12-18 22:58:00 +01:00
|
|
|
options: IWidget<string, any>['properties'];
|
2023-01-06 11:18:29 +09:00
|
|
|
widgetOptions: IWidget<string, any>['properties'];
|
2022-12-10 22:14:31 +01:00
|
|
|
};
|
|
|
|
|
|
2022-12-18 22:58:00 +01:00
|
|
|
type IntegrationOptionsValueType = IWidget<string, any>['properties'][string];
|
|
|
|
|
|
2022-12-16 21:01:06 +01:00
|
|
|
export const WidgetsEditModal = ({
|
2022-12-10 22:14:31 +01:00
|
|
|
context,
|
|
|
|
|
id,
|
|
|
|
|
innerProps,
|
2022-12-16 21:01:06 +01:00
|
|
|
}: ContextModalProps<WidgetEditModalInnerProps>) => {
|
2022-12-19 19:01:29 +01:00
|
|
|
const { t } = useTranslation([`modules/${innerProps.widgetId}`, 'common']);
|
2022-12-10 22:14:31 +01:00
|
|
|
const [moduleProperties, setModuleProperties] = useState(innerProps.options);
|
2023-01-06 11:18:29 +09:00
|
|
|
// const items = Object.entries(moduleProperties ?? {}) as [string, IntegrationOptionsValueType][];
|
|
|
|
|
const items = Object.entries(innerProps.widgetOptions ?? {}) as [
|
|
|
|
|
string,
|
|
|
|
|
IntegrationOptionsValueType
|
|
|
|
|
][];
|
2022-12-10 22:14:31 +01:00
|
|
|
|
2022-12-24 17:18:16 +09:00
|
|
|
// Find the Key in the "Widgets" Object that matches the widgetId
|
|
|
|
|
const currentWidgetDefinition = Widgets[innerProps.widgetId as keyof typeof Widgets];
|
2022-12-10 22:14:31 +01:00
|
|
|
const { name: configName } = useConfigContext();
|
|
|
|
|
const updateConfig = useConfigStore((x) => x.updateConfig);
|
|
|
|
|
|
|
|
|
|
if (!configName || !innerProps.options) return null;
|
|
|
|
|
|
|
|
|
|
const handleChange = (key: string, value: IntegrationOptionsValueType) => {
|
|
|
|
|
setModuleProperties((prev) => {
|
2022-12-17 00:28:46 +09:00
|
|
|
const copyOfPrev: any = { ...prev };
|
2022-12-10 22:14:31 +01:00
|
|
|
copyOfPrev[key] = value;
|
|
|
|
|
return copyOfPrev;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-18 22:58:00 +01:00
|
|
|
const getMutliselectData = (option: string) => {
|
2022-12-19 19:01:29 +01:00
|
|
|
const currentWidgetDefinition = Widgets[innerProps.widgetId as keyof typeof Widgets];
|
2022-12-18 22:58:00 +01:00
|
|
|
if (!Widgets) return [];
|
|
|
|
|
|
|
|
|
|
const options = currentWidgetDefinition.options as any;
|
|
|
|
|
return options[option]?.data ?? [];
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-10 22:14:31 +01:00
|
|
|
const handleSave = () => {
|
2022-12-19 21:27:44 +01:00
|
|
|
updateConfig(
|
|
|
|
|
configName,
|
|
|
|
|
(prev) => {
|
2022-12-20 11:45:33 +09:00
|
|
|
const currentWidget = prev.widgets.find((x) => x.id === innerProps.widgetId);
|
2022-12-19 21:27:44 +01:00
|
|
|
currentWidget!.properties = moduleProperties;
|
2022-12-19 19:01:29 +01:00
|
|
|
|
2022-12-19 21:27:44 +01:00
|
|
|
return {
|
|
|
|
|
...prev,
|
|
|
|
|
widgets: [...prev.widgets.filter((x) => x.id !== innerProps.widgetId), currentWidget!],
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
true
|
|
|
|
|
);
|
2022-12-10 22:14:31 +01:00
|
|
|
context.closeModal(id);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Stack>
|
2023-01-06 11:18:29 +09:00
|
|
|
{items.map(([key, defaultValue], index) => {
|
2022-12-24 17:18:16 +09:00
|
|
|
const option = (currentWidgetDefinition as any).options[key] as IWidgetOptionValue;
|
2023-01-06 11:18:29 +09:00
|
|
|
const value = moduleProperties[key] ?? defaultValue;
|
2022-12-31 17:48:46 +01:00
|
|
|
|
|
|
|
|
if (!option) {
|
|
|
|
|
return (
|
|
|
|
|
<Alert icon={<IconAlertTriangle />} color="red">
|
|
|
|
|
<Text>
|
|
|
|
|
<Trans
|
|
|
|
|
i18nKey="modules/common:errors.unmappedOptions.text"
|
|
|
|
|
values={{ key }}
|
|
|
|
|
components={{ b: <b />, code: <code /> }}
|
|
|
|
|
/>
|
|
|
|
|
</Text>
|
|
|
|
|
</Alert>
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-01-05 22:43:56 +09:00
|
|
|
return WidgetOptionTypeSwitch(
|
|
|
|
|
option,
|
|
|
|
|
index,
|
|
|
|
|
t,
|
|
|
|
|
key,
|
|
|
|
|
value,
|
|
|
|
|
handleChange,
|
|
|
|
|
getMutliselectData
|
|
|
|
|
);
|
2022-12-24 17:18:16 +09:00
|
|
|
})}
|
2022-12-10 22:14:31 +01:00
|
|
|
<Group position="right">
|
|
|
|
|
<Button onClick={() => context.closeModal(id)} variant="light">
|
2022-12-20 16:54:22 +09:00
|
|
|
{t('common:cancel')}
|
2022-12-10 22:14:31 +01:00
|
|
|
</Button>
|
2022-12-20 16:54:22 +09:00
|
|
|
<Button onClick={handleSave}>{t('common:save')}</Button>
|
2022-12-10 22:14:31 +01:00
|
|
|
</Group>
|
|
|
|
|
</Stack>
|
|
|
|
|
);
|
|
|
|
|
};
|
2023-01-05 22:43:56 +09:00
|
|
|
|
2023-01-05 23:14:08 +09:00
|
|
|
// Widget switch
|
2023-01-05 22:43:56 +09:00
|
|
|
// Widget options are computed based on their type.
|
|
|
|
|
// here you can define new types for options (along with editing the widgets.d.ts file)
|
|
|
|
|
function WidgetOptionTypeSwitch(
|
|
|
|
|
option: IWidgetOptionValue,
|
|
|
|
|
index: number,
|
|
|
|
|
t: any,
|
|
|
|
|
key: string,
|
|
|
|
|
value: string | number | boolean | string[],
|
|
|
|
|
handleChange: (key: string, value: IntegrationOptionsValueType) => void,
|
|
|
|
|
getMutliselectData: (option: string) => any
|
|
|
|
|
) {
|
|
|
|
|
const { primaryColor, secondaryColor } = useColorTheme();
|
|
|
|
|
switch (option.type) {
|
|
|
|
|
case 'switch':
|
|
|
|
|
return (
|
|
|
|
|
<Switch
|
|
|
|
|
key={`${option.type}-${index}`}
|
|
|
|
|
label={t(`descriptor.settings.${key}.label`)}
|
|
|
|
|
checked={value as boolean}
|
|
|
|
|
onChange={(ev) => handleChange(key, ev.currentTarget.checked)}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
case 'text':
|
|
|
|
|
return (
|
|
|
|
|
<TextInput
|
|
|
|
|
color={primaryColor}
|
|
|
|
|
key={`${option.type}-${index}`}
|
|
|
|
|
label={t(`descriptor.settings.${key}.label`)}
|
|
|
|
|
value={value as string}
|
|
|
|
|
onChange={(ev) => handleChange(key, ev.currentTarget.value)}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
case 'multi-select':
|
|
|
|
|
return (
|
|
|
|
|
<MultiSelect
|
|
|
|
|
color={primaryColor}
|
|
|
|
|
key={`${option.type}-${index}`}
|
|
|
|
|
data={getMutliselectData(key)}
|
|
|
|
|
label={t(`descriptor.settings.${key}.label`)}
|
|
|
|
|
value={value as string[]}
|
|
|
|
|
onChange={(v) => handleChange(key, v)}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
case 'number':
|
|
|
|
|
return (
|
|
|
|
|
<NumberInput
|
|
|
|
|
color={primaryColor}
|
|
|
|
|
key={`${option.type}-${index}`}
|
|
|
|
|
label={t(`descriptor.settings.${key}.label`)}
|
|
|
|
|
value={value as number}
|
|
|
|
|
onChange={(v) => handleChange(key, v!)}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
case 'slider':
|
|
|
|
|
return (
|
2023-01-05 23:14:08 +09:00
|
|
|
<Stack spacing="xs">
|
|
|
|
|
<Text>{t(`descriptor.settings.${key}.label`)}</Text>
|
|
|
|
|
<Slider
|
|
|
|
|
color={primaryColor}
|
|
|
|
|
key={`${option.type}-${index}`}
|
|
|
|
|
value={value as number}
|
|
|
|
|
min={option.min}
|
|
|
|
|
max={option.max}
|
|
|
|
|
step={option.step}
|
|
|
|
|
onChange={(v) => handleChange(key, v)}
|
|
|
|
|
/>
|
|
|
|
|
</Stack>
|
2023-01-05 22:43:56 +09:00
|
|
|
);
|
|
|
|
|
default:
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|