'use client' import React from 'react' import { useRouter } from 'next/navigation' import useNavigationStore from '@/app/store/navigationStore' import AreaChart from '../dashboard/AreaChart' interface AlertType { id: number detector_id: number detector_name: string type: string status: string message: string timestamp: string location: string object: string acknowledged: boolean priority: string } interface AlertMenuProps { alert: AlertType isOpen: boolean onClose: () => void getStatusText: (status: string) => string compact?: boolean anchor?: { left: number; top: number } | null } const AlertMenu: React.FC = ({ alert, isOpen, onClose, getStatusText, compact = false, anchor = null }) => { const router = useRouter() const { setSelectedDetector, currentObject } = useNavigationStore() if (!isOpen) return null const formatDate = (dateString: string) => { const date = new Date(dateString) return date.toLocaleString('ru-RU', { day: '2-digit', month: '2-digit', year: 'numeric', hour: '2-digit', minute: '2-digit' }) } const getPriorityText = (priority: string) => { if (typeof priority !== 'string') return 'Неизвестно'; switch (priority.toLowerCase()) { case 'high': return 'Высокий' case 'medium': return 'Средний' case 'low': return 'Низкий' default: return priority } } const getStatusColorCircle = (status: string) => { // Use hex colors from Alerts submenu system if (status === '#b3261e') return 'bg-red-500' if (status === '#fd7c22') return 'bg-orange-500' if (status === '#00ff00') return 'bg-green-500' // Fallback for text-based status switch (status?.toLowerCase()) { case 'critical': return 'bg-red-500' case 'warning': return 'bg-orange-500' case 'normal': return 'bg-green-500' default: return 'bg-gray-500' } } const getTimeAgo = (timestamp: string) => { const now = new Date() const alertTime = new Date(timestamp) const diffMs = now.getTime() - alertTime.getTime() const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24)) const diffMonths = Math.floor(diffDays / 30) if (diffMonths > 0) { return `${diffMonths} ${diffMonths === 1 ? 'месяц' : diffMonths < 5 ? 'месяца' : 'месяцев'}` } else if (diffDays > 0) { return `${diffDays} ${diffDays === 1 ? 'день' : diffDays < 5 ? 'дня' : 'дней'}` } else { const diffHours = Math.floor(diffMs / (1000 * 60 * 60)) if (diffHours > 0) { return `${diffHours} ${diffHours === 1 ? 'час' : diffHours < 5 ? 'часа' : 'часов'}` } else { const diffMinutes = Math.floor(diffMs / (1000 * 60)) return `${diffMinutes} ${diffMinutes === 1 ? 'минута' : diffMinutes < 5 ? 'минуты' : 'минут'}` } } } const handleReportsClick = () => { const currentUrl = new URL(window.location.href) const objectId = currentUrl.searchParams.get('objectId') || currentObject.id const objectTitle = currentUrl.searchParams.get('objectTitle') || currentObject.title const detectorData = { detector_id: alert.detector_id, name: alert.detector_name, serial_number: '', object: alert.object || '', status: '', type: '', detector_type: '', location: alert.location || '', floor: 0, checked: false, notifications: [] } setSelectedDetector(detectorData) let reportsUrl = '/reports' const params = new URLSearchParams() if (objectId) params.set('objectId', objectId) if (objectTitle) params.set('objectTitle', objectTitle) if (params.toString()) { reportsUrl += `?${params.toString()}` } router.push(reportsUrl) } const handleHistoryClick = () => { const currentUrl = new URL(window.location.href) const objectId = currentUrl.searchParams.get('objectId') || currentObject.id const objectTitle = currentUrl.searchParams.get('objectTitle') || currentObject.title const detectorData = { detector_id: alert.detector_id, name: alert.detector_name, serial_number: '', object: alert.object || '', status: '', type: '', detector_type: '', location: alert.location || '', floor: 0, checked: false, notifications: [] } setSelectedDetector(detectorData) let alertsUrl = '/alerts' const params = new URLSearchParams() if (objectId) params.set('objectId', objectId) if (objectTitle) params.set('objectTitle', objectTitle) if (params.toString()) { alertsUrl += `?${params.toString()}` } router.push(alertsUrl) } // Данные для графика (мок данные) const chartData: { timestamp: string; value: number }[] = [ { timestamp: new Date(Date.now() - 6 * 24 * 60 * 60 * 1000).toISOString(), value: 75 }, { timestamp: new Date(Date.now() - 5 * 24 * 60 * 60 * 1000).toISOString(), value: 82 }, { timestamp: new Date(Date.now() - 4 * 24 * 60 * 60 * 1000).toISOString(), value: 78 }, { timestamp: new Date(Date.now() - 3 * 24 * 60 * 60 * 1000).toISOString(), value: 85 }, { timestamp: new Date(Date.now() - 2 * 24 * 60 * 60 * 1000).toISOString(), value: 90 }, { timestamp: new Date(Date.now() - 1 * 24 * 60 * 60 * 1000).toISOString(), value: 88 }, { timestamp: new Date().toISOString(), value: 92 } ] if (compact && anchor) { return (
{alert.detector_name}
{/* Тело: 3 строки / 2 колонки */}
{/* Строка 1: Статус */}
Статус
{getStatusText(alert.status)}
{/* Строка 2: Причина тревоги */}
Причина тревоги
{alert.message}
{/* Строка 3: Временная метка */}
Временная метка
{getTimeAgo(alert.timestamp)}
{/* Добавлен раздел дата/время и график для компактного режима */}
{formatDate(alert.timestamp)}
) } return (

{alert.detector_name}

{/* Тело: Две колонки - Три строки */}
{/* Колонка 1 - Строка 1: Статус */}
Статус
{getStatusText(alert.status)}
{/* Колонка 1 - Строка 2: Причина тревоги */}
Причина тревоги
{alert.message}
Причина тревоги
{alert.message}
{/* Колонка 1 - Строка 3: Временная метка */}
Временная метка
{getTimeAgo(alert.timestamp)}
{/* Колонка 2 - Строка 1: Приоритет */}
Приоритет
{getPriorityText(alert.priority)}
{/* Колонка 2 - Строка 2: Локация */}
Локация
{alert.location}
{/* Колонка 2 - Строка 3: Объект */}
Объект
{alert.object}
{/* Колонка 2 - Строка 4: Отчет */}
Отчет
Доступен
{/* Колонка 2 - Строка 5: История */}
История
Просмотр
{/* Низ: Две строки - первая содержит дату/время, вторая строка ниже - наш график */}
{/* Строка 1: Дата/время */}
Дата/время
{formatDate(alert.timestamp)}
{/* Charts */}
) } export default AlertMenu