'use client' import { useState, useEffect, useCallback } from 'react' import { Button } from '@/components/ui/button' import { Badge } from '@/components/ui/badge' import { Bell, Check, X, Clock, AlertCircle, CheckCircle2, Circle, Share2 } from 'lucide-react' import { Popover, PopoverContent, PopoverTrigger, } from '@/components/ui/popover' import { getPendingShareRequests, respondToShareRequest, getNotesWithReminders, toggleReminderDone } from '@/app/actions/notes' import { toast } from 'sonner' import { useNoteRefreshOptional } from '@/context/NoteRefreshContext' import { cn } from '@/lib/utils' import { useLanguage } from '@/lib/i18n' import { formatDistanceToNow } from 'date-fns' interface ShareRequest { id: string status: string permission: string createdAt: Date note: { id: string title: string | null content: string color: string createdAt: Date } sharer: { id: string name: string | null email: string image: string | null } } interface ReminderNote { id: string title: string | null content: string reminder: Date | string | null isReminderDone: boolean } export function NotificationPanel() { const { triggerRefresh } = useNoteRefreshOptional() const { t } = useLanguage() const [requests, setRequests] = useState([]) const [reminders, setReminders] = useState([]) const [isLoading, setIsLoading] = useState(false) const [open, setOpen] = useState(false) const loadData = useCallback(async () => { try { const [shareData, reminderData] = await Promise.all([ getPendingShareRequests(), getNotesWithReminders(), ]) setRequests(shareData as any) setReminders((reminderData as any) || []) } catch (error: any) { console.error('Failed to load notifications:', error) } }, []) useEffect(() => { loadData() const interval = setInterval(loadData, 30000) const onFocus = () => loadData() window.addEventListener('focus', onFocus) return () => { clearInterval(interval) window.removeEventListener('focus', onFocus) } }, [loadData]) const now = new Date() const activeReminders = reminders.filter(r => !r.isReminderDone && r.reminder) const overdueReminders = activeReminders.filter(r => new Date(r.reminder!) < now) const upcomingReminders = activeReminders.filter(r => new Date(r.reminder!) >= now) const pendingCount = requests.length + overdueReminders.length const handleAccept = async (shareId: string) => { try { await respondToShareRequest(shareId, 'accept') setRequests(prev => prev.filter(r => r.id !== shareId)) toast.success(t('notification.accepted'), { description: t('collaboration.nowHasAccess', { name: 'Note' }), duration: 3000, }) triggerRefresh() setOpen(false) } catch (error: any) { console.error('[NOTIFICATION] Error:', error) toast.error(error.message || t('general.error')) } } const handleDecline = async (shareId: string) => { try { await respondToShareRequest(shareId, 'decline') setRequests(prev => prev.filter(r => r.id !== shareId)) toast.info(t('notification.declined')) if (requests.length <= 1) setOpen(false) } catch (error: any) { console.error('[NOTIFICATION] Error:', error) toast.error(error.message || t('general.error')) } } const handleToggleReminder = async (noteId: string, done: boolean) => { try { await toggleReminderDone(noteId, done) setReminders(prev => prev.map(r => r.id === noteId ? { ...r, isReminderDone: done } : r)) triggerRefresh() } catch { toast.error(t('general.error')) } } const hasContent = requests.length > 0 || activeReminders.length > 0 return (
{t('notification.notifications')}
{pendingCount > 0 && ( {pendingCount} )}
{isLoading ? (
) : !hasContent ? (

{t('notification.noNotifications') || 'No new notifications'}

) : (
{/* Overdue reminders */} {overdueReminders.map((note) => (
{t('reminders.overdue')}

{note.title || t('notification.untitled')}

{note.reminder && formatDistanceToNow(new Date(note.reminder), { addSuffix: true })}
))} {/* Upcoming reminders */} {upcomingReminders.slice(0, 5).map((note) => (

{note.title || t('notification.untitled')}

{note.reminder && new Date(note.reminder).toLocaleDateString(undefined, { month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' })}
))} {/* Share requests */} {requests.map((request) => (
{(request.sharer.name || request.sharer.email)[0].toUpperCase()}

{request.sharer.name || request.sharer.email}

{t('notification.shared', { title: request.note.title || t('notification.untitled') })}

))}
)} {/* Footer link to reminders page */} {activeReminders.length > 0 && ( )} ) }