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

82.14% Statements 23/28
65% Branches 13/20
85.71% Functions 6/7
80.76% Lines 21/26

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 168 169 170 171 172 173          34x 34x                                                                                                                                 34x                       144x       144x       144x       145x 145x   145x 145x       146x 4x     145x 145x     145x               145x     145x                 34x               79x 79x                                                     34x          
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 = false;
 
/**
 * 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 => {
  Iif (id !== PRESENTATION_TYPE_ID) {
    return;
  }
 
  Iif (!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'];
  Iif (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);
  }
 
  Iif (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
);