'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 } from 'lucide-react' import { Popover, PopoverContent, PopoverTrigger, } from '@/components/ui/popover' import { getPendingShareRequests, respondToShareRequest } from '@/app/actions/notes' import { toast } from 'sonner' import { useNoteRefresh } from '@/context/NoteRefreshContext' import { cn } from '@/lib/utils' import { useLanguage } from '@/lib/i18n' 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 } } export function NotificationPanel() { const { triggerRefresh } = useNoteRefresh() const { t } = useLanguage() const [requests, setRequests] = useState([]) const [isLoading, setIsLoading] = useState(false) const [open, setOpen] = useState(false) const loadRequests = useCallback(async () => { try { const data = await getPendingShareRequests() setRequests(data as any) } catch (error: any) { console.error('Failed to load share requests:', error) } }, []) useEffect(() => { loadRequests() const interval = setInterval(loadRequests, 10000) const onFocus = () => loadRequests() window.addEventListener('focus', onFocus) return () => { clearInterval(interval) window.removeEventListener('focus', onFocus) } }, [loadRequests]) const pendingCount = requests.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')) } } return (
{t('notification.notifications')}
{pendingCount > 0 && ( {pendingCount} )}
{isLoading ? (
) : requests.length === 0 ? (

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

) : (
{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') })}

{new Date(request.createdAt).toLocaleDateString()}
))}
)} ) }