All files / extensions/cornerstone/src/utils promptHydrationDialog.ts

86.53% Statements 45/52
82.6% Branches 19/23
91.66% Functions 11/12
86.53% Lines 45/52

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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198    204x                                               204x                     74x   49x   17x   8x             74x   49x   17x   8x                           76x 76x 76x     76x   76x       76x   76x       73x   71x       71x   15x 15x 15x       15x         56x   48x 48x 48x         48x         8x   8x   8x                   2x               2x                       74x 74x 74x 74x                           74x 69x 69x     74x               2x 2x     1x                  
export type HydrationCallback = (params: any) => Promise<boolean>;
 
export const HydrationType = {
  SEG: 'SEG',
  SR: 'SR',
  RTSTRUCT: 'RTSTRUCT',
} as const;
 
export interface HydrationDialogProps {
  servicesManager: AppTypes.ServicesManager;
  viewportId: string;
  displaySet: AppTypes.DisplaySet;
  preHydrateCallbacks?: HydrationCallback[];
  hydrateCallback: HydrationCallback;
  type: string;
}
 
export interface HydrationSRResult {
  userResponse: number;
  displaySetInstanceUID: string;
  srSeriesInstanceUID: string;
  viewportId: string;
  StudyInstanceUID?: string;
  SeriesInstanceUIDs?: string[];
}
 
const RESPONSE = {
  NO_NEVER: -1,
  CANCEL: 0,
  CREATE_REPORT: 1,
  ADD_SERIES: 2,
  SET_STUDY_AND_SERIES: 3,
  NO_NOT_FOR_SERIES: 4,
  HYDRATE: 5,
};
 
function getCustomizationMessageKey(type: string): string {
  switch (type) {
    case HydrationType.RTSTRUCT:
      return 'viewportNotification.hydrateRTMessage';
    case HydrationType.SEG:
      return 'viewportNotification.hydrateSEGMessage';
    case HydrationType.SR:
      return 'viewportNotification.hydrateSRMessage';
    default:
      return 'viewportNotification.hydrateMessage';
  }
}
 
function getDialogId(type: string): string {
  switch (type) {
    case HydrationType.RTSTRUCT:
      return 'promptHydrateRT';
    case HydrationType.SEG:
      return 'promptHydrateSEG';
    case HydrationType.SR:
      return 'promptHydrateSR';
    default:
      return 'promptHydrate';
  }
}
 
function promptHydrationDialog({
  servicesManager,
  viewportId,
  displaySet,
  preHydrateCallbacks = [],
  hydrateCallback,
  type,
}: HydrationDialogProps): Promise<boolean | HydrationSRResult> {
  const { uiViewportDialogService, customizationService } = servicesManager.services;
  const extensionManager = servicesManager._extensionManager;
  const appConfig = extensionManager.appConfig;
 
  // Todo: make this use enum from the extension, we should move the enum
  const standardMode = appConfig?.measurementTrackingMode === 'standard';
 
  return new Promise(async function (resolve, reject) {
    // For RT and SEG, we check disableConfirmationPrompts
    // For SR, we check if standardMode is true
    const shouldPrompt =
      type === HydrationType.SR ? standardMode : !appConfig?.disableConfirmationPrompts;
 
    const promptResult = shouldPrompt
      ? await _askHydrate(uiViewportDialogService, customizationService, viewportId, type)
      : RESPONSE.HYDRATE;
 
    if (promptResult === RESPONSE.HYDRATE) {
      // Execute preHydrate callbacks
      preHydrateCallbacks?.forEach(callback => {
        callback();
      });
 
      if (type === HydrationType.SEG) {
        // SEG needs setTimeout
        window.setTimeout(async () => {
          try {
            const isHydrated = await hydrateCallback({
              segDisplaySet: displaySet,
              viewportId,
            });
            resolve(isHydrated);
          } catch (error) {
            reject(error);
          }
        }, 0);
      } else if (type === HydrationType.RTSTRUCT) {
        // RT hydration
        window.setTimeout(async () => {
          try {
            const isHydrated = await hydrateCallback({
              rtDisplaySet: displaySet,
              viewportId,
              servicesManager,
            });
            resolve(isHydrated);
          } catch (error) {
            reject(error);
          }
        }, 0);
      } else if (type === HydrationType.SR) {
        // SR has a different result structure
        const hydrationResult = await hydrateCallback(displaySet);
 
        resolve({
          userResponse: promptResult,
          displaySetInstanceUID: displaySet.displaySetInstanceUID,
          srSeriesInstanceUID: displaySet.SeriesInstanceUID,
          viewportId,
          StudyInstanceUID: hydrationResult?.StudyInstanceUID,
          SeriesInstanceUIDs: hydrationResult?.SeriesInstanceUIDs,
        });
      }
    } else {
      Iif (type === HydrationType.SR) {
        resolve({
          userResponse: promptResult,
          displaySetInstanceUID: displaySet.displaySetInstanceUID,
          srSeriesInstanceUID: displaySet.SeriesInstanceUID,
          viewportId,
        });
      } else {
        resolve(false);
      }
    }
  });
}
 
function _askHydrate(
  uiViewportDialogService: AppTypes.UIViewportDialogService,
  customizationService: AppTypes.CustomizationService,
  viewportId: string,
  type: string
) {
  return new Promise(function (resolve, reject) {
    const messageKey = getCustomizationMessageKey(type);
    const message = customizationService.getCustomization(messageKey);
    const actions = [
      {
        id: 'no-hydrate',
        type: 'secondary',
        text: 'No',
        value: RESPONSE.CANCEL,
      },
      {
        id: 'yes-hydrate',
        type: 'primary',
        text: 'Yes',
        value: RESPONSE.HYDRATE,
      },
    ];
    const onSubmit = result => {
      uiViewportDialogService.hide();
      resolve(result);
    };
 
    uiViewportDialogService.show({
      id: getDialogId(type),
      viewportId,
      type: 'info',
      message,
      actions,
      onSubmit,
      onOutsideClick: () => {
        uiViewportDialogService.hide();
        resolve(RESPONSE.CANCEL);
      },
      onKeyPress: event => {
        Iif (event.key === 'Enter') {
          onSubmit(RESPONSE.HYDRATE);
        }
      },
    });
  });
}
 
export default promptHydrationDialog;