| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- /**
- * DICOM Overlay Redux Slice
- * 管理四角信息显示的状态
- */
- import { createSlice, PayloadAction } from '@reduxjs/toolkit';
- import { RootState } from '../store';
- import { OverlayConfig } from '@/config/overlayConfig/types/overlayConfig';
- import { ConfigSource } from '@/config/overlayConfig/OverlayConfigManager';
- /**
- * Overlay State 接口
- */
- export interface DicomOverlayState {
- /** 是否启用四角信息显示 */
- enabled: boolean;
-
- /** 配置源 */
- configSource: ConfigSource;
-
- /** 当前配置 */
- config: OverlayConfig | null;
-
- /** 加载状态 */
- loading: boolean;
-
- /** 错误信息 */
- error: string | null;
- }
- /**
- * 初始状态
- */
- const initialState: DicomOverlayState = {
- enabled: false,
- configSource: 'local',
- config: null,
- loading: false,
- error: null,
- };
- /**
- * DICOM Overlay Slice
- */
- const dicomOverlaySlice = createSlice({
- name: 'dicomOverlay',
- initialState,
- reducers: {
- /**
- * 切换 overlay 启用状态
- */
- toggleOverlay: (state) => {
- state.enabled = !state.enabled;
- console.log(`[dicomOverlaySlice] Overlay ${state.enabled ? 'enabled' : 'disabled'}`);
- },
- /**
- * 设置 overlay 启用状态
- */
- setOverlayEnabled: (state, action: PayloadAction<boolean>) => {
- state.enabled = action.payload;
- console.log(`[dicomOverlaySlice] Overlay set to ${state.enabled ? 'enabled' : 'disabled'}`);
- },
- /**
- * 设置配置源
- */
- setConfigSource: (state, action: PayloadAction<ConfigSource>) => {
- state.configSource = action.payload;
- // 切换配置源时清空当前配置,强制重新加载
- state.config = null;
- console.log(`[dicomOverlaySlice] Config source set to ${action.payload}`);
- },
- /**
- * 更新配置
- */
- updateConfig: (state, action: PayloadAction<OverlayConfig>) => {
- state.config = action.payload;
- state.error = null;
- console.log('[dicomOverlaySlice] Config updated');
- },
- /**
- * 开始加载配置
- */
- startLoading: (state) => {
- state.loading = true;
- state.error = null;
- },
- /**
- * 加载配置成功
- */
- loadConfigSuccess: (state, action: PayloadAction<OverlayConfig>) => {
- state.loading = false;
- state.config = action.payload;
- state.error = null;
- console.log('[dicomOverlaySlice] Config loaded successfully');
- },
- /**
- * 加载配置失败
- */
- loadConfigFailure: (state, action: PayloadAction<string>) => {
- state.loading = false;
- state.error = action.payload;
- console.error('[dicomOverlaySlice] Config load failed:', action.payload);
- },
- /**
- * 清除错误
- */
- clearError: (state) => {
- state.error = null;
- },
- /**
- * 重置 overlay 状态
- */
- resetOverlay: (state) => {
- state.enabled = false;
- state.configSource = 'local';
- state.config = null;
- state.loading = false;
- state.error = null;
- console.log('[dicomOverlaySlice] Overlay state reset');
- },
- },
- });
- // ==================== Actions ====================
- export const {
- toggleOverlay,
- setOverlayEnabled,
- setConfigSource,
- updateConfig,
- startLoading,
- loadConfigSuccess,
- loadConfigFailure,
- clearError,
- resetOverlay,
- } = dicomOverlaySlice.actions;
- // ==================== Selectors ====================
- /**
- * 选择 overlay 是否启用
- */
- export const selectOverlayEnabled = (state: RootState): boolean =>
- state.dicomOverlay.enabled;
- /**
- * 选择配置源
- */
- export const selectConfigSource = (state: RootState): ConfigSource =>
- state.dicomOverlay.configSource;
- /**
- * 选择当前配置
- */
- export const selectOverlayConfig = (state: RootState): OverlayConfig | null =>
- state.dicomOverlay.config;
- /**
- * 选择加载状态
- */
- export const selectOverlayLoading = (state: RootState): boolean =>
- state.dicomOverlay.loading;
- /**
- * 选择错误信息
- */
- export const selectOverlayError = (state: RootState): string | null =>
- state.dicomOverlay.error;
- /**
- * 选择完整的 overlay 状态
- */
- export const selectDicomOverlayState = (state: RootState): DicomOverlayState =>
- state.dicomOverlay;
- // ==================== Reducer ====================
- export default dicomOverlaySlice.reducer;
|