|
1
|
import { Toast } from "@/components/toast" |
|
2
|
import { ToastAction, ToastVariant, ToasterPosition } from "@/types" |
|
3
|
import * as React from "react" |
|
4
|
import { ExternalToast, toast as toastFn } from "sonner" |
|
5
|
|
|
6
|
const DEFAULT_TOAST_POSITION = "top-right" |
|
7
|
|
|
8
|
interface BaseToastProps { |
|
9
|
id?: string | number |
|
10
|
position?: ToasterPosition |
|
11
|
duration?: number |
|
12
|
dismissable?: boolean |
|
13
|
icon?: React.ReactNode |
|
14
|
} |
|
15
|
|
|
16
|
interface ToastProps extends BaseToastProps { |
|
17
|
description?: React.ReactNode |
|
18
|
action?: ToastAction |
|
19
|
} |
|
20
|
|
|
21
|
function create( |
|
22
|
variant: ToastVariant, |
|
23
|
title: React.ReactNode, |
|
24
|
props: ToastProps = {} |
|
25
|
) { |
|
26
|
const external: ExternalToast = { |
|
27
|
position: props.position, |
|
28
|
duration: props.duration, |
|
29
|
dismissible: props.dismissable, |
|
30
|
} |
|
31
|
|
|
32
|
if (props.id) { |
|
33
|
external.id = props.id |
|
34
|
} |
|
35
|
|
|
36
|
return toastFn.custom((t) => { |
|
37
|
return ( |
|
38
|
<Toast |
|
39
|
id={t} |
|
40
|
title={title} |
|
41
|
description={props.description} |
|
42
|
dismissable={props.dismissable} |
|
43
|
variant={variant} |
|
44
|
action={props.action} |
|
45
|
icon={props.icon} |
|
46
|
/> |
|
47
|
) |
|
48
|
}, external) |
|
49
|
} |
|
50
|
|
|
51
|
function message( |
|
52
|
/** |
|
53
|
* The title of the toast. |
|
54
|
*/ |
|
55
|
title: string, |
|
56
|
/** |
|
57
|
* The props of the toast. |
|
58
|
*/ |
|
59
|
props: ToastProps = { |
|
60
|
position: DEFAULT_TOAST_POSITION, |
|
61
|
} |
|
62
|
) { |
|
63
|
return create("message", title, props) |
|
64
|
} |
|
65
|
|
|
66
|
function custom() { |
|
67
|
return create("message", "Custom") |
|
68
|
} |
|
69
|
|
|
70
|
interface VariantToastProps extends Omit<ToastProps, "icon"> {} |
|
71
|
|
|
72
|
function info( |
|
73
|
/** |
|
74
|
* The title of the toast. |
|
75
|
*/ title: string, |
|
76
|
/** |
|
77
|
* The props of the toast. |
|
78
|
*/ |
|
79
|
props: VariantToastProps = { |
|
80
|
position: DEFAULT_TOAST_POSITION, |
|
81
|
} |
|
82
|
) { |
|
83
|
return create("info", title, props) |
|
84
|
} |
|
85
|
|
|
86
|
function error( |
|
87
|
/** |
|
88
|
* The title of the toast. |
|
89
|
*/ title: string, |
|
90
|
/** |
|
91
|
* The props of the toast. |
|
92
|
*/ |
|
93
|
props: VariantToastProps = { |
|
94
|
position: DEFAULT_TOAST_POSITION, |
|
95
|
} |
|
96
|
) { |
|
97
|
return create("error", title, props) |
|
98
|
} |
|
99
|
|
|
100
|
function success( |
|
101
|
/** |
|
102
|
* The title of the toast. |
|
103
|
*/ title: string, |
|
104
|
/** |
|
105
|
* The props of the toast. |
|
106
|
*/ |
|
107
|
props: VariantToastProps = { |
|
108
|
position: DEFAULT_TOAST_POSITION, |
|
109
|
} |
|
110
|
) { |
|
111
|
return create("success", title, props) |
|
112
|
} |
|
113
|
|
|
114
|
function warning( |
|
115
|
/** |
|
116
|
* The title of the toast. |
|
117
|
*/ title: string, |
|
118
|
/** |
|
119
|
* The props of the toast. |
|
120
|
*/ |
|
121
|
props: VariantToastProps = { |
|
122
|
position: DEFAULT_TOAST_POSITION, |
|
123
|
} |
|
124
|
) { |
|
125
|
return create("warning", title, props) |
|
126
|
} |
|
127
|
|
|
128
|
function loading( |
|
129
|
/** |
|
130
|
* The title of the toast. |
|
131
|
*/ title: string, |
|
132
|
/** |
|
133
|
* The props of the toast. |
|
134
|
*/ |
|
135
|
props: VariantToastProps = { |
|
136
|
position: DEFAULT_TOAST_POSITION, |
|
137
|
} |
|
138
|
) { |
|
139
|
return create("loading", title, { ...props, dismissable: false }) |
|
140
|
} |
|
141
|
|
|
142
|
type PromiseStateProps = |
|
143
|
| string |
|
144
|
| { |
|
145
|
title: string |
|
146
|
description?: string |
|
147
|
} |
|
148
|
|
|
149
|
interface PromiseToastProps extends Omit<BaseToastProps, "icon"> { |
|
150
|
loading: PromiseStateProps |
|
151
|
success: PromiseStateProps |
|
152
|
error: PromiseStateProps |
|
153
|
} |
|
154
|
|
|
155
|
function createUniqueID() { |
|
156
|
return Math.random().toString(36).slice(2, 8) |
|
157
|
} |
|
158
|
|
|
159
|
async function promise<TData>( |
|
160
|
/** |
|
161
|
* The promise to be resolved. |
|
162
|
*/ |
|
163
|
promise: Promise<TData> | (() => Promise<TData>), |
|
164
|
/** |
|
165
|
* The props of the toast. |
|
166
|
*/ |
|
167
|
props: PromiseToastProps |
|
168
|
) { |
|
169
|
let id: string | number | undefined = props.id || createUniqueID() |
|
170
|
let shouldDismiss = id !== undefined |
|
171
|
|
|
172
|
id = create( |
|
173
|
"loading", |
|
174
|
typeof props.loading === "string" ? props.loading : props.loading.title, |
|
175
|
{ |
|
176
|
id: id, |
|
177
|
position: props.position, |
|
178
|
description: |
|
179
|
typeof props.loading === "string" |
|
180
|
? undefined |
|
181
|
: props.loading.description, |
|
182
|
duration: Infinity, |
|
183
|
dismissable: false, |
|
184
|
} |
|
185
|
) |
|
186
|
|
|
187
|
const p = promise instanceof Promise ? promise : promise() |
|
188
|
|
|
189
|
p.then(() => { |
|
190
|
shouldDismiss = false |
|
191
|
create( |
|
192
|
"success", |
|
193
|
typeof props.success === "string" ? props.success : props.success.title, |
|
194
|
{ |
|
195
|
id: id, |
|
196
|
position: props.position, |
|
197
|
description: |
|
198
|
typeof props.success === "string" |
|
199
|
? undefined |
|
200
|
: props.success.description, |
|
201
|
} |
|
202
|
) |
|
203
|
}) |
|
204
|
.catch(() => { |
|
205
|
shouldDismiss = false |
|
206
|
create( |
|
207
|
"error", |
|
208
|
typeof props.error === "string" ? props.error : props.error.title, |
|
209
|
{ |
|
210
|
id: id, |
|
211
|
position: props.position, |
|
212
|
description: |
|
213
|
typeof props.error === "string" |
|
214
|
? undefined |
|
215
|
: props.error.description, |
|
216
|
} |
|
217
|
) |
|
218
|
}) |
|
219
|
.finally(() => { |
|
220
|
if (shouldDismiss) { |
|
221
|
toastFn.dismiss(id) |
|
222
|
id = undefined |
|
223
|
} |
|
224
|
}) |
|
225
|
|
|
226
|
return id |
|
227
|
} |
|
228
|
|
|
229
|
export const toast = Object.assign(message, { |
|
230
|
info, |
|
231
|
error, |
|
232
|
warning, |
|
233
|
success, |
|
234
|
promise, |
|
235
|
loading, |
|
236
|
dismiss: toastFn.dismiss, |
|
237
|
}) |