import { createSlice, PayloadAction } from '@reduxjs/toolkit'; import emitter from '../utils/eventEmitter'; import store from './store'; import { MqttMessage } from '../domain/mqttService'; export interface BusinessFlowState { currentKey: string; lastKey?: string; // Optional shouldKeepSelection?: boolean; // 🆕 标记是否应该保持选中的体位 keptSelectionSopUid?: string; // 🆕 要保持选中的体位SOP UID } const initialState: BusinessFlowState = { currentKey: 'register', lastKey: '', shouldKeepSelection: false, keptSelectionSopUid: undefined, }; const BusinessFlowSlice = createSlice({ name: 'BusinessFlow', initialState, reducers: { setBusinessFlow: (state, action: PayloadAction) => { const lastKey = state.currentKey; // Save the last key before changing // 🆕 只在 exam->process 的流程切换时保持标记(TASK_SUCCESS 场景) const isExamToProcess = lastKey === 'exam' && action.payload === 'process'; return { ...state, lastKey: lastKey, currentKey: action.payload, // 🆕 exam->process 时保持标记,其他情况清除 shouldKeepSelection: isExamToProcess ? state.shouldKeepSelection : false, keptSelectionSopUid: isExamToProcess ? state.keptSelectionSopUid : undefined, }; }, setKeepSelection: (state, action: PayloadAction<{ sopUid: string }>) => { state.shouldKeepSelection = true; state.keptSelectionSopUid = action.payload.sopUid; console.log( `[BusinessFlowSlice] setKeepSelection: ${action.payload.sopUid}` ); }, clearKeepSelection: (state) => { state.shouldKeepSelection = false; state.keptSelectionSopUid = undefined; }, }, }); emitter.on('TASK_SUCCESS', (message: MqttMessage) => { console.log('[BusinessFlowSlice] TASK_SUCCESS received, switching to process'); // 获取当前选中的体位 const state = store.getState(); const selectedBodyPosition = state.bodyPositionList.selectedBodyPosition; // 🆕 先标记要保持选中(在切换流程之前) if (selectedBodyPosition) { console.log( `[BusinessFlowSlice] Marking to keep selected position: ${selectedBodyPosition.sop_instance_uid}` ); store.dispatch(setKeepSelection({ sopUid: selectedBodyPosition.sop_instance_uid })); } // 然后切换到 process 模式 store.dispatch(setBusinessFlow('process')); }); export const { setBusinessFlow, setKeepSelection, clearKeepSelection } = BusinessFlowSlice.actions; export default BusinessFlowSlice.reducer;