import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { AlertTriangle, AlertCircle, Link2, Clock, Sparkles } from 'lucide-react'; import { formatDistanceToNow } from 'date-fns'; import type { CorrelatedAlert } from '@/hooks/admin/useCorrelatedAlerts'; import { useCreateIncident } from '@/hooks/admin/useIncidents'; interface CorrelatedAlertsPanelProps { correlations?: CorrelatedAlert[]; isLoading: boolean; } const SEVERITY_CONFIG = { critical: { color: 'text-destructive', icon: AlertCircle, badge: 'bg-destructive/10 text-destructive' }, high: { color: 'text-orange-500', icon: AlertTriangle, badge: 'bg-orange-500/10 text-orange-500' }, medium: { color: 'text-yellow-500', icon: AlertTriangle, badge: 'bg-yellow-500/10 text-yellow-500' }, low: { color: 'text-blue-500', icon: AlertTriangle, badge: 'bg-blue-500/10 text-blue-500' }, }; export function CorrelatedAlertsPanel({ correlations, isLoading }: CorrelatedAlertsPanelProps) { const createIncident = useCreateIncident(); const handleCreateIncident = (correlation: CorrelatedAlert) => { createIncident.mutate({ ruleId: correlation.rule_id, title: correlation.incident_title_template, description: correlation.rule_description, severity: correlation.incident_severity, alertIds: correlation.alert_ids, alertSources: correlation.alert_sources as ('system' | 'rate_limit')[], }); }; if (isLoading) { return ( Correlated Alerts Loading correlation patterns...
); } if (!correlations || correlations.length === 0) { return ( Correlated Alerts No correlated alert patterns detected

Alert correlation engine is active

Incidents will be auto-detected when patterns match

); } return ( Correlated Alerts {correlations.length} {correlations.length === 1 ? 'pattern' : 'patterns'} detected Multiple related alerts indicating potential incidents {correlations.map((correlation) => { const config = SEVERITY_CONFIG[correlation.incident_severity]; const Icon = config.icon; return (
{config.badge.split(' ')[1].split('-')[0].toUpperCase()} Correlated {correlation.matching_alerts_count} alerts

{correlation.rule_name}

{correlation.rule_description}

Window: {correlation.time_window_minutes}m First: {formatDistanceToNow(new Date(correlation.first_alert_at), { addSuffix: true })} Last: {formatDistanceToNow(new Date(correlation.last_alert_at), { addSuffix: true })}
{correlation.can_create_incident ? ( ) : ( Incident exists )}
{correlation.alert_messages.length > 0 && (

Sample alerts:

{correlation.alert_messages.slice(0, 3).map((message, idx) => (
{message}
))}
)}
); })}
); }