* refactor: remove central validation export to improve typescript performance * fix: missing package exports change in validation package * chore: address pull request feedback
46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
import { useCallback } from "react";
|
|
import type { z } from "zod";
|
|
|
|
import { useUpdateBoard } from "@homarr/boards/updater";
|
|
import type { dynamicSectionOptionsSchema } from "@homarr/validation/shared";
|
|
|
|
import { addDynamicSectionCallback } from "./actions/add-dynamic-section";
|
|
import type { RemoveDynamicSectionInput } from "./actions/remove-dynamic-section";
|
|
import { removeDynamicSectionCallback } from "./actions/remove-dynamic-section";
|
|
|
|
interface UpdateDynamicOptions {
|
|
itemId: string;
|
|
newOptions: z.infer<typeof dynamicSectionOptionsSchema>;
|
|
}
|
|
|
|
export const useDynamicSectionActions = () => {
|
|
const { updateBoard } = useUpdateBoard();
|
|
|
|
const addDynamicSection = useCallback(() => {
|
|
updateBoard(addDynamicSectionCallback());
|
|
}, [updateBoard]);
|
|
|
|
const updateDynamicSection = useCallback(
|
|
({ itemId, newOptions }: UpdateDynamicOptions) => {
|
|
updateBoard((previous) => ({
|
|
...previous,
|
|
sections: previous.sections.map((item) => (item.id !== itemId ? item : { ...item, options: newOptions })),
|
|
}));
|
|
},
|
|
[updateBoard],
|
|
);
|
|
|
|
const removeDynamicSection = useCallback(
|
|
(input: RemoveDynamicSectionInput) => {
|
|
updateBoard(removeDynamicSectionCallback(input));
|
|
},
|
|
[updateBoard],
|
|
);
|
|
|
|
return {
|
|
addDynamicSection,
|
|
updateDynamicSection,
|
|
removeDynamicSection,
|
|
};
|
|
};
|