refactor: moved addNode function outside the handleNode definition

Made sure to minimize responsibilities per function, by taking the addNode logic and moving it into its own function.

ref: N25B-114
This commit is contained in:
JGerla
2025-10-25 18:29:53 +02:00
parent e1d131e642
commit b7cd925c2c

View File

@@ -68,38 +68,13 @@ function DraggableNode({className, children, nodeType, onDrop}: DraggableNodePro
); );
} }
/** function addNode(nodeType: string, position: XYPosition) {
* the DndToolbar defines how the drag and drop toolbar component works const {setNodes} = useFlowStore.getState();
* and includes the default onDrop behavior through handleNodeDrop const nds = useFlowStore.getState().nodes;
* @constructor
*/
export function DndToolbar() {
const {screenToFlowPosition} = useReactFlow();
const {setNodes} = useReactFlow();
/**
* handleNodeDrop implements the default onDrop behavior
*/
const handleNodeDrop = useCallback(
(nodeType: string, screenPosition: XYPosition) => {
const flow = document.querySelector('.react-flow');
const flowRect = flow?.getBoundingClientRect();
const isInFlow =
flowRect &&
screenPosition.x >= flowRect.left &&
screenPosition.x <= flowRect.right &&
screenPosition.y >= flowRect.top &&
screenPosition.y <= flowRect.bottom;
// Create a new node and add it to the flow
if (isInFlow) {
const position = screenToFlowPosition(screenPosition);
const newNode = () => { const newNode = () => {
switch (nodeType) { switch (nodeType) {
case "phase": case "phase":
{ {
const nds = useFlowStore.getState().nodes;
const phaseNumber = nds.filter((node) => node.type === 'phase').length; const phaseNumber = nds.filter((node) => node.type === 'phase').length;
return { return {
id: `phase-${phaseNumber}`, id: `phase-${phaseNumber}`,
@@ -108,7 +83,6 @@ export function DndToolbar() {
data: {label: `"new"`, number: phaseNumber}, data: {label: `"new"`, number: phaseNumber},
}; };
} }
case "start": case "start":
return { return {
id: getId(), id: getId(),
@@ -125,7 +99,6 @@ export function DndToolbar() {
}; };
case "norm": case "norm":
{ {
const nds = useFlowStore.getState().nodes;
const normNumber = nds.filter((node) => node.type === 'norm').length; const normNumber = nds.filter((node) => node.type === 'norm').length;
return { return {
id: `norm-${normNumber}`, id: `norm-${normNumber}`,
@@ -145,10 +118,37 @@ export function DndToolbar() {
} }
} }
setNodes((nds) => nds.concat(newNode())); setNodes(nds.concat(newNode()));
}
/**
* the DndToolbar defines how the drag and drop toolbar component works
* and includes the default onDrop behavior through handleNodeDrop
* @constructor
*/
export function DndToolbar() {
const {screenToFlowPosition} = useReactFlow();
/**
* handleNodeDrop implements the default onDrop behavior
*/
const handleNodeDrop = useCallback(
(nodeType: string, screenPosition: XYPosition) => {
const flow = document.querySelector('.react-flow');
const flowRect = flow?.getBoundingClientRect();
const isInFlow =
flowRect &&
screenPosition.x >= flowRect.left &&
screenPosition.x <= flowRect.right &&
screenPosition.y >= flowRect.top &&
screenPosition.y <= flowRect.bottom;
// Create a new node and add it to the flow
if (isInFlow) {
const position = screenToFlowPosition(screenPosition);
addNode(nodeType, position);
} }
}, },
[setNodes, screenToFlowPosition], [screenToFlowPosition],
); );
return ( return (