forked from Cylo-Traders/Agrocylo-PIP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToastContext.tsx
More file actions
125 lines (108 loc) · 2.95 KB
/
Copy pathToastContext.tsx
File metadata and controls
125 lines (108 loc) · 2.95 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
123
124
125
import {
createContext,
useCallback,
useContext,
useMemo,
useRef,
useState,
type ReactNode,
} from 'react';
import {
ToastViewport,
type ToastItem,
type ToastTone,
} from '../components/ui/Toast/Toast';
export interface ToastOptions {
title?: string;
description?: string;
tone?: ToastTone;
/** Auto-dismiss duration in ms. Use `0` to keep until dismissed. */
durationMs?: number;
}
export interface ToastApi {
push: (options: ToastOptions) => string;
success: (title: string, description?: string) => string;
error: (title: string, description?: string) => string;
dismiss: (id: string) => void;
clear: () => void;
}
const ToastContext = createContext<ToastApi | null>(null);
const DEFAULT_DURATION_MS = 5000;
let toastSeq = 0;
function nextId(): string {
toastSeq += 1;
return `toast-${toastSeq}-${Date.now()}`;
}
export function ToastProvider({ children }: { children: ReactNode }) {
const [toasts, setToasts] = useState<ToastItem[]>([]);
const timersRef = useRef<Map<string, ReturnType<typeof setTimeout>>>(
new Map(),
);
const dismiss = useCallback((id: string) => {
const timer = timersRef.current.get(id);
if (timer) {
clearTimeout(timer);
timersRef.current.delete(id);
}
setToasts((prev) => prev.filter((toast) => toast.id !== id));
}, []);
const push = useCallback(
(options: ToastOptions) => {
const id = nextId();
const tone = options.tone ?? 'info';
const durationMs =
options.durationMs === undefined
? DEFAULT_DURATION_MS
: options.durationMs;
setToasts((prev) => [
...prev,
{
id,
title:
options.title ??
(tone === 'error' ? 'Something went wrong' : 'Notice'),
description: options.description,
tone,
},
]);
if (durationMs > 0) {
const timer = setTimeout(() => dismiss(id), durationMs);
timersRef.current.set(id, timer);
}
return id;
},
[dismiss],
);
const success = useCallback(
(title: string, description?: string) =>
push({ title, description, tone: 'success' }),
[push],
);
const error = useCallback(
(title: string, description?: string) =>
push({ title, description, tone: 'error', durationMs: 7000 }),
[push],
);
const clear = useCallback(() => {
for (const timer of timersRef.current.values()) clearTimeout(timer);
timersRef.current.clear();
setToasts([]);
}, []);
const api = useMemo<ToastApi>(
() => ({ push, success, error, dismiss, clear }),
[push, success, error, dismiss, clear],
);
return (
<ToastContext.Provider value={api}>
{children}
<ToastViewport toasts={toasts} onDismiss={dismiss} />
</ToastContext.Provider>
);
}
export function useToast(): ToastApi {
const ctx = useContext(ToastContext);
if (!ctx) {
throw new Error('useToast must be used within a ToastProvider');
}
return ctx;
}