Skip to content

Commit c63c17d

Browse files
committed
refactor: extract filters and show on all read screen
Signed-off-by: Adam Setch <[email protected]>
1 parent 12e73f2 commit c63c17d

File tree

8 files changed

+230
-55
lines changed

8 files changed

+230
-55
lines changed

src/renderer/components/AllRead.test.tsx

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,49 @@
11
import { render } from '@testing-library/react';
2+
import { MemoryRouter } from 'react-router-dom';
3+
import { mockSettings } from '../__mocks__/state-mocks';
24
import { ensureStableEmojis } from '../__mocks__/utils';
5+
import { AppContext } from '../context/App';
36
import { AllRead } from './AllRead';
47

58
describe('renderer/components/AllRead.tsx', () => {
69
beforeEach(() => {
710
ensureStableEmojis();
811
});
912

10-
it('should render itself & its children', () => {
11-
const tree = render(<AllRead />);
13+
it('should render itself & its children - no filters', () => {
14+
const tree = render(
15+
<AppContext.Provider
16+
value={{
17+
settings: {
18+
...mockSettings,
19+
},
20+
}}
21+
>
22+
<MemoryRouter>
23+
<AllRead />
24+
</MemoryRouter>
25+
</AppContext.Provider>,
26+
);
27+
28+
expect(tree).toMatchSnapshot();
29+
});
30+
31+
it('should render itself & its children - with filters', () => {
32+
const tree = render(
33+
<AppContext.Provider
34+
value={{
35+
settings: {
36+
...mockSettings,
37+
filterReasons: ['author'],
38+
hideBots: true,
39+
},
40+
}}
41+
>
42+
<MemoryRouter>
43+
<AllRead />
44+
</MemoryRouter>
45+
</AppContext.Provider>,
46+
);
1247

1348
expect(tree).toMatchSnapshot();
1449
});

src/renderer/components/AllRead.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
import { type FC, useMemo } from 'react';
1+
import { type FC, useContext, useMemo } from 'react';
22

33
import { Stack } from '@primer/react';
44

5+
import { AppContext } from '../context/App';
56
import { Constants } from '../utils/constants';
7+
import { hasFiltersSet } from '../utils/filters';
68
import { Centered } from './primitives/Centered';
79
import { EmojiText } from './primitives/EmojiText';
810

911
export const AllRead: FC = () => {
12+
const { settings } = useContext(AppContext);
13+
14+
const hasFilters = hasFiltersSet(settings);
15+
1016
const emoji = useMemo(
1117
() =>
1218
Constants.ALL_READ_EMOJIS[
@@ -22,7 +28,13 @@ export const AllRead: FC = () => {
2228
<EmojiText text={emoji} />
2329
</div>
2430

25-
<div className="mb-2 text-xl font-semibold">No new notifications</div>
31+
{hasFilters ? (
32+
<div className="mb-2 text-xl font-semibold">
33+
No new filtered notifications
34+
</div>
35+
) : (
36+
<div className="mb-2 text-xl font-semibold">No new notifications</div>
37+
)}
2638
</Stack>
2739
</Centered>
2840
);

src/renderer/components/Sidebar.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { IconButton, Stack } from '@primer/react';
1515
import { AppContext } from '../context/App';
1616
import { quitApp } from '../utils/comms';
1717
import { Constants } from '../utils/constants';
18-
import { getFilterCount } from '../utils/helpers';
18+
import { hasFiltersSet } from '../utils/filters';
1919
import {
2020
openGitHubIssues,
2121
openGitHubNotifications,
@@ -67,10 +67,6 @@ export const Sidebar: FC = () => {
6767
return getNotificationCount(notifications);
6868
}, [notifications]);
6969

70-
const filterCount = useMemo(() => {
71-
return getFilterCount(settings);
72-
}, [settings]);
73-
7470
return (
7571
<div className="fixed left-12 -ml-12 flex h-full w-12 flex-col overflow-y-auto bg-gray-sidebar">
7672
<div className="flex flex-1 flex-col">
@@ -113,7 +109,7 @@ export const Sidebar: FC = () => {
113109
description="Filter notifications"
114110
unsafeDisableTooltip={false}
115111
size="small"
116-
variant={filterCount > 0 ? 'primary' : 'invisible'}
112+
variant={hasFiltersSet(settings) ? 'primary' : 'invisible'}
117113
tooltipDirection="e"
118114
onClick={() => toggleFilters()}
119115
data-testid="sidebar-filter-notifications"

src/renderer/components/__snapshots__/AllRead.test.tsx.snap

Lines changed: 124 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/renderer/utils/filters.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { defaultSettings } from '../context/App';
2+
import type { SettingsState } from '../types';
3+
import { getFilterCount, hasFiltersSet } from './filters';
4+
5+
describe('renderer/utils/filters.ts', () => {
6+
it('default filter settings', () => {
7+
expect(getFilterCount(defaultSettings)).toBe(0);
8+
expect(hasFiltersSet(defaultSettings)).toBe(false);
9+
});
10+
11+
it('non-default reason filters', () => {
12+
const settings = {
13+
...defaultSettings,
14+
filterReasons: ['subscribed', 'manual'],
15+
} as SettingsState;
16+
expect(getFilterCount(settings)).toBe(2);
17+
expect(hasFiltersSet(settings)).toBe(true);
18+
});
19+
20+
it('non-default bot filters', () => {
21+
const settings = {
22+
...defaultSettings,
23+
hideBots: true,
24+
} as SettingsState;
25+
expect(getFilterCount(settings)).toBe(1);
26+
expect(hasFiltersSet(settings)).toBe(true);
27+
});
28+
});

src/renderer/utils/filters.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { defaultSettings } from '../context/App';
2+
import type { SettingsState } from '../types';
3+
4+
export function getFilterCount(settings: SettingsState): number {
5+
let count = 0;
6+
7+
if (settings.filterReasons.length !== defaultSettings.filterReasons.length) {
8+
count += settings.filterReasons.length;
9+
}
10+
11+
if (
12+
settings.detailedNotifications &&
13+
settings.hideBots !== defaultSettings.hideBots
14+
) {
15+
count += 1;
16+
}
17+
18+
return count;
19+
}
20+
21+
export function hasFiltersSet(settings: SettingsState): boolean {
22+
return getFilterCount(settings) > 0;
23+
}

src/renderer/utils/helpers.test.ts

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import {
1111
ChevronRightIcon,
1212
} from '@primer/octicons-react';
1313
import log from 'electron-log';
14-
import { defaultSettings } from '../context/App';
15-
import type { Hostname, Link, SettingsState } from '../types';
14+
import type { Hostname, Link } from '../types';
1615
import type { SubjectType } from '../typesGitHub';
1716
import {
1817
mockGraphQLResponse,
@@ -25,7 +24,6 @@ import {
2524
generateGitHubWebUrl,
2625
generateNotificationReferrerId,
2726
getChevronDetails,
28-
getFilterCount,
2927
getPlatformFromHostname,
3028
isEnterpriseServerHost,
3129
isMarkAsDoneFeatureSupported,
@@ -614,28 +612,6 @@ describe('renderer/utils/helpers.ts', () => {
614612
});
615613
});
616614

617-
describe('filter count', () => {
618-
it('default filter settings', () => {
619-
expect(getFilterCount(defaultSettings)).toBe(0);
620-
});
621-
622-
it('non-default reason filters', () => {
623-
const settings = {
624-
...defaultSettings,
625-
filterReasons: ['subscribed', 'manual'],
626-
} as SettingsState;
627-
expect(getFilterCount(settings)).toBe(2);
628-
});
629-
630-
it('non-default bot filters', () => {
631-
const settings = {
632-
...defaultSettings,
633-
hideBots: true,
634-
} as SettingsState;
635-
expect(getFilterCount(settings)).toBe(1);
636-
});
637-
});
638-
639615
describe('getChevronDetails', () => {
640616
it('should return correct chevron details', () => {
641617
expect(getChevronDetails(true, true, 'account')).toEqual({

src/renderer/utils/helpers.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import {
55
} from '@primer/octicons-react';
66
import { formatDistanceToNowStrict, parseISO } from 'date-fns';
77
import log from 'electron-log';
8-
import { defaultSettings } from '../context/App';
9-
import type { Account, Chevron, Hostname, Link, SettingsState } from '../types';
8+
import type { Account, Chevron, Hostname, Link } from '../types';
109
import type { Notification } from '../typesGitHub';
1110
import { getHtmlUrl, getLatestDiscussion } from './api/client';
1211
import type { PlatformType } from './auth/types';
@@ -206,23 +205,6 @@ export function formatNotificationUpdatedAt(
206205
return '';
207206
}
208207

209-
export function getFilterCount(settings: SettingsState): number {
210-
let count = 0;
211-
212-
if (settings.filterReasons.length !== defaultSettings.filterReasons.length) {
213-
count += settings.filterReasons.length;
214-
}
215-
216-
if (
217-
settings.detailedNotifications &&
218-
settings.hideBots !== defaultSettings.hideBots
219-
) {
220-
count += 1;
221-
}
222-
223-
return count;
224-
}
225-
226208
export function getChevronDetails(
227209
hasNotifications: boolean,
228210
isVisible: boolean,

0 commit comments

Comments
 (0)