import { ArrowRightIcon, CircleCheckIcon, InfoIcon, OctagonXIcon, TriangleAlertIcon, XIcon, type LucideIcon, } from "lucide-react" import * as React from "react" import { toast, Toaster } from "sonner" import { cn } from "../lib/utils" export type NotifyType = "success" | "info" | "warning" | "error" const TOASTER_ID = "notify" /** Переход по href из карточки; без роутера — обычная навигация браузера. */ const NavigateContext = React.createContext<(href: string) => void>((href) => window.location.assign(href)) const typeMeta: Record = { success: { icon: CircleCheckIcon, tile: "bg-success-soft text-success", bar: "bg-success", duration: 5000 }, info: { icon: InfoIcon, tile: "bg-info-soft text-info", bar: "bg-info", duration: 6000 }, warning: { icon: TriangleAlertIcon, tile: "bg-warning-soft text-warning", bar: "bg-warning", duration: 8000 }, error: { icon: OctagonXIcon, tile: "bg-coral-soft text-coral", bar: "bg-coral", duration: 10000 }, } export interface NotifyOptions { type?: NotifyType title: string description?: React.ReactNode /** Маршрут для кнопки «Перейти». */ href?: string /** Текст кнопки перехода, по умолчанию «Перейти». */ actionLabel?: string /** Мс до автозакрытия; Infinity — висит до закрытия руками. */ duration?: number } /** Всплывающее уведомление справа сверху. Возвращает id для notify.dismiss(id). */ export function notify({ type = "info", duration, ...rest }: NotifyOptions) { return toast.custom((id) => , { toasterId: TOASTER_ID, duration: duration ?? typeMeta[type].duration, }) } notify.success = (o: Omit) => notify({ ...o, type: "success" }) notify.info = (o: Omit) => notify({ ...o, type: "info" }) notify.warning = (o: Omit) => notify({ ...o, type: "warning" }) notify.error = (o: Omit) => notify({ ...o, type: "error" }) notify.dismiss = (id?: string | number) => toast.dismiss(id) /** * Контейнер уведомлений, монтируется один раз. `navigate` — переход для кнопки «Перейти» * (с react-router: `useNavigate()`), по умолчанию `window.location.assign`. */ export function NotifyToaster({ navigate }: { navigate?: (href: string) => void }) { const toaster = ( ) return navigate ? {toaster} : toaster } function NotifyCard({ id, type, title, description, href, actionLabel = "Перейти", }: Omit & { id: string | number; type: NotifyType }) { const navigate = React.useContext(NavigateContext) const { icon: Icon, tile, bar } = typeMeta[type] return (
{title}
{description &&
{description}
} {href && ( )}
) }