Files
Homarr/src/components/Dashboard/Modals/ChangePosition/ChangeWidgetPositionModal.tsx

98 lines
3.3 KiB
TypeScript
Raw Normal View History

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';
import widgets from '../../../../widgets';
import { WidgetChangePositionModalInnerProps } from '../../Tiles/Widgets/WidgetsMenu';
import { useGridstackStore, useWrapperColumnCount } from '../../Wrappers/gridstack/store';
2022-12-11 13:12:39 +01:00
import { ChangePositionModal } from './ChangePositionModal';
export const ChangeWidgetPositionModal = ({
2022-12-11 13:12:39 +01:00
context,
id,
innerProps,
}: ContextModalProps<WidgetChangePositionModalInnerProps>) => {
2022-12-11 13:12:39 +01:00
const { name: configName } = useConfigContext();
const updateConfig = useConfigStore((x) => x.updateConfig);
2023-01-06 22:46:07 +01:00
const shapeSize = useGridstackStore(x => x.currentShapeSize);
2022-12-11 13:12:39 +01:00
const handleSubmit = (x: number, y: number, width: number, height: number) => {
if (!configName) {
return;
}
updateConfig(
configName,
(prev) => {
2022-12-20 11:45:33 +09:00
const currentWidget = prev.widgets.find((x) => x.id === innerProps.widgetId);
2023-01-06 22:46:07 +01:00
currentWidget!.shape[shapeSize] = {
location: {
x,
y,
},
size: {
height,
width,
},
};
return {
...prev,
widgets: [...prev.widgets.filter((x) => x.id !== innerProps.widgetId), currentWidget!],
};
},
true
);
2022-12-11 13:12:39 +01:00
context.closeModal(id);
};
const handleCancel = () => {
closeModal(id);
};
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}
2023-01-06 22:46:07 +01:00
initialX={innerProps.widget.shape[shapeSize].location.x}
initialY={innerProps.widget.shape[shapeSize].location.y}
initialWidth={innerProps.widget.shape[shapeSize].size.width}
initialHeight={innerProps.widget.shape[shapeSize].size.height}
2022-12-11 13:12:39 +01:00
/>
);
};
const useWidthData = (integration: string): SelectItem[] => {
const wrapperColumnCount = useWrapperColumnCount();
const currentWidget = widgets[integration as keyof typeof widgets];
if (!currentWidget) return [];
const offset = currentWidget.gridstack.minWidth ?? 2;
const length = (currentWidget.gridstack.maxWidth > wrapperColumnCount!
? wrapperColumnCount!
: currentWidget.gridstack.maxWidth) - offset;
return Array.from({ length: length + 1 }, (_, i) => i + offset).map((n) => ({
2022-12-11 13:12:39 +01:00
value: n.toString(),
// eslint-disable-next-line no-mixed-operators
label: `${(100 / wrapperColumnCount! * n).toFixed(2)}%`,
2022-12-11 13:12:39 +01:00
}));
};
const useHeightData = (integration: string): SelectItem[] => {
const mainAreaWidth = useGridstackStore((x) => x.mainAreaWidth);
const wrapperColumnCount = useWrapperColumnCount();
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: `${(mainAreaWidth! / wrapperColumnCount!) * n}px`,
2022-12-11 13:12:39 +01:00
}));
};