gohorsejobs/frontend/src/hooks/use-toast.ts
Tiago Yamamoto 1c7ef95c1a first commit
2025-12-09 19:04:48 -03:00

45 lines
954 B
TypeScript

import { useState, useCallback } from "react"
export interface Toast {
id: string
title?: string
description?: string
action?: React.ReactNode
variant?: "default" | "destructive"
}
export function useToast() {
const [toasts, setToasts] = useState<Toast[]>([])
const toast = useCallback(
({
title,
description,
action,
variant = "default",
}: Omit<Toast, "id">) => {
const id = Math.random().toString(36).substr(2, 9)
const newToast: Toast = { id, title, description, action, variant }
setToasts((prev) => [...prev, newToast])
// Auto-dismiss after 3 seconds
setTimeout(() => {
setToasts((prev) => prev.filter((t) => t.id !== id))
}, 3000)
return id
},
[]
)
const dismiss = useCallback((toastId: string) => {
setToasts((prev) => prev.filter((t) => t.id !== toastId))
}, [])
return {
toast,
toasts,
dismiss,
}
}