Skip to content

Commit 096735c

Browse files
authored
feat: toggle repository notification (#1245)
* feat: toggle repository notification * feat: toggle repository notification
1 parent 0bd2620 commit 096735c

File tree

5 files changed

+550
-41
lines changed

5 files changed

+550
-41
lines changed

src/components/AccountNotifications.tsx

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,31 @@ export const AccountNotifications = (props: IProps) => {
3131
),
3232
);
3333

34-
const [showNotifications, setShowNotifications] = useState(true);
34+
const [showAccountNotifications, setShowAccountNotifications] =
35+
useState(true);
3536

36-
const toggleNotifications = () => {
37-
setShowNotifications(!showNotifications);
37+
const toggleAccountNotifications = () => {
38+
setShowAccountNotifications(!showAccountNotifications);
3839
};
3940

4041
const ChevronIcon =
4142
notifications.length === 0
4243
? ChevronLeftIcon
43-
: showNotifications
44+
: showAccountNotifications
4445
? ChevronDownIcon
4546
: ChevronUpIcon;
4647

47-
const toggleNotificationsLabel =
48+
const toggleAccountNotificationsLabel =
4849
notifications.length === 0
4950
? 'No notifications for account'
50-
: showNotifications
51+
: showAccountNotifications
5152
? 'Hide account notifications'
5253
: 'Show account notifications';
5354

5455
return (
5556
<>
5657
{showAccountHostname && (
57-
<div className="flex items-center justify-between bg-gray-300 px-3 py-2 text-sm font-semibold dark:bg-gray-darkest dark:text-white">
58+
<div className="group flex items-center justify-between bg-gray-300 px-3 py-2 text-sm font-semibold dark:bg-gray-darkest dark:text-white">
5859
<div>
5960
<PlatformIcon type={account.platform} size={16} />
6061
<button
@@ -66,19 +67,20 @@ export const AccountNotifications = (props: IProps) => {
6667
@{account.user.login}
6768
</button>
6869
</div>
69-
<div>
70+
<div className="opacity-0 transition-opacity group-hover:opacity-80">
7071
<button
7172
type="button"
72-
title={toggleNotificationsLabel}
73-
onClick={toggleNotifications}
73+
className="h-full hover:text-green-500 focus:outline-none"
74+
title={toggleAccountNotificationsLabel}
75+
onClick={toggleAccountNotifications}
7476
>
7577
<ChevronIcon size={20} />
7678
</button>
7779
</div>
7880
</div>
7981
)}
8082

81-
{showNotifications &&
83+
{showAccountNotifications &&
8284
Object.values(groupedNotifications).map((repoNotifications) => {
8385
const repoSlug = repoNotifications[0].repository.full_name;
8486

src/components/Repository.test.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fireEvent, render, screen } from '@testing-library/react';
1+
import { act, fireEvent, render, screen } from '@testing-library/react';
22
import { mockGitHubCloudAccount } from '../__mocks__/state-mocks';
33
import { AppContext } from '../context/App';
44
import type { Link } from '../types';
@@ -91,4 +91,15 @@ describe('components/Repository.tsx', () => {
9191
);
9292
expect(tree).toMatchSnapshot();
9393
});
94+
95+
it('should toggle account notifications visibility', async () => {
96+
await act(async () => {
97+
render(<RepositoryNotifications {...props} />);
98+
});
99+
100+
fireEvent.click(screen.getByTitle('Hide repository notifications'));
101+
102+
const tree = render(<RepositoryNotifications {...props} />);
103+
expect(tree).toMatchSnapshot();
104+
});
94105
});

src/components/Repository.tsx

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
import { CheckIcon, MarkGithubIcon, ReadIcon } from '@primer/octicons-react';
2-
import { type FC, useCallback, useContext } from 'react';
1+
import {
2+
CheckIcon,
3+
ChevronDownIcon,
4+
ChevronUpIcon,
5+
MarkGithubIcon,
6+
ReadIcon,
7+
} from '@primer/octicons-react';
8+
import { type FC, useCallback, useContext, useState } from 'react';
39
import { AppContext } from '../context/App';
410
import type { Notification } from '../typesGitHub';
511
import { openRepository } from '../utils/links';
@@ -28,10 +34,25 @@ export const RepositoryNotifications: FC<IProps> = ({
2834
const avatarUrl = repoNotifications[0].repository.owner.avatar_url;
2935
const repoSlug = repoNotifications[0].repository.full_name;
3036

37+
const [showRepositoryNotifications, setShowRepositoryNotifications] =
38+
useState(true);
39+
40+
const toggleRepositoryNotifications = () => {
41+
setShowRepositoryNotifications(!showRepositoryNotifications);
42+
};
43+
44+
const ChevronIcon = showRepositoryNotifications
45+
? ChevronDownIcon
46+
: ChevronUpIcon;
47+
48+
const toggleRepositoryNotificationsLabel = showRepositoryNotifications
49+
? 'Hide repository notifications'
50+
: 'Show repository notifications';
51+
3152
return (
3253
<>
33-
<div className="group flex bg-gray-100 px-3 py-2 dark:bg-gray-darker dark:text-white">
34-
<div className="mt-0 flex flex-1 items-center space-x-3 overflow-hidden overflow-ellipsis whitespace-nowrap text-sm font-medium">
54+
<div className="group flex items-center justify-between bg-gray-100 px-3 py-2 dark:bg-gray-darker dark:text-white">
55+
<div className="mt-0 flex flex-1 space-x-3 overflow-hidden overflow-ellipsis whitespace-nowrap text-sm font-medium">
3556
{avatarUrl ? (
3657
<img
3758
className="size-5 rounded"
@@ -51,6 +72,14 @@ export const RepositoryNotifications: FC<IProps> = ({
5172
</div>
5273

5374
<div className="flex items-center justify-center gap-2 opacity-0 transition-opacity group-hover:opacity-80">
75+
<button
76+
type="button"
77+
className="h-full hover:text-green-500 focus:outline-none"
78+
title={toggleRepositoryNotificationsLabel}
79+
onClick={toggleRepositoryNotifications}
80+
>
81+
<ChevronIcon size={16} />
82+
</button>
5483
<button
5584
type="button"
5685
className="h-full hover:text-green-500 focus:outline-none"
@@ -73,9 +102,10 @@ export const RepositoryNotifications: FC<IProps> = ({
73102
</div>
74103
</div>
75104

76-
{repoNotifications.map((obj) => (
77-
<NotificationRow key={obj.id} notification={obj} />
78-
))}
105+
{showRepositoryNotifications &&
106+
repoNotifications.map((obj) => (
107+
<NotificationRow key={obj.id} notification={obj} />
108+
))}
79109
</>
80110
);
81111
};

src/components/__snapshots__/AccountNotifications.test.tsx.snap

Lines changed: 35 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)