forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToast.tsx
More file actions
122 lines (104 loc) · 3.32 KB
/
Copy pathToast.tsx
File metadata and controls
122 lines (104 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
'use client';
import { useState, useEffect, useCallback, createContext, useContext, ReactNode } from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import { X, CheckCircle, AlertCircle, Info } from 'lucide-react';
import { cn } from '@/lib/utils';
export type ToastType = 'success' | 'error' | 'info';
interface Toast {
id: string;
message: string;
type: ToastType;
duration?: number;
}
interface ToastContextType {
showToast: (message: string, type?: ToastType, duration?: number) => void;
}
const ToastContext = createContext<ToastContextType | null>(null);
export function useToast() {
const context = useContext(ToastContext);
if (!context) {
throw new Error('useToast must be used within a ToastProvider');
}
return context;
}
interface ToastProviderProps {
children: ReactNode;
}
export function ToastProvider({ children }: ToastProviderProps) {
const [toasts, setToasts] = useState<Toast[]>([]);
const showToast = useCallback((message: string, type: ToastType = 'info', duration = 4000) => {
const id = `toast-${Date.now()}-${Math.random()}`;
setToasts((prev) => [...prev, { id, message, type, duration }]);
}, []);
const removeToast = useCallback((id: string) => {
setToasts((prev) => prev.filter((t) => t.id !== id));
}, []);
return (
<ToastContext.Provider value={{ showToast }}>
{children}
<ToastContainer toasts={toasts} onRemove={removeToast} />
</ToastContext.Provider>
);
}
interface ToastContainerProps {
toasts: Toast[];
onRemove: (id: string) => void;
}
function ToastContainer({ toasts, onRemove }: ToastContainerProps) {
return (
<div className="fixed bottom-4 right-4 z-[200] flex flex-col gap-2">
<AnimatePresence>
{toasts.map((toast) => (
<ToastItem key={toast.id} toast={toast} onRemove={onRemove} />
))}
</AnimatePresence>
</div>
);
}
interface ToastItemProps {
toast: Toast;
onRemove: (id: string) => void;
}
function ToastItem({ toast, onRemove }: ToastItemProps) {
useEffect(() => {
if (toast.duration) {
const timer = setTimeout(() => {
onRemove(toast.id);
}, toast.duration);
return () => clearTimeout(timer);
}
}, [toast.id, toast.duration, onRemove]);
const Icon = {
success: CheckCircle,
error: AlertCircle,
info: Info,
}[toast.type];
const colors = {
success: 'bg-green-500/10 border-green-500/30 text-green-400',
error: 'bg-error/10 border-error/30 text-error',
info: 'bg-purple-primary/10 border-purple-primary/30 text-purple-primary',
}[toast.type];
return (
<motion.div
initial={{ opacity: 0, y: 20, scale: 0.95 }}
animate={{ opacity: 1, y: 0, scale: 1 }}
exit={{ opacity: 0, y: -20, scale: 0.95 }}
transition={{ duration: 0.2 }}
className={cn(
'flex items-center gap-3 px-4 py-3 rounded-xl',
'border backdrop-blur-lg shadow-lg',
'min-w-[280px] max-w-[400px]',
colors
)}
>
<Icon className="w-5 h-5 flex-shrink-0" />
<span className="flex-1 text-sm font-medium text-text-primary">{toast.message}</span>
<button
onClick={() => onRemove(toast.id)}
className="p-1 rounded-lg hover:bg-white/10 transition-colors"
>
<X className="w-4 h-4 text-text-tertiary" />
</button>
</motion.div>
);
}