All files / src/utils statusUtils.ts

100% Statements 26/26
100% Branches 22/22
100% Functions 3/3
100% Lines 15/15

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                                                                                              43x 43x 41x 38x 36x 33x 31x                                                       31x 29x 27x 7x 3x                                                         10x 7x 3x    
import { StatusType } from "../types/common/StatusTypes";
 
/**
 * Status utility functions for badge variants and styling
 * 
 * Provides consistent status-to-style mapping for badges, alerts, and
 * visual indicators throughout the application. Ensures uniform color
 * representation of risk levels and compliance status.
 * 
 * @example
 * ```typescript
 * import { getStatusVariant, getRiskColorClass } from './statusUtils';
 * 
 * // Get badge variant
 * const variant = getStatusVariant('high'); // 'success'
 * 
 * // Get risk color class
 * const colorClass = getRiskColorClass('High Risk'); // 'text-orange-600 dark:text-orange-400'
 * ```
 */
 
/**
 * Converts a risk level string to a status badge variant
 * 
 * Maps security levels to appropriate badge variants for consistent
 * visual representation. Uses color-coded variants that align with
 * security posture intuition (green=secure, red=insecure).
 *
 * @param level - The risk level string (e.g., "Low Risk", "High Risk")
 * @returns The corresponding StatusType for the badge
 * 
 * @example
 * ```typescript
 * getStatusVariant('none')       // 'error' (red - critical)
 * getStatusVariant('low')        // 'warning' (yellow)
 * getStatusVariant('moderate')   // 'info' (blue)
 * getStatusVariant('high')       // 'success' (green)
 * getStatusVariant('very high')  // 'purple' (premium)
 * getStatusVariant('unknown')    // 'neutral' (gray)
 * 
 * // Usage in component
 * <StatusBadge variant={getStatusVariant(securityLevel)}>
 *   {securityLevel}
 * </StatusBadge>
 * ```
 */
export function getStatusVariant(level: string): StatusType {
  const normalizedLevel = level.toLowerCase();
  if (normalizedLevel === "none") return "error";
  if (normalizedLevel === "low") return "warning";
  if (normalizedLevel === "moderate") return "info";
  if (normalizedLevel === "high") return "success";
  if (normalizedLevel === "very high") return "purple";
  return "neutral";
}
 
/**
 * Gets the appropriate Tailwind CSS color class for a risk level
 * 
 * Provides Tailwind CSS color classes for risk level text styling with
 * dark mode support. Colors semantically represent risk severity using
 * industry-standard color conventions.
 *
 * @param risk - The risk level string (e.g., "Low Risk", "Critical Risk")
 * @returns Tailwind CSS class string for text color with dark mode support
 * 
 * @example
 * ```typescript
 * getRiskColorClass('Low Risk')      // 'text-green-600 dark:text-green-400'
 * getRiskColorClass('Medium Risk')   // 'text-yellow-600 dark:text-yellow-400'
 * getRiskColorClass('High Risk')     // 'text-orange-600 dark:text-orange-400'
 * getRiskColorClass('Critical Risk') // 'text-red-600 dark:text-red-400'
 * getRiskColorClass('Unknown')       // 'text-gray-600 dark:text-gray-400'
 * 
 * // Usage in component
 * <span className={getRiskColorClass(riskLevel)}>
 *   Risk Level: {riskLevel}
 * </span>
 * ```
 */
export function getRiskColorClass(risk: string): string {
  if (risk.includes("Low")) return "text-green-600 dark:text-green-400";
  if (risk.includes("Medium")) return "text-yellow-600 dark:text-yellow-400";
  if (risk.includes("High")) return "text-orange-600 dark:text-orange-400";
  if (risk.includes("Critical")) return "text-red-600 dark:text-red-400";
  return "text-gray-600 dark:text-gray-400";
}
 
/**
 * Gets compliance status text based on compliance score
 * 
 * Translates numeric compliance scores into human-readable status messages
 * for executives and stakeholders. Uses industry-standard thresholds for
 * compliance categorization.
 *
 * @param complianceScore - The compliance score percentage (0-100)
 * @returns Human-readable compliance status text
 * 
 * @example
 * ```typescript
 * getComplianceStatusText(95)  // 'Strong compliance position'
 * getComplianceStatusText(80)  // 'Strong compliance position'
 * getComplianceStatusText(65)  // 'Moderate compliance position'
 * getComplianceStatusText(50)  // 'Moderate compliance position'
 * getComplianceStatusText(30)  // 'Compliance gaps detected'
 * getComplianceStatusText(0)   // 'Compliance gaps detected'
 * 
 * // Usage in dashboard
 * const score = calculateComplianceScore();
 * const status = getComplianceStatusText(score);
 * <ComplianceCard score={score} status={status} />
 * ```
 */
export function getComplianceStatusText(complianceScore: number): string {
  if (complianceScore >= 80) return "Strong compliance position";
  if (complianceScore >= 50) return "Moderate compliance position";
  return "Compliance gaps detected";
}