Download src/components/Toast.tsx from AnesKAM/gen: direct link, hf CLI and curl.
- Browser
- Download file 1.97 kB
-
https://huggingface.co/spaces/AnesKAM/gen/resolve/main/src/components/Toast.tsx
- Command line
-
hf download hf://spaces/AnesKAM/gen/src/components/Toast.tsx
-
curl -L -o Toast.tsx https://huggingface.co/spaces/AnesKAM/gen/resolve/main/src/components/Toast.tsx
1.97 kB
| "use client"; | |
| import { | |
| createContext, | |
| useCallback, | |
| useContext, | |
| useRef, | |
| useState, | |
| type ReactNode, | |
| } from "react"; | |
| import Icon from "@/components/Icon"; | |
| import { cn } from "@/lib/utils"; | |
| type ToastType = "success" | "error" | "info"; | |
| interface Toast { | |
| id: number; | |
| message: string; | |
| type: ToastType; | |
| } | |
| const ToastContext = createContext<{ toast: (message: string, type?: ToastType) => void }>({ | |
| toast: () => undefined, | |
| }); | |
| export function useToast() { | |
| return useContext(ToastContext); | |
| } | |
| export function ToastProvider({ children }: { children: ReactNode }) { | |
| const [toasts, setToasts] = useState<Toast[]>([]); | |
| const nextId = useRef(1); | |
| const toast = useCallback((message: string, type: ToastType = "success") => { | |
| const id = nextId.current++; | |
| setToasts((prev) => [...prev.slice(-3), { id, message, type }]); | |
| setTimeout(() => { | |
| setToasts((prev) => prev.filter((t) => t.id !== id)); | |
| }, 3400); | |
| }, []); | |
| return ( | |
| <ToastContext.Provider value={{ toast }}> | |
| {children} | |
| <div className="pointer-events-none fixed bottom-5 right-5 z-[100] flex w-[calc(100vw-2.5rem)] max-w-sm flex-col gap-2"> | |
| {toasts.map((t) => ( | |
| <div | |
| key={t.id} | |
| className={cn( | |
| "pointer-events-auto flex animate-toast-in items-center gap-2.5 rounded-xl border px-4 py-3 text-sm font-medium shadow-lg", | |
| t.type === "error" | |
| ? "border-red-200 bg-red-50 text-red-800" | |
| : t.type === "info" | |
| ? "border-slate-200 bg-slate-900 text-white" | |
| : "border-emerald-200 bg-emerald-50 text-emerald-800", | |
| )} | |
| > | |
| <Icon | |
| name={t.type === "error" ? "alert" : t.type === "info" ? "sparkles" : "check"} | |
| className="h-4 w-4" | |
| /> | |
| <span className="flex-1">{t.message}</span> | |
| </div> | |
| ))} | |
| </div> | |
| </ToastContext.Provider> | |
| ); | |
| } | |