dicomOverlaySlice.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * DICOM Overlay Redux Slice
  3. * 管理四角信息显示的状态
  4. */
  5. import { createSlice, PayloadAction } from '@reduxjs/toolkit';
  6. import { RootState } from '../store';
  7. import { OverlayConfig } from '@/config/overlayConfig/types/overlayConfig';
  8. import { ConfigSource } from '@/config/overlayConfig/OverlayConfigManager';
  9. /**
  10. * Overlay State 接口
  11. */
  12. export interface DicomOverlayState {
  13. /** 是否启用四角信息显示 */
  14. enabled: boolean;
  15. /** 配置源 */
  16. configSource: ConfigSource;
  17. /** 当前配置 */
  18. config: OverlayConfig | null;
  19. /** 加载状态 */
  20. loading: boolean;
  21. /** 错误信息 */
  22. error: string | null;
  23. }
  24. /**
  25. * 初始状态
  26. */
  27. const initialState: DicomOverlayState = {
  28. enabled: false,
  29. configSource: 'local',
  30. config: null,
  31. loading: false,
  32. error: null,
  33. };
  34. /**
  35. * DICOM Overlay Slice
  36. */
  37. const dicomOverlaySlice = createSlice({
  38. name: 'dicomOverlay',
  39. initialState,
  40. reducers: {
  41. /**
  42. * 切换 overlay 启用状态
  43. */
  44. toggleOverlay: (state) => {
  45. state.enabled = !state.enabled;
  46. console.log(`[dicomOverlaySlice] Overlay ${state.enabled ? 'enabled' : 'disabled'}`);
  47. },
  48. /**
  49. * 设置 overlay 启用状态
  50. */
  51. setOverlayEnabled: (state, action: PayloadAction<boolean>) => {
  52. state.enabled = action.payload;
  53. console.log(`[dicomOverlaySlice] Overlay set to ${state.enabled ? 'enabled' : 'disabled'}`);
  54. },
  55. /**
  56. * 设置配置源
  57. */
  58. setConfigSource: (state, action: PayloadAction<ConfigSource>) => {
  59. state.configSource = action.payload;
  60. // 切换配置源时清空当前配置,强制重新加载
  61. state.config = null;
  62. console.log(`[dicomOverlaySlice] Config source set to ${action.payload}`);
  63. },
  64. /**
  65. * 更新配置
  66. */
  67. updateConfig: (state, action: PayloadAction<OverlayConfig>) => {
  68. state.config = action.payload;
  69. state.error = null;
  70. console.log('[dicomOverlaySlice] Config updated');
  71. },
  72. /**
  73. * 开始加载配置
  74. */
  75. startLoading: (state) => {
  76. state.loading = true;
  77. state.error = null;
  78. },
  79. /**
  80. * 加载配置成功
  81. */
  82. loadConfigSuccess: (state, action: PayloadAction<OverlayConfig>) => {
  83. state.loading = false;
  84. state.config = action.payload;
  85. state.error = null;
  86. console.log('[dicomOverlaySlice] Config loaded successfully');
  87. },
  88. /**
  89. * 加载配置失败
  90. */
  91. loadConfigFailure: (state, action: PayloadAction<string>) => {
  92. state.loading = false;
  93. state.error = action.payload;
  94. console.error('[dicomOverlaySlice] Config load failed:', action.payload);
  95. },
  96. /**
  97. * 清除错误
  98. */
  99. clearError: (state) => {
  100. state.error = null;
  101. },
  102. /**
  103. * 重置 overlay 状态
  104. */
  105. resetOverlay: (state) => {
  106. state.enabled = false;
  107. state.configSource = 'local';
  108. state.config = null;
  109. state.loading = false;
  110. state.error = null;
  111. console.log('[dicomOverlaySlice] Overlay state reset');
  112. },
  113. },
  114. });
  115. // ==================== Actions ====================
  116. export const {
  117. toggleOverlay,
  118. setOverlayEnabled,
  119. setConfigSource,
  120. updateConfig,
  121. startLoading,
  122. loadConfigSuccess,
  123. loadConfigFailure,
  124. clearError,
  125. resetOverlay,
  126. } = dicomOverlaySlice.actions;
  127. // ==================== Selectors ====================
  128. /**
  129. * 选择 overlay 是否启用
  130. */
  131. export const selectOverlayEnabled = (state: RootState): boolean =>
  132. state.dicomOverlay.enabled;
  133. /**
  134. * 选择配置源
  135. */
  136. export const selectConfigSource = (state: RootState): ConfigSource =>
  137. state.dicomOverlay.configSource;
  138. /**
  139. * 选择当前配置
  140. */
  141. export const selectOverlayConfig = (state: RootState): OverlayConfig | null =>
  142. state.dicomOverlay.config;
  143. /**
  144. * 选择加载状态
  145. */
  146. export const selectOverlayLoading = (state: RootState): boolean =>
  147. state.dicomOverlay.loading;
  148. /**
  149. * 选择错误信息
  150. */
  151. export const selectOverlayError = (state: RootState): string | null =>
  152. state.dicomOverlay.error;
  153. /**
  154. * 选择完整的 overlay 状态
  155. */
  156. export const selectDicomOverlayState = (state: RootState): DicomOverlayState =>
  157. state.dicomOverlay;
  158. // ==================== Reducer ====================
  159. export default dicomOverlaySlice.reducer;