-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathRepositoryNotifications.tsx
More file actions
146 lines (126 loc) · 4.53 KB
/
RepositoryNotifications.tsx
File metadata and controls
146 lines (126 loc) · 4.53 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
import { type FC, type MouseEvent, useState } from 'react';
import { CheckIcon, ReadIcon } from '@primer/octicons-react';
import { Button, Stack } from '@primer/react';
import { useAppContext } from '../../hooks/useAppContext';
import { HoverButton } from '../primitives/HoverButton';
import { HoverGroup } from '../primitives/HoverGroup';
import { type GitifyNotification, Opacity, Size } from '../../types';
import { isMarkAsDoneFeatureSupported } from '../../utils/api/features';
import { shouldRemoveNotificationsFromState } from '../../utils/notifications/remove';
import { openRepository } from '../../utils/system/links';
import { cn } from '../../utils/ui/cn';
import { getChevronDetails } from '../../utils/ui/display';
import { AvatarWithFallback } from '../avatars/AvatarWithFallback';
import { NotificationRow } from './NotificationRow';
export interface RepositoryNotificationsProps {
repoNotifications: GitifyNotification[];
repoName: string;
}
export const RepositoryNotifications: FC<RepositoryNotificationsProps> = ({
repoName,
repoNotifications,
}) => {
const { settings, markNotificationsAsRead, markNotificationsAsDone } =
useAppContext();
const [shouldAnimateRepositoryExit, setShouldAnimateRepositoryExit] =
useState(false);
const [
isRepositoryNotificationsVisible,
setIsRepositoryNotificationsVisible,
] = useState(true);
const avatarUrl = repoNotifications[0].repository.owner.avatarUrl;
const shouldAnimateExit = shouldRemoveNotificationsFromState(settings);
const actionRepositoryInteraction = () => {
openRepository(repoNotifications[0].repository);
};
const actionMarkAsDone = () => {
setShouldAnimateRepositoryExit(shouldAnimateExit);
markNotificationsAsDone(repoNotifications);
};
const actionMarkAsRead = () => {
setShouldAnimateRepositoryExit(shouldAnimateExit);
markNotificationsAsRead(repoNotifications);
};
const actionToggleRepositoryNotifications = () => {
setIsRepositoryNotificationsVisible(!isRepositoryNotificationsVisible);
};
const areAllRepoNotificationsRead = repoNotifications.every(
(notification) => !notification.unread,
);
const Chevron = getChevronDetails(
true,
isRepositoryNotificationsVisible,
'repository',
);
return (
<>
<Stack
className={cn(
'group relative pr-1 py-0.5',
'bg-gitify-repository',
shouldAnimateRepositoryExit &&
'translate-x-full opacity-0 transition duration-350 ease-in-out',
areAllRepoNotificationsRead && Opacity.READ,
)}
direction="horizontal"
onClick={actionToggleRepositoryNotifications}
>
<Button
alignContent="center"
count={repoNotifications.length}
data-testid="open-repository"
onClick={(event: MouseEvent<HTMLElement>) => {
// Don't trigger onClick of parent element.
event.stopPropagation();
actionRepositoryInteraction();
}}
title="Open repository ↗"
variant="invisible"
>
<AvatarWithFallback
alt={repoName}
name={repoName}
size={Size.LARGE}
src={avatarUrl}
userType={repoNotifications[0].repository.owner.type}
/>
</Button>
{!shouldAnimateRepositoryExit && (
<HoverGroup bgColor="group-hover:bg-gitify-repository">
<HoverButton
action={actionMarkAsRead}
enabled={!areAllRepoNotificationsRead}
icon={ReadIcon}
label="Mark repository as read"
testid="repository-mark-as-read"
/>
<HoverButton
action={actionMarkAsDone}
enabled={
isMarkAsDoneFeatureSupported(repoNotifications[0].account) &&
!areAllRepoNotificationsRead
}
icon={CheckIcon}
label="Mark repository as done"
testid="repository-mark-as-done"
/>
<HoverButton
action={actionToggleRepositoryNotifications}
icon={Chevron.icon}
label={Chevron.label}
testid="repository-toggle"
/>
</HoverGroup>
)}
</Stack>
{isRepositoryNotificationsVisible &&
repoNotifications.map((notification) => (
<NotificationRow
isRepositoryAnimatingExit={shouldAnimateRepositoryExit}
key={notification.id}
notification={notification}
/>
))}
</>
);
};