'use client'; import '@/app/styles/notification-bell.scss'; import { useState, useEffect } from 'react'; import { fetchApi } from '@/lib/utils/client'; import { useNotification, NotificationItem } from '@/hooks/useNotification'; import { NotificationListResponse } from '@/types/response/notification/list'; import { Sheet, SheetContent, SheetTitle, SheetClose } from '@/components/ui/sheet'; import { BellIcon } from 'lucide-react'; export default function NotificationBell() { const { unreadNotifCount, unreadNoteCount, markNotifRead } = useNotification(); const [open, setOpen] = useState(false); const [notifications, setNotifications] = useState([]); const [loading, setLoading] = useState(false); const totalUnread = unreadNotifCount + unreadNoteCount; // Sheet 열릴 때 첫 로드 useEffect(() => { if (!open || notifications.length > 0) { return; } setLoading(true); fetchApi('/api/notification/list?page=1&perPage=10', { silent: true }).then((res) => { if (res.data?.list) { setNotifications(res.data.list); } }).catch(() => {}).finally(() => { setLoading(false); }); }, [open, notifications.length]); // 읽음 처리 const handleRead = async (item: NotificationItem) => { if (!item.isRead) { await markNotifRead(item.id); setNotifications(prev => prev.map(n => n.id === item.id ? { ...n, isRead: true } : n)); } if (item.actionUrl) { window.location.href = item.actionUrl; } }; // 전체 읽음 const handleReadAll = async () => { await markNotifRead(); setNotifications(prev => prev.map(n => ({ ...n, isRead: true }))); }; const formatTime = (dateStr: string) => { const d = new Date(dateStr); const now = new Date(); const diff = Math.floor((now.getTime() - d.getTime()) / 60000); if (diff < 1) { return '방금'; } if (diff < 60) { return `${diff}분 전`; } if (diff < 1440) { return `${Math.floor(diff / 60)}시간 전`; } return `${Math.floor(diff / 1440)}일 전`; }; return (
{/* 벨 버튼 */} {/* Drawer */}

알림

{unreadNotifCount > 0 && ( )}
{loading &&
준비 중...
} {!loading && notifications.length === 0 &&
알림이 없습니다
} {notifications.map(n => (
handleRead(n)} className={`notification-bell__item ${n.isRead ? '' : 'notification-bell__item--unread'}`}> {n.imageUrl && {n.title}}
{n.title}
{n.message}
{formatTime(n.createdAt)}
{!n.isRead &&
}
))}
); }