All files / src/hooks useComplianceService.ts

80% Statements 16/20
25% Branches 2/8
100% Functions 5/5
80% Lines 16/20

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                104x 104x   104x   51x                 51x                           104x 51x   51x 51x   51x   51x 51x                       51x   51x 51x       104x                
import { useEffect, useMemo, useState } from "react";
import { ComplianceServiceAdapter } from "../services/ComplianceServiceAdapter";
import { createEmptyCIADetails } from "../utils/serviceUtils";
 
/**
 * Hook to access compliance service functionality
 */
export function useComplianceService() {
  const [isLoading, setIsLoading] = useState(true);
  const [error, setError] = useState<Error | null>(null);
 
  const complianceService = useMemo(() => {
    // Create default empty options that satisfy the type requirements
    const emptySecurityLevelRecord = {
      None: createEmptyCIADetails(),
      Low: createEmptyCIADetails(),
      Moderate: createEmptyCIADetails(),
      High: createEmptyCIADetails(),
      "Very High": createEmptyCIADetails(),
    };
 
    // Create minimal valid data provider for the service
    return new ComplianceServiceAdapter({
      availabilityOptions: emptySecurityLevelRecord,
      integrityOptions: emptySecurityLevelRecord,
      confidentialityOptions: emptySecurityLevelRecord,
      roiEstimates: {
        NONE: { returnRate: "0%", description: "No ROI" },
        LOW: { returnRate: "50%", description: "Low ROI" },
        MODERATE: { returnRate: "150%", description: "Moderate ROI" },
        HIGH: { returnRate: "300%", description: "High ROI" },
        VERY_HIGH: { returnRate: "500%", description: "Very high ROI" },
      },
    });
  }, []);
 
  useEffect(() => {
    let isMounted = true;
 
    const initializeService = async () => {
      try {
        // Check if service exists first
        Eif (complianceService) {
          // No need to call initialize as it doesn't exist
          Eif (isMounted) {
            setIsLoading(false);
          }
        }
      } catch (err) {
        console.error("Failed to initialize compliance service:", err);
        if (isMounted) {
          setError(err instanceof Error ? err : new Error("An error occurred"));
          setIsLoading(false);
        }
      }
    };
 
    initializeService();
 
    return () => {
      isMounted = false;
    };
  }, [complianceService]);
 
  return {
    isLoading,
    complianceService,
    error,
  };
}
 
export default useComplianceService;