"use client"; import { useState, useEffect } from "react"; import { useRouter } from "next/navigation"; import Link from "next/link"; import { Heart, MessageCircle, CheckCheck, Bell, ChevronRight } from "lucide-react"; import { apiFetchNotifications, apiMarkRead, apiMarkAllRead, type NotificationItem, } from "@/lib/api"; export default function NotificationsPage() { const router = useRouter(); const [notifications, setNotifications] = useState([]); const [loading, setLoading] = useState(true); const load = async () => { setLoading(true); try { const res = await apiFetchNotifications(1); setNotifications(res.notifications || []); } catch { setNotifications([]); } setLoading(false); }; useEffect(() => { load(); }, []); const handleClick = async (n: NotificationItem) => { if (!n.is_read) { apiMarkRead(n.id).catch(() => {}); setNotifications((list) => list.map((item) => (item.id === n.id ? { ...item, is_read: true } : item)) ); } router.push(`/post/${n.post_id}`); }; const handleMarkAll = async () => { await apiMarkAllRead(); setNotifications((list) => list.map((item) => ({ ...item, is_read: true }))); }; const formatTime = (dateStr: string) => { const diff = Date.now() - new Date(dateStr).getTime(); const min = Math.floor(diff / 60000); if (min < 1) return "刚刚"; if (min < 60) return `${min} 分钟前`; const hour = Math.floor(min / 60); if (hour < 24) return `${hour} 小时前`; return `${Math.floor(hour / 24)} 天前`; }; const hasUnread = notifications.some((n) => !n.is_read); return (

通知中心

{hasUnread && ( )}
{loading ? (

正在加载通知…

) : notifications.length === 0 ? (

还没有通知

当有人评论或点赞你的帖子时,会在这里提醒你

) : (
{notifications.map((n, i) => ( ))}
)}
); }