-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathuseToast.ts
135 lines (122 loc) · 4.08 KB
/
useToast.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
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
126
127
128
129
130
131
132
133
134
135
import type { ContainerMethods, Settings, Status, Toast, ToastOptions } from '../type';
import useSettings from './useSettings';
import useVtEvents from './useVtEvents';
import { isBody } from '../utils';
export type CustomMethods = Record<string, (status?: Status, title?: string) => string>;
export interface ToastPluginAPI {
notify: (status: Status, title?: string) => Toast['id'];
success: (status: Status, title?: string) => Toast['id'];
info: (status: Status, title?: string) => Toast['id'];
warning: (status: Status, title?: string) => Toast['id'];
error: (status: Status, title?: string) => Toast['id'];
loader: (status: Status, title?: string) => Toast['id'];
prompt: <T>(status: Omit<ToastOptions, 'mode' | 'answers'> & { answers: Record<string, T> }) => Promise<T>;
updateToast: (id: Toast['id'], status: ToastOptions) => boolean;
settings: (settings?: Settings) => Settings;
findToast: <T extends Toast['id']>(id: T) => Toast | undefined;
getToasts: () => Toast[];
stopLoader: (id?: Toast['id']) => number;
remove: (id?: Toast['id']) => number;
}
// @ts-expect-error
//eslint-disable-next-line prefer-const
export let app: { container: ContainerMethods } = {};
const notify = (status: Status, title?: string): ReturnType<ToastPluginAPI['notify']> => {
if (isBody(status)) {
status = {
body: status
};
}
if (title) {
status.title = title;
}
if (!status.type) {
status.type = 'success';
}
return app.container.add(status);
};
export const toastMethods: ToastPluginAPI = {
notify,
success: (status: Status, title?: string) => notify(status, title),
info: (status: Status, title?: string) => {
if (isBody(status)) {
status = {
body: status
};
}
if (title) {
status.title = title;
}
status.type = 'info';
return notify(status);
},
warning: (status: Status, title?: string) => {
if (isBody(status)) {
status = {
body: status
};
}
if (title) {
status.title = title;
}
status.type = 'warning';
return notify(status);
},
error: (status: Status, title?: string) => {
const notification = {} as ToastOptions;
if (isBody(status)) {
notification.body = status;
}
if (title) {
notification.title = title;
}
notification.type = 'error';
return notify(notification);
},
loader: (status: Status, title?: string) => {
if (isBody(status)) {
status = {
body: status
};
}
if (title) {
status.title = title;
}
status.mode = 'loader';
return notify(status);
},
prompt: async <T>(status: Omit<ToastOptions, 'mode' | 'answers'> & { answers: Record<string, T> }) => {
(status as ToastOptions).mode = 'prompt';
const events = useVtEvents();
const toastId = app.container.add(status);
return new Promise<T>(resolve => {
events.once('vtPromptResponse', payload => {
if (payload.id === toastId) {
resolve(payload.response as T);
}
});
});
},
stopLoader(id?: Toast['id']): number {
return app.container.stopLoader(id);
},
findToast(id?: Toast['id']): Toast | undefined {
return app.container.get(id);
},
getToasts(): Toast[] {
return app.container.get() as unknown as Toast[];
},
updateToast(id: Toast['id'], status: ToastOptions): boolean {
return app.container.set(id, status);
},
remove(id?: Toast['id']): number {
return app.container.remove(id);
},
settings(settings?: Settings): Settings {
const settingsComposable = useSettings();
return settings ? settingsComposable.updateSettings(settings) : settingsComposable.settings;
}
};
export default function useToast(): ToastPluginAPI {
return toastMethods;
}