All files / src/components/widgets/businessvalue ComplianceStatusWidget.tsx

79.54% Statements 70/88
77.87% Branches 88/113
90% Functions 18/20
78.48% Lines 62/79

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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555                                  3x 3x 3x 3x                         3x                           3x     19x     19x   19x                 19x             15x             15x       15x 45x   15x   19x           13x 13x                           19x 19x 19x 19x   17x 17x 14x           13x 13x         13x 13x       13x   13x                                 19x   19x 19x 19x                                     19x 17x   17x 19x             17x 2x                               19x 19x 19x         6x               6x                                                                               19x   19x                                                                                                                                                                               15x     15x                                               15x     15x                                               15x     15x                                                                           2x   2x         6x 2x   6x                                                       2x                                                                                                                             19x             19x        
import React, { useCallback, useMemo, useState } from "react";
import { WIDGET_ICONS, WIDGET_TITLES, UI_DISPLAY_LIMITS } from "../../../constants/appConstants";
import { COMPLIANCE_TEST_IDS } from "../../../constants/testIds";
import { SECURITY_ICONS } from "../../../constants/uiConstants";
import { useComplianceService } from "../../../hooks/useComplianceService";
import { CIAComponent, SecurityLevel } from "../../../types/cia";
import { StatusType } from "../../../types/common/StatusTypes";
import { ComplianceStatusDetails } from "../../../types/compliance";
import { ComplianceStatusWidgetProps } from "../../../types/widget-props";
import { isNullish } from "../../../utils/typeGuards";
import { getWidgetAriaDescription } from "../../../utils/accessibility";
import { WidgetClasses, cn } from "../../../utils/tailwindClassHelpers";
import StatusBadge from "../../common/StatusBadge";
import WidgetContainer from "../../common/WidgetContainer";
import WidgetErrorBoundary from "../../common/WidgetErrorBoundary";
 
// Add function to determine badge status with proper typing
const getBadgeStatus = (complianceScore: number): StatusType => {
  Eif (complianceScore >= 80) return "success";
  Eif (complianceScore >= 50) return "warning";
  return "error";
};
 
/**
 * ComplianceStatusWidget displays status of compliance with various frameworks
 *
 * ## Business Perspective
 *
 * This widget helps executives and compliance officers understand how their
 * security controls align with regulatory requirements and industry standards.
 * It helps organizations identify compliance gaps and prioritize security
 * investments to meet their regulatory obligations. 📋
 */
