Files
homarr/apps/nextjs/src/components/board/sections/section-actions.tsx
2025-02-07 22:10:35 +01:00

66 lines
1.6 KiB
TypeScript

import { useCallback } from "react";
import { useUpdateBoard } from "@homarr/boards/updater";
interface MoveAndResizeInnerSection {
innerSectionId: string;
xOffset: number;
yOffset: number;
width: number;
height: number;
}
interface MoveInnerSectionToSection {
innerSectionId: string;
sectionId: string;
xOffset: number;
yOffset: number;
width: number;
height: number;
}
export const useSectionActions = () => {
const { updateBoard } = useUpdateBoard();
const moveAndResizeInnerSection = useCallback(
({ innerSectionId, ...positionProps }: MoveAndResizeInnerSection) => {
updateBoard((previous) => ({
...previous,
sections: previous.sections.map((section) => {
// Return same section if section is not the one we're moving
if (section.id !== innerSectionId) return section;
return {
...section,
...positionProps,
};
}),
}));
},
[updateBoard],
);
const moveInnerSectionToSection = useCallback(
({ innerSectionId, sectionId, ...positionProps }: MoveInnerSectionToSection) => {
updateBoard((previous) => {
return {
...previous,
sections: previous.sections.map((section) => {
// Return section without changes when not the section we're moving
if (section.id !== innerSectionId) return section;
return {
...section,
...positionProps,
parentSectionId: sectionId,
};
}),
};
});
},
[updateBoard],
);
return {
moveAndResizeInnerSection,
moveInnerSectionToSection,
};
};