All files / extensions/cornerstone/src/stores useSelectedSegmentationsForViewportStore.ts

87.5% Statements 7/8
50% Branches 1/2
75% Functions 3/4
87.5% Lines 7/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86        67x 67x                                                                         67x 67x               166x 166x                                                   67x              
import { create } from 'zustand';
import { devtools } from 'zustand/middleware';
import { SelectedSegmentationByTypeMap } from '../types/SelectedSegmentationByTypeMap';
 
const PRESENTATION_TYPE_ID = 'selectedSegmentationsForViewportId';
const DEBUG_STORE = false;
 
/**
 * Represents the state and actions for managing selected segmentations for a viewport by representation type.
 */
type SelectedSegmentationsForViewportState = {
  /**
   * Stores a mapping from viewport id to a map of representation type to segmentation id.
   */
  selectedSegmentationsForViewport: Record<string, SelectedSegmentationByTypeMap>;
 
  /**
   * Sets the selected segmentations for a given viewport id.
   *
   * @param key - The viewport id.
   * @param value - The `SelectedSegmentationByTypeMap` to associate with the viewport id.
   */
  setSelectedSegmentationsForViewport: (key: string, value: SelectedSegmentationByTypeMap) => void;
 
  /**
   * Clears all selected segmentations for all viewports.
   */
  clearSelectedSegmentationsForViewportStore: () => void;
 
  /**
   * Type identifier for the store.
   */
  type: string;
};
 
/**
 * Creates the Selected Segmentations For Viewport store.
 *
 * @param set - The zustand set function.
 * @returns The selected segmentations for viewport store state and actions.
 */
const createSelectedSegmentationsForViewportStore = (
  set
): SelectedSegmentationsForViewportState => ({
  selectedSegmentationsForViewport: {},
  type: PRESENTATION_TYPE_ID,
 
  /**
   * Sets the selected segmentations for a given viewport id.
   */
  setSelectedSegmentationsForViewport: (key, value) =>
    set(
      state => ({
        selectedSegmentationsForViewport: {
          ...state.selectedSegmentationsForViewport,
          [key]: value,
        },
      }),
      false,
      'setSelectedSegmentationsForViewport'
    ),
 
  /**
   * Clears all selected segmentations for all viewports.
   */
  clearSelectedSegmentationsForViewportStore: () =>
    set(
      { selectedSegmentationsForViewport: {} },
      false,
      'clearSelectedSegmentationsForViewportStore'
    ),
});
 
/**
 * Zustand store for managing selected segmentations for a viewport by representation type.
 * Applies devtools middleware when DEBUG_STORE is enabled.
 */
export const useSelectedSegmentationsForViewportStore =
  create<SelectedSegmentationsForViewportState>()(
    DEBUG_STORE
      ? devtools(createSelectedSegmentationsForViewportStore, {
          name: 'SelectedSegmentationsForViewportStore',
        })
      : createSelectedSegmentationsForViewportStore
  );