-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Notification.component.tsx
98 lines (86 loc) · 2.04 KB
/
Notification.component.tsx
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
import React, { useEffect, useState, useCallback } from 'react';
import { useDispatch } from 'react-redux';
import {
Notification as INotification,
NotificationId,
hideNotification
} from 'stores/notifications';
import { injectClassNames } from 'utils/css';
import { ANIMATION_DURATION, NOTIFICATION_TTL } from 'components/notificationList';
import styles from './Notification.module.scss';
type NotificationProps = {
notificationId: NotificationId,
notification: INotification
};
const {
notification,
notificationOpen,
notificationClose
} = styles;
const useHideNotification = (
notificationId: NotificationId,
setIsClosing: (isClosing: boolean) => void
): () => void => {
const dispatch = useDispatch();
return () => {
setIsClosing(true);
setTimeout(() => {
dispatch(hideNotification(notificationId));
}, ANIMATION_DURATION);
};
};
const useIsClosing = (
notificationId: NotificationId,
isExpirable: boolean
): [
boolean,
() => void
] => {
const [isClosing, setIsClosing] = useState(false);
const hideNotification = useCallback(
useHideNotification(notificationId, setIsClosing),
[notificationId]
);
useEffect(() => {
const timeout = (
isExpirable && setTimeout(
hideNotification,
NOTIFICATION_TTL
)
);
return () => {
if (timeout) {
clearTimeout(timeout);
}
};
}, []);
return [isClosing, hideNotification];
};
export default function Notification({
notificationId,
notification: { message, isExpirable }
}: NotificationProps): JSX.Element {
const [isClosing, hideNotification] = useIsClosing(notificationId, isExpirable);
const notificationState = isClosing
? notificationClose
: notificationOpen;
return (
<li
className={
injectClassNames(
notification,
notificationState
)
}
>
<button
type="button"
aria-label="close notification"
onClick={ hideNotification }
/>
<p>
{ message }
</p>
</li>
);
}