Pārlūkot izejas kodu

fix(exam): preserve body position on exam->process switch

Fixed body position reset to 1st exposed position when auto-switching
to process mode after TASK_SUCCESS. Now correctly preserves user's
selected position using Redux state flags.

Changes:
- BusinessFlowSlice: Add shouldKeepSelection state & conditional clearing
- BodyPositionList: Check Redux flag to skip auto-selection
dengdx 5 dienas atpakaļ
vecāks
revīzija
5a27949f76

+ 15 - 1
src/pages/exam/components/BodyPositionList.tsx

@@ -66,10 +66,24 @@ const BodyPositionList: React.FC<BodyPositionListProps> = ({
   // 🔧 修复:使用 ref 跟踪是否已经执行过初始化
   // 避免在 judgeImage 等操作导致 bodyPositions 引用改变时重复执行
   const hasInitializedRef = useRef(false);
+  
+  // 🆕 从 Redux 获取是否应该保持选中的标记
+  const shouldKeepSelection = useSelector(
+    (state: RootState) => state.BusinessFlow.shouldKeepSelection
+  );
 
   useEffect(() => {
     // 只在第一次体位列表有值时执行自动选择
     if (bodyPositions.length > 0 && !hasInitializedRef.current) {
+      // 🆕 如果标记了应该保持选中,则跳过自动选择
+      if (shouldKeepSelection) {
+        console.log(
+          '[BodyPositionList] Should keep selection, skip auto-select'
+        );
+        hasInitializedRef.current = true;
+        return;
+      }
+      
       hasInitializedRef.current = true;
       console.log(
         '[BodyPositionList] Auto-selecting first body position on component mount'
@@ -82,7 +96,7 @@ const BodyPositionList: React.FC<BodyPositionListProps> = ({
         }
       );
     }
-  }, [bodyPositions, dispatch, currentKey]);
+  }, [bodyPositions, dispatch, currentKey, shouldKeepSelection]);
 
   const [isAppendModalOpen, setIsAppendModalOpen] = useState(false);
 

+ 47 - 5
src/states/BusinessFlowSlice.ts

@@ -1,15 +1,20 @@
 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({
@@ -18,16 +23,53 @@ const BusinessFlowSlice = createSlice({
   reducers: {
     setBusinessFlow: (state, action: PayloadAction<string>) => {
       const lastKey = state.currentKey; // Save the last key before changing
-      //state={lastKey:lastKey,currentKey:action.payload}; // Update state with new key and last key
-
-      return { lastKey: lastKey, currentKey: action.payload };
+      
+      // 🆕 只在 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', () => {
+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 } = BusinessFlowSlice.actions;
+export const { setBusinessFlow, setKeepSelection, clearKeepSelection } = BusinessFlowSlice.actions;
 export default BusinessFlowSlice.reducer;