All files / src/components/features/admin/monitoring/alerts/AlertConfigList index.tsx

98.21% Statements 275/280
84.09% Branches 37/44
100% Functions 10/10
98.21% Lines 275/280

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 2811x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 30x 30x 30x 27x 30x 1x 30x 1x 30x 1x 30x   30x 30x 1x 1x 1x 1x 30x 30x 30x 30x 28x 30x 1x 30x 1x 30x   30x 30x 1x 1x 1x 1x 1x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 1x 1x 1x 1x 1x 1x 1x 31x 31x 31x 1x 1x 1x 1x 1x 1x 1x 1x 31x 31x 31x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 29x 29x 29x 29x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 2x 2x 2x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 4x 4x 4x 4x 4x 4x 4x 4x 1x 4x 3x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 30x 26x 26x 26x 26x 26x 26x 26x 26x 26x 30x 30x 30x 30x 30x 30x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 30x 30x 30x 29x 29x 29x 29x 1x 1x  
'use client';
 
import { useState } from 'react';
import { cn } from '@/lib/core';
import { StatusIndicator } from '../StatusIndicator';
import { Button } from '@/components/ui/Button';
import { Icon } from '@/components/ui/icons';
import type { PerformanceAlertConfig, PerformanceAlert } from '@prisma/client';
 
export interface AlertConfigListProps {
  /** List of alert configurations */
  configs: (PerformanceAlertConfig & {
    alerts: PerformanceAlert[];
    _count: { alerts: number };
  })[];
  /** Callback when editing a config */
  onEdit?: (config: PerformanceAlertConfig) => void;
  /** Callback when toggling enabled status */
  onToggle?: (id: string, enabled: boolean) => Promise<void>;
  /** Callback when deleting a config */
  onDelete?: (id: string) => Promise<void>;
  /** Additional CSS classes */
  className?: string;
}
 
/**
 * Get metric label for display
 */
function getMetricLabel(metricType: string): string {
  switch (metricType) {
    case 'response_time':
      return 'Response Time';
    case 'p95_latency':
      return 'P95 Latency';
    case 'error_rate':
      return 'Error Rate';
    case 'throughput':
      return 'Throughput';
    default:
      return metricType;
  }
}
 
/**
 * Get metric unit for display
 */
function getMetricUnit(metricType: string): string {
  switch (metricType) {
    case 'response_time':
    case 'p95_latency':
      return 'ms';
    case 'error_rate':
      return '%';
    case 'throughput':
      return ' req/min';
    default:
      return '';
  }
}
 
/**
 * AlertConfigList displays a list of alert configurations
 * with options to edit, toggle, and delete.
 */
