Skip to content

Commit

Permalink
remove nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
toyamarinyon committed Oct 25, 2024
1 parent b9c1044 commit 139c235
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
19 changes: 18 additions & 1 deletion app/(playground)/p/[agentId]/beta-proto/graph/v2/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { GiselleNode, GiselleNodeId } from "../../giselle-node/types";
const v2NodeActionTypes = {
updateNode: "v2.updateNode",
add: "v2.addNode",
set: "v2.setNodes",
} as const;
type V2NodeActionType =
(typeof v2NodeActionTypes)[keyof typeof v2NodeActionTypes];
Expand Down Expand Up @@ -42,7 +43,21 @@ export function addNode({ input }: { input: AddNodeInput }): AddNodeAction {
};
}

export type V2NodeAction = UpdateNodeAction | AddNodeAction;
interface SetNodesAction {
type: Extract<V2NodeActionType, "v2.setNodes">;
input: SetNodeInput;
}
interface SetNodeInput {
nodes: GiselleNode[];
}
export function setNodes({ input }: { input: SetNodeInput }) {
return {
type: v2NodeActionTypes.set,
input,
};
}

export type V2NodeAction = UpdateNodeAction | AddNodeAction | SetNodesAction;

export function v2NodeReducer(
nodes: GiselleNode[],
Expand All @@ -61,6 +76,8 @@ export function v2NodeReducer(
});
case v2NodeActionTypes.add:
return [...nodes, action.input.node];
case v2NodeActionTypes.set:
return action.input.nodes;
}
return nodes;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import { useGraph } from "../graph/context";
import type { Graph } from "../graph/types";
import { removeConnector } from "../graph/v2/composition/remove-connector";
import { setNodes } from "../graph/v2/node";
import { setXyFlowEdges, setXyFlowNodes } from "../graph/v2/xy-flow";
import {
type ReactFlowEdge,
Expand Down Expand Up @@ -89,11 +90,19 @@ export const useReactFlowNodeEventHandler = () => {
}),
);
} else if (change.type === "remove") {
console.log(`remove node ${change.id}`);
dispatch(
setNodes({
input: {
nodes: state.graph.nodes.filter(
(node) => node.id !== change.id,
),
},
}),
);
}
});
},
[dispatch, state.graph.xyFlow.nodes],
[dispatch, state.graph.xyFlow.nodes, state.graph.nodes],
);
return { handleNodesChange };
};
Expand Down

0 comments on commit 139c235

Please sign in to comment.