2022-12-11 13:12:39 +01:00
|
|
|
import { SelectItem } from '@mantine/core';
|
|
|
|
|
import { closeModal, ContextModalProps } from '@mantine/modals';
|
|
|
|
|
import { useConfigContext } from '../../../../config/provider';
|
|
|
|
|
import { useConfigStore } from '../../../../config/store';
|
2022-12-19 19:55:42 +01:00
|
|
|
import widgets from '../../../../widgets';
|
2022-12-16 21:01:06 +01:00
|
|
|
import { WidgetChangePositionModalInnerProps } from '../../Tiles/Widgets/WidgetsMenu';
|
2022-12-11 13:12:39 +01:00
|
|
|
import { ChangePositionModal } from './ChangePositionModal';
|
|
|
|
|
|
2022-12-19 19:55:42 +01:00
|
|
|
export const ChangeWidgetPositionModal = ({
|
2022-12-11 13:12:39 +01:00
|
|
|
context,
|
|
|
|
|
id,
|
|
|
|
|
innerProps,
|
2022-12-16 21:01:06 +01:00
|
|
|
}: ContextModalProps<WidgetChangePositionModalInnerProps>) => {
|
2022-12-11 13:12:39 +01:00
|
|
|
const { name: configName } = useConfigContext();
|
|
|
|
|
const updateConfig = useConfigStore((x) => x.updateConfig);
|
|
|
|
|
|
|
|
|
|
const handleSubmit = (x: number, y: number, width: number, height: number) => {
|
|
|
|
|
if (!configName) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-19 19:55:42 +01:00
|
|
|
updateConfig(configName, (prev) => {
|
|
|
|
|
let currentWidget = prev.widgets.find((x) => x.id === innerProps.widgetId);
|
|
|
|
|
currentWidget!.shape = {
|
|
|
|
|
location: {
|
|
|
|
|
x,
|
|
|
|
|
y,
|
|
|
|
|
},
|
|
|
|
|
size: {
|
|
|
|
|
height,
|
|
|
|
|
width,
|
2022-12-11 13:12:39 +01:00
|
|
|
},
|
2022-12-19 19:55:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...prev,
|
|
|
|
|
widgets: [...prev.widgets.filter((x) => x.id !== innerProps.widgetId), currentWidget!],
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2022-12-11 13:12:39 +01:00
|
|
|
context.closeModal(id);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleCancel = () => {
|
|
|
|
|
closeModal(id);
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-19 19:55:42 +01:00
|
|
|
const widthData = useWidthData(innerProps.widgetId);
|
|
|
|
|
const heightData = useHeightData(innerProps.widgetId);
|
2022-12-11 13:12:39 +01:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<ChangePositionModal
|
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
|
onCancel={handleCancel}
|
|
|
|
|
heightData={heightData}
|
|
|
|
|
widthData={widthData}
|
2022-12-19 17:03:39 +01:00
|
|
|
initialX={innerProps.widget.shape.location.x}
|
|
|
|
|
initialY={innerProps.widget.shape.location.y}
|
|
|
|
|
initialWidth={innerProps.widget.shape.size.width}
|
|
|
|
|
initialHeight={innerProps.widget.shape.size.height}
|
2022-12-11 13:12:39 +01:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-18 22:58:00 +01:00
|
|
|
const useWidthData = (integration: string): SelectItem[] => {
|
|
|
|
|
const currentWidget = widgets[integration as keyof typeof widgets];
|
|
|
|
|
if (!currentWidget) return [];
|
|
|
|
|
const offset = currentWidget.gridstack.minWidth ?? 2;
|
|
|
|
|
const length = (currentWidget.gridstack.maxWidth ?? 12) - offset;
|
2022-12-11 13:12:39 +01:00
|
|
|
return Array.from({ length }, (_, i) => i + offset).map((n) => ({
|
|
|
|
|
value: n.toString(),
|
|
|
|
|
label: `${64 * n}px`,
|
|
|
|
|
}));
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-18 22:58:00 +01:00
|
|
|
const useHeightData = (integration: string): SelectItem[] => {
|
|
|
|
|
const currentWidget = widgets[integration as keyof typeof widgets];
|
|
|
|
|
if (!currentWidget) return [];
|
|
|
|
|
const offset = currentWidget.gridstack.minHeight ?? 2;
|
|
|
|
|
const length = (currentWidget.gridstack.maxHeight ?? 12) - offset;
|
2022-12-11 13:12:39 +01:00
|
|
|
return Array.from({ length }, (_, i) => i + offset).map((n) => ({
|
|
|
|
|
value: n.toString(),
|
|
|
|
|
label: `${64 * n}px`,
|
|
|
|
|
}));
|
|
|
|
|
};
|