-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathhasuraToast.tsx
More file actions
222 lines (208 loc) · 6.01 KB
/
hasuraToast.tsx
File metadata and controls
222 lines (208 loc) · 6.01 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import clsx from 'clsx';
import { deepmerge } from 'deepmerge-ts';
import React, { MouseEventHandler, PropsWithChildren, ReactNode } from 'react';
import { Toast, ToastOptions, toast } from 'react-hot-toast/headless';
import { FaTimesCircle, FaCopy } from 'react-icons/fa';
import { HtmlAnalyticsAttributes } from '../../features/Analytics';
export type ToastType = 'error' | 'success' | 'info' | 'warning';
const TOASTS_OPTIONS: ToastOptions = {
duration: 3000,
style: {
borderRadius: '2px',
borderTop: '2px solid',
boxSizing: 'border-box',
fontSize: '13px',
margin: '0px',
minWidth: 'min(400px, 75vw)',
maxWidth: '75vw',
padding: '10px',
width: 'fit-content',
},
};
const TOASTS_TYPES_OPTIONS: Record<ToastType, ToastOptions> = {
error: {
duration: Infinity,
style: {
backgroundColor: 'rgb(244, 233, 233)',
borderColor: 'rgb(236, 61, 61)',
boxShadow: 'rgb(236 61 61 / 90%) 0px 0px 1px',
color: 'rgb(65, 47, 47)',
},
iconTheme: {
primary: 'rgb(228, 190, 190)',
secondary: 'rgb(244, 233, 233)',
},
},
success: {
duration: 5000,
style: {
backgroundColor: 'rgb(240, 245, 234)',
borderColor: 'rgb(94, 164, 0)',
boxShadow: 'rgb(94 164 0 / 90%) 0px 0px 1px',
color: 'rgb(75, 88, 58)',
},
iconTheme: {
primary: 'rgb(176, 202, 146)',
secondary: 'rgb(240, 245, 234)',
},
},
info: {
duration: 5000,
style: {
backgroundColor: '#EAF1F4',
borderColor: '#297393',
boxShadow: 'rgb(54 156 199 / 90%) 0px 0px 1px',
color: '#297393',
},
iconTheme: {
primary: '#C3D8E1',
secondary: '#C3D8E1',
},
},
warning: {
duration: Infinity,
style: {
backgroundColor: 'rgb(249, 246, 240)',
borderColor: 'rgb(235, 173, 26)',
boxShadow: 'rgb(235 173 23 / 90%) 0px 0px 1px',
color: 'rgb(90, 83, 67)',
},
iconTheme: {
primary: 'rgb(225, 207, 172)',
secondary: 'rgb(249, 246, 240)',
},
},
};
export type ToastProps = PropsWithChildren<{
type?: ToastType;
title?: string;
message?: string;
button?: {
label: ReactNode;
onClick: MouseEventHandler<HTMLButtonElement>;
dataAttributes?: HtmlAnalyticsAttributes;
};
onRemove?: () => void;
toastOptions?: ToastOptions;
}>;
export const dismissAllToasts = () => {
toast.dismiss();
};
export const hasuraToast = ({
type = 'info',
title,
message,
button,
onRemove = () => {},
toastOptions = {},
children,
}: ToastProps) => {
const handleOnRemove = (t: Toast) => () => {
toast.dismiss(t.id);
onRemove();
};
const extractTextFromChildren = (node: ReactNode): string => {
if (typeof node === 'string' || typeof node === 'number') {
return String(node);
}
if (React.isValidElement(node)) {
if (node.props.children) {
if (Array.isArray(node.props.children)) {
return node.props.children
.map((child) => extractTextFromChildren(child))
.join('');
}
return extractTextFromChildren(node.props.children);
}
}
if (Array.isArray(node)) {
return node.map((child) => extractTextFromChildren(child)).join('');
}
return '';
};
const handleCopy = () => {
if (!navigator.clipboard) {
console.warn('Clipboard API not available');
return;
}
let copyText = '';
if (title) copyText += `${title}\n`;
if (message) copyText += `${message}\n`;
if (children) {
const childText = extractTextFromChildren(children);
if (childText.trim()) {
copyText += `${childText.trim()}\n`;
}
}
navigator.clipboard.writeText(copyText.trim()).catch(err => {
console.error('Failed to copy notification:', err);
});
};
return toast.custom(
t => (
<div
className={clsx(
'font-sans flex between items-start w-full',
t.visible ? 'animate-notificationOpen' : 'animate-notificationClose'
)}
style={{
...t.style,
}}
data-testid="notification"
data-notificationtype={type}
data-toastid={t.id}
>
<div className="flex flex-col flex-grow items-stretch">
{title ? (
<div
className="font-semibold text-base w-full"
style={{
color: TOASTS_TYPES_OPTIONS[type].style?.borderColor,
}}
>
{title}
</div>
) : null}
<div className="w-full whitespace-pre-line">{message}</div>
<div className="w-full">{children}</div>
{button ? (
<div>
<button
className="mt-2 text-white h-btn px-sm justify-center inline-flex items-center text-sm font-sans font-semibold bg-gradient-to-t border rounded shadow-sm focus-visible:outline-none focus-visible:bg-gradient-to-t focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:ring-yellow-400 disabled:opacity-60"
style={{
backgroundColor:
TOASTS_TYPES_OPTIONS[type].style?.borderColor,
}}
onClick={button.onClick}
{...button.dataAttributes}
>
{button.label}
</button>
</div>
) : null}
</div>
<div className="flex items-center gap-1">
<button
onClick={handleCopy}
className="p-1 hover:bg-gray-700 rounded"
title="Copy notification content"
>
<FaCopy
color={TOASTS_TYPES_OPTIONS[type].iconTheme?.primary}
size={14}
/>
</button>
<button onClick={handleOnRemove(t)} className="p-1 hover:bg-gray-700 rounded">
<FaTimesCircle
color={TOASTS_TYPES_OPTIONS[type].iconTheme?.primary}
/>
</button>
</div>
</div>
),
deepmerge(
deepmerge(TOASTS_OPTIONS, TOASTS_TYPES_OPTIONS[type]),
toastOptions
)
);
};