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

83.33% Statements 25/30
58.33% Branches 7/12
85.71% Functions 6/7
82.14% Lines 23/28

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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167                34x           34x                                                                                                                     34x                       144x       144x 144x     144x     144x 144x 140x   4x     144x       144x 144x 144x   144x 144x     144x   144x                 34x               57x 57x                                                   34x          
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 => {
  Iif (id !== PRESENTATION_TYPE_ID) {
    return;
  }
 
  const getLutId = (ds): string => {
    Iif (!ds || !ds.options) {
      return DEFAULT_STR;
    }
    Iif (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);
  };
 
  Iif (!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
);