Skip to content

Commit 67a383c

Browse files
authored
refactor: chevron helper (#1574)
Signed-off-by: Adam Setch <[email protected]>
1 parent 88d9d88 commit 67a383c

File tree

5 files changed

+81
-36
lines changed

5 files changed

+81
-36
lines changed

Diff for: src/renderer/components/AccountNotifications.tsx

+8-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
import {
2-
ChevronDownIcon,
3-
ChevronLeftIcon,
4-
ChevronRightIcon,
52
FeedPersonIcon,
63
GitPullRequestIcon,
74
IssueOpenedIcon,
@@ -11,6 +8,7 @@ import { AppContext } from '../context/App';
118
import { type Account, type GitifyError, Opacity, Size } from '../types';
129
import type { Notification } from '../typesGitHub';
1310
import { cn } from '../utils/cn';
11+
import { getChevronDetails } from '../utils/helpers';
1412
import {
1513
openAccountProfile,
1614
openGitHubIssues,
@@ -63,19 +61,11 @@ export const AccountNotifications: FC<IAccountNotifications> = (
6361
setShowAccountNotifications(!showAccountNotifications);
6462
};
6563

66-
const ChevronIcon =
67-
notifications.length === 0
68-
? ChevronLeftIcon
69-
: showAccountNotifications
70-
? ChevronDownIcon
71-
: ChevronRightIcon;
72-
73-
const toggleAccountNotificationsLabel =
74-
notifications.length === 0
75-
? 'No notifications for account'
76-
: showAccountNotifications
77-
? 'Hide account notifications'
78-
: 'Show account notifications';
64+
const Chevron = getChevronDetails(
65+
hasNotifications,
66+
showAccountNotifications,
67+
'account',
68+
);
7969

8070
const isGroupByRepository = settings.groupBy === 'REPOSITORY';
8171

@@ -136,8 +126,8 @@ export const AccountNotifications: FC<IAccountNotifications> = (
136126
}}
137127
/>
138128
<InteractionButton
139-
title={toggleAccountNotificationsLabel}
140-
icon={ChevronIcon}
129+
title={Chevron.label}
130+
icon={Chevron.icon}
141131
size={Size.SMALL}
142132
onClick={toggleAccountNotifications}
143133
/>

Diff for: src/renderer/components/RepositoryNotifications.tsx

+12-17
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
import {
2-
CheckIcon,
3-
ChevronDownIcon,
4-
ChevronRightIcon,
5-
MarkGithubIcon,
6-
ReadIcon,
7-
} from '@primer/octicons-react';
1+
import { CheckIcon, MarkGithubIcon, ReadIcon } from '@primer/octicons-react';
82
import { type FC, type MouseEvent, useContext, useState } from 'react';
93
import { AppContext } from '../context/App';
104
import { Opacity, Size } from '../types';
115
import type { Notification } from '../typesGitHub';
126
import { cn } from '../utils/cn';
13-
import { isMarkAsDoneFeatureSupported } from '../utils/helpers';
7+
import {
8+
getChevronDetails,
9+
isMarkAsDoneFeatureSupported,
10+
} from '../utils/helpers';
1411
import { openRepository } from '../utils/links';
1512
import { HoverGroup } from './HoverGroup';
1613
import { NotificationRow } from './NotificationRow';
@@ -39,13 +36,11 @@ export const RepositoryNotifications: FC<IRepositoryNotifications> = ({
3936
setShowRepositoryNotifications(!showRepositoryNotifications);
4037
};
4138

42-
const ChevronIcon = showRepositoryNotifications
43-
? ChevronDownIcon
44-
: ChevronRightIcon;
45-
46-
const toggleRepositoryNotificationsLabel = showRepositoryNotifications
47-
? 'Hide repository notifications'
48-
: 'Show repository notifications';
39+
const Chevron = getChevronDetails(
40+
true,
41+
showRepositoryNotifications,
42+
'repository',
43+
);
4944

5045
return (
5146
<>
@@ -108,8 +103,8 @@ export const RepositoryNotifications: FC<IRepositoryNotifications> = ({
108103
}}
109104
/>
110105
<InteractionButton
111-
title={toggleRepositoryNotificationsLabel}
112-
icon={ChevronIcon}
106+
title={Chevron.label}
107+
icon={Chevron.icon}
113108
size={Size.SMALL}
114109
onClick={toggleRepositoryNotifications}
115110
/>

Diff for: src/renderer/types.ts

+5
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,8 @@ export enum Size {
184184
LARGE = 18,
185185
XLARGE = 20,
186186
}
187+
188+
export type Chevron = {
189+
icon: FC<OcticonProps>;
190+
label: string;
191+
};

Diff for: src/renderer/utils/helpers.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import {
55
mockPersonalAccessTokenAccount,
66
} from '../__mocks__/state-mocks';
77

8+
import {
9+
ChevronDownIcon,
10+
ChevronLeftIcon,
11+
ChevronRightIcon,
12+
} from '@primer/octicons-react';
813
import log from 'electron-log';
914
import { defaultSettings } from '../context/App';
1015
import type { Hostname, Link, SettingsState } from '../types';
@@ -19,6 +24,7 @@ import {
1924
formatNotificationUpdatedAt,
2025
generateGitHubWebUrl,
2126
generateNotificationReferrerId,
27+
getChevronDetails,
2228
getFilterCount,
2329
getPlatformFromHostname,
2430
isEnterpriseServerHost,
@@ -629,4 +635,23 @@ describe('renderer/utils/helpers.ts', () => {
629635
expect(getFilterCount(settings)).toBe(1);
630636
});
631637
});
638+
639+
describe('getChevronDetails', () => {
640+
it('should return correct chevron details', () => {
641+
expect(getChevronDetails(true, true, 'account')).toEqual({
642+
icon: ChevronDownIcon,
643+
label: 'Hide account notifications',
644+
});
645+
646+
expect(getChevronDetails(true, false, 'account')).toEqual({
647+
icon: ChevronRightIcon,
648+
label: 'Show account notifications',
649+
});
650+
651+
expect(getChevronDetails(false, false, 'account')).toEqual({
652+
icon: ChevronLeftIcon,
653+
label: 'No notifications for account',
654+
});
655+
});
656+
});
632657
});

Diff for: src/renderer/utils/helpers.ts

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
import {
2+
ChevronDownIcon,
3+
ChevronLeftIcon,
4+
ChevronRightIcon,
5+
} from '@primer/octicons-react';
16
import { formatDistanceToNowStrict, parseISO } from 'date-fns';
27
import log from 'electron-log';
38
import { defaultSettings } from '../context/App';
4-
import type { Account, Hostname, Link, SettingsState } from '../types';
9+
import type { Account, Chevron, Hostname, Link, SettingsState } from '../types';
510
import type { Notification } from '../typesGitHub';
611
import { getHtmlUrl, getLatestDiscussion } from './api/client';
712
import type { PlatformType } from './auth/types';
@@ -209,3 +214,28 @@ export function getFilterCount(settings: SettingsState): number {
209214

210215
return count;
211216
}
217+
218+
export function getChevronDetails(
219+
hasNotifications: boolean,
220+
isVisible: boolean,
221+
type: 'account' | 'repository',
222+
): Chevron {
223+
if (!hasNotifications) {
224+
return {
225+
icon: ChevronLeftIcon,
226+
label: `No notifications for ${type}`,
227+
};
228+
}
229+
230+
if (isVisible) {
231+
return {
232+
icon: ChevronDownIcon,
233+
label: `Hide ${type} notifications`,
234+
};
235+
}
236+
237+
return {
238+
icon: ChevronRightIcon,
239+
label: `Show ${type} notifications`,
240+
};
241+
}

0 commit comments

Comments
 (0)