export function AlertConfigList({
  configs,
  onEdit,
  onToggle,
  onDelete,
  className,
}: AlertConfigListProps) {
  const [loadingId, setLoadingId] = useState<string | null>(null);
  const [deleteConfirmId, setDeleteConfirmId] = useState<string | null>(null);
 
  const handleToggle = async (config: PerformanceAlertConfig) => {
    if (!onToggle) return;
    setLoadingId(config.id);
    try {
      await onToggle(config.id, !config.enabled);
    } finally {
      setLoadingId(null);
    }
  };
 
  const handleDelete = async (id: string) => {
    if (!onDelete) return;
    setLoadingId(id);
    try {
      await onDelete(id);
      setDeleteConfirmId(null);
    } finally {
      setLoadingId(null);
    }
  };
 
  if (configs.length === 0) {
    return (
      <div className={cn('bg-gray-50 dark:bg-gray-800/50 rounded-lg p-8 text-center', className)}>
        <Icon name="bell" className="mx-auto h-12 w-12 text-gray-400" />
        <h3 className="mt-3 text-lg font-medium text-gray-900 dark:text-white">
          No Alert Configurations
        </h3>
        <p className="mt-1 text-sm text-gray-500 dark:text-gray-400">
          Create your first alert configuration to start monitoring performance.
        </p>
      </div>
    );
  }
 
  return (
    <div className={cn('space-y-4', className)}>
      {configs.map((config) => {
        const unit = getMetricUnit(config.metricType);
        const isLoading = loadingId === config.id;
        const showDeleteConfirm = deleteConfirmId === config.id;
 
        return (
          <div
            key={config.id}
            className={cn(
              'bg-white dark:bg-gray-800 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700 p-4',
              !config.enabled && 'opacity-60'
            )}
          >
            <div className="flex items-start justify-between gap-4">
              <div className="flex items-start gap-4 min-w-0">
                <StatusIndicator
                  status={config.currentStatus as 'ok' | 'warning' | 'critical'}
                  size="lg"
                  animate={config.currentStatus !== 'ok'}
                />
                <div className="min-w-0">
                  <div className="flex items-center gap-2">
                    <h3 className="font-medium text-gray-900 dark:text-white truncate">
                      {config.name}
                    </h3>
                    {!config.enabled && (
                      <span className="inline-flex items-center rounded-full bg-gray-100 dark:bg-gray-700 px-2 py-0.5 text-xs font-medium text-gray-600 dark:text-gray-400">
                        Disabled
                      </span>
                    )}
                  </div>
                  {config.description && (
                    <p className="mt-1 text-sm text-gray-500 dark:text-gray-400 line-clamp-1">
                      {config.description}
                    </p>
                  )}
                  <div className="mt-2 flex flex-wrap items-center gap-x-4 gap-y-1 text-xs text-gray-500 dark:text-gray-400">
                    <span className="inline-flex items-center gap-1">
                      <Icon name="activity" className="h-3.5 w-3.5" />
                      {getMetricLabel(config.metricType)}
                    </span>
                    <span>
                      Scope: <code className="bg-gray-100 dark:bg-gray-700 px-1 rounded">{config.scope}</code>
                    </span>
                    <span className="text-yellow-600 dark:text-yellow-400">
                      Warning: {config.warningThreshold}{unit}
                    </span>
                    <span className="text-red-600 dark:text-red-400">
                      Critical: {config.criticalThreshold}{unit}
                    </span>
                    {config.lastValue !== null && (
                      <span>
                        Current: <strong>{config.lastValue.toFixed(2)}{unit}</strong>
                      </span>
                    )}
                    <span className="text-gray-400">
                      {config._count.alerts} alert{config._count.alerts !== 1 ? 's' : ''} total
                    </span>
                  </div>
                </div>
              </div>
 
              <div className="flex items-center gap-2 shrink-0">
                {/* Toggle Button */}
                <button
                  onClick={() => handleToggle(config)}
                  disabled={isLoading}
                  className={cn(
                    'relative inline-flex h-6 w-11 items-center rounded-full transition-colors',
                    config.enabled
                      ? 'bg-blue-600'
                      : 'bg-gray-200 dark:bg-gray-700'
                  )}
                  aria-label={config.enabled ? 'Disable alert' : 'Enable alert'}
                >
                  <span
                    className={cn(
                      'inline-block h-4 w-4 transform rounded-full bg-white transition-transform',
                      config.enabled ? 'translate-x-6' : 'translate-x-1'
                    )}
                  />
                </button>
 
                {/* Edit Button */}
                <Button
                  variant="ghost"
                  size="sm"
                  onClick={() => onEdit?.(config)}
                  disabled={isLoading}
                  aria-label="Edit configuration"
                >
                  <Icon name="edit" className="h-4 w-4" />
                </Button>
 
                {/* Delete Button */}
                {showDeleteConfirm ? (
                  <div className="flex items-center gap-1">
                    <Button
                      variant="danger"
                      size="sm"
                      onClick={() => handleDelete(config.id)}
                      disabled={isLoading}
                    >
                      {isLoading ? (
                        <Icon name="spinner" className="h-4 w-4 animate-spin" />
                      ) : (
                        'Confirm'
                      )}
                    </Button>
                    <Button
                      variant="ghost"
                      size="sm"
                      onClick={() => setDeleteConfirmId(null)}
                      disabled={isLoading}
                    >
                      Cancel
                    </Button>
                  </div>
                ) : (
                  <Button
                    variant="ghost"
                    size="sm"
                    onClick={() => setDeleteConfirmId(config.id)}
                    disabled={isLoading}
                    aria-label="Delete configuration"
                  >
                    <Icon name="trash" className="h-4 w-4 text-red-500" />
                  </Button>
                )}
              </div>
            </div>
 
            {/* Active Alerts Preview */}
            {config.alerts.length > 0 && (
              <div className="mt-3 pt-3 border-t border-gray-200 dark:border-gray-700">
                <p className="text-xs font-medium text-gray-500 dark:text-gray-400 mb-2">
                  Active Alerts ({config.alerts.length})
                </p>
                <div className="flex flex-wrap gap-2">
                  {config.alerts.slice(0, 3).map((alert) => (
                    <span
                      key={alert.id}
                      className={cn(
                        'inline-flex items-center gap-1 rounded-full px-2 py-0.5 text-xs font-medium',
                        alert.severity === 'critical'
                          ? 'bg-red-100 dark:bg-red-900/30 text-red-700 dark:text-red-400'
                          : 'bg-yellow-100 dark:bg-yellow-900/30 text-yellow-700 dark:text-yellow-400'
                      )}
                    >
                      <StatusIndicator status={alert.severity as 'warning' | 'critical'} size="sm" />
                      {alert.value.toFixed(1)}{unit}
                    </span>
                  ))}
                  {config.alerts.length > 3 && (
                    <span className="text-xs text-gray-500 dark:text-gray-400">
                      +{config.alerts.length - 3} more
                    </span>
                  )}
                </div>
              </div>
            )}
          </div>
        );
      })}
    </div>
  );
}
 
export default AlertConfigList;