'use client' import { useState, useEffect } from 'react' import { Bell } from 'lucide-react' import { motion, AnimatePresence } from 'framer-motion' import { useRouter } from 'next/navigation' import { cn } from '@/lib/utils' interface Notification { id: string type: string title: string message: string is_read: boolean created_at: string lead_id: string } interface NotificationBellProps { align?: 'right' | 'left' | 'right-side' } export default function NotificationBell({ align = 'right' }: NotificationBellProps) { const router = useRouter() const [unreadCount, setUnreadCount] = useState(0) const [notifications, setNotifications] = useState([]) const [isOpen, setIsOpen] = useState(false) const [loading, setLoading] = useState(false) const containerRef = useState(null) // Fetch unread count useEffect(() => { fetchUnreadCount() const interval = setInterval(fetchUnreadCount, 30000) // Every 30s return () => clearInterval(interval) }, []) const fetchUnreadCount = async () => { try { const token = localStorage.getItem('auth_token') const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/v1/notifications/unread-count`, { headers: { 'Authorization': `Bearer ${token}` } }) const data = await response.json() setUnreadCount(data.unread_count) } catch (error) { console.error('Failed to fetch unread count:', error) } } const fetchNotifications = async () => { try { setLoading(true) const token = localStorage.getItem('auth_token') const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/v1/notifications`, { headers: { 'Authorization': `Bearer ${token}` } }) const data = await response.json() setNotifications(data) } catch (error) { console.error('Failed to fetch notifications:', error) } finally { setLoading(false) } } const handleClick = async () => { if (!isOpen) { await fetchNotifications() } setIsOpen(!isOpen) } const handleNotificationClick = async (notification: Notification) => { // Mark as read const token = localStorage.getItem('auth_token') await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/v1/notifications/${notification.id}/mark-read`, { method: 'POST', headers: { 'Authorization': `Bearer ${token}` } }) // Navigate to chat router.push(`/chats?lead=${notification.lead_id}`) setIsOpen(false) fetchUnreadCount() } const getIcon = (type: string) => { switch (type) { case 'order': return '🛍️'; case 'booking': return '📅'; case 'lead_heat': return '🔥'; case 'message': return '💬'; default: return '🔔'; } } const formatTime = (dateStr: string) => { const date = new Date(dateStr) const now = new Date() const diffMs = now.getTime() - date.getTime() const diffMins = Math.round(diffMs / 60000) const diffHrs = Math.round(diffMs / 3600000) if (diffMins < 1) return 'Только что' if (diffMins < 60) return `${diffMins} мин. назад` if (diffHrs < 24) return `${diffHrs} ч. назад` return date.toLocaleDateString('ru-RU', { day: 'numeric', month: 'short' }) } const markAllRead = async () => { const token = localStorage.getItem('auth_token') await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/v1/notifications/mark-all-read`, { method: 'POST', headers: { 'Authorization': `Bearer ${token}` } }) fetchUnreadCount() fetchNotifications() } return (
{/* Bell Icon */} {/* Dropdown */} {isOpen && ( {/* Header */}

Уведомления

{unreadCount > 0 && ( )}
{/* Notifications List */}
{loading && notifications.length === 0 ? (
Загрузка...
) : notifications.length === 0 ? (

Нет новых уведомлений

) : (
{notifications.map((notification) => ( ))}
)}
)}
) }