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

249 lines
7.6 KiB
TypeScript
Raw Normal View History

import {
Alert,
Button,
Group,
MultiSelect,
NumberInput,
Select,
Slider,
Stack,
Switch,
Text,
TextInput,
} 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';
import { FC, useState } from 'react';
import { useConfigContext } from '../../../../config/provider';
import { useConfigStore } from '../../../../config/store';
import { useColorTheme } from '../../../../tools/color';
import Widgets from '../../../../widgets';
import type { IDraggableListInputValue, IWidgetOptionValue } from '../../../../widgets/widgets';
import { IWidget } from '../../../../widgets/widgets';
import { DraggableList } from './DraggableList';
2022-12-10 22:14:31 +01:00
export type WidgetEditModalInnerProps = {
widgetId: string;
options: IWidget<string, any>['properties'];
widgetOptions: IWidget<string, any>['properties'];
2022-12-10 22:14:31 +01:00
};
type IntegrationOptionsValueType = IWidget<string, any>['properties'][string];
export const WidgetsEditModal = ({
2022-12-10 22:14:31 +01:00
context,
id,
innerProps,
}: ContextModalProps<WidgetEditModalInnerProps>) => {
const { t } = useTranslation([`modules/${innerProps.widgetId}`, 'common']);
2022-12-10 22:14:31 +01:00
const [moduleProperties, setModuleProperties] = useState(innerProps.options);
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;
});
};
const handleSave = () => {
updateConfig(
configName,
(prev) => {
2022-12-20 11:45:33 +09:00
const 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
);
2022-12-10 22:14:31 +01:00
context.closeModal(id);
};
return (
<Stack>
2023-01-13 18:46:59 +09:00
{items.map(([key, _], index) => {
2022-12-24 17:18:16 +09:00
const option = (currentWidgetDefinition as any).options[key] as IWidgetOptionValue;
2023-01-13 18:46:59 +09:00
const value = moduleProperties[key] ?? option.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>
);
}
return (
<WidgetOptionTypeSwitch
key={`${key}.${index}`}
option={option}
widgetId={innerProps.widgetId}
propName={key}
value={value}
handleChange={handleChange}
/>
);
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 23:14:08 +09:00
// Widget switch
// Widget options are computed based on their type.
// here you can define new types for options (along with editing the widgets.d.ts file)
const WidgetOptionTypeSwitch: FC<{
option: IWidgetOptionValue;
widgetId: string;
propName: string;
value: any;
handleChange: (key: string, value: IntegrationOptionsValueType) => void;
}> = ({ option, widgetId, propName: key, value, handleChange }) => {
const { t } = useTranslation([`modules/${widgetId}`, 'common']);
const { primaryColor } = useColorTheme();
switch (option.type) {
case 'switch':
return (
<Switch
label={t(`descriptor.settings.${key}.label`)}
checked={value as boolean}
onChange={(ev) => handleChange(key, ev.currentTarget.checked)}
/>
);
case 'text':
return (
<TextInput
color={primaryColor}
label={t(`descriptor.settings.${key}.label`)}
value={value as string}
onChange={(ev) => handleChange(key, ev.currentTarget.value)}
/>
);
case 'multi-select':
return (
<MultiSelect
color={primaryColor}
2023-01-11 08:43:28 +09:00
data={option.data}
label={t(`descriptor.settings.${key}.label`)}
value={value as string[]}
2023-01-11 08:43:28 +09:00
defaultValue={option.defaultValue}
onChange={(v) => handleChange(key, v)}
/>
);
case 'select':
return (
<Select
color={primaryColor}
defaultValue={option.defaultValue}
data={option.data}
label={t(`descriptor.settings.${key}.label`)}
value={value as string}
2023-01-11 08:49:23 +09:00
onChange={(v) => handleChange(key, v ?? option.defaultValue)}
/>
);
case 'number':
return (
<NumberInput
color={primaryColor}
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">
<Slider
color={primaryColor}
2023-01-13 18:46:59 +09:00
label={value}
2023-01-05 23:14:08 +09:00
value={value as number}
min={option.min}
max={option.max}
step={option.step}
onChange={(v) => handleChange(key, v)}
/>
</Stack>
);
case 'draggable-list':
// eslint-disable-next-line no-case-declarations
const typedVal = value as IDraggableListInputValue['defaultValue'];
return (
<Stack spacing="xs">
<Text>{t(`descriptor.settings.${key}.label`)}</Text>
<DraggableList
value={typedVal}
onChange={(v) => handleChange(key, v)}
labels={Object.fromEntries(
Object.entries(option.items).map(([graphName]) => [
graphName,
t(`descriptor.settings.${key}.${graphName}.label`),
])
)}
>
{Object.fromEntries(
Object.entries(option.items).map(([graphName, graph]) => [
graphName,
Object.entries(graph).map(([subKey, setting], i) => (
<WidgetOptionTypeSwitch
key={`${graphName}.${subKey}.${i}`}
option={setting as IWidgetOptionValue}
widgetId={widgetId}
propName={`${key}.${graphName}.${subKey}`}
value={typedVal.find((v) => v.key === graphName)?.subValues?.[subKey]}
handleChange={(_, newVal) =>
handleChange(
key,
typedVal.map((oldVal) =>
oldVal.key === graphName
? {
...oldVal,
subValues: {
...oldVal.subValues,
[subKey]: newVal,
},
}
: oldVal
)
)
}
/>
)),
])
)}
</DraggableList>
</Stack>
);
default:
return null;
}
};