All files / extensions/cornerstone/src/hooks useSegmentations.ts

64.7% Statements 33/51
36.84% Branches 7/19
80% Functions 8/10
66% Lines 33/50

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              1x     1x       1x 1x   1x                                                 1x       1x 1x           1x 1x 1x     1x     1x   1x 1x             1x                                     9x 9x 9x   9x   9x 1x 1x   1x         1x 1x     1x       1x   1x   1x                     1x                 1x               9x    
import { useState, useEffect } from 'react';
import debounce from 'lodash.debounce';
import { roundNumber } from '@ohif/core/src/utils';
import { SegmentationData } from '../services/SegmentationService/SegmentationService';
import { useSystem } from '@ohif/core';
 
function mapSegmentationToDisplay(segmentation, customizationService) {
  const { label, segments } = segmentation;
 
  // Get the readable text mapping once
  const readableTextMap = customizationService.getCustomization('panelSegmentation.readableText');
 
  // Helper function to recursively map cachedStats to readable display text
  function mapStatsToDisplay(stats, indent = 0) {
    const primary = [];
    const indentation = '  '.repeat(indent);
 
    for (const key in stats) {
      Iif (Object.prototype.hasOwnProperty.call(stats, key)) {
        const value = stats[key];
        const readableText = readableTextMap?.[key];
 
        Iif (!readableText) {
          continue;
        }
 
        if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
          // Add empty row before category (except for the first category)
          Iif (primary.length > 0) {
            primary.push('');
          }
          // Add category title
          primary.push(`${indentation}${readableText}`);
          // Recursively handle nested objects
          primary.push(...mapStatsToDisplay(value, indent + 1));
        } else {
          // For non-nested values, don't add empty rows
          primary.push(`${indentation}${readableText}: ${roundNumber(value, 2)}`);
        }
      }
    }
 
    return primary;
  }
 
  // Get customization for display text mapping
  const displayTextMapper = segment => {
    const defaultDisplay = {
      primary: [],
      secondary: [],
    };
 
    // If the segment has cachedStats, map it to readable text
    if (segment.cachedStats) {
      const primary = mapStatsToDisplay(segment.cachedStats);
      defaultDisplay.primary = primary;
    }
 
    return defaultDisplay;
  };
 
  const updatedSegments = {};
 
  Object.entries(segments).forEach(([segmentIndex, segment]) => {
    updatedSegments[segmentIndex] = {
      ...segment,
      displayText: displayTextMapper(segment),
    };
  });
 
  // Map the segments and apply the display text mapper
  return {
    ...segmentation,
    label,
    segments: updatedSegments,
  };
}
 
/**
 * Custom hook that provides segmentation data.
 * @param options - The options object.
 * @param options.servicesManager - The services manager object.
 * @param options.subscribeToDataModified - Whether to subscribe to segmentation data modifications.
 * @param options.debounceTime - Debounce time in milliseconds for updates.
 * @returns An array of segmentation data.
 */
export function useSegmentations(options?: {
  subscribeToDataModified?: boolean;
  debounceTime?: number;
}): SegmentationData[] {
  const { subscribeToDataModified = false, debounceTime = 0 } = options || {};
  const { servicesManager } = useSystem();
  const { segmentationService, customizationService } = servicesManager.services;
 
  const [segmentations, setSegmentations] = useState<SegmentationData[]>([]);
 
  useEffect(() => {
    const update = () => {
      const segmentations = segmentationService.getSegmentations();
 
      Iif (!segmentations?.length) {
        setSegmentations([]);
        return;
      }
 
      const mappedSegmentations = segmentations.map(segmentation =>
        mapSegmentationToDisplay(segmentation, customizationService)
      );
 
      setSegmentations(mappedSegmentations);
    };
 
    const debouncedUpdate =
      debounceTime > 0 ? debounce(update, debounceTime, { leading: true, trailing: true }) : update;
 
    update();
 
    const subscriptions = [
      segmentationService.subscribe(
        segmentationService.EVENTS.SEGMENTATION_MODIFIED,
        debouncedUpdate
      ),
      segmentationService.subscribe(
        segmentationService.EVENTS.SEGMENTATION_REMOVED,
        debouncedUpdate
      ),
    ];
 
    Iif (subscribeToDataModified) {
      subscriptions.push(
        segmentationService.subscribe(
          segmentationService.EVENTS.SEGMENTATION_DATA_MODIFIED,
          debouncedUpdate
        )
      );
    }
 
    return () => {
      subscriptions.forEach(subscription => subscription.unsubscribe());
      Iif (debounceTime > 0) {
        debouncedUpdate.cancel();
      }
    };
  }, [segmentationService, customizationService, debounceTime, subscribeToDataModified]);
 
  return segmentations;
}