forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToasts.tsx
More file actions
206 lines (177 loc) · 5.04 KB
/
Toasts.tsx
File metadata and controls
206 lines (177 loc) · 5.04 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
import * as React from 'react';
import { useTransition, animated } from 'react-spring';
import Portal from './Portal';
import { NotificationContainer } from './elements';
import {
NotificationState,
NotificationMessage,
NotificationUpdatedEvent,
NotificationChange,
NotificationStatus,
} from '../state';
import { Toast, IColors, IButtonType } from './Toast';
export interface NotificationToast {
id: string;
createdAt: number;
notification: NotificationMessage;
}
const convertMapToToasts = (
notifications: Map<string, NotificationMessage>
): NotificationToast[] =>
Array.from(notifications.keys()).map(id => ({
id,
createdAt: Date.now(),
notification: notifications.get(id),
}));
const convertNotificationEventToToast = (
notificationEvent: NotificationUpdatedEvent
): NotificationToast => ({
id: notificationEvent.id,
createdAt: Date.now(),
notification: notificationEvent.notification,
});
const isSticky = (toast: NotificationToast) => {
if (
toast.notification.status === NotificationStatus.ERROR &&
toast.notification.actions
) {
return true;
}
if (toast.notification.sticky) {
return true;
}
return false;
};
const TIME_ALIVE = {
[NotificationStatus.SUCCESS]: 7000,
[NotificationStatus.NOTICE]: 10000,
[NotificationStatus.WARNING]: 12000,
[NotificationStatus.ERROR]: 20000,
};
interface Props {
state: NotificationState;
Button?: IButtonType;
colors?: IColors;
}
const DEFAULT_COLORS = {
[NotificationStatus.ERROR]: '#DC3545',
[NotificationStatus.WARNING]: '#FFD399',
[NotificationStatus.SUCCESS]: '#5da700',
[NotificationStatus.NOTICE]: '#40A9F3',
};
const DEFAULT_BUTTON: IButtonType = props =>
React.createElement('button', props); // eslint-disable-line
export function Toasts({
state,
colors = DEFAULT_COLORS,
Button = DEFAULT_BUTTON,
}: Props) {
const [refMap] = React.useState(
() => new WeakMap<NotificationToast, HTMLDivElement>()
);
const mouseOverRef = React.useRef(false);
const [notificationsToShow, setNotificationsToShow] = React.useState(
convertMapToToasts(state.getNotifications())
);
const removeNotification = React.useCallback((id: string) => {
setNotificationsToShow(notifs => {
const newNotifs = notifs.filter(notif => notif.id !== id);
const notifToHide = notifs.find(notif => notif.id === id);
if (newNotifs.length !== notifs.length) {
return newNotifs;
}
if (notifToHide && notifToHide.notification.onHide) {
notifToHide.notification.onHide();
}
return notifs;
});
}, []);
React.useEffect(() => {
const interval = setInterval(() => {
setNotificationsToShow(notifs => {
if (mouseOverRef.current) {
// Don't remove notifs if there is a mouse there
return notifs;
}
const newNotifs = notifs.filter(
notif =>
isSticky(notif) ||
Date.now() <
notif.createdAt +
(notif.notification.timeAlive ||
TIME_ALIVE[notif.notification.status])
);
if (newNotifs.length !== notifs.length) {
return newNotifs;
}
return notifs;
});
}, 100);
return () => {
clearInterval(interval);
};
}, []);
React.useEffect(() => {
const addListener = state.onNotificationUpdated(event => {
if (event.type === NotificationChange.ADD) {
setNotificationsToShow(notifications => [
...notifications,
convertNotificationEventToToast(event),
]);
}
});
return () => {
addListener();
};
}, [state]);
React.useEffect(() => {
const removeListener = state.onNotificationRemoved(event => {
setNotificationsToShow(notifications =>
notifications.filter(n => n.id !== event.id)
);
});
return () => {
removeListener();
};
}, [state]);
const transitions = useTransition<
NotificationToast,
{ overflow: string; opacity: number; height: number }
>(notificationsToShow, n => n.id, {
from: { overflow: 'hidden', opacity: 0, height: 0 },
// @ts-ignore: not typed properly in react-spring
enter: item => next =>
next({
overflow: 'hidden',
opacity: 1,
height: refMap.get(item) ? refMap.get(item).offsetHeight + 16 : 0,
}),
leave: { overflow: 'hidden', opacity: 0, height: 0 },
});
return (
<Portal>
<NotificationContainer
onMouseEnter={() => {
mouseOverRef.current = true;
}}
onMouseLeave={() => {
mouseOverRef.current = false;
}}
>
{transitions.map(({ item, props, key }) => (
<animated.div key={key} style={props}>
<Toast
colors={colors}
Button={Button}
getRef={ref => ref && refMap.set(item, ref)}
toast={item}
removeToast={(id: string) => {
removeNotification(id);
}}
/>
</animated.div>
))}
</NotificationContainer>
</Portal>
);
}