import { AlertTriangle, CheckCircle2, Clock, ShieldAlert, XCircle } from 'lucide-react'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; import { formatDistanceToNow } from 'date-fns'; import { useMutation, useQueryClient } from '@tanstack/react-query'; import { supabase } from '@/integrations/supabase/client'; import { toast } from 'sonner'; import { Link } from 'react-router-dom'; import type { CombinedAlert } from '@/hooks/admin/useCombinedAlerts'; interface CriticalAlertsPanelProps { alerts?: CombinedAlert[]; isLoading: boolean; } const SEVERITY_CONFIG = { critical: { color: 'destructive' as const, icon: XCircle, label: 'Critical' }, high: { color: 'destructive' as const, icon: AlertTriangle, label: 'High' }, medium: { color: 'secondary' as const, icon: Clock, label: 'Medium' }, low: { color: 'secondary' as const, icon: Clock, label: 'Low' }, }; export function CriticalAlertsPanel({ alerts, isLoading }: CriticalAlertsPanelProps) { const queryClient = useQueryClient(); const resolveSystemAlert = useMutation({ mutationFn: async (alertId: string) => { const { error } = await supabase .from('system_alerts') .update({ resolved_at: new Date().toISOString() }) .eq('id', alertId); if (error) throw error; }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['system-alerts'] }); queryClient.invalidateQueries({ queryKey: ['monitoring'] }); toast.success('Alert resolved'); }, onError: () => { toast.error('Failed to resolve alert'); }, }); const resolveRateLimitAlert = useMutation({ mutationFn: async (alertId: string) => { const { error } = await supabase .from('rate_limit_alerts') .update({ resolved_at: new Date().toISOString() }) .eq('id', alertId); if (error) throw error; }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['rate-limit-alerts'] }); queryClient.invalidateQueries({ queryKey: ['monitoring'] }); toast.success('Alert resolved'); }, onError: () => { toast.error('Failed to resolve alert'); }, }); const handleResolve = (alert: CombinedAlert) => { if (alert.source === 'system') { resolveSystemAlert.mutate(alert.id); } else { resolveRateLimitAlert.mutate(alert.id); } }; if (isLoading) { return ( Critical Alerts Loading alerts... ); } if (!alerts || alerts.length === 0) { return ( Critical Alerts All Systems Operational No active alerts detected ); } return ( Critical Alerts {alerts.length} View All {alerts.map((alert) => { const config = SEVERITY_CONFIG[alert.severity]; const SeverityIcon = config.icon; return ( {config.label} {alert.source === 'system' ? 'System' : 'Rate Limit'} {alert.alert_type && ( {alert.alert_type.replace(/_/g, ' ')} )} {alert.message} {formatDistanceToNow(new Date(alert.created_at), { addSuffix: true })} handleResolve(alert)} loading={resolveSystemAlert.isPending || resolveRateLimitAlert.isPending} className="flex-shrink-0" > Resolve ); })} ); }
{alert.message}
{formatDistanceToNow(new Date(alert.created_at), { addSuffix: true })}