-
Notifications
You must be signed in to change notification settings - Fork 271
/
Copy pathNotificationRow.tsx
117 lines (103 loc) · 3.33 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
import React, { useCallback, useContext } from 'react';
import { formatDistanceToNow, parseISO } from 'date-fns';
import { CheckIcon, BellSlashIcon, ReadIcon } from '@primer/octicons-react';
import {
formatReason,
getNotificationTypeIcon,
getNotificationTypeIconColor,
} from '../utils/github-api';
import { openInBrowser } from '../utils/helpers';
import { Notification } from '../typesGithub';
import { AppContext } from '../context/App';
interface IProps {
hostname: string;
notification: Notification;
}
export const NotificationRow: React.FC<IProps> = ({
notification,
hostname,
}) => {
const {
settings,
accounts,
markNotification,
markNotificationDone,
unsubscribeNotification,
} = useContext(AppContext);
const pressTitle = useCallback(() => {
openBrowser();
}, [settings]);
const openBrowser = useCallback(
() => openInBrowser(notification, accounts),
[notification],
);
const unsubscribe = (event: React.MouseEvent<HTMLElement>) => {
// Don't trigger onClick of parent element.
event.stopPropagation();
unsubscribeNotification(notification.id, hostname);
};
const reason = formatReason(notification.reason);
const NotificationIcon = getNotificationTypeIcon(notification.subject.type);
const iconColor = getNotificationTypeIconColor(notification.subject.state);
const realIconColor = settings
? (settings.colors && iconColor) || ''
: iconColor;
const updatedAt = formatDistanceToNow(parseISO(notification.updated_at), {
addSuffix: true,
});
return (
<div className="flex space-x-2 p-2 bg-white dark:bg-gray-dark dark:text-white hover:bg-gray-100 dark:hover:bg-gray-darker border-b border-gray-100 dark:border-gray-darker">
<div className={`flex justify-center items-center w-8 ${realIconColor}`}>
<NotificationIcon size={18} aria-label={notification.subject.type} />
</div>
<div
className="flex-1 overflow-hidden"
onClick={() => pressTitle()}
role="main"
>
<div className="mb-1 text-sm whitespace-nowrap overflow-ellipsis overflow-hidden">
{notification.subject.title}
</div>
<div className="text-xs text-capitalize">
<span title={reason.description}>{reason.type}</span> - Updated{' '}
{updatedAt}
</div>
</div>
<div className="flex justify-center items-center gap-2">
<button
className="focus:outline-none"
title="Mark as Done"
onClick={() => markNotificationDone(notification.id, hostname)}
>
<CheckIcon
className="hover:text-green-500"
size={16}
aria-label="Mark as Done"
/>
</button>
<button
className="focus:outline-none"
title="Mark as Read"
onClick={() => markNotification(notification.id, hostname)}
>
<ReadIcon
className="hover:text-green-500"
size={14}
aria-label="Mark as Read"
/>
</button>
<button
className="border-0 bg-none float-right"
title="Unsubscribe"
onClick={unsubscribe}
>
<BellSlashIcon
className="hover:text-red-500"
size={14}
aria-label="Unsubscribe"
/>
</button>
</div>
</div>
);
};