BusinessFlowSlice.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { createSlice, PayloadAction } from '@reduxjs/toolkit';
  2. import emitter from '../utils/eventEmitter';
  3. import store from './store';
  4. import { MqttMessage } from '../domain/mqttService';
  5. export interface BusinessFlowState {
  6. currentKey: string;
  7. lastKey?: string; // Optional
  8. shouldKeepSelection?: boolean; // 🆕 标记是否应该保持选中的体位
  9. keptSelectionSopUid?: string; // 🆕 要保持选中的体位SOP UID
  10. }
  11. const initialState: BusinessFlowState = {
  12. currentKey: 'register',
  13. lastKey: '',
  14. shouldKeepSelection: false,
  15. keptSelectionSopUid: undefined,
  16. };
  17. const BusinessFlowSlice = createSlice({
  18. name: 'BusinessFlow',
  19. initialState,
  20. reducers: {
  21. setBusinessFlow: (state, action: PayloadAction<string>) => {
  22. const lastKey = state.currentKey; // Save the last key before changing
  23. // 🆕 只在 exam->process 的流程切换时保持标记(TASK_SUCCESS 场景)
  24. const isExamToProcess = lastKey === 'exam' && action.payload === 'process';
  25. return {
  26. ...state,
  27. lastKey: lastKey,
  28. currentKey: action.payload,
  29. // 🆕 exam->process 时保持标记,其他情况清除
  30. shouldKeepSelection: isExamToProcess ? state.shouldKeepSelection : false,
  31. keptSelectionSopUid: isExamToProcess ? state.keptSelectionSopUid : undefined,
  32. };
  33. },
  34. setKeepSelection: (state, action: PayloadAction<{ sopUid: string }>) => {
  35. state.shouldKeepSelection = true;
  36. state.keptSelectionSopUid = action.payload.sopUid;
  37. console.log(
  38. `[BusinessFlowSlice] setKeepSelection: ${action.payload.sopUid}`
  39. );
  40. },
  41. clearKeepSelection: (state) => {
  42. state.shouldKeepSelection = false;
  43. state.keptSelectionSopUid = undefined;
  44. },
  45. },
  46. });
  47. emitter.on('TASK_SUCCESS', (message: MqttMessage) => {
  48. console.log('[BusinessFlowSlice] TASK_SUCCESS received, switching to process');
  49. // 获取当前选中的体位
  50. const state = store.getState();
  51. const selectedBodyPosition = state.bodyPositionList.selectedBodyPosition;
  52. // 🆕 先标记要保持选中(在切换流程之前)
  53. if (selectedBodyPosition) {
  54. console.log(
  55. `[BusinessFlowSlice] Marking to keep selected position: ${selectedBodyPosition.sop_instance_uid}`
  56. );
  57. store.dispatch(setKeepSelection({
  58. sopUid: selectedBodyPosition.sop_instance_uid
  59. }));
  60. }
  61. // 然后切换到 process 模式
  62. store.dispatch(setBusinessFlow('process'));
  63. });
  64. export const { setBusinessFlow, setKeepSelection, clearKeepSelection } = BusinessFlowSlice.actions;
  65. export default BusinessFlowSlice.reducer;