All files / src/components/widgets/impactanalysis IntegrityImpactWidget.tsx

83.33% Statements 30/36
85.36% Branches 35/41
100% Functions 7/7
83.33% Lines 30/36

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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208                                                                        6x                   33x         33x     33x 33x 33x 21x     12x       12x               33x 33x 33x 21x     12x       12x               33x 33x 33x 21x     12x                   33x 33x 11x   22x       33x 33x 11x   22x     33x                                                                                                                                           2x                                
import React, { useMemo } from "react";
import { WIDGET_ICONS, WIDGET_TITLES } from "../../../constants/appConstants";
import { INTEGRITY_IMPACT_TEST_IDS } from "../../../constants/testIds";
import {
  getDefaultErrorRate,
  getDefaultValidationLevel,
} from "../../../data/ciaOptionsData";
import { useCIAContentService } from "../../../hooks/useCIAContentService";
import { ComponentImpactBaseProps } from "../../../types/widgets";
import { getSecurityLevelBackgroundClass } from "../../../utils/colorUtils";
import { getDefaultComponentImpact } from "../../../utils/riskUtils";
import { normalizeSecurityLevel } from "../../../utils/securityLevelUtils";
import { isNullish } from "../../../utils/typeGuards";
import BusinessImpactSection from "../../common/BusinessImpactSection";
import SecurityLevelBadge from "../../common/SecurityLevelBadge";
import WidgetContainer from "../../common/WidgetContainer";
 
/**
 * Props for IntegrityImpactWidget component
 */
export interface IntegrityImpactWidgetProps extends ComponentImpactBaseProps {
  /**
   * Flag to show extended details (optional)
   */
  showExtendedDetails?: boolean;
}
 
/**
 * Widget that displays the impact of selected integrity level
 *
 * ## Business Perspective
 *
 * This widget helps stakeholders understand the business impact of
 * integrity controls, including how data accuracy and validation
 * mechanisms protect business operations and decision-making. 📊
 */
const IntegrityImpactWidget: React.FC<IntegrityImpactWidgetProps> = ({
  level, // For backward compatibility
  availabilityLevel,
  integrityLevel,
  confidentialityLevel,
  className = "",
  testId = INTEGRITY_IMPACT_TEST_IDS.INTEGRITY_IMPACT_PREFIX,
  showExtendedDetails = false,
}) => {
  // Use the utility for consistent security level normalization
  const effectiveLevel = normalizeSecurityLevel(
    integrityLevel || level || "Moderate"
  );
 
  // Get CIA content service
  const { ciaContentService, error, isLoading } = useCIAContentService();
 
  // Get integrity details from service
  const integrityDetails = useMemo(() => {
    try {
      if (isNullish(ciaContentService) || isNullish(effectiveLevel)) {
        return null;
      }
 
      const details = ciaContentService.getComponentDetails(
        "integrity",
        effectiveLevel
      );
      return isNullish(details) ? null : details;
    } catch (err) {
      console.error("Error getting integrity details:", err);
      return null;
    }
  }, [ciaContentService, effectiveLevel]);
 
  // Get business impact from service with fallback to our utility
  const businessImpact = useMemo(() => {
    try {
      if (isNullish(ciaContentService) || isNullish(effectiveLevel)) {
        return getDefaultComponentImpact("integrity", effectiveLevel);
      }
 
      const impact = ciaContentService.getBusinessImpact(
        "integrity",
        effectiveLevel
      );
      return impact || getDefaultComponentImpact("integrity", effectiveLevel);
    } catch (err) {
      console.error("Error getting integrity business impact:", err);
      return getDefaultComponentImpact("integrity", effectiveLevel);
    }
  }, [ciaContentService, effectiveLevel]);
 
  // Get recommendations from service
  const recommendations = useMemo(() => {
    try {
      if (isNullish(ciaContentService) || isNullish(effectiveLevel)) {
        return [];
      }
 
      return (
        ciaContentService.getRecommendations("integrity", effectiveLevel) || []
      );
    } catch (err) {
      console.error("Error getting recommendations:", err);
      return [];
    }
  }, [ciaContentService, effectiveLevel]);
 
  // Get validation level with fallback to utility function
  const validationLevel = useMemo(() => {
    if (!isNullish(integrityDetails) && integrityDetails.validationLevel) {
      return integrityDetails.validationLevel;
    }
    return getDefaultValidationLevel(effectiveLevel);
  }, [integrityDetails, effectiveLevel]);
 
  // Get error rate with fallback to utility function
  const errorRate = useMemo(() => {
    if (!isNullish(integrityDetails) && integrityDetails.errorRate) {
      return integrityDetails.errorRate;
    }
    return getDefaultErrorRate(effectiveLevel);
  }, [integrityDetails, effectiveLevel]);
 
  return (
    <WidgetContainer
      title={WIDGET_TITLES.INTEGRITY_IMPACT || "Integrity Impact Analysis"}
      icon={WIDGET_ICONS.INTEGRITY_IMPACT || "✓"}
      className={className}
      testId={testId}
      isLoading={isLoading}
      error={error}
    >
      <div className="p-4">
        {/* Security level indicator */}
        <div className="mb-4">
          <SecurityLevelBadge
            category="Integrity"
            level={effectiveLevel}
            // Use utility for consistent styling
            colorClass={getSecurityLevelBackgroundClass("green")}
            textClass="text-green-800 dark:text-green-300"
            testId={`${testId}-integrity-badge`}
          />
        </div>
 
        {/* Business impact */}
        {businessImpact && (
          <div
            className="mt-4"
            data-testid={`${testId}-business-impact-container`}
          >
            <BusinessImpactSection
              impact={businessImpact}
              color="green"
              testId={`${testId}-business-impact`}
            />
          </div>
        )}
 
        {/* Data Integrity metrics */}
        <div
          className="mb-4 p-3 bg-green-50 dark:bg-green-900 dark:bg-opacity-20 rounded-lg"
          data-testid={`${testId}-metrics`}
        >
          <h3 className="text-lg font-medium mb-2 text-green-800 dark:text-green-300">
            Data Integrity Metrics
          </h3>
          <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
            <div className="p-2 bg-white dark:bg-gray-800 rounded-lg">
              <div className="text-sm font-medium mb-1">
                Data Validation Controls:
              </div>
              <div className="text-lg font-bold text-green-600 dark:text-green-400">
                {validationLevel}
              </div>
            </div>
            <div className="p-2 bg-white dark:bg-gray-800 rounded-lg">
              <div className="text-sm font-medium mb-1">
                Acceptable Error Rate:
              </div>
              <div className="text-lg font-bold text-green-600 dark:text-green-400">
                {errorRate}
              </div>
            </div>
          </div>
        </div>
 
        {/* Recommendations (visible only when showExtendedDetails is true) */}
        {showExtendedDetails && recommendations.length > 0 && (
          <div className="mt-4">
            <h3 className="text-lg font-medium mb-2">Recommendations</h3>
            <ul className="list-disc pl-5 space-y-1">
              {recommendations.map((rec, index) => (
                <li
                  key={index}
                  className="text-sm text-gray-600 dark:text-gray-400"
                >
                  {rec}
                </li>
              ))}
            </ul>
          </div>
        )}
      </div>
    </WidgetContainer>
  );
};
 
export default IntegrityImpactWidget;