All files / src/components/common MetricsCard.tsx

79.12% Statements 72/91
75% Branches 6/8
33.33% Functions 1/3
79.12% Lines 72/91

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  1x                                                                                                                                                       1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x   10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x   10x 10x     10x           10x           10x 10x 10x 10x 10x 10x 1x 9x     10x 10x 10x 10x 10x 10x 10x 10x 10x                 10x                 10x 10x   10x   1x  
import React, { ReactNode } from 'react';
import { COMMON_COMPONENT_TEST_IDS } from '../../constants/testIds';
 
export type MetricsCardVariant =
  | "default"
  | "success"
  | "warning"
  | "error"
  | "info"
  | "purple";
 
interface MetricsCardProps {
  /**
   * Card title
   */
  title: string;
  
  /**
   * Card value (main metric)
   */
  value: ReactNode;
  
  /**
   * Optional icon to display
   */
  icon?: ReactNode;
  
  /**
   * Optional label to display below the value
   */
  label?: string;
  
  /**
   * Optional trend indicator (+10%, -5%, etc.)
   */
  trend?: string;
  
  /**
   * Direction of the trend (up, down, neutral)
   */
  trendDirection?: 'up' | 'down' | 'neutral';
  
  /**
   * Optional CSS class for the card
   */
  className?: string;
  
  /**
   * Optional test ID for automated testing
   */
  testId?: string;
  
  /**
   * Optional variant for the card
   */
  variant?: MetricsCardVariant;
  
  /**
   * Optional accent color for the card
   */
  accentColor?: string;
  
  /**
   * Optional compact mode for the card
   */
  compact?: boolean;
}
 
/**
 * Displays a metric card with a title, value, and optional trend
 * 
 * ## Business Perspective
 * 
 * This component provides a standardized way to display key metrics
 * across security dashboards, helping executives and security teams
 * quickly identify important information and trends. 📊
 */
const MetricsCard: React.FC<MetricsCardProps> = ({
  title,
  value,
  icon,
  label,
  trend,
  trendDirection = 'neutral',
  className = '',
  testId = COMMON_COMPONENT_TEST_IDS.METRICS_CARD,
  variant = "default",
  accentColor,
  compact = false,
}) => {
  // Color variants
  const variants = {
    default: {
      bg: "bg-gray-50 dark:bg-gray-800",
      text: "text-gray-800 dark:text-gray-200",
      border: "border-gray-200 dark:border-gray-700",
    },
    success: {
      bg: "bg-green-50 dark:bg-green-900 dark:bg-opacity-20",
      text: "text-green-700 dark:text-green-300",
      border: "border-green-200 dark:border-green-800",
    },
    warning: {
      bg: "bg-yellow-50 dark:bg-yellow-900 dark:bg-opacity-20",
      text: "text-yellow-700 dark:text-yellow-300",
      border: "border-yellow-200 dark:border-yellow-800",
    },
    error: {
      bg: "bg-red-50 dark:bg-red-900 dark:bg-opacity-20",
      text: "text-red-700 dark:text-red-300",
      border: "border-red-200 dark:border-red-800",
    },
    info: {
      bg: "bg-blue-50 dark:bg-blue-900 dark:bg-opacity-20",
      text: "text-blue-700 dark:text-blue-300",
      border: "border-blue-200 dark:border-blue-800",
    },
    purple: {
      bg: "bg-purple-50 dark:bg-purple-900 dark:bg-opacity-20",
      text: "text-purple-700 dark:text-purple-300",
      border: "border-purple-200 dark:border-purple-800",
    },
  };
 
  const { bg, text, border } = variants[variant];
  const padding = compact ? "p-3" : "p-4";
 
  // Determine trend color and icon
  const getTrendColor = () => {
    if (trendDirection === 'up') return 'text-green-500 dark:text-green-400';
    if (trendDirection === 'down') return 'text-red-500 dark:text-red-400';
    return 'text-gray-500 dark:text-gray-400';
  };
  
  const getTrendIcon = () => {
    if (trendDirection === 'up') return '↑';
    if (trendDirection === 'down') return '↓';
    return '→';
  };
 
  return (
    <div
      className={`${bg} rounded-lg border ${border} ${padding} shadow-sm ${className}`}
      data-testid={testId}
      style={
        accentColor
          ? { borderLeftColor: accentColor, borderLeftWidth: "4px" }
          : undefined
      }
    >
      <div className="flex items-center justify-between">
        <div className="text-sm font-medium text-gray-500 dark:text-gray-400">
          {title}
        </div>
        {icon && <div className="text-xl">{icon}</div>}
      </div>
      <div className={`text-xl font-bold mt-2 ${text}`}>{value}</div>
      <div className="flex items-center">
        {label && (
          <div 
            className="text-sm text-gray-500 dark:text-gray-400"
            data-testid={`${testId}-label`}
          >
            {label}
          </div>
        )}
        
        {trend && (
          <div 
            className={`ml-auto text-sm ${getTrendColor()}`}
            data-testid={`${testId}-trend`}
          >
            <span className="mr-1">{getTrendIcon()}</span>
            {trend}
          </div>
        )}
      </div>
    </div>
  );
};
 
export default MetricsCard;