-
Notifications
You must be signed in to change notification settings - Fork 272
/
Copy pathNotificationRow.tsx
171 lines (151 loc) · 5.07 KB
/
NotificationRow.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
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
import {
type FC,
type MouseEvent,
useCallback,
useContext,
useState,
} from 'react';
import { BellSlashIcon, CheckIcon, ReadIcon } from '@primer/octicons-react';
import { IconButton, Tooltip } from '@primer/react';
import { AppContext } from '../../context/App';
import { Opacity, Size } from '../../types';
import type { Notification } from '../../typesGitHub';
import { cn } from '../../utils/cn';
import { isMarkAsDoneFeatureSupported } from '../../utils/features';
import { formatForDisplay } from '../../utils/helpers';
import {
getNotificationTypeIcon,
getNotificationTypeIconColor,
} from '../../utils/icons';
import { openNotification } from '../../utils/links';
import { HoverGroup } from '../primitives/HoverGroup';
import { NotificationFooter } from './NotificationFooter';
import { NotificationHeader } from './NotificationHeader';
interface INotificationRow {
notification: Notification;
isAnimated?: boolean;
isRead?: boolean;
}
export const NotificationRow: FC<INotificationRow> = ({
notification,
isAnimated = false,
isRead = false,
}: INotificationRow) => {
const {
settings,
markNotificationsAsRead,
markNotificationsAsDone,
unsubscribeNotification,
} = useContext(AppContext);
const [animateExit, setAnimateExit] = useState(false);
const [showAsRead, setShowAsRead] = useState(false);
const handleNotification = useCallback(() => {
setAnimateExit(!settings.delayNotificationState);
setShowAsRead(settings.delayNotificationState);
openNotification(notification);
if (settings.markAsDoneOnOpen) {
markNotificationsAsDone([notification]);
} else {
markNotificationsAsRead([notification]);
}
}, [
notification,
markNotificationsAsRead,
markNotificationsAsDone,
settings,
]);
const unsubscribeFromThread = (event: MouseEvent<HTMLElement>) => {
// Don't trigger onClick of parent element.
event.stopPropagation();
unsubscribeNotification(notification);
};
const NotificationIcon = getNotificationTypeIcon(notification.subject);
const iconColor = getNotificationTypeIconColor(notification.subject);
const notificationType = formatForDisplay([
notification.subject.state,
notification.subject.type,
]);
const notificationNumber = notification.subject?.number
? `#${notification.subject.number}`
: '';
const notificationTitle =
`${notification.subject.title} ${notificationNumber}`.trim();
return (
<div
id={notification.id}
className={cn(
'group flex border-b pl-3 pr-1 py-1.5 text-gitify-font border-gitify-notifications-border bg-gitify-notifications-rest hover:bg-gitify-notifications-hover',
(isAnimated || animateExit) &&
'translate-x-full opacity-0 transition duration-[350ms] ease-in-out',
(isRead || showAsRead) && Opacity.READ,
)}
>
<div className="mr-3 flex items-center justify-center">
<Tooltip text={notificationType} direction="e">
<NotificationIcon size={Size.LARGE} className={iconColor} />
</Tooltip>
</div>
<div
className="flex-1 truncate cursor-pointer"
onClick={() => handleNotification()}
>
<NotificationHeader notification={notification} />
<div
className="flex gap-1 items-center mb-1 truncate text-sm"
title={notificationTitle}
data-testid="notification-row"
>
<span className="truncate">{notification.subject.title}</span>
<span
className={cn(
'text-xxs',
Opacity.READ,
!settings.showNumber && 'hidden',
)}
>
{notificationNumber}
</span>
</div>
<NotificationFooter notification={notification} />
</div>
{!animateExit && (
<HoverGroup>
{isMarkAsDoneFeatureSupported(notification.account) && (
<IconButton
aria-label="Mark as done"
icon={CheckIcon}
size="small"
variant="invisible"
onClick={() => {
setAnimateExit(!settings.delayNotificationState);
setShowAsRead(settings.delayNotificationState);
markNotificationsAsDone([notification]);
}}
data-testid="notification-mark-as-done"
/>
)}
<IconButton
aria-label="Mark as read"
icon={ReadIcon}
size="small"
variant="invisible"
onClick={() => {
setAnimateExit(!settings.delayNotificationState);
setShowAsRead(settings.delayNotificationState);
markNotificationsAsRead([notification]);
}}
data-testid="notification-mark-as-read"
/>
<IconButton
aria-label="Unsubscribe from thread"
icon={BellSlashIcon}
size="small"
variant="invisible"
onClick={unsubscribeFromThread}
data-testid="notification-unsubscribe-from-thread"
/>
</HoverGroup>
)}
</div>
);
};