import React, { useState, useEffect } from 'react';
import api from '../services/api';
import { format } from 'date-fns';
import {
PlusIcon,
BellIcon,
CheckCircleIcon,
XCircleIcon,
ExclamationTriangleIcon
} from '@heroicons/react/24/outline';
const Alerts = () => {
const [alertRules, setAlertRules] = useState([]);
const [alertLogs, setAlertLogs] = useState([]);
const [alertStats, setAlertStats] = useState(null);
const [loading, setLoading] = useState(true);
const [activeTab, setActiveTab] = useState('rules');
const [showCreateModal, setShowCreateModal] = useState(false);
useEffect(() => {
fetchAlertData();
}, []);
const fetchAlertData = async () => {
try {
const [rulesRes, logsRes, statsRes] = await Promise.all([
api.get('/alerts/rules'),
api.get('/alerts/logs?limit=50'),
api.get('/alerts/stats?hours=24')
]);
setAlertRules(rulesRes.data.data);
setAlertLogs(logsRes.data.data);
setAlertStats(statsRes.data.data);
} catch (error) {
console.error('Error fetching alert data:', error);
} finally {
setLoading(false);
}
};
const handleDeleteRule = async (ruleId) => {
if (window.confirm('Are you sure you want to delete this alert rule?')) {
try {
await api.delete(`/alerts/rules/${ruleId}`);
fetchAlertData();
} catch (error) {
console.error('Error deleting alert rule:', error);
}
}
};
const getStatusIcon = (status) => {
switch (status) {
case 'sent':
return
Configure and monitor alert rules for drone detections
Get started by creating your first alert rule.
| Name | Priority | Channels | Conditions | Status | Created | Actions |
|---|---|---|---|---|---|---|
|
{rule.name}
{rule.description && (
{rule.description}
)}
|
{rule.priority} |
{rule.alert_channels.map((channel, index) => (
{channel}
))}
|
{rule.min_detections > 1 && (
Min detections: {rule.min_detections}
)}
{rule.time_window && (
Time window: {rule.time_window}s
)}
{rule.cooldown_period && (
Cooldown: {rule.cooldown_period}s
)}
|
{rule.is_active ? 'Active' : 'Inactive'} |
{format(new Date(rule.created_at), 'MMM dd, yyyy')}
|
|
Alert logs will appear here when alerts are triggered.
| Status | Type | Recipient | Rule | Message | Sent At |
|---|---|---|---|---|---|
|
{getStatusIcon(log.status)}
{log.status}
|
{log.alert_type} |
{log.recipient}
|
{log.rule?.name || 'Unknown Rule'}
|
{log.message}
|
{log.sent_at
? format(new Date(log.sent_at), 'MMM dd, HH:mm')
: 'Not sent'
}
|