import { createSlice } from '@reduxjs/toolkit'; const initialState = { workflow: null, nodes: [], connections: [], isEditing: false, selectedNode: null, }; const workflowSlice = createSlice({ name: 'workflow', initialState, reducers: { setWorkflow: (state, action) => { state.workflow = action.payload; state.nodes = action.payload?.nodes || []; state.connections = action.payload?.connections || []; }, addNode: (state, action) => { state.nodes.push(action.payload); }, updateNode: (state, action) => { const index = state.nodes.findIndex(n => n.id === action.payload.id); if (index !== -1) { state.nodes[index] = action.payload; } }, removeNode: (state, action) => { state.nodes = state.nodes.filter(n => n.id !== action.payload); // 同时删除相关连线 state.connections = state.connections.filter( c => c.source !== action.payload && c.target !== action.payload ); }, addConnection: (state, action) => { state.connections.push(action.payload); }, removeConnection: (state, action) => { state.connections = state.connections.filter( c => !(c.source === action.payload.source && c.target === action.payload.target) ); }, setEditing: (state, action) => { state.isEditing = action.payload; }, selectNode: (state, action) => { state.selectedNode = action.payload; }, clearWorkflow: (state) => { state.workflow = null; state.nodes = []; state.connections = []; state.selectedNode = null; } }, }); export const { setWorkflow, addNode, updateNode, removeNode, addConnection, removeConnection, setEditing, selectNode, clearWorkflow, } = workflowSlice.actions; export default workflowSlice.reducer;