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 | 5x 5x 5x 25x 25x 19x 19x 17x 17x 17x 21x 17x 25x 19x 17x 17x 17x 17x 83x 83x 24x 5x 83x 83x 83x 83x 83x 61x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 83x 3x 2x 83x 38x 11x 24x 11x 21x 16x 16x 21x 21x 2x 83x | import React, { useMemo, useCallback } from "react";
import { WIDGET_ICONS, WIDGET_TITLES } from "../../../constants/appConstants";
import {
AVAILABILITY_IMPACT_TEST_IDS,
AVAILABILITY_IMPACT_WIDGET_IDS,
INTEGRITY_IMPACT_TEST_IDS,
INTEGRITY_IMPACT_WIDGET_IDS,
CONFIDENTIALITY_IMPACT_TEST_IDS,
CONFIDENTIALITY_IMPACT_WIDGET_IDS,
} from "../../../constants/testIds";
import type { CIAComponentColor } from "../../../types/componentPropExports";
import {
getDefaultSLAMetrics,
getDefaultErrorRate,
getDefaultValidationLevel,
getDefaultPrivacyImpact,
} from "../../../data/ciaOptionsData";
import { useBusinessImpact, useComponentDetails } from "../../../hooks";
import { useCIAContentService } from "../../../hooks/useCIAContentService";
import type { CIAComponent, SecurityLevel } from "../../../types/cia";
import type { ImpactWidgetProps } from "../../../types/widget-props";
import { getSecurityLevelBackgroundClass } from "../../../utils/colorUtils";
import { normalizeSecurityLevel } from "../../../utils/securityLevelUtils";
import { getWidgetAriaDescription } from "../../../utils/accessibility";
import { WidgetClasses, cn } from "../../../utils/tailwindClassHelpers";
import BusinessImpactSection from "../../common/BusinessImpactSection";
import MetricCard from "../../common/MetricCard";
import SecurityLevelBadge from "../../common/SecurityLevelBadge";
import WidgetContainer from "../../common/WidgetContainer";
import WidgetErrorBoundary from "../../common/WidgetErrorBoundary";
import WidgetSection from "../../common/WidgetSection";
/**
* Configuration for each CIA component type
*
* Defines component-specific settings including titles, icons, colors,
* ARIA descriptions, and CSS classes for consistent rendering across
* all three impact widgets.
*/
interface ComponentConfig {
titleKey: keyof typeof WIDGET_TITLES;
iconKey: keyof typeof WIDGET_ICONS;
defaultTitle: string;
defaultIcon: string;
color: CIAComponentColor;
textClass: string;
ariaDescription: string;
categoryLabel: string;
/** Container-specific CSS classes */
containerClassName: string;
/** Content area-specific CSS classes */
contentClassName: string;
/** Function to generate security badge testId */
getSecurityBadgeTestId: (effectiveTestId: string, widgetIds: typeof AVAILABILITY_IMPACT_WIDGET_IDS) => string;
}
/**
* Retrieves the configuration used to render a specific CIA (Confidentiality, Integrity, Availability)
* impact widget variant.
*
* Maps the provided {@link CIAComponent} to its corresponding {@link ComponentConfig}, which includes
* titles, icons, colors, ARIA descriptions, layout classes, and test ID helpers for the widget UI.
*
* @param component - The CIA component type to configure. Supported values are
* `"availability"`, `"integrity"`, and `"confidentiality"`. Any other value will cause an error
* to be thrown to preserve exhaustive type checking.
* @returns A {@link ComponentConfig} object containing all visual and accessibility settings for
* the specified CIA component.
*
* @example
* ```ts
* // Get configuration for the availability impact widget
* const availabilityConfig = getComponentConfig("availability");
*
* console.log(availabilityConfig.defaultTitle);
* // → "Availability Impact Analysis"
*
* // Use the test ID helper to build a badge test ID
* const badgeTestId = availabilityConfig.getSecurityBadgeTestId(
* "availability-impact",
* AVAILABILITY_IMPACT_WIDGET_IDS
* );
* ```
*/
const getComponentConfig = (component: CIAComponent): ComponentConfig => {
switch (component) {
case "availability":
return {
titleKey: "AVAILABILITY_IMPACT",
iconKey: "AVAILABILITY_IMPACT",
defaultTitle: "Availability Impact Analysis",
defaultIcon: "⏱️",
color: "blue",
textClass: "text-blue-800 dark:text-blue-300",
ariaDescription: "Business impact of availability controls including uptime targets and recovery objectives",
categoryLabel: "Availability",
containerClassName: "cia-availability",
contentClassName: "cia-widget",
getSecurityBadgeTestId: (_effectiveTestId, widgetIds) => widgetIds.label("security-level"),
};
case "integrity":
return {
titleKey: "INTEGRITY_IMPACT",
iconKey: "INTEGRITY_IMPACT",
defaultTitle: "Integrity Impact Analysis",
defaultIcon: "✓",
color: "green",
textClass: "text-green-800 dark:text-green-300",
ariaDescription: "Business impact of integrity controls including data accuracy and validation mechanisms",
categoryLabel: "Integrity",
containerClassName: "",
contentClassName: "",
getSecurityBadgeTestId: (_effectiveTestId, widgetIds) => widgetIds.label("security-badge"),
};
case "confidentiality":
return {
titleKey: "CONFIDENTIALITY_IMPACT",
iconKey: "CONFIDENTIALITY_IMPACT",
defaultTitle: "Confidentiality Impact Analysis",
defaultIcon: "🔒",
color: "orange",
textClass: "text-orange-800 dark:text-orange-300",
ariaDescription: "Business impact of confidentiality controls including data classification and privacy measures",
categoryLabel: "Confidentiality",
containerClassName: "overflow-visible",
contentClassName: "max-h-[550px] overflow-y-auto pr-1",
getSecurityBadgeTestId: (_effectiveTestId, widgetIds) => widgetIds.label("security-badge"),
};
default: {
// Exhaustive check to ensure all CIAComponent values are handled
const exhaustiveCheck: never = component;
throw new Error(`Unsupported CIA component in ImpactWidget: ${String(exhaustiveCheck)}`);
}
}
};
/**
* Resolves test ID configuration for a given CIA component.
*
* Maps the logical CIA component (`availability`, `integrity`, or `confidentiality`)
* to the corresponding test ID prefix and widget-specific test ID constants.
* This ensures that all impact widgets use a consistent and centrally managed
* set of test IDs for automated testing and QA tooling.
*
* @param component - The CIA component type for which test IDs should be resolved.
* @returns An object containing:
* - `prefix`: The base test ID prefix for the selected CIA component.
* - `widgetIds`: The collection of widget-specific test ID constants associated
* with the selected CIA component.
*
* @example
* ```ts
* // Get test ID configuration for the availability impact widget
* const { prefix, widgetIds } = getTestIds("availability");
*
* // Example usage when building a test ID for the security level badge
* const securityBadgeTestId = `${prefix}${widgetIds.SECURITY_LEVEL_BADGE}`;
* ```
*/
const getTestIds = (component: CIAComponent) => {
switch (component) {
case "availability":
return {
prefix: AVAILABILITY_IMPACT_TEST_IDS.AVAILABILITY_IMPACT_PREFIX,
widgetIds: AVAILABILITY_IMPACT_WIDGET_IDS,
};
case "integrity":
return {
prefix: INTEGRITY_IMPACT_TEST_IDS.INTEGRITY_IMPACT_PREFIX,
widgetIds: INTEGRITY_IMPACT_WIDGET_IDS,
};
case "confidentiality":
return {
prefix: CONFIDENTIALITY_IMPACT_TEST_IDS.CONFIDENTIALITY_IMPACT_PREFIX,
widgetIds: CONFIDENTIALITY_IMPACT_WIDGET_IDS,
};
default: {
// Exhaustive check to ensure all CIAComponent values are handled
const exhaustiveCheck: never = component;
throw new Error(`Unsupported CIA component in getTestIds: ${String(exhaustiveCheck)}`);
}
}
};
/**
* Get border color class based on component color (explicit for Tailwind purging)
* @param color - Color name (blue, green, orange)
* @returns Explicit border color className string
*/
const getBorderColorClass = (color: CIAComponentColor): string => {
switch (color) {
case "blue": return "border-blue-500";
case "green": return "border-green-500";
case "orange": return "border-orange-500";
default: {
// Exhaustive check - TypeScript will error if new colors are added
const exhaustiveCheck: never = color;
throw new Error(`Unsupported color: ${String(exhaustiveCheck)}`);
}
}
};
/**
* Shared widget component for displaying CIA component impact analysis
*
* ## Business Perspective
*
* This widget provides a unified interface for displaying the business impact
* of security levels across all CIA triad components (Availability, Integrity,
* Confidentiality). It helps stakeholders understand the practical implications
* of their security choices through metrics, business impact analysis, and
* technical details. 📊
*/
const ImpactWidget = React.memo<ImpactWidgetProps>(({
component,
level,
className = "",
testId,
showExtendedDetails = false,
onError,
}) => {
// Get component-specific configuration (memoized for performance)
const config = useMemo(() => getComponentConfig(component), [component]);
const testIds = useMemo(() => getTestIds(component), [component]);
const effectiveTestId = testId || testIds.prefix;
// Use security level utility for consistent normalization
const effectiveLevel = useMemo(() =>
normalizeSecurityLevel(level || "Moderate"),
[level]
);
// Get CIA content service for loading/error states
const { ciaContentService, error, isLoading } = useCIAContentService();
// Invoke error callback when service error occurs (memoized callback)
const handleError = useCallback((err: Error) => {
Iif (onError) {
onError(err);
}
}, [onError]);
React.useEffect(() => {
Eif (error) {
handleError(error);
}
}, [error, handleError]);
// Use custom hooks for data fetching
const details = useComponentDetails(component, effectiveLevel);
const businessImpact = useBusinessImpact(component, effectiveLevel);
// Get recommendations for extended details
const recommendations = useMemo(() => {
Eif (!showExtendedDetails || !ciaContentService) return [];
try {
return ciaContentService.getRecommendations(component, effectiveLevel) || [];
} catch (err) {
console.error(`Error getting ${component} recommendations:`, err);
return [];
}
}, [showExtendedDetails, ciaContentService, component, effectiveLevel]);
// Get component-specific metrics
const metrics = useMemo(() => {
switch (component) {
case "availability": {
const defaultMetrics = getDefaultSLAMetrics(effectiveLevel);
return {
type: "availability" as const,
data: {
uptime: details?.uptime || defaultMetrics.uptime,
rto: details?.rto || defaultMetrics.rto,
rpo: details?.rpo || defaultMetrics.rpo,
mttr: details?.mttr || defaultMetrics.mttr,
sla: details?.sla || defaultMetrics.sla,
},
};
}
case "integrity":
return {
type: "integrity" as const,
data: {
validationLevel: details?.validationLevel || getDefaultValidationLevel(effectiveLevel),
errorRate: details?.errorRate || getDefaultErrorRate(effectiveLevel),
},
};
case "confidentiality": {
// Get data classification with proper error handling
let dataClassification: string;
if (ciaContentService) {
try {
dataClassification = ciaContentService.getInformationSensitivity(effectiveLevel);
} catch {
dataClassification = `${effectiveLevel} Classification`;
}
} else {
dataClassification = `${effectiveLevel} Classification`;
}
return {
type: "confidentiality" as const,
data: {
dataClassification,
privacyImpact: details?.privacyImpact || getDefaultPrivacyImpact(effectiveLevel),
},
};
}
default: {
// Exhaustive check to ensure all CIAComponent values are handled
const exhaustiveCheck: never = component;
throw new Error(`Unsupported CIA component in metrics calculation: ${String(exhaustiveCheck)}`);
}
}
}, [component, effectiveLevel, details, ciaContentService]);
return (
<WidgetErrorBoundary widgetName={`${config.categoryLabel} Impact`}>
<WidgetContainer
title={WIDGET_TITLES[config.titleKey] || config.defaultTitle}
icon={WIDGET_ICONS[config.iconKey] || config.defaultIcon}
className={`${className} ${config.containerClassName}`}
testId={effectiveTestId}
isLoading={isLoading}
error={error}
>
<div
className={cn(
"p-sm sm:p-md border-l-4",
getBorderColorClass(config.color),
config.contentClassName
)}
role="region"
aria-label={getWidgetAriaDescription(config.defaultTitle, config.ariaDescription)}
>
{/* Security level indicator */}
<section
className="mb-md"
aria-labelledby={`${component}-level-heading`}
>
<h3 id={`${component}-level-heading`} className="sr-only">
Current {config.categoryLabel} Security Level
</h3>
<SecurityLevelBadge
category={config.categoryLabel}
level={effectiveLevel}
colorClass={getSecurityLevelBackgroundClass(config.color)}
textClass={config.textClass}
testId={config.getSecurityBadgeTestId(effectiveTestId, testIds.widgetIds)}
/>
</section>
{/* Business Impact Analysis */}
{businessImpact && (
<section
className="mb-sm"
aria-labelledby={`${component}-business-impact-heading`}
>
<h3 id={`${component}-business-impact-heading`} className={WidgetClasses.heading}>
Business Impact
</h3>
<BusinessImpactSection
impact={businessImpact}
color={config.color}
testId={testIds.widgetIds.section("business-impact")}
/>
</section>
)}
{/* Component-specific metrics */}
{metrics.type === "availability" && (
<WidgetSection
title="SLA Metrics"
icon="⏱️"
variant="info"
className="mb-md"
ariaLabelledBy="sla-metrics-heading"
testId={testIds.widgetIds.section("sla-metrics")}
>
<div
className={cn(WidgetClasses.grid3Cols, "mb-md")}
role="group"
aria-label="Service level agreement metrics"
>
<MetricCard
label="Uptime Target"
value={metrics.data.uptime}
icon="📈"
description="Expected system availability"
variant="info"
testId={testIds.widgetIds.label("uptime")}
/>
<MetricCard
label="Recovery Time Objective"
value={metrics.data.rto}
icon="⏱️"
description="Time to restore service"
variant="info"
testId={testIds.widgetIds.label("rto")}
/>
<MetricCard
label="Recovery Point Objective"
value={metrics.data.rpo}
icon="💾"
description="Maximum data loss allowed"
variant="info"
testId={testIds.widgetIds.label("rpo")}
/>
</div>
<div className={WidgetClasses.grid2Cols}>
<MetricCard
label="Service Level Agreement"
value={metrics.data.sla}
icon="📋"
description="Support coverage period"
variant="info"
testId={testIds.widgetIds.label("sla")}
/>
</div>
</WidgetSection>
)}
{metrics.type === "integrity" && (
<WidgetSection
title="Data Integrity Metrics"
icon="📊"
variant="success"
className="mb-md"
testId={testIds.widgetIds.section("metrics")}
>
<div className={WidgetClasses.grid2Cols}>
<MetricCard
label="Data Validation Controls"
value={metrics.data.validationLevel}
icon="✓"
variant="success"
testId={testIds.widgetIds.label("validation")}
/>
<MetricCard
label="Acceptable Error Rate"
value={metrics.data.errorRate}
icon="📉"
variant="success"
testId={testIds.widgetIds.label("error-rate")}
/>
</div>
</WidgetSection>
)}
{metrics.type === "confidentiality" && (
<>
<WidgetSection
title="Data Protection"
icon="📊"
variant="primary"
className="mb-lg"
testId={testIds.widgetIds.section("data-protection")}
>
<div className={WidgetClasses.grid2Cols}>
<MetricCard
label="Data Classification"
value={metrics.data.dataClassification}
icon="🏷️"
description="Level of data sensitivity"
variant="primary"
testId={testIds.widgetIds.label("classification")}
/>
</div>
</WidgetSection>
<WidgetSection
title="Privacy Impact"
icon="🔒"
variant="primary"
className="mb-lg"
testId={testIds.widgetIds.section("privacy-impact")}
>
<div className="grid grid-cols-1 lg:grid-cols-2 gap-md">
<MetricCard
label="Privacy Impact"
value={metrics.data.privacyImpact}
icon="🛡️"
variant="primary"
testId={testIds.widgetIds.label("privacy")}
/>
</div>
</WidgetSection>
</>
)}
{/*
Recommendations (visible only for integrity when showExtendedDetails is true)
Note: Only IntegrityImpactWidget supports recommendations in the original implementation.
This maintains backward compatibility with the existing API.
*/}
{showExtendedDetails && component === "integrity" && recommendations.length > 0 && (
<div className="mt-md">
<h3 className="text-lg font-medium mb-sm">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>
</WidgetErrorBoundary>
);
});
// Set display name for debugging
ImpactWidget.displayName = "ImpactWidget";
export default ImpactWidget;
|