-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathuseToast.ts
More file actions
63 lines (56 loc) · 1.81 KB
/
Copy pathuseToast.ts
File metadata and controls
63 lines (56 loc) · 1.81 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
import { useCallback } from 'react';
import { useNotifications } from '../context/NotificationContext';
import type { NotificationCategory } from '../types/notification';
interface ToastOptions {
/** Category controls per-preference muting. Defaults to 'system'. */
category?: NotificationCategory;
/** Auto-dismiss delay in ms. Defaults to 5 500. */
duration?: number;
/** When true the toast stays until manually dismissed. */
persistent?: boolean;
/** Optional inline call-to-action rendered inside the toast. */
action?: { label: string; onClick: () => void };
}
/**
* Convenience wrapper around `useNotifications().addToast`.
*
* Usage:
* ```ts
* const toast = useToast();
* toast.success('Repayment sent', 'Your payment is being processed.');
* toast.error('Connection failed', 'Could not reach the Stellar network.');
* ```
*
* Returns the toast id from each helper so callers can dismiss programmatically
* via `toast.dismiss(id)` when needed.
*/
export function useToast() {
const { addToast, dismissToast } = useNotifications();
const success = useCallback(
(title: string, message: string, opts?: ToastOptions) =>
addToast({ type: 'success', title, message, ...opts }),
[addToast],
);
const error = useCallback(
(title: string, message: string, opts?: ToastOptions) =>
addToast({ type: 'error', title, message, ...opts }),
[addToast],
);
const warning = useCallback(
(title: string, message: string, opts?: ToastOptions) =>
addToast({ type: 'warning', title, message, ...opts }),
[addToast],
);
const info = useCallback(
(title: string, message: string, opts?: ToastOptions) =>
addToast({ type: 'info', title, message, ...opts }),
[addToast],
);
return {
success,
error,
warning,
info,
dismiss: dismissToast,
};
}