All files / extensions/default/src/utils reuseCachedLayouts.ts

87.87% Statements 29/33
73.33% Branches 11/15
100% Functions 2/2
87.5% Lines 28/32

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                                      34x 15x 15x   15x       15x 15x   15x 15x   15x   15x 15x 15x 15x   15x       15x   15x       15x 17x 17x     17x 17x 17x     17x 15x   17x 17x                   15x   15x                
import { HangingProtocolService, Types } from '@ohif/core';
import { useViewportGridStore } from '../stores/useViewportGridStore';
import { useDisplaySetSelectorStore } from '../stores/useDisplaySetSelectorStore';
import { useHangingProtocolStageIndexStore } from '../stores/useHangingProtocolStageIndexStore';
 
export type ReturnType = {
  hangingProtocolStageIndexMap: Record<string, Types.HangingProtocol.HPInfo>;
  viewportGridStore: Record<string, unknown>;
  displaySetSelectorMap: Record<string, string>;
};
 
/**
 * Calculates a set of state information for hanging protocols and viewport grid
 * which defines the currently applied hanging protocol state.
 * @param state is the viewport grid state
 * @param syncService is the state sync service to use for getting existing state
 * @returns Set of states that can be applied to the state sync to remember
 *   the current view state.
 */
const reuseCachedLayout = (state, hangingProtocolService: HangingProtocolService): ReturnType => {
  const { activeViewportId } = state;
  const { protocol } = hangingProtocolService.getActiveProtocol();
 
  Iif (!protocol) {
    return;
  }
 
  const hpInfo = hangingProtocolService.getState();
  const { protocolId, stageIndex, activeStudyUID } = hpInfo;
 
  const { viewportGridState, setViewportGridState } = useViewportGridStore.getState();
  const { displaySetSelectorMap, setDisplaySetSelector } = useDisplaySetSelectorStore.getState();
  const { hangingProtocolStageIndexMap, setHangingProtocolStageIndex } =
    useHangingProtocolStageIndexStore.getState();
 
  const stage = protocol.stages[stageIndex];
  const storeId = `${activeStudyUID}:${protocolId}:${stageIndex}`;
  const cacheId = `${activeStudyUID}:${protocolId}`;
  const { rows, columns } = stage.viewportStructure.properties;
  const custom =
    stage.viewports.length !== state.viewports.size ||
    state.layout.numRows !== rows ||
    state.layout.numCols !== columns;
 
  hangingProtocolStageIndexMap[cacheId] = hpInfo;
 
  Iif (storeId && custom) {
    setViewportGridState(storeId, { ...state });
  }
 
  state.viewports.forEach((viewport, viewportId) => {
    const { displaySetOptions, displaySetInstanceUIDs } = viewport;
    Iif (!displaySetOptions) {
      return;
    }
    for (let i = 0; i < displaySetOptions.length; i++) {
      const displaySetUID = displaySetInstanceUIDs[i];
      Iif (!displaySetUID) {
        continue;
      }
      if (viewportId === activeViewportId && i === 0) {
        setDisplaySetSelector(`${activeStudyUID}:activeDisplaySet:0`, displaySetUID);
      }
      if (displaySetOptions[i]?.id) {
        setDisplaySetSelector(
          `${activeStudyUID}:${displaySetOptions[i].id}:${
            displaySetOptions[i].matchedDisplaySetsIndex || 0
          }`,
          displaySetUID
        );
      }
    }
  });
 
  setHangingProtocolStageIndex(cacheId, hpInfo);
 
  return {
    hangingProtocolStageIndexMap,
    viewportGridStore: viewportGridState,
    displaySetSelectorMap,
  };
};
 
export default reuseCachedLayout;