"use client" import { useState, createContext, useContext, type ReactNode } from "react" import { Card, CardContent } from "@/app/components/ui/card" import { Button } from "@/app/components/ui/button" import { X, CheckCircle, AlertCircle, Info, AlertTriangle } from "lucide-react" import { cn } from "@/app/lib/utils" export type NotificationType = "success" | "error" | "warning" | "info" export interface Notification { id: string type: NotificationType title: string message?: string duration?: number persistent?: boolean actions?: Array<{ label: string onClick: () => void variant?: "default" | "outline" }> } interface NotificationContextType { notifications: Notification[] addNotification: (notification: Omit) => void removeNotification: (id: string) => void clearAll: () => void } const NotificationContext = createContext(undefined) export function useNotifications() { const context = useContext(NotificationContext) if (!context) { throw new Error("useNotifications must be used within a NotificationProvider") } return context } export function NotificationProvider({ children }: { children: ReactNode }) { const [notifications, setNotifications] = useState([]) const addNotification = (notification: Omit) => { const id = Math.random().toString(36).substr(2, 9) const newNotification: Notification = { ...notification, id, duration: notification.duration ?? 5000, } setNotifications((prev) => [newNotification, ...prev]) // 自动移除非持久化通知 if (!notification.persistent && newNotification.duration > 0) { setTimeout(() => { removeNotification(id) }, newNotification.duration) } } const removeNotification = (id: string) => { setNotifications((prev) => prev.filter((notification) => notification.id !== id)) } const clearAll = () => { setNotifications([]) } return ( {children} ) } function NotificationContainer() { const { notifications, removeNotification } = useNotifications() if (notifications.length === 0) return null return (
{notifications.map((notification) => ( ))}
) } function NotificationItem({ notification, onRemove, }: { notification: Notification onRemove: (id: string) => void }) { const getIcon = () => { switch (notification.type) { case "success": return case "error": return case "warning": return case "info": return } } const getBorderColor = () => { switch (notification.type) { case "success": return "border-l-green-500" case "error": return "border-l-red-500" case "warning": return "border-l-yellow-500" case "info": return "border-l-blue-500" } } return (
{getIcon()}

{notification.title}

{notification.message &&

{notification.message}

} {notification.actions && notification.actions.length > 0 && (
{notification.actions.map((action, index) => ( ))}
)}
) } // 便捷的通知钩子 export function useNotify() { const { addNotification } = useNotifications() return { success: (title: string, message?: string, options?: Partial) => addNotification({ type: "success", title, message, ...options }), error: (title: string, message?: string, options?: Partial) => addNotification({ type: "error", title, message, ...options }), warning: (title: string, message?: string, options?: Partial) => addNotification({ type: "warning", title, message, ...options }), info: (title: string, message?: string, options?: Partial) => addNotification({ type: "info", title, message, ...options }), } }