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 | 20x 20x 20x 10x 10x 10x 10x 10x 10x 20x 10x 10x 10x 1x 20x 20x 20x 20x 20x 2x 18x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 18x 20x 10x 10x 10x 10x 10x 20x 10x 10x 10x 1x 20x 10x 9x 1x 20x 10x 20x 20x 20x 20x 20x 20x 2x 20x 20x 20x 20x 20x 20x 2x 20x 10x 10x 10x 20x | import { useMemo } from "react";
import { SecurityLevel } from "../types/cia";
import {
CIAContentService,
ComplianceService,
} from "../types/cia-services";
import { ComplianceStatusType } from "../types/compliance";
import { calculateROIEstimate } from "../utils/businessValueUtils";
import { calculateTotalSecurityCost } from "../utils/costCalculationUtils";
import {
calculateOverallSecurityLevel,
getSecurityLevelDescription,
getSecurityLevelValue,
} from "../utils/securityLevelUtils";
import {
getStatusVariant,
getRiskColorClass,
} from "../utils/statusUtils";
import { hasMethod, isNullish } from "../utils/typeGuards";
/**
* Custom hook for SecuritySummaryWidget data calculations
* Extracts all data transformation logic for better testability and reusability
*
* @param availabilityLevel - Selected availability security level
* @param integrityLevel - Selected integrity security level
* @param confidentialityLevel - Selected confidentiality security level
* @param ciaContentService - CIA content service instance
* @param complianceService - Compliance service instance
* @returns Computed security summary data and helper functions
*/
export function useSecuritySummaryData(
availabilityLevel: SecurityLevel,
integrityLevel: SecurityLevel,
confidentialityLevel: SecurityLevel,
ciaContentService: unknown,
complianceService: unknown
) {
const overallSecurityLevel = useMemo(
() =>
calculateOverallSecurityLevel(
availabilityLevel,
integrityLevel,
confidentialityLevel
),
[availabilityLevel, integrityLevel, confidentialityLevel]
);
const securityLevelDescription = useMemo(
() => getSecurityLevelDescription(overallSecurityLevel),
[overallSecurityLevel]
);
const securityScore = useMemo(() => {
const availabilityValue = getSecurityLevelValue(availabilityLevel);
const integrityValue = getSecurityLevelValue(integrityLevel);
const confidentialityValue = getSecurityLevelValue(confidentialityLevel);
const totalValue =
availabilityValue + integrityValue + confidentialityValue;
const maxPossibleValue = 12; // 3 components x maximum value of 4
return Math.round((totalValue / maxPossibleValue) * 100);
}, [availabilityLevel, integrityLevel, confidentialityLevel]);
const riskLevel = useMemo(() => {
Iif (securityScore >= 80) return "Low Risk";
Iif (securityScore >= 60) return "Medium Risk";
if (securityScore >= 40) return "High Risk";
return "Critical Risk";
}, [securityScore]);
const securityClassification = useMemo((): string => {
Eif (!isNullish(ciaContentService)) {
try {
Iif (hasMethod(ciaContentService, "getSecurityClassification")) {
const classification =
ciaContentService.getSecurityClassification(overallSecurityLevel);
if (!isNullish(classification)) return classification as string;
}
} catch (err) {
console.error("Error fetching security classification:", err);
}
}
switch (overallSecurityLevel) {
case "None":
return "Minimal Security";
case "Low":
return "Basic Security";
case "Moderate":
return "Standard Security";
case "High":
return "Enhanced Security";
case "Very High":
return "Maximum Security";
default:
return "Unknown Security Level";
}
}, [ciaContentService, overallSecurityLevel]);
const dataClassification = useMemo((): string => {
Eif (
!isNullish(ciaContentService) &&
hasMethod(ciaContentService, "getInformationSensitivity")
) {
try {
const sensitivity = (ciaContentService as CIAContentService).getInformationSensitivity?.(
confidentialityLevel
);
Eif (!isNullish(sensitivity)) return sensitivity as string;
} catch (err) {
console.error("Error fetching information sensitivity:", err);
}
}
switch (confidentialityLevel) {
case "None":
return "Public Data";
case "Low":
return "Internal Data";
case "Moderate":
return "Confidential Data";
case "High":
return "Restricted Data";
case "Very High":
return "Classified Data";
default:
return "Unclassified Data";
}
}, [ciaContentService, confidentialityLevel]);
const implementationComplexity = useMemo((): string => {
Iif (
!isNullish(ciaContentService) &&
hasMethod(ciaContentService, "getImplementationComplexity")
) {
try {
const complexity = ciaContentService.getImplementationComplexity(
availabilityLevel,
integrityLevel,
confidentialityLevel
);
if (!isNullish(complexity)) return complexity as string;
} catch (err) {
console.error("Error fetching implementation complexity:", err);
}
}
const levelValues: Record<SecurityLevel, number> = {
None: 0,
Low: 1,
Moderate: 2,
High: 3,
"Very High": 4,
};
const availabilityValue = levelValues[availabilityLevel] || 0;
const integrityValue = levelValues[integrityLevel] || 0;
const confidentialityValue = levelValues[confidentialityLevel] || 0;
const totalValue =
availabilityValue + integrityValue + confidentialityValue;
if (totalValue <= 3) return "Low";
Eif (totalValue <= 6) return "Moderate";
if (totalValue <= 9) return "High";
return "Very High";
}, [
ciaContentService,
availabilityLevel,
integrityLevel,
confidentialityLevel,
]);
const complianceStatus = useMemo((): ComplianceStatusType | null => {
try {
Iif (isNullish(complianceService)) return null;
const status = (complianceService as ComplianceService).getComplianceStatus?.(
availabilityLevel,
integrityLevel,
confidentialityLevel
);
Eif (status) {
return {
...status,
compliantFrameworks: status.compliantFrameworks || [],
partiallyCompliantFrameworks:
status.partiallyCompliantFrameworks || [],
nonCompliantFrameworks: status.nonCompliantFrameworks || [],
};
}
return null;
} catch (err) {
console.error("Error fetching compliance status:", err);
return null;
}
}, [
complianceService,
availabilityLevel,
integrityLevel,
confidentialityLevel,
]);
const businessMaturityLevel = useMemo(() => {
Iif (securityScore >= 80) return "Strategic";
Iif (securityScore >= 60) return "Advanced";
if (securityScore >= 40) return "Standard";
return "Basic";
}, [securityScore]);
const businessMaturityDescription = useMemo(() => {
switch (businessMaturityLevel) {
case "Strategic":
return "Enables competitive advantage and innovation";
case "Advanced":
return "Supports business growth and trusted partnerships";
case "Standard":
return "Maintains core business operations securely";
case "Basic":
return "Enables fundamental business activities";
default:
return "Unknown business maturity level";
}
}, [businessMaturityLevel]);
const costDetails = useMemo(() => {
return calculateTotalSecurityCost(
availabilityLevel,
integrityLevel,
confidentialityLevel,
"medium",
"general"
);
}, [availabilityLevel, integrityLevel, confidentialityLevel]);
const implementationTime = useMemo((): string => {
try {
Iif (
!isNullish(ciaContentService) &&
hasMethod(ciaContentService, "getTotalImplementationTime")
) {
const time = ciaContentService.getTotalImplementationTime(
availabilityLevel,
integrityLevel,
confidentialityLevel
);
if (!isNullish(time)) return time as string;
}
} catch (err) {
console.error("Error fetching implementation time:", err);
}
Iif (securityScore >= 80) return "3-6 months";
Iif (securityScore >= 60) return "2-4 months";
if (securityScore >= 40) return "1-2 months";
return "2-4 weeks";
}, [ciaContentService, securityScore, availabilityLevel, integrityLevel, confidentialityLevel]);
const requiredResources = useMemo((): string => {
try {
Iif (
!isNullish(ciaContentService) &&
hasMethod(ciaContentService, "getRequiredExpertise")
) {
const resources = ciaContentService.getRequiredExpertise(
availabilityLevel,
integrityLevel,
confidentialityLevel
);
if (!isNullish(resources)) return resources as string;
}
} catch (err) {
console.error("Error fetching resource requirements:", err);
}
Iif (securityScore >= 80) return "Specialized Team";
Iif (securityScore >= 60) return "Dedicated Team";
if (securityScore >= 40) return "Small Team";
return "Individual Effort";
}, [ciaContentService, securityScore, availabilityLevel, integrityLevel, confidentialityLevel]);
const roiEstimate = useMemo(() => {
try {
const estimate = calculateROIEstimate(
availabilityLevel,
integrityLevel,
confidentialityLevel
);
return estimate.value ?? "N/A";
} catch (err) {
console.error("Error calculating ROI estimate:", err);
return "N/A";
}
}, [availabilityLevel, integrityLevel, confidentialityLevel]);
return {
overallSecurityLevel,
securityLevelDescription,
securityScore,
riskLevel,
securityClassification,
dataClassification,
implementationComplexity,
complianceStatus,
businessMaturityLevel,
businessMaturityDescription,
costDetails,
implementationTime,
requiredResources,
roiEstimate,
getStatusVariant,
getRiskColorClass,
};
}
|