import { useState } from "react" import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query" import { notificationList, notificationMarkRead, notificationMarkAllRead, } from "@/client/api" import type { NotificationResponse } from "@/client/model" import { Bell, CheckCheck, Mail, MailOpen, Loader2 } from "lucide-react" import { Badge } from "@/components/ui/badge" import { Button } from "@/components/ui/button" import { Card, CardContent } from "@/components/ui/card" import { Empty, EmptyDescription, EmptyHeader, EmptyMedia, EmptyTitle, } from "@/components/ui/empty" const NOTIFICATION_TYPE_LABELS: Record = { mention: "Mention", invitation: "Invitation", role_change: "Role Change", room_created: "Room Created", room_deleted: "Room Deleted", system_announcement: "Announcement", project_invitation: "Project Invitation", } function NotificationItem({ notification, }: { notification: NotificationResponse }) { const queryClient = useQueryClient() const markReadMutation = useMutation({ mutationFn: () => notificationMarkRead(notification.id), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["notificationList"] }) }, }) return ( ) } export function NotificationList() { const queryClient = useQueryClient() const [onlyUnread, setOnlyUnread] = useState(false) const { data, isLoading, isError } = useQuery({ queryKey: ["notificationList", onlyUnread], queryFn: () => notificationList({ only_unread: onlyUnread || undefined, limit: 50 }), }) const markAllReadMutation = useMutation({ mutationFn: () => notificationMarkAllRead(), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["notificationList"] }) }, }) if (isLoading) { return (
) } if (isError) { return ( 通知加载失败 请刷新页面或检查网络连接。 ) } const notifications = data?.data?.data?.notifications ?? [] const unreadCount = data?.data?.data?.unread_count ?? 0 return (
{unreadCount > 0 ? `${unreadCount} unread` : "All read"}
{unreadCount > 0 && ( )}
{notifications.length === 0 ? ( 暂无通知 {onlyUnread ? "没有未读通知" : "你已经查看完了"} ) : (
{notifications.map((n) => ( ))}
)}
) }