const ComplianceStatusWidget: React.FC<ComplianceStatusWidgetProps> = ({
  availabilityLevel,
  integrityLevel,
  confidentialityLevel,
  industry,
  region,
  className = "",
  testId = COMPLIANCE_TEST_IDS.WIDGET,
}) => {
  // Use the compliance service
  const {
    complianceService,
    error: serviceError,
    isLoading,
  } = useComplianceService();
 
  // Active framework for detailed view
  const [activeFramework, setActiveFramework] = useState<string | null>(null);
 
  // Calculate overall security level with proper type safety
  const overallSecurityLevel = useMemo(() => {
    // Convert security levels to numeric values
    const levelValues: Record<SecurityLevel, number> = {
      None: 0,
      Low: 1,
      Moderate: 2,
      High: 3,
      "Very High": 4,
    };
 
    // Calculate the minimum security level as the overall level
    const minValue = Math.min(
      levelValues[availabilityLevel],
      levelValues[integrityLevel],
      levelValues[confidentialityLevel]
    );
 
    // Map numeric value back to SecurityLevel using a type-safe approach
    const levels: SecurityLevel[] = [
      "None",
      "Low",
      "Moderate",
      "High",
      "Very High",
    ];
    return levels.find((_, index) => index === minValue) || "Moderate";
  }, [availabilityLevel, integrityLevel, confidentialityLevel]);
 
  // Get compliance status with proper error handling
  const complianceStatus = useMemo((): ComplianceStatusDetails | null => {
    if (isLoading || serviceError || !complianceService) return null;
 
    try {
      // Use industry and region context when appropriate
      return complianceService.getComplianceStatus(
        availabilityLevel,
        integrityLevel,
        confidentialityLevel
      );
    } catch (err) {
      console.error("Error getting compliance status:", err);
      return null;
    }
  }, [
    complianceService,
    availabilityLevel,
    integrityLevel,
    confidentialityLevel,
    industry, // Keep this dependency for potential future implementation
    region, // Keep this dependency for potential future implementation
    isLoading,
    serviceError,
  ]);
 
  // Get status text for display with proper error handling
  const getComplianceStatusText = useCallback((): string => {
    Eif (isLoading) return "Loading compliance status...";
    Eif (serviceError) return "Error loading compliance status";
    Eif (!complianceStatus) return "Unable to determine compliance status";
 
    try {
      if (complianceService?.getComplianceStatusText) {
        const statusText = complianceService.getComplianceStatusText(
          availabilityLevel,
          integrityLevel,
          confidentialityLevel
        );
 
        Eif (!isNullish(statusText)) {
          return statusText;
        }
      }
 
      // Fallback if service doesn't provide status text
      Eif (complianceStatus.status) {
        return complianceStatus.status;
      }
 
      // Final fallback
      return `Based on ${overallSecurityLevel} security level`;
    } catch (err) {
      console.error("Error getting compliance status text:", err);
      return "Unable to determine compliance status";
    }
  }, [
    complianceService,
    complianceStatus,
    availabilityLevel,
    integrityLevel,
    confidentialityLevel,
    overallSecurityLevel,
    isLoading,
    serviceError,
  ]);
 
  // Define a single helper function for framework status badges
  const getFrameworkStatusBadge = useCallback(
    (framework: string): StatusType => {
      Eif (!complianceStatus) return "neutral";
 
      if (complianceStatus.compliantFrameworks.includes(framework)) {
        return "success";
      } else if (
        complianceStatus.partiallyCompliantFrameworks.includes(framework)
      ) {
        return "warning";
      } else {
        return "error";
      }
    },
    [complianceStatus]
  );
 
  // Get status text
  const statusText = useMemo(
    () => getComplianceStatusText(),
    [getComplianceStatusText]
  );
 
  // Define gapAnalysis variable
  const gapAnalysis = useMemo(() => {
    if (isLoading || serviceError || !complianceService || !activeFramework)
      return null;
 
    try {
      return complianceService.getComplianceGapAnalysis(
        availabilityLevel,
        integrityLevel,
        confidentialityLevel,
        activeFramework
      );
    } catch (err) {
      console.error("Error getting gap analysis:", err);
      return null;
    }
  }, [
    complianceService,
    activeFramework,
    availabilityLevel,
    integrityLevel,
    confidentialityLevel,
    isLoading,
    serviceError,
  ]);
 
  // Create a type-safe implementation of getFrameworkRequiredLevel
  const getFrameworkRequiredLevel = useCallback(
    (framework: string, component: CIAComponent): SecurityLevel => {
      // If the service is available, use it
      Eif (complianceService?.getFrameworkRequiredLevel) {
        try {
          return complianceService.getFrameworkRequiredLevel(
            framework,
            component
          );
        } catch (err) {
          console.error(`Error getting required level for ${framework}:`, err);
        }
      }
 
      // Default fallback levels based on typical requirements
      const defaultLevels: Record<
        string,
        Record<CIAComponent, SecurityLevel>
      > = {
        "PCI DSS": {
          availability: "High",
          integrity: "High",
          confidentiality: "High",
        },
        HIPAA: {
          availability: "High",
          integrity: "High",
          confidentiality: "High",
        },
        GDPR: {
          availability: "Moderate",
          integrity: "High",
          confidentiality: "High",
        },
        "ISO 27001": {
          availability: "Moderate",
          integrity: "Moderate",
          confidentiality: "Moderate",
        },
        "SOC 2": {
          availability: "Moderate",
          integrity: "Moderate",
          confidentiality: "Moderate",
        },
      };
 
      // Return the default if available, otherwise return Moderate as a safe fallback
      return defaultLevels[framework]?.[component] || "Moderate";
    },
    [complianceService]
  );
 
  // Get framework description with error handling
  const getFrameworkDescription = useCallback(
    (framework: string): string => {
      if (!complianceService) return `${framework} requirements`;
 
      try {
        Eif (typeof complianceService.getFrameworkDescription === "function") {
          const description =
            complianceService.getFrameworkDescription(framework);
          return description || `${framework} requirements`;
        }
        return `${framework} requirements`;
      } catch (err) {
        console.error(`Error getting description for ${framework}:`, err);
        return `${framework} requirements`;
      }
    },
    [complianceService]
  );
 
  return (
    <WidgetErrorBoundary widgetName="Compliance Status">
      <WidgetContainer
        title={WIDGET_TITLES.COMPLIANCE_STATUS || "Compliance Status"}
        icon={WIDGET_ICONS.COMPLIANCE_STATUS || "📋"}
        className={className}
        testId={testId}
        isLoading={isLoading}
        error={serviceError}
      >
      <div 
        className="p-md"
        role="region"
        aria-label={getWidgetAriaDescription(
          "Compliance Status",
          "Status of compliance with regulatory frameworks and industry standards"
        )}
      >
        {/* Overall Compliance Status - Compact */}
        <section 
          className="mb-md"
          aria-label="Overall Compliance Status"
        >
          <div className="grid grid-cols-1 sm:grid-cols-2 gap-sm mb-sm">
            <div className="p-sm bg-info-light/10 dark:bg-info-dark/20 rounded-md border border-info-light/30 dark:border-info-dark/30">
              <div className="text-caption text-info-dark dark:text-info-light font-medium mb-xs">Compliance Status</div>
              <div className="text-body-lg font-bold text-info-dark dark:text-info-light">
                {statusText}
              </div>
            </div>
            <div className="p-sm bg-success-light/10 dark:bg-success-dark/20 rounded-md border border-success-light/30 dark:border-success-dark/30">
              <div className="text-caption text-success-dark dark:text-success-light font-medium mb-xs">Score</div>
              <div className="flex items-center">
                <div className="text-body-lg font-bold text-success-dark dark:text-success-light mr-sm" data-testid={COMPLIANCE_TEST_IDS.COMPLIANCE_SCORE}>
                  {complianceStatus?.complianceScore ?? 0}%
                </div>
                <StatusBadge
                  status={getBadgeStatus(complianceStatus?.complianceScore ?? 0)}
                  testId={COMPLIANCE_TEST_IDS.COMPLIANCE_STATUS_BADGE}
                >
                  {getBadgeStatus(complianceStatus?.complianceScore ?? 0)}
                </StatusBadge>
              </div>
            </div>
          </div>
          
          {/* Progress bar */}
          {complianceStatus && (
            <div 
              className="relative h-1.5 rounded-full bg-info-light/20 dark:bg-info-dark overflow-hidden"
              role="progressbar"
              aria-valuenow={complianceStatus.complianceScore ?? 0}
              aria-valuemin={0}
              aria-valuemax={100}
              aria-label={`Compliance score: ${complianceStatus.complianceScore ?? 0} percent`}
            >
              <div
                style={{ width: `${complianceStatus.complianceScore ?? 0}%` }}
                className="h-full bg-info dark:bg-info-light"
                data-testid={COMPLIANCE_TEST_IDS.COMPLIANCE_SCORE_BAR}
              ></div>
            </div>
          )}
        </section>
 
        {/* Framework Status - Horizontal Badge Layout */}
        {complianceStatus && (
          <div className="mb-md">
            <h3 className="text-body-lg font-medium mb-sm">Framework Status</h3>
            
            {/* Compliant Frameworks - Horizontal badges */}
            {complianceStatus.compliantFrameworks.length > 0 && (
              <div className="mb-sm">
                <div className="text-caption text-success-dark dark:text-success-light font-medium mb-xs"><span aria-hidden="true">✓</span> Compliant</div>
                <div className="flex flex-wrap gap-xs" data-testid={COMPLIANCE_TEST_IDS.COMPLIANT_FRAMEWORKS_LIST}>
                  {complianceStatus.compliantFrameworks.map((framework, index) => (
                    <button
                      type="button"
                      key={framework}
                      onClick={() => setActiveFramework(framework)}
                      className={cn(
                        "px-sm py-xs rounded-md text-caption font-medium transition-all",
                        "bg-success-light/20 dark:bg-success-dark/30 text-success-dark dark:text-success-light",
                        "border border-success-light/50 dark:border-success-dark/50",
                        "hover:bg-success-light/30 dark:hover:bg-success-dark/40",
                        activeFramework === framework && "ring-2 ring-success dark:ring-success-light"
                      )}
                      data-testid={`${COMPLIANCE_TEST_IDS.FRAMEWORK_ITEM_PREFIX}-${index}`}
                      aria-label={`View ${framework} compliance details`}
                    >
                      {framework}
                    </button>
                  ))}
                </div>
              </div>
            )}
 
            {/* Partially Compliant - Horizontal badges */}
            {complianceStatus.partiallyCompliantFrameworks.length > 0 && (
              <div className="mb-sm">
                <div className="text-caption text-warning-dark dark:text-warning-light font-medium mb-xs"><span aria-hidden="true">âš </span> Partial</div>
                <div className="flex flex-wrap gap-xs" data-testid={COMPLIANCE_TEST_IDS.PARTIALLY_COMPLIANT_FRAMEWORKS_LIST}>
                  {complianceStatus.partiallyCompliantFrameworks.map((framework, index) => (
                    <button
                      type="button"
                      key={framework}
                      onClick={() => setActiveFramework(framework)}
                      className={cn(
                        "px-sm py-xs rounded-md text-caption font-medium transition-all",
                        "bg-warning-light/20 dark:bg-warning-dark/30 text-warning-dark dark:text-warning-light",
                        "border border-warning-light/50 dark:border-warning-dark/50",
                        "hover:bg-warning-light/30 dark:hover:bg-warning-dark/40",
                        activeFramework === framework && "ring-2 ring-warning dark:ring-warning-light"
                      )}
                      data-testid={`${COMPLIANCE_TEST_IDS.FRAMEWORK_ITEM_PREFIX}-partial-${index}`}
                      aria-label={`View ${framework} compliance details`}
                    >
                      {framework}
                    </button>
                  ))}
                </div>
              </div>
            )}
 
            {/* Non-Compliant - Horizontal badges */}
            {complianceStatus.nonCompliantFrameworks.length > 0 && (
              <div className="mb-sm">
                <div className="text-caption text-error-dark dark:text-error-light font-medium mb-xs"><span aria-hidden="true">✗</span> Non-Compliant</div>
                <div className="flex flex-wrap gap-xs" data-testid={COMPLIANCE_TEST_IDS.NON_COMPLIANT_FRAMEWORKS_LIST}>
                  {complianceStatus.nonCompliantFrameworks.map((framework, index) => (
                    <button
                      type="button"
                      key={framework}
                      onClick={() => setActiveFramework(framework)}
                      className={cn(
                        "px-sm py-xs rounded-md text-caption font-medium transition-all",
                        "bg-error-light/20 dark:bg-error-dark/30 text-error-dark dark:text-error-light",
                        "border border-error-light/50 dark:border-error-dark/50",
                        "hover:bg-error-light/30 dark:hover:bg-error-dark/40",
                        activeFramework === framework && "ring-2 ring-error dark:ring-error-light"
                      )}
                      data-testid={`${COMPLIANCE_TEST_IDS.FRAMEWORK_ITEM_PREFIX}-non-${index}`}
                      aria-label={`View ${framework} compliance details`}
                    >
                      {framework}
                    </button>
                  ))}
                </div>
              </div>
            )}
          </div>
        )}
 
        {/* Framework Gap Analysis - Compact */}
        {activeFramework && (
          <div className="mb-md">
            <div className="p-sm bg-neutral-light/5 dark:bg-neutral-dark/10 rounded-md border border-neutral-light/20 dark:border-neutral-dark/20">
              <h3 className="text-body-lg font-medium mb-sm">{activeFramework} Gap Analysis</h3>
              
              {gapAnalysis ? (
                <div className="space-y-sm">
                  {/* Status */}
                  <div className="text-caption text-neutral-dark dark:text-neutral-light">
                    {gapAnalysis.isCompliant
                      ? `✓ Meeting ${activeFramework} requirements`
                      : `✗ Not fully meeting ${activeFramework} requirements`}
                  </div>
 
                  {/* Component Requirements - Compact Grid */}
                  <div className="grid grid-cols-1 sm:grid-cols-3 gap-xs">
                    {["availability", "integrity", "confidentiality"].map((comp) => {
                      const componentType = comp as CIAComponent;
                      const currentLevel =
                        componentType === "availability"
                          ? availabilityLevel
                          : componentType === "integrity"
                          ? integrityLevel
                          : confidentialityLevel;
                      const requiredLevel = getFrameworkRequiredLevel(activeFramework, componentType);
                      const isMeeting = getSecurityLevelValue(currentLevel) >= getSecurityLevelValue(requiredLevel);
 
                      return (
                        <div
                          key={comp}
                          className={cn(
                            "p-xs rounded text-caption",
                            isMeeting
                              ? "bg-success-light/10 dark:bg-success-dark/20 text-success-dark dark:text-success-light"
                              : "bg-error-light/10 dark:bg-error-dark/20 text-error-dark dark:text-error-light"
                          )}
                        >
                          <div className="font-medium mb-xs">
                            {componentType.charAt(0).toUpperCase() + componentType.slice(1)}
                          </div>
                          <div className="flex flex-col">
                            <span>{currentLevel}</span>
                            <span className="opacity-70">→ {requiredLevel}</span>
                          </div>
                        </div>
                      );
                    })}
                  </div>
 
                  {/* Gaps and Recommendations - Compact */}
                  {gapAnalysis.gaps && gapAnalysis.gaps.length > 0 && (
                    <div className="text-caption text-error-dark dark:text-error-light">
                      <div className="font-medium mb-xs">Gaps:</div>
                      <ul className="space-y-xs pl-sm">
                        {gapAnalysis.gaps.slice(0, UI_DISPLAY_LIMITS.MAX_DISPLAYED_GAPS).map((gap, index) => (
                          <li key={index}>
                            • {typeof gap === "string" ? gap : gap.framework || gap.frameworkDescription || "Undefined gap"}
                          </li>
                        ))}
                        {gapAnalysis.gaps.length > UI_DISPLAY_LIMITS.MAX_DISPLAYED_GAPS && (
                          <li className="opacity-70">
                            + {gapAnalysis.gaps.length - UI_DISPLAY_LIMITS.MAX_DISPLAYED_GAPS} more gap
                            {gapAnalysis.gaps.length - UI_DISPLAY_LIMITS.MAX_DISPLAYED_GAPS !== 1 ? "s" : ""}
                          </li>
                        )}
                      </ul>
                    </div>
                  )}
                </div>
              ) : (
                <div className="text-center text-caption text-neutral-dark/70 dark:text-neutral-light/70 py-sm">
                  No gap analysis available
                </div>
              )}
            </div>
          </div>
        )}
 
        {/* Compliance Tips - Compact */}
        <div className="p-sm bg-info-light/5 dark:bg-info-dark/10 rounded-md border border-info-light/20 dark:border-info-dark/20">
          <div className="flex items-center mb-xs">
            <span className="text-info-dark dark:text-info-light mr-xs" aria-hidden="true">💡</span>
            <h3 className="text-body font-medium">Tips</h3>
          </div>
          <ul className="text-caption text-neutral-dark dark:text-neutral-light space-y-xs pl-sm">
            {complianceStatus && complianceStatus.complianceScore ? (
              complianceStatus.complianceScore < 50 ? (
                <>
                  <li>• Focus on foundational security controls</li>
                  <li>• Prioritize multi-framework controls</li>
                </>
              ) : complianceStatus.complianceScore < 80 ? (
                <>
                  <li>• Address specific gaps in partial frameworks</li>
                  <li>• Implement regular compliance monitoring</li>
                </>
              ) : (
                <>
                  <li>• Maintain strong posture with regular reviews</li>
                  <li>• Prepare for upcoming regulatory changes</li>
                </>
              )
            ) : (
              <>
                <li>• Implement controls across all CIA components</li>
                <li>• Start with industry-relevant frameworks</li>
              </>
            )}
          </ul>
        </div>
      </div>
    </WidgetContainer>
    </WidgetErrorBoundary>
  );
};
 
// Helper function to convert security level to numeric value
function getSecurityLevelValue(level: SecurityLevel): number {
  const levelValues: Record<SecurityLevel, number> = {
    None: 0,
    Low: 1,
    Moderate: 2,
    High: 3,
    "Very High": 4,
  };
  return levelValues[level] || 0;
}
 
export default ComplianceStatusWidget;