import { cva } from "class-variance-authority"; import React from "react"; import { toast, Toaster } from "sonner"; import { CheckCircleIcon, InformationCircleIcon, XCircleIcon, } from "@sparkle/icons/app"; import { assertNever } from "@sparkle/lib/internal_utils"; import { cn } from "@sparkle/lib/utils"; import { Icon } from "./Icon"; const NOTIFICATION_DELAY = 3000; export type NotificationType = { title?: string; description?: string; type: "success" | "error" | "info"; }; const NotificationsContext = React.createContext<(n: NotificationType) => void>( (n) => n ); const notificationVariants = cva("s-pt-0.5", { variants: { type: { success: "s-text-success-600 dark:s-text-success-400-night", error: "s-text-warning-500 dark:s-text-warning-500-night", info: "s-text-info-600 dark:s-text-info-400-night", }, }, }); function NotificationContent({ type, title, description, onDismiss, }: NotificationType & { onDismiss?: () => void }) { const icon = (() => { switch (type) { case "success": return CheckCircleIcon; case "error": return XCircleIcon; case "info": return InformationCircleIcon; default: assertNever(type); } })(); return (
); } export const Notification = { Area: ({ children }: { children: React.ReactNode }) => { const sendNotification = React.useCallback( (notification: NotificationType) => { toast.custom( (t) => ( toast.dismiss(t)} /> ), { duration: NOTIFICATION_DELAY, } ); }, [] ); return ( {children} ); }, }; export const useSendNotification = () => React.useContext(NotificationsContext);