'use client'; import React, { useState } from 'react'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import { Progress } from '@/components/ui/progress'; import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'; import { Button } from '@/components/ui/button'; import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'; import { AlertTriangle, AlertCircle, Info, TrendingUp, Activity, Clock, ChevronRight, } from 'lucide-react'; import { AnomalyDetection } from '@/lib/api/analytics'; import { formatDistanceToNow } from 'date-fns'; interface AnomalyAlertsPanelProps { data: AnomalyDetection | null; loading?: boolean; error?: Error | null; } export function AnomalyAlertsPanel({ data, loading, error }: AnomalyAlertsPanelProps) { const [expandedAnomaly, setExpandedAnomaly] = useState(null); if (loading) { return (
Analyzing patterns for anomalies...
); } if (error) { return ( Error loading anomaly detection data ); } if (!data) { return null; } const getSeverityColor = (severity: string) => { switch (severity) { case 'high': case 'critical': return 'bg-red-100 text-red-800 border-red-300'; case 'medium': case 'warning': return 'bg-yellow-100 text-yellow-800 border-yellow-300'; case 'low': case 'info': return 'bg-blue-100 text-blue-800 border-blue-300'; default: return 'bg-gray-100 text-gray-800 border-gray-300'; } }; const getSeverityIcon = (severity: string) => { switch (severity) { case 'high': case 'critical': return ; case 'medium': case 'warning': return ; default: return ; } }; const criticalAlerts = data.alerts.filter(a => a.severity === 'critical'); const warningAlerts = data.alerts.filter(a => a.severity === 'warning'); const infoAlerts = data.alerts.filter(a => a.severity === 'info'); return ( Anomaly Detection
Confidence {Math.round(data.confidenceScore * 100)}%
{/* Confidence Score Bar */}

Analysis based on {data.anomalies.length} detected patterns

{/* Alert Summary */} {(criticalAlerts.length > 0 || warningAlerts.length > 0 || infoAlerts.length > 0) && (
{criticalAlerts.length > 0 && (
{criticalAlerts.length} Critical
)} {warningAlerts.length > 0 && (
{warningAlerts.length} Warning
)} {infoAlerts.length > 0 && (
{infoAlerts.length} Info
)}
)} Alerts ({data.alerts.length}) Anomalies ({data.anomalies.length}) {data.alerts.length === 0 ? (
No alerts detected - everything looks normal!
) : ( data.alerts.map((alert, index) => (
{getSeverityIcon(alert.severity)}
{alert.type} {alert.message} {alert.recommendations && alert.recommendations.length > 0 && (

Recommendations:

    {alert.recommendations.map((rec, idx) => (
  • {rec}
  • ))}
)}
)) )}
{data.anomalies.length === 0 ? (
No anomalies detected in recent activities
) : ( data.anomalies.map((anomaly) => (
setExpandedAnomaly( expandedAnomaly === anomaly.activityId ? null : anomaly.activityId )} >
{anomaly.severity}

{anomaly.type} Activity

{formatDistanceToNow(anomaly.timestamp, { addSuffix: true })}

Deviation

{anomaly.deviation.toFixed(1)}σ

{expandedAnomaly === anomaly.activityId && (

{anomaly.description}

Statistical deviation: {anomaly.deviation.toFixed(2)} standard deviations from normal
)}
)) )}
); }