-
Notifications
You must be signed in to change notification settings - Fork 272
/
Copy pathNotificationRow.tsx
201 lines (180 loc) · 6.19 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
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
import {
BellSlashIcon,
CheckIcon,
FeedPersonIcon,
ReadIcon,
} from '@primer/octicons-react';
import { formatDistanceToNow, parseISO } from 'date-fns';
import {
type FC,
type KeyboardEvent,
type MouseEvent,
useCallback,
useContext,
} from 'react';
import { AppContext } from '../context/App';
import type { Notification } from '../typesGithub';
import { openExternalLink } from '../utils/comms';
import Constants from '../utils/constants';
import { formatForDisplay, openInBrowser } from '../utils/helpers';
import {
getNotificationTypeIcon,
getNotificationTypeIconColor,
} from '../utils/icons';
import { formatReason } from '../utils/reason';
interface IProps {
hostname: string;
notification: Notification;
}
export const NotificationRow: FC<IProps> = ({ notification, hostname }) => {
const {
settings,
accounts,
markNotificationRead,
markNotificationDone,
removeNotificationFromState,
unsubscribeNotification,
notifications,
} = useContext(AppContext);
const openNotification = useCallback(() => {
openInBrowser(notification, accounts);
if (settings.markAsDoneOnOpen) {
markNotificationDone(notification.id, hostname);
}
handleNotificationState();
}, [notifications, notification, accounts, settings]); // notifications required here to prevent weird state issues
const unsubscribe = (event: MouseEvent<HTMLElement>) => {
// Don't trigger onClick of parent element.
event.stopPropagation();
unsubscribeNotification(notification.id, hostname);
markNotificationRead(notification.id, hostname);
handleNotificationState();
};
const markAsRead = () => {
markNotificationRead(notification.id, hostname);
handleNotificationState();
};
const markAsDone = () => {
markNotificationDone(notification.id, hostname);
handleNotificationState();
};
const openUserProfile = (
event: MouseEvent<HTMLElement> | KeyboardEvent<HTMLElement>,
) => {
// Don't trigger onClick of parent element.
event.stopPropagation();
openExternalLink(notification.subject.user.html_url);
};
const handleNotificationState = useCallback(() => {
if (!settings.showAllNotifications) {
removeNotificationFromState(notification.id, hostname);
}
if (notification.unread) {
const notificationRow = document.getElementById(notification.id);
notificationRow.className += Constants.READ_CLASS_NAME;
}
// TODO FIXME - this is not updating the notification count
notification.unread = false;
}, [notification, notifications]);
const reason = formatReason(notification.reason);
const NotificationIcon = getNotificationTypeIcon(notification.subject);
const iconColor = getNotificationTypeIconColor(notification.subject);
const updatedAt = formatDistanceToNow(parseISO(notification.updated_at), {
addSuffix: true,
});
const updatedLabel = notification.subject.user
? `${notification.subject.user.login} updated ${updatedAt}`
: `Updated ${updatedAt}`;
const notificationTitle = formatForDisplay([
notification.subject.state,
notification.subject.type,
]);
return (
<div
id={notification.id}
className={`flex space-x-3 py-2 px-3 bg-white border-b border-gray-100 dark:border-gray-darker group dark:bg-gray-dark dark:text-white hover:bg-gray-100 dark:hover:bg-gray-darker
${!notification.unread ? Constants.READ_CLASS_NAME : ''}`}
>
<div
className={`flex flex-col justify-center items-center w-5 ${iconColor}`}
title={notificationTitle}
>
<NotificationIcon size={18} aria-label={notification.subject.type} />
</div>
<div
className="flex-1 overflow-hidden"
onClick={openNotification}
onKeyDown={openNotification}
>
<div
className="mb-1 text-sm whitespace-nowrap overflow-ellipsis overflow-hidden cursor-pointer"
role="main"
title={notification.subject.title}
>
{notification.subject.title}
</div>
<div className="text-xs text-capitalize whitespace-nowrap overflow-ellipsis overflow-hidden">
<span className="flex items-center">
<span title={updatedLabel} className="flex">
{notification.subject.user ? (
<span
title="View User Profile"
onClick={openUserProfile}
onKeyDown={openUserProfile}
>
<img
className="rounded-full w-4 h-4 cursor-pointer"
src={notification.subject.user.avatar_url}
title={notification.subject.user.login}
alt={`${notification.subject.user.login}'s avatar`}
/>
</span>
) : (
<span>
<FeedPersonIcon
size={16}
className="text-gray-500 dark:text-gray-300"
/>
</span>
)}
<span className="ml-1" title={reason.description}>
{reason.title}
</span>
<span className="ml-1">{updatedAt}</span>
</span>
</span>
</div>
</div>
<div className="flex justify-center items-center gap-2 opacity-0 group-hover:opacity-80 transition-opacity">
<button
type="button"
className="focus:outline-none h-full hover:text-green-500"
title="Mark as Done"
onClick={markAsDone}
>
<CheckIcon size={16} aria-label="Mark as Done" />
</button>
<button
type="button"
className="focus:outline-none h-full hover:text-red-500"
title="Unsubscribe"
onClick={unsubscribe}
>
<BellSlashIcon size={14} aria-label="Unsubscribe" />
</button>
{notification.unread ? (
<button
type="button"
className="focus:outline-none h-full hover:text-green-500"
title="Mark as Read"
onClick={markAsRead}
>
<ReadIcon size={14} aria-label="Mark as Read" />
</button>
) : (
<div className="w-[14px]" />
)}
</div>
</div>
);
};