"use client" import { useEffect, useState } from "react" import { AnimatePresence, motion } from "framer-motion" import { X } from "lucide-react" import { eventBus, EVENT_TYPES, type Notification } from "@/lib/event-bus" import { cn } from "@/lib/utils" export function NotificationProvider() { const [notifications, setNotifications] = useState<(Notification & { id: number })[]>([]) let lastId = 0 useEffect(() => { const handleNotification = (notification: Notification) => { const id = ++lastId setNotifications((prev) => [...prev, { ...notification, id }]) // 自动移除通知 if (notification.duration !== 0) { const duration = notification.duration || 5000 setTimeout(() => { removeNotification(id) }, duration) } } // 订阅通知事件 eventBus.on(EVENT_TYPES.NOTIFICATION, handleNotification) // 订阅API错误事件 eventBus.on(EVENT_TYPES.API_ERROR, (error) => { handleNotification({ title: "请求失败", message: error.message || "服务器返回错误", type: "error", duration: 5000, }) }) return () => { eventBus.off(EVENT_TYPES.NOTIFICATION) eventBus.off(EVENT_TYPES.API_ERROR) } }, []) const removeNotification = (id: number) => { setNotifications((prev) => prev.filter((notification) => notification.id !== id)) } return (
{notifications.map((notification) => (

{notification.title}

{notification.message}

))}
) }