All files / src/hooks useComplianceService.ts

89.28% Statements 50/56
83.33% Branches 5/6
100% Functions 2/2
89.28% Lines 50/56

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 731x 1x 1x         1x 42x 42x   42x   21x 21x 21x 21x 21x 21x 21x     21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 42x   42x 21x   21x 21x   21x   21x 21x 21x 21x 21x             21x   21x   21x 21x 21x 42x   42x 42x 42x 42x 42x 42x   1x  
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
        if (complianceService) {
          // No need to call initialize as it doesn't exist
          if (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;