-
-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathuse-notifier.ts
55 lines (49 loc) · 1.12 KB
/
use-notifier.ts
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
import { useId } from "./use-ids";
interface Notification {
id: string;
message: string;
type: "success" | "error" | "info";
}
const notifications = ref<Notification[]>([]);
function addNotification(notification: Notification) {
notifications.value.unshift(notification);
if (notifications.value.length > 4) {
notifications.value.pop();
} else {
setTimeout(() => {
// Remove notification with ID
notifications.value = notifications.value.filter(n => n.id !== notification.id);
}, 5000);
}
}
export function useNotifications() {
return {
notifications,
dropNotification: (idx: number) => notifications.value.splice(idx, 1),
};
}
export function useNotifier() {
return {
success: (message: string) => {
addNotification({
id: useId(),
message,
type: "success",
});
},
error: (message: string) => {
addNotification({
id: useId(),
message,
type: "error",
});
},
info: (message: string) => {
addNotification({
id: useId(),
message,
type: "info",
});
},
};
}