All files / src/hooks useBusinessImpact.ts

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

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                                                                  169x   169x 117x 66x     51x 51x 51x   1x 1x       169x    
import { useMemo } from 'react';
import { CIAComponent, SecurityLevel } from '../types/cia';
import { useCIAContentService } from './useCIAContentService';
import { getDefaultComponentImpact } from '../utils/riskUtils';
import { isNullish } from '../utils/typeGuards';
 
/**
 * Custom hook for fetching business impact details with fallback
 * 
 * ## Business Perspective
 * 
 * This hook provides consistent access to business impact information
 * across all widget components. It automatically falls back to default
 * values when the service is unavailable, ensuring widgets always display
 * meaningful impact information to stakeholders. 💼
 * 
 * @param component - CIA component (availability, integrity, confidentiality)
 * @param level - Security level for the component
 * @returns Business impact details with automatic fallback
 * 
 * @example
 * ```typescript
 * const impact = useBusinessImpact('confidentiality', 'Very High');
 * 
 * // Always returns impact data, never null
 * console.log(impact.financialImpact);
 * console.log(impact.operationalImpact);
 * ```
 */
export function useBusinessImpact(
  component: CIAComponent,
  level: SecurityLevel
) {
  const { ciaContentService } = useCIAContentService();
 
  const businessImpact = useMemo(() => {
    if (isNullish(ciaContentService)) {
      return getDefaultComponentImpact(component, level);
    }
 
    try {
      const impact = ciaContentService.getBusinessImpact(component, level);
      return impact || getDefaultComponentImpact(component, level);
    } catch (err) {
      console.error(`Error getting ${component} business impact:`, err);
      return getDefaultComponentImpact(component, level);
    }
  }, [ciaContentService, component, level]);
 
  return businessImpact;
}