import { createSlice, PayloadAction } from '@reduxjs/toolkit'; import { RootState } from '@/states/store'; import { workSelectionSlice } from './workSlice'; import { historySelectionSlice } from './history'; /** * 选中患者的照片信息状态 */ interface SelectedPatientState { patientId: string | null; patientName: string | null; portraitStatus: string | null; portraitFile: string | null; } const initialState: SelectedPatientState = { patientId: null, patientName: null, portraitStatus: null, portraitFile: null, }; /** * 选中患者照片状态管理 Slice * * 功能: * 1. 监听 workSelectionSlice 和 historySelectionSlice 的选中变化 * 2. 自动更新选中患者的照片信息 * 3. 提供清除选中状态的 action */ const selectedPatientSlice = createSlice({ name: 'selectedPatient', initialState, reducers: { /** * 手动更新选中患者信息 */ updateSelectedPatient: ( state, action: PayloadAction<{ patientId: string; patientName: string; portraitStatus?: string; portraitFile?: string; }> ) => { state.patientId = action.payload.patientId; state.patientName = action.payload.patientName; state.portraitStatus = action.payload.portraitStatus || null; state.portraitFile = action.payload.portraitFile || null; }, /** * 清除选中患者信息 */ clearSelectedPatient: (state) => { state.patientId = null; state.patientName = null; state.portraitStatus = null; state.portraitFile = null; }, }, extraReducers: (builder) => { // 监听 worklist 选中变化 builder.addCase( workSelectionSlice.actions.setSelectedIds, (state, action: PayloadAction) => { const selectedIds = action.payload; if (selectedIds.length === 0) { // 如果没有选中项,清空状态 state.patientId = null; state.patientName = null; state.portraitStatus = null; state.portraitFile = null; } else { // 选中了项目,但我们需要从 store 中获取完整的 Task 数据 // 这里只保存 patientId,实际的照片信息会在组件中从 workEntities 中获取 state.patientId = selectedIds[0]; // 只处理单选 // 其他字段会在组件中从 entities 获取 } } ); // 监听 historylist 选中变化 builder.addCase( historySelectionSlice.actions.setSelectedIds, (state, action: PayloadAction) => { const selectedIds = action.payload; if (selectedIds.length === 0) { // 如果没有选中项,清空状态 state.patientId = null; state.patientName = null; state.portraitStatus = null; state.portraitFile = null; } else { // 选中了项目 state.patientId = selectedIds[0]; // 只处理单选 // 其他字段会在组件中从 entities 获取 } } ); }, }); export const { updateSelectedPatient, clearSelectedPatient } = selectedPatientSlice.actions; // Selectors export const selectSelectedPatient = (state: RootState) => state.selectedPatient; export const selectPatientId = (state: RootState) => state.selectedPatient.patientId; export const selectPortraitInfo = (state: RootState) => ({ portraitStatus: state.selectedPatient.portraitStatus, portraitFile: state.selectedPatient.portraitFile, }); export default selectedPatientSlice.reducer;