OHIF with Cornerstone3D 2.0 (#4347)
This commit is contained in:
1 parent
8981a61b8c
commit
7ce7127d7c
803 files changed
+32760
-15893
No files matched your search
@@ -0,0 +1,4 @@
|
||||
export { useLutPresentationStore } from './useLutPresentationStore';
|
||||
export { usePositionPresentationStore } from './usePositionPresentationStore';
|
||||
export { useSegmentationPresentationStore } from './useSegmentationPresentationStore';
|
||||
export { useSynchronizersStore } from './useSynchronizersStore';
|
||||
@@ -0,0 +1,42 @@
|
||||
const JOIN_STR = '&';
|
||||
|
||||
// The default lut presentation id if none defined
|
||||
const DEFAULT_STR = 'default';
|
||||
|
||||
// This code finds the first unique index to add to the presentation id so that
|
||||
// two viewports containing the same display set in the same type of viewport
|
||||
// can have different presentation information. This allows comparison of
|
||||
// a single display set in two or more viewports, when the user has simply
|
||||
// dragged and dropped the view in twice. For example, it allows displaying
|
||||
// bone, brain and soft tissue views of a single display set, and to still
|
||||
// remember the specific changes to each viewport.
|
||||
const addUniqueIndex = (
|
||||
arr,
|
||||
key,
|
||||
viewports: AppTypes.ViewportGrid.Viewports,
|
||||
isUpdatingSameViewport
|
||||
) => {
|
||||
arr.push(0);
|
||||
|
||||
// If we are updating the viewport, we should not increment the index
|
||||
if (isUpdatingSameViewport) {
|
||||
return;
|
||||
}
|
||||
|
||||
// The 128 is just a value that is larger than how many viewports we
|
||||
// display at once, used as an upper bound on how many unique presentation
|
||||
// ID's might exist for a single display set at once.
|
||||
for (let displayInstance = 0; displayInstance < 128; displayInstance++) {
|
||||
arr[arr.length - 1] = displayInstance;
|
||||
const testId = arr.join(JOIN_STR);
|
||||
if (
|
||||
!Array.from(viewports.values()).find(
|
||||
viewport => viewport.viewportOptions?.presentationIds?.[key] === testId
|
||||
)
|
||||
) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export { addUniqueIndex, DEFAULT_STR, JOIN_STR };
|
||||
@@ -0,0 +1,166 @@
|
||||
import { create } from 'zustand';
|
||||
import { devtools } from 'zustand/middleware';
|
||||
import { LutPresentation } from '../types/Presentation';
|
||||
import { addUniqueIndex, DEFAULT_STR, JOIN_STR } from './presentationUtils';
|
||||
|
||||
/**
|
||||
* Identifier for the LUT Presentation store type.
|
||||
*/
|
||||
const PRESENTATION_TYPE_ID = 'lutPresentationId';
|
||||
|
||||
/**
|
||||
* Flag to enable or disable debug mode for the store.
|
||||
* Set to `true` to enable zustand devtools.
|
||||
*/
|
||||
const DEBUG_STORE = false;
|
||||
|
||||
/**
|
||||
* Represents the state and actions for managing LUT presentations.
|
||||
*/
|
||||
type LutPresentationState = {
|
||||
/**
|
||||
* Type identifier for the store.
|
||||
*/
|
||||
type: string;
|
||||
|
||||
/**
|
||||
* Stores LUT presentations indexed by their presentation ID.
|
||||
*/
|
||||
lutPresentationStore: Record<string, LutPresentation>;
|
||||
|
||||
/**
|
||||
* Sets the LUT presentation for a given key.
|
||||
*
|
||||
* @param key - The key identifying the LUT presentation.
|
||||
* @param value - The `LutPresentation` to associate with the key.
|
||||
*/
|
||||
setLutPresentation: (key: string, value: LutPresentation) => void;
|
||||
|
||||
/**
|
||||
* Clears all LUT presentations from the store.
|
||||
*/
|
||||
clearLutPresentationStore: () => void;
|
||||
|
||||
/**
|
||||
* Retrieves the presentation ID based on the provided parameters.
|
||||
*
|
||||
* @param id - The presentation ID to check.
|
||||
* @param options - Configuration options.
|
||||
* @param options.viewport - The current viewport in grid
|
||||
* @param options.viewports - All available viewports in grid
|
||||
* @param options.isUpdatingSameViewport - Indicates if the same viewport is being updated.
|
||||
* @returns The presentation ID or undefined.
|
||||
*/
|
||||
getPresentationId: (
|
||||
id: string,
|
||||
options: {
|
||||
viewport: AppTypes.ViewportGrid.Viewport;
|
||||
viewports: AppTypes.ViewportGrid.Viewports;
|
||||
isUpdatingSameViewport: boolean;
|
||||
}
|
||||
) => string | undefined;
|
||||
};
|
||||
|
||||
/**
|
||||
* Generates a presentation ID for LUT based on the viewport configuration.
|
||||
*
|
||||
* @param id - The ID to check.
|
||||
* @param options - Configuration options.
|
||||
* @param options.viewport - The current viewport.
|
||||
* @param options.viewports - All available viewports.
|
||||
* @param options.isUpdatingSameViewport - Indicates if the same viewport is being updated.
|
||||
* @returns The LUT presentation ID or undefined.
|
||||
*/
|
||||
const getLutPresentationId = (
|
||||
id: string,
|
||||
{
|
||||
viewport,
|
||||
viewports,
|
||||
isUpdatingSameViewport,
|
||||
}: {
|
||||
viewport: AppTypes.ViewportGrid.Viewport;
|
||||
viewports: AppTypes.ViewportGrid.Viewports;
|
||||
isUpdatingSameViewport: boolean;
|
||||
}
|
||||
): string | undefined => {
|
||||
if (id !== PRESENTATION_TYPE_ID) {
|
||||
return;
|
||||
}
|
||||
|
||||
const getLutId = (ds): string => {
|
||||
if (!ds || !ds.options) {
|
||||
return DEFAULT_STR;
|
||||
}
|
||||
if (ds.options.id) {
|
||||
return ds.options.id;
|
||||
}
|
||||
const arr = Object.entries(ds.options).map(([key, val]) => `${key}=${val}`);
|
||||
if (!arr.length) {
|
||||
return DEFAULT_STR;
|
||||
}
|
||||
return arr.join(JOIN_STR);
|
||||
};
|
||||
|
||||
if (!viewport || !viewport.viewportOptions || !viewport.displaySetInstanceUIDs?.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { displaySetOptions, displaySetInstanceUIDs } = viewport;
|
||||
const lutId = getLutId(displaySetOptions[0]);
|
||||
const lutPresentationArr = [lutId];
|
||||
|
||||
for (const uid of displaySetInstanceUIDs) {
|
||||
lutPresentationArr.push(uid);
|
||||
}
|
||||
|
||||
addUniqueIndex(lutPresentationArr, PRESENTATION_TYPE_ID, viewports, isUpdatingSameViewport);
|
||||
|
||||
return lutPresentationArr.join(JOIN_STR);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates the LUT Presentation store.
|
||||
*
|
||||
* @param set - The zustand set function.
|
||||
* @returns The LUT Presentation store state and actions.
|
||||
*/
|
||||
const createLutPresentationStore = (set): LutPresentationState => ({
|
||||
type: PRESENTATION_TYPE_ID,
|
||||
lutPresentationStore: {},
|
||||
|
||||
/**
|
||||
* Sets the LUT presentation for a given key.
|
||||
*/
|
||||
setLutPresentation: (key, value) =>
|
||||
set(
|
||||
state => ({
|
||||
lutPresentationStore: {
|
||||
...state.lutPresentationStore,
|
||||
[key]: value,
|
||||
},
|
||||
}),
|
||||
false,
|
||||
'setLutPresentation'
|
||||
),
|
||||
|
||||
/**
|
||||
* Clears all LUT presentations from the store.
|
||||
*/
|
||||
clearLutPresentationStore: () =>
|
||||
set({ lutPresentationStore: {} }, false, 'clearLutPresentationStore'),
|
||||
|
||||
/**
|
||||
* Retrieves the presentation ID based on the provided parameters.
|
||||
*/
|
||||
getPresentationId: getLutPresentationId,
|
||||
});
|
||||
|
||||
/**
|
||||
* Zustand store for managing LUT presentations.
|
||||
* Applies devtools middleware when DEBUG_STORE is enabled.
|
||||
*/
|
||||
export const useLutPresentationStore = create<LutPresentationState>()(
|
||||
DEBUG_STORE
|
||||
? devtools(createLutPresentationStore, { name: 'LutPresentationStore' })
|
||||
: createLutPresentationStore
|
||||
);
|
||||
@@ -0,0 +1,172 @@
|
||||
import { create } from 'zustand';
|
||||
import { devtools } from 'zustand/middleware';
|
||||
import { PositionPresentation } from '../types/Presentation';
|
||||
import { addUniqueIndex, JOIN_STR } from './presentationUtils';
|
||||
|
||||
const PRESENTATION_TYPE_ID = 'positionPresentationId';
|
||||
const DEBUG_STORE = true;
|
||||
|
||||
/**
|
||||
* Represents the state and actions for managing position presentations.
|
||||
*/
|
||||
type PositionPresentationState = {
|
||||
/**
|
||||
* Type identifier for the store.
|
||||
*/
|
||||
type: string;
|
||||
|
||||
/**
|
||||
* Stores position presentations indexed by their presentation ID.
|
||||
*/
|
||||
positionPresentationStore: Record<string, PositionPresentation>;
|
||||
|
||||
/**
|
||||
* Sets the position presentation for a given key.
|
||||
*
|
||||
* @param key - The key identifying the position presentation.
|
||||
* @param value - The `PositionPresentation` to associate with the key.
|
||||
*/
|
||||
setPositionPresentation: (key: string, value: PositionPresentation) => void;
|
||||
|
||||
/**
|
||||
* Clears all position presentations from the store.
|
||||
*/
|
||||
clearPositionPresentationStore: () => void;
|
||||
|
||||
/**
|
||||
* Retrieves the presentation ID based on the provided parameters.
|
||||
*
|
||||
* @param id - The ID to check.
|
||||
* @param options - Configuration options.
|
||||
* @param options.viewport - The current viewport.
|
||||
* @param options.viewports - All available viewports.
|
||||
* @param options.isUpdatingSameViewport - Indicates if the same viewport is being updated.
|
||||
* @returns The position presentation ID or undefined.
|
||||
*/
|
||||
getPresentationId: (
|
||||
id: string,
|
||||
options: {
|
||||
viewport: any;
|
||||
viewports: any;
|
||||
isUpdatingSameViewport: boolean;
|
||||
}
|
||||
) => string | undefined;
|
||||
|
||||
getPositionPresentationId: (
|
||||
viewport: any,
|
||||
viewports?: any,
|
||||
isUpdatingSameViewport?: boolean
|
||||
) => string | undefined;
|
||||
};
|
||||
|
||||
/**
|
||||
* Generates a position presentation ID based on the viewport configuration.
|
||||
*
|
||||
* @param id - The ID to check.
|
||||
* @param options - Configuration options.
|
||||
* @param options.viewport - The current viewport.
|
||||
* @param options.viewports - All available viewports.
|
||||
* @param options.isUpdatingSameViewport - Indicates if the same viewport is being updated.
|
||||
* @returns The position presentation ID or undefined.
|
||||
*/
|
||||
const getPresentationId = (
|
||||
id: string,
|
||||
{
|
||||
viewport,
|
||||
viewports,
|
||||
isUpdatingSameViewport,
|
||||
}: {
|
||||
viewport: any;
|
||||
viewports: any;
|
||||
isUpdatingSameViewport: boolean;
|
||||
}
|
||||
): string | undefined => {
|
||||
if (id !== PRESENTATION_TYPE_ID) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!viewport?.viewportOptions || !viewport.displaySetInstanceUIDs?.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
return getPositionPresentationId(viewport, viewports, isUpdatingSameViewport);
|
||||
};
|
||||
|
||||
function getPositionPresentationId(viewport, viewports, isUpdatingSameViewport) {
|
||||
const { viewportOptions = {}, displaySetInstanceUIDs = [], displaySetOptions = [] } = viewport;
|
||||
const { id: viewportOptionId, orientation } = viewportOptions;
|
||||
|
||||
const positionPresentationArr = [orientation || 'acquisition'];
|
||||
if (viewportOptionId) {
|
||||
positionPresentationArr.push(viewportOptionId);
|
||||
}
|
||||
|
||||
if (displaySetOptions?.some(ds => ds.options?.blendMode || ds.options?.displayPreset)) {
|
||||
positionPresentationArr.push(`custom`);
|
||||
}
|
||||
|
||||
for (const uid of displaySetInstanceUIDs) {
|
||||
positionPresentationArr.push(uid);
|
||||
}
|
||||
|
||||
if (viewports && viewports.length && isUpdatingSameViewport !== undefined) {
|
||||
addUniqueIndex(
|
||||
positionPresentationArr,
|
||||
PRESENTATION_TYPE_ID,
|
||||
viewports,
|
||||
isUpdatingSameViewport
|
||||
);
|
||||
} else {
|
||||
positionPresentationArr.push(0);
|
||||
}
|
||||
|
||||
return positionPresentationArr.join(JOIN_STR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the Position Presentation store.
|
||||
*
|
||||
* @param set - The zustand set function.
|
||||
* @returns The Position Presentation store state and actions.
|
||||
*/
|
||||
const createPositionPresentationStore = set => ({
|
||||
type: PRESENTATION_TYPE_ID,
|
||||
positionPresentationStore: {},
|
||||
|
||||
/**
|
||||
* Sets the position presentation for a given key.
|
||||
*/
|
||||
setPositionPresentation: (key, value) =>
|
||||
set(
|
||||
state => ({
|
||||
positionPresentationStore: {
|
||||
...state.positionPresentationStore,
|
||||
[key]: value,
|
||||
},
|
||||
}),
|
||||
false,
|
||||
'setPositionPresentation'
|
||||
),
|
||||
|
||||
/**
|
||||
* Clears all position presentations from the store.
|
||||
*/
|
||||
clearPositionPresentationStore: () =>
|
||||
set({ positionPresentationStore: {} }, false, 'clearPositionPresentationStore'),
|
||||
|
||||
/**
|
||||
* Retrieves the presentation ID based on the provided parameters.
|
||||
*/
|
||||
getPresentationId,
|
||||
getPositionPresentationId: getPositionPresentationId,
|
||||
});
|
||||
|
||||
/**
|
||||
* Zustand store for managing position presentations.
|
||||
* Applies devtools middleware when DEBUG_STORE is enabled.
|
||||
*/
|
||||
export const usePositionPresentationStore = create<PositionPresentationState>()(
|
||||
DEBUG_STORE
|
||||
? devtools(createPositionPresentationStore, { name: 'PositionPresentationStore' })
|
||||
: createPositionPresentationStore
|
||||
);
|
||||
@@ -0,0 +1,238 @@
|
||||
import { create } from 'zustand';
|
||||
import { devtools } from 'zustand/middleware';
|
||||
import { SegmentationPresentation } from '../types/Presentation';
|
||||
import { JOIN_STR } from './presentationUtils';
|
||||
import { getViewportOrientationFromImageOrientationPatient } from '../utils/getViewportOrientationFromImageOrientationPatient';
|
||||
|
||||
const PRESENTATION_TYPE_ID = 'segmentationPresentationId';
|
||||
const DEBUG_STORE = false;
|
||||
|
||||
/**
|
||||
* The keys are the presentationId.
|
||||
*/
|
||||
type SegmentationPresentationStore = {
|
||||
/**
|
||||
* Type identifier for the store.
|
||||
*/
|
||||
type: string;
|
||||
|
||||
/**
|
||||
* Stores segmentation presentations indexed by their presentation ID.
|
||||
*/
|
||||
segmentationPresentationStore: Record<string, SegmentationPresentation>;
|
||||
|
||||
/**
|
||||
* Sets the segmentation presentation for a given segmentation ID.
|
||||
*
|
||||
* @param presentationId - The presentation ID.
|
||||
* @param value - The `SegmentationPresentation` to associate with the ID.
|
||||
*/
|
||||
setSegmentationPresentation: (presentationId: string, value: SegmentationPresentation) => void;
|
||||
|
||||
/**
|
||||
* Clears all segmentation presentations from the store.
|
||||
*/
|
||||
clearSegmentationPresentationStore: () => void;
|
||||
|
||||
/**
|
||||
* Retrieves the presentation ID based on the provided parameters.
|
||||
*
|
||||
* @param id - The ID to check.
|
||||
* @param options - Configuration options.
|
||||
* @param options.viewport - The current viewport.
|
||||
* @param options.viewports - All available viewports.
|
||||
* @param options.isUpdatingSameViewport - Indicates if the same viewport is being updated.
|
||||
* @param options.servicesManager - The services manager instance.
|
||||
* @returns The segmentation presentation ID or undefined.
|
||||
*/
|
||||
getPresentationId: (
|
||||
id: string,
|
||||
options: {
|
||||
viewport: AppTypes.ViewportGrid.Viewport;
|
||||
viewports: AppTypes.ViewportGrid.Viewports;
|
||||
isUpdatingSameViewport: boolean;
|
||||
servicesManager: AppTypes.ServicesManager;
|
||||
}
|
||||
) => string | undefined;
|
||||
|
||||
/**
|
||||
* Adds a new segmentation presentation state.
|
||||
*
|
||||
* @param presentationId - The presentation ID.
|
||||
* @param segmentationPresentation - The `SegmentationPresentation` to add.
|
||||
* @param servicesManager - The services manager instance.
|
||||
*/
|
||||
addSegmentationPresentation: (
|
||||
presentationId: string,
|
||||
segmentationPresentation: SegmentationPresentation,
|
||||
{ servicesManager }: { servicesManager: AppTypes.ServicesManager }
|
||||
) => void;
|
||||
|
||||
/**
|
||||
* Gets the current segmentation presentation ID.
|
||||
*
|
||||
* @param params - Parameters for retrieving the segmentation presentation ID.
|
||||
* @param params.viewport - The current viewport.
|
||||
* @param params.servicesManager - The services manager instance.
|
||||
* @returns The current segmentation presentation ID.
|
||||
*/
|
||||
getSegmentationPresentationId: ({
|
||||
viewport,
|
||||
servicesManager,
|
||||
}: {
|
||||
viewport: AppTypes.ViewportGrid.Viewport;
|
||||
servicesManager: AppTypes.ServicesManager;
|
||||
}) => string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Generates a segmentation presentation ID based on the viewport configuration.
|
||||
*
|
||||
* @param id - The ID to check.
|
||||
* @param options - Configuration options.
|
||||
* @param options.viewport - The current viewport.
|
||||
* @param options.viewports - All available viewports.
|
||||
* @param options.isUpdatingSameViewport - Indicates if the same viewport is being updated.
|
||||
* @param options.servicesManager - The services manager instance.
|
||||
* @returns The segmentation presentation ID or undefined.
|
||||
*/
|
||||
const getPresentationId = (
|
||||
id: string,
|
||||
{
|
||||
viewport,
|
||||
viewports,
|
||||
isUpdatingSameViewport,
|
||||
servicesManager,
|
||||
}: {
|
||||
viewport: AppTypes.ViewportGrid.Viewport;
|
||||
viewports: AppTypes.ViewportGrid.Viewports;
|
||||
isUpdatingSameViewport: boolean;
|
||||
servicesManager: AppTypes.ServicesManager;
|
||||
}
|
||||
): string | undefined => {
|
||||
if (id !== PRESENTATION_TYPE_ID) {
|
||||
return;
|
||||
}
|
||||
|
||||
return _getSegmentationPresentationId({ viewport, servicesManager });
|
||||
};
|
||||
|
||||
/**
|
||||
* Helper function to generate the segmentation presentation ID.
|
||||
*
|
||||
* @param params - Parameters for generating the segmentation presentation ID.
|
||||
* @param params.viewport - The current viewport.
|
||||
* @param params.servicesManager - The services manager instance.
|
||||
* @returns The segmentation presentation ID or undefined.
|
||||
*/
|
||||
const _getSegmentationPresentationId = ({
|
||||
viewport,
|
||||
servicesManager,
|
||||
}: {
|
||||
viewport: AppTypes.ViewportGrid.Viewport;
|
||||
servicesManager: AppTypes.ServicesManager;
|
||||
}) => {
|
||||
if (!viewport?.viewportOptions || !viewport.displaySetInstanceUIDs?.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { displaySetInstanceUIDs, viewportOptions } = viewport;
|
||||
|
||||
let orientation = viewportOptions.orientation;
|
||||
|
||||
if (!orientation) {
|
||||
// Calculate orientation from the viewport sample image
|
||||
const displaySet = servicesManager.services.displaySetService.getDisplaySetByUID(
|
||||
displaySetInstanceUIDs[0]
|
||||
);
|
||||
const sampleImage = displaySet.images?.[0];
|
||||
const imageOrientationPatient = sampleImage?.ImageOrientationPatient;
|
||||
|
||||
orientation = getViewportOrientationFromImageOrientationPatient(imageOrientationPatient);
|
||||
}
|
||||
|
||||
const segmentationPresentationArr = [];
|
||||
|
||||
segmentationPresentationArr.push(...displaySetInstanceUIDs);
|
||||
|
||||
// Uncomment if unique indexing is needed
|
||||
// addUniqueIndex(
|
||||
// segmentationPresentationArr,
|
||||
// 'segmentationPresentationId',
|
||||
// viewports,
|
||||
// isUpdatingSameViewport
|
||||
// );
|
||||
|
||||
return segmentationPresentationArr.join(JOIN_STR);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates the Segmentation Presentation store.
|
||||
*
|
||||
* @param set - The zustand set function.
|
||||
* @returns The Segmentation Presentation store state and actions.
|
||||
*/
|
||||
const createSegmentationPresentationStore = set => ({
|
||||
type: PRESENTATION_TYPE_ID,
|
||||
segmentationPresentationStore: {},
|
||||
|
||||
/**
|
||||
* Clears all segmentation presentations from the store.
|
||||
*/
|
||||
clearSegmentationPresentationStore: () =>
|
||||
set({ segmentationPresentationStore: {} }, false, 'clearSegmentationPresentationStore'),
|
||||
|
||||
/**
|
||||
* Adds a new segmentation presentation to the store.
|
||||
*/
|
||||
addSegmentationPresentation: (
|
||||
presentationId: string,
|
||||
segmentationPresentation: SegmentationPresentation,
|
||||
{ servicesManager }: { servicesManager: AppTypes.ServicesManager }
|
||||
) =>
|
||||
set(
|
||||
state => ({
|
||||
segmentationPresentationStore: {
|
||||
...state.segmentationPresentationStore,
|
||||
[presentationId]: segmentationPresentation,
|
||||
},
|
||||
}),
|
||||
false,
|
||||
'addSegmentationPresentation'
|
||||
),
|
||||
|
||||
/**
|
||||
* Sets the segmentation presentation for a given presentation ID.
|
||||
*/
|
||||
setSegmentationPresentation: (presentationId: string, value: SegmentationPresentation) =>
|
||||
set(
|
||||
state => ({
|
||||
segmentationPresentationStore: {
|
||||
...state.segmentationPresentationStore,
|
||||
[presentationId]: value,
|
||||
},
|
||||
}),
|
||||
false,
|
||||
'setSegmentationPresentation'
|
||||
),
|
||||
|
||||
/**
|
||||
* Retrieves the presentation ID based on the provided parameters.
|
||||
*/
|
||||
getPresentationId,
|
||||
|
||||
/**
|
||||
* Retrieves the current segmentation presentation ID.
|
||||
*/
|
||||
getSegmentationPresentationId: _getSegmentationPresentationId,
|
||||
});
|
||||
|
||||
/**
|
||||
* Zustand store for managing segmentation presentations.
|
||||
* Applies devtools middleware when DEBUG_STORE is enabled.
|
||||
*/
|
||||
export const useSegmentationPresentationStore = create<SegmentationPresentationStore>()(
|
||||
DEBUG_STORE
|
||||
? devtools(createSegmentationPresentationStore, { name: 'Segmentation Presentation Store' })
|
||||
: createSegmentationPresentationStore
|
||||
);
|
||||
@@ -0,0 +1,84 @@
|
||||
import { create } from 'zustand';
|
||||
import { devtools } from 'zustand/middleware';
|
||||
|
||||
/**
|
||||
* Identifier for the synchronizers store type.
|
||||
*/
|
||||
const PRESENTATION_TYPE_ID = 'synchronizersStoreId';
|
||||
|
||||
/**
|
||||
* Flag to enable or disable debug mode for the store.
|
||||
* Set to `true` to enable zustand devtools.
|
||||
*/
|
||||
const DEBUG_STORE = false;
|
||||
|
||||
/**
|
||||
* Information about a single synchronizer.
|
||||
*/
|
||||
type SynchronizerInfo = {
|
||||
id: string;
|
||||
type: string;
|
||||
sourceViewports: Array<{ viewportId: string; renderingEngineId: string }>;
|
||||
targetViewports: Array<{ viewportId: string; renderingEngineId: string }>;
|
||||
};
|
||||
|
||||
/**
|
||||
* State shape for the Synchronizers store.
|
||||
*/
|
||||
type SynchronizersState = {
|
||||
/**
|
||||
* Stores synchronizer information indexed by a unique key.
|
||||
*/
|
||||
synchronizersStore: Record<string, SynchronizerInfo[]>;
|
||||
|
||||
/**
|
||||
* Sets the synchronizers for a specific viewport.
|
||||
*
|
||||
* @param viewportId - The ID of the viewport.
|
||||
* @param synchronizers - An array of SynchronizerInfo.
|
||||
*/
|
||||
setSynchronizers: (viewportId: string, synchronizers: SynchronizerInfo[]) => void;
|
||||
|
||||
/**
|
||||
* Clears the entire synchronizers store.
|
||||
*/
|
||||
clearSynchronizersStore: () => void;
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates the Synchronizers store.
|
||||
*
|
||||
* @param set - The zustand set function.
|
||||
* @returns The synchronizers store state and actions.
|
||||
*/
|
||||
const createSynchronizersStore = (set): SynchronizersState => ({
|
||||
synchronizersStore: {},
|
||||
type: PRESENTATION_TYPE_ID,
|
||||
|
||||
setSynchronizers: (viewportId: string, synchronizers: SynchronizerInfo[]) => {
|
||||
set(
|
||||
state => ({
|
||||
synchronizersStore: {
|
||||
...state.synchronizersStore,
|
||||
[viewportId]: synchronizers,
|
||||
},
|
||||
}),
|
||||
false,
|
||||
'setSynchronizers'
|
||||
);
|
||||
},
|
||||
|
||||
clearSynchronizersStore: () => {
|
||||
set({ synchronizersStore: {} }, false, 'clearSynchronizersStore');
|
||||
},
|
||||
});
|
||||
|
||||
/**
|
||||
* Zustand store for managing synchronizers.
|
||||
* Applies devtools middleware when DEBUG_STORE is enabled.
|
||||
*/
|
||||
export const useSynchronizersStore = create<SynchronizersState>()(
|
||||
DEBUG_STORE
|
||||
? devtools(createSynchronizersStore, { name: 'SynchronizersStore' })
|
||||
: createSynchronizersStore
|
||||
);
|
||||
Reference in new issue
Block a user