All files / src/hooks useComponentDetails.ts

100% Statements 10/10
100% Branches 4/4
100% Functions 2/2
100% Lines 9/9

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                                                                167x   167x 115x   50x 50x 50x   1x 1x       167x    
import { useMemo } from 'react';
import { CIAComponent, SecurityLevel } from '../types/cia';
import { useCIAContentService } from './useCIAContentService';
import { isNullish } from '../utils/typeGuards';
 
/**
 * Custom hook for fetching CIA component details with error handling
 * 
 * ## Business Perspective
 * 
 * This hook encapsulates the common pattern of fetching component details
 * from the CIA content service, providing consistent error handling and
 * null safety across all widget components. This reduces duplication and
 * ensures consistent behavior across the application. 🔄
 * 
 * @param component - CIA component (availability, integrity, confidentiality)
 * @param level - Security level for the component
 * @returns Component details or null if unavailable
 * 
 * @example
 * ```typescript
 * const details = useComponentDetails('availability', 'High');
 * 
 * if (details) {
 *   console.log(details.uptime);
 * }
 * ```
 */
export function useComponentDetails(
  component: CIAComponent,
  level: SecurityLevel
) {
  const { ciaContentService } = useCIAContentService();
 
  const details = useMemo(() => {
    if (isNullish(ciaContentService)) return null;
 
    try {
      const result = ciaContentService.getComponentDetails(component, level);
      return isNullish(result) ? null : result;
    } catch (err) {
      console.error(`Error getting ${component} details:`, err);
      return null;
    }
  }, [ciaContentService, component, level]);
 
  return details;
}