-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathRepositoryNotifications.tsx
More file actions
142 lines (123 loc) · 4.41 KB
/
RepositoryNotifications.tsx
File metadata and controls
142 lines (123 loc) · 4.41 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
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 { cn } from '../../utils/cn';
import { isMarkAsDoneFeatureSupported } from '../../utils/features';
import { getChevronDetails } from '../../utils/helpers';
import { openRepository } from '../../utils/links';
import { shouldRemoveNotificationsFromState } from '../../utils/notifications/remove';
import { AvatarWithFallback } from '../avatars/AvatarWithFallback';
import { NotificationRow } from './NotificationRow';
interface RepositoryNotificationsProps {
repoNotifications: GitifyNotification[];
repoName: string;
}
export const RepositoryNotifications: FC<RepositoryNotificationsProps> = ({
repoName,
repoNotifications,
}) => {
const { settings, markNotificationsAsRead, markNotificationsAsDone } =
useAppContext();
const [shouldAnimateExitTransition, setShouldAnimateExitTransition] =
useState(false);
const [
isRepositoryNotificationsVisible,
setIsRepositoryNotificationsVisible,
] = useState(true);
const avatarUrl = repoNotifications[0].repository.owner.avatarUrl;
const shouldAnimateExit = shouldRemoveNotificationsFromState(settings);
const actionMarkAsDone = () => {
setShouldAnimateExitTransition(shouldAnimateExit);
markNotificationsAsDone(repoNotifications);
};
const actionMarkAsRead = () => {
setShouldAnimateExitTransition(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',
shouldAnimateExitTransition &&
'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();
openRepository(repoNotifications[0].repository);
}}
title="Open repository ↗"
variant="invisible"
>
<AvatarWithFallback
alt={repoName}
name={repoName}
size={Size.LARGE}
src={avatarUrl}
userType={repoNotifications[0].repository.owner.type}
/>
</Button>
{!shouldAnimateExitTransition && (
<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
isAnimated={shouldAnimateExitTransition}
key={notification.id}
notification={notification}
/>
))}
</>
);
};