All files / src/hooks useSecurityLevelState.ts

100% Statements 9/9
100% Branches 7/7
100% Functions 5/5
100% Lines 8/8

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                                                                                                                                                                97x           97x 27x     97x 6x             97x 8x     97x    
import { useState, useCallback } from 'react';
import { SecurityLevel, CIAComponent } from '../types/cia';
 
/**
 * State object containing security levels for all CIA components
 * 
 * @property availability - Availability security level
 * @property integrity - Integrity security level
 * @property confidentiality - Confidentiality security level
 */
export interface SecurityLevelState {
  /** Availability security level */
  availability: SecurityLevel;
  /** Integrity security level */
  integrity: SecurityLevel;
  /** Confidentiality security level */
  confidentiality: SecurityLevel;
}
 
/**
 * Return type for useSecurityLevelState hook
 */
export interface UseSecurityLevelStateReturn {
  /** Current security levels for all components */
  levels: SecurityLevelState;
  
  /** Updates security level for a specific component */
  setLevel: (component: CIAComponent, level: SecurityLevel) => void;
  
  /** Resets all security levels to default */
  resetLevels: (defaultLevel?: SecurityLevel) => void;
  
  /** Gets security level for a specific component */
  getLevel: (component: CIAComponent) => SecurityLevel;
}
 
/**
 * Custom hook for managing CIA triad security levels
 * 
 * ## Business Perspective
 * 
 * Provides unified state management for security officers to configure
 * organizational security posture across all three CIA components. This
 * centralization ensures consistent security level handling and simplifies
 * state management across the application. 🔒
 * 
 * ## Technical Perspective
 * 
 * Extracts common security level state management pattern found in 8+ widgets,
 * reducing code duplication by ~20% and ensuring consistent behavior. Uses
 * React hooks best practices with proper memoization for optimal performance.
 * 
 * @param initialLevels - Initial security levels (defaults to 'Moderate' for all components)
 * @returns Security level state and update functions
 * 
 * @example
 * ```tsx
 * // Basic usage with defaults
 * const { levels, setLevel, getLevel } = useSecurityLevelState();
 * 
 * // Update a level
 * setLevel('availability', 'High');
 * 
 * // Get a level
 * const currentLevel = getLevel('integrity');
 * 
 * // Initialize with custom levels
 * const { levels } = useSecurityLevelState({
 *   availability: 'High',
 *   integrity: 'Moderate',
 *   confidentiality: 'Very High'
 * });
 * 
 * // Reset all levels
 * resetLevels('Low');
 * ```
 */
export function useSecurityLevelState(
  initialLevels?: Partial<SecurityLevelState>
): UseSecurityLevelStateReturn {
  const [levels, setLevels] = useState<SecurityLevelState>({
    availability: initialLevels?.availability ?? 'Moderate',
    integrity: initialLevels?.integrity ?? 'Moderate',
    confidentiality: initialLevels?.confidentiality ?? 'Moderate',
  });
 
  const setLevel = useCallback((component: CIAComponent, level: SecurityLevel) => {
    setLevels(prev => ({ ...prev, [component]: level }));
  }, []);
 
  const resetLevels = useCallback((defaultLevel: SecurityLevel = 'Moderate') => {
    setLevels({
      availability: defaultLevel,
      integrity: defaultLevel,
      confidentiality: defaultLevel,
    });
  }, []);
 
  const getLevel = useCallback((component: CIAComponent): SecurityLevel => {
    return levels[component];
  }, [levels]);
 
  return { levels, setLevel, resetLevels, getLevel };
}