feat(toolbar): new Toolbar to enable reactive state synchronization (#3983)

This commit is contained in:
Alireza authored and GitHub committed 2024-03-27 16:01:32 -04:00
1 parent 79d5c36bda
commit 566b25a544
155 files changed
+6311 -6577

No files matched your search

@@ -73,7 +73,7 @@ export default function CineProvider({ children, service }) {
const api = {
getState,
setCine,
setIsCineEnabled,
setIsCineEnabled: isCineEnabled => service.setIsCineEnabled(isCineEnabled),
playClip: (element, playClipOptions) => service.playClip(element, playClipOptions),
stopClip: element => service.stopClip(element),
};
@@ -1,18 +1,17 @@
import React, { createContext, useContext, useReducer, useMemo } from 'react';
import React, { ReactNode, createContext, useContext, useMemo } from 'react';
// export const IMAGE_VIEWER_DEFAULT_VALUE = {
// StudyInstanceUIDs: [],
// setImageViewer: () => {},
// };
interface ImageViewerProviderProps {
StudyInstanceUIDs: string[];
children: ReactNode;
}
export const ImageViewerContext = createContext();
export const ImageViewerContext = createContext(null);
export const useImageViewer = () => useContext(ImageViewerContext);
export function ImageViewerProvider({ StudyInstanceUIDs, reducer, initialState, children }) {
export function ImageViewerProvider({ StudyInstanceUIDs, children }: ImageViewerProviderProps) {
const value = useMemo(() => {
return { StudyInstanceUIDs };
}, [StudyInstanceUIDs]);
return <ImageViewerContext.Provider value={value}>{children}</ImageViewerContext.Provider>;
}
export const useImageViewer = () => useContext(ImageViewerContext);
@@ -0,0 +1,106 @@
import React, { createContext, useContext, useReducer } from 'react';
export const initialState = {};
export const toolboxReducer = (state, action) => {
const { toolbarSectionId } = action.payload;
if (!state[toolbarSectionId]) {
state[toolbarSectionId] = { activeTool: null, toolOptions: {} };
}
switch (action.type) {
case 'SET_ACTIVE_TOOL':
return {
...state,
[toolbarSectionId]: {
...state[toolbarSectionId],
activeTool: action.payload.activeTool,
},
};
case 'UPDATE_TOOL_OPTION':
const { toolName, optionName, value } = action.payload;
return {
...state,
[toolbarSectionId]: {
...state[toolbarSectionId],
toolOptions: {
...state[toolbarSectionId].toolOptions,
[toolName]: state[toolbarSectionId].toolOptions[toolName].map(option =>
option.id === optionName ? { ...option, value } : option
),
},
},
};
case 'INITIALIZE_TOOL_OPTIONS':
// Initialize tool options for each toolbarSectionId
return {
...state,
[action.toolbarSectionId]: {
...state[action.toolbarSectionId],
toolOptions: action.payload,
},
};
default:
return state;
}
};
const ToolboxContext = createContext();
export const ToolboxProvider = ({ children }) => {
const [state, dispatch] = useReducer(toolboxReducer, initialState);
const handleToolSelect = (toolbarSectionId, toolName) => {
dispatch({
type: 'SET_ACTIVE_TOOL',
payload: { toolbarSectionId, activeTool: toolName },
});
};
const handleToolOptionChange = (toolbarSectionId, toolName, optionName, newValue) => {
dispatch({
type: 'UPDATE_TOOL_OPTION',
payload: { toolbarSectionId, toolName, optionName, value: newValue },
});
};
const initializeToolOptions = (toolbarSectionId, toolOptions) => {
dispatch({
type: 'INITIALIZE_TOOL_OPTIONS',
toolbarSectionId,
payload: toolOptions,
});
};
const api = { handleToolSelect, handleToolOptionChange, initializeToolOptions };
const value = { state, api };
return <ToolboxContext.Provider value={value}>{children}</ToolboxContext.Provider>;
};
/**
* Custom hook for accessing toolbox state and actions for a specific toolbar section.
* You can use this hook to access the state and actions for a specific toolbar section (
* defined by the toolbarSectionId) in your custom toolbar components. This hook
* helps to manage the state and actions for the tools and their options in the toolbar.
*/
export const useToolbox = toolbarSectionId => {
const context = useContext(ToolboxContext);
if (context === undefined) {
throw new Error('useToolbox must be used within a ToolboxProvider');
}
const { state, api } = context;
return {
state: state[toolbarSectionId] || { activeTool: null, toolOptions: {} },
api: {
handleToolSelect: toolName => api.handleToolSelect(toolbarSectionId, toolName),
handleToolOptionChange: (toolName, optionName, value) =>
api.handleToolOptionChange(toolbarSectionId, toolName, optionName, value),
initializeToolOptions: toolOptions =>
api.initializeToolOptions(toolbarSectionId, toolOptions),
},
};
};
@@ -0,0 +1,66 @@
export function toolboxReducer(state, action) {
let newState = { ...state };
const { payload } = action;
const { stateSyncService, buttonSectionId } = payload || {};
switch (action.type) {
case 'SET_ACTIVE_TOOL':
newState = {
...state,
activeTool: payload.activeTool,
};
break;
case 'UPDATE_TOOL_OPTION':
newState = {
...state,
toolOptions: {
...state.toolOptions,
[payload.toolName]: state.toolOptions[payload.toolName].map(option =>
option.id === payload.optionName ? { ...option, value: payload.value } : option
),
},
};
break;
case 'INITIALIZE_TOOL_OPTIONS':
// eslint-disable-next-line no-case-declarations
const newToolOptions = Object.keys(payload?.toolOptions || {}).reduce((acc, toolId) => {
const tool = payload.toolOptions[toolId];
if (state.toolOptions[toolId]) {
// Preserve existing options, potentially merging with new ones if necessary
acc[toolId] = state.toolOptions[toolId].map(existingOption => {
const initialOption = tool.find(option => option.id === existingOption.id);
return initialOption
? { ...initialOption, value: existingOption.value }
: existingOption;
});
} else {
acc[toolId] = tool;
}
return acc;
}, {});
newState = {
...state,
toolOptions: newToolOptions,
};
break;
default:
break;
}
// store the state in the stateSyncService
if (payload?.stateSyncService) {
const prevState = stateSyncService.getState()?.['uiStateStore']?.[buttonSectionId];
stateSyncService.store({
uiStateStore: {
...stateSyncService.getState()?.['uiStateStore'],
[buttonSectionId]: {
...prevState,
...newState,
},
},
});
}
return newState;
}
@@ -16,6 +16,7 @@ interface Viewport {
width: number;
height: number;
viewportLabel: any;
isReady: boolean;
}
interface Layout {
@@ -45,6 +46,7 @@ const DEFAULT_STATE: DefaultState = {
default: {
viewportId: 'default',
displaySetInstanceUIDs: [],
isReady: false,
viewportOptions: {
viewportId: 'default',
},
@@ -252,6 +254,7 @@ export function ViewportGridProvider({ children, service }) {
});
viewport.viewportLabel = getViewportLabel(viewports, viewport.viewportId);
viewport.isReady = false;
if (!viewport.viewportOptions.presentationIds) {
viewport.viewportOptions.presentationIds = ViewportGridService.getPresentationIds(
@@ -289,6 +292,25 @@ export function ViewportGridProvider({ children, service }) {
};
}
case 'VIEWPORT_IS_READY': {
const { viewportId, isReady } = action.payload;
const viewports = new Map(state.viewports);
const viewport = viewports.get(viewportId);
if (!viewport) {
return;
}
viewports.set(viewportId, {
...viewport,
isReady,
});
return {
...state,
viewports,
};
}
default:
return action.payload;
}
@@ -319,6 +341,25 @@ export function ViewportGridProvider({ children, service }) {
[dispatch]
);
const setViewportIsReady = useCallback(
(viewportId, isReady) => {
dispatch({
type: 'VIEWPORT_IS_READY',
payload: {
viewportId,
isReady,
},
});
},
[dispatch, viewportGridState]
);
const getGridViewportsReady = useCallback(() => {
const { viewports } = viewportGridState;
const readyViewports = Array.from(viewports.values()).filter(viewport => viewport.isReady);
return readyViewports.length === viewports.size;
}, [viewportGridState]);
const setLayout = useCallback(
({
layoutType,
@@ -382,6 +423,8 @@ export function ViewportGridProvider({ children, service }) {
onModeExit: reset,
set,
getNumViewportPanes,
setViewportIsReady,
getGridViewportsReady,
});
}
}, [
@@ -393,6 +436,8 @@ export function ViewportGridProvider({ children, service }) {
reset,
set,
getNumViewportPanes,
setViewportIsReady,
getGridViewportsReady,
]);
// run many of the calls through the service itself since we want to publish events
@@ -405,8 +450,11 @@ export function ViewportGridProvider({ children, service }) {
reset: () => service.reset(),
set: gridLayoutState => service.setState(gridLayoutState), // run it through the service itself since we want to publish events
getNumViewportPanes,
setViewportIsReady,
getGridViewportsReady,
getActiveViewportOptionByKey,
setViewportGridSizeChanged: props => service.setViewportGridSizeChanged(props),
publishViewportsReady: () => service.publishViewportsReady(),
};
return (
@@ -14,6 +14,8 @@ export { default as ViewportDialogProvider, useViewportDialog } from './Viewport
export { ViewportGridContext, ViewportGridProvider, useViewportGrid } from './ViewportGridProvider';
export { useToolbox, ToolboxProvider } from './Toolbox/ToolboxContext';
export {
UserAuthenticationContext,
UserAuthenticationProvider,