Skip to content

Commit c3a80f0

Browse files
committed
refactor: use handler for icon color
Signed-off-by: Adam Setch <[email protected]>
1 parent a6fbbca commit c3a80f0

File tree

17 files changed

+83
-237
lines changed

17 files changed

+83
-237
lines changed

src/renderer/components/notifications/NotificationRow.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import type { Notification } from '../../typesGitHub';
99
import { cn } from '../../utils/cn';
1010
import { isMarkAsDoneFeatureSupported } from '../../utils/features';
1111
import { formatForDisplay } from '../../utils/helpers';
12-
import { getNotificationTypeIconColor } from '../../utils/icons';
1312
import { openNotification } from '../../utils/links';
1413
import { createNotificationHandler } from '../../utils/notifications/handlers';
1514
import { HoverButton } from '../primitives/HoverButton';
@@ -74,7 +73,7 @@ export const NotificationRow: FC<INotificationRow> = ({
7473
const handler = createNotificationHandler(notification);
7574

7675
const NotificationIcon = handler.getIcon(notification.subject);
77-
const iconColor = getNotificationTypeIconColor(notification.subject);
76+
const iconColor = handler.getIconColor(notification.subject);
7877

7978
const notificationType = formatForDisplay([
8079
notification.subject.state,

src/renderer/utils/__snapshots__/icons.test.ts.snap

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

src/renderer/utils/icons.test.ts

Lines changed: 1 addition & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -8,118 +8,15 @@ import {
88
} from '@primer/octicons-react';
99

1010
import { IconColor } from '../types';
11-
import type {
12-
GitifyPullRequestReview,
13-
StateType,
14-
Subject,
15-
SubjectType,
16-
} from '../typesGitHub';
11+
import type { GitifyPullRequestReview } from '../typesGitHub';
1712
import {
1813
getAuthMethodIcon,
1914
getDefaultUserIcon,
20-
getNotificationTypeIconColor,
2115
getPlatformIcon,
2216
getPullRequestReviewIcon,
2317
} from './icons';
2418

2519
describe('renderer/utils/icons.ts', () => {
26-
describe('getNotificationTypeIconColor', () => {
27-
it('should format the notification color for check suite', () => {
28-
expect(
29-
getNotificationTypeIconColor(
30-
createSubjectMock({
31-
type: 'CheckSuite',
32-
state: 'cancelled',
33-
}),
34-
),
35-
).toMatchSnapshot();
36-
37-
expect(
38-
getNotificationTypeIconColor(
39-
createSubjectMock({
40-
type: 'CheckSuite',
41-
state: 'failure',
42-
}),
43-
),
44-
).toMatchSnapshot();
45-
46-
expect(
47-
getNotificationTypeIconColor(
48-
createSubjectMock({
49-
type: 'CheckSuite',
50-
state: 'skipped',
51-
}),
52-
),
53-
).toMatchSnapshot();
54-
55-
expect(
56-
getNotificationTypeIconColor(
57-
createSubjectMock({
58-
type: 'CheckSuite',
59-
state: 'success',
60-
}),
61-
),
62-
).toMatchSnapshot();
63-
64-
expect(
65-
getNotificationTypeIconColor(
66-
createSubjectMock({
67-
type: 'CheckSuite',
68-
state: null,
69-
}),
70-
),
71-
).toMatchSnapshot();
72-
});
73-
74-
it('should format the notification color for state', () => {
75-
expect(
76-
getNotificationTypeIconColor(createSubjectMock({ state: 'ANSWERED' })),
77-
).toMatchSnapshot();
78-
79-
expect(
80-
getNotificationTypeIconColor(createSubjectMock({ state: 'closed' })),
81-
).toMatchSnapshot();
82-
83-
expect(
84-
getNotificationTypeIconColor(createSubjectMock({ state: 'completed' })),
85-
).toMatchSnapshot();
86-
87-
expect(
88-
getNotificationTypeIconColor(createSubjectMock({ state: 'draft' })),
89-
).toMatchSnapshot();
90-
91-
expect(
92-
getNotificationTypeIconColor(createSubjectMock({ state: 'merged' })),
93-
).toMatchSnapshot();
94-
95-
expect(
96-
getNotificationTypeIconColor(
97-
createSubjectMock({ state: 'not_planned' }),
98-
),
99-
).toMatchSnapshot();
100-
101-
expect(
102-
getNotificationTypeIconColor(createSubjectMock({ state: 'open' })),
103-
).toMatchSnapshot();
104-
105-
expect(
106-
getNotificationTypeIconColor(createSubjectMock({ state: 'reopened' })),
107-
).toMatchSnapshot();
108-
109-
expect(
110-
getNotificationTypeIconColor(createSubjectMock({ state: 'RESOLVED' })),
111-
).toMatchSnapshot();
112-
113-
expect(
114-
getNotificationTypeIconColor(
115-
createSubjectMock({
116-
state: 'something_else_unknown' as StateType,
117-
}),
118-
),
119-
).toMatchSnapshot();
120-
});
121-
});
122-
12320
describe('getPullRequestReviewIcon', () => {
12421
let mockReviewSingleReviewer: GitifyPullRequestReview;
12522
let mockReviewMultipleReviewer: GitifyPullRequestReview;
@@ -235,17 +132,3 @@ describe('renderer/utils/icons.ts', () => {
235132
expect(getDefaultUserIcon('User')).toBe(FeedPersonIcon);
236133
});
237134
});
238-
239-
function createSubjectMock(mocks: {
240-
title?: string;
241-
type?: SubjectType;
242-
state?: StateType;
243-
}): Subject {
244-
return {
245-
title: mocks.title ?? 'Mock Subject',
246-
type: mocks.type ?? ('Unknown' as SubjectType),
247-
state: mocks.state ?? ('Unknown' as StateType),
248-
url: null,
249-
latest_comment_url: null,
250-
};
251-
}

src/renderer/utils/icons.ts

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,9 @@ import {
1515
} from '@primer/octicons-react';
1616

1717
import { IconColor, type PullRequestApprovalIcon } from '../types';
18-
import type {
19-
GitifyPullRequestReview,
20-
Subject,
21-
UserType,
22-
} from '../typesGitHub';
18+
import type { GitifyPullRequestReview, UserType } from '../typesGitHub';
2319
import type { AuthMethod, PlatformType } from './auth/types';
2420

25-
export function getNotificationTypeIconColor(subject: Subject): IconColor {
26-
switch (subject.state) {
27-
case 'open':
28-
case 'reopened':
29-
case 'ANSWERED':
30-
case 'success':
31-
return IconColor.GREEN;
32-
case 'closed':
33-
case 'failure':
34-
return IconColor.RED;
35-
case 'completed':
36-
case 'RESOLVED':
37-
case 'merged':
38-
return IconColor.PURPLE;
39-
default:
40-
return IconColor.GRAY;
41-
}
42-
}
43-
4421
export function getPullRequestReviewIcon(
4522
review: GitifyPullRequestReview,
4623
): PullRequestApprovalIcon | null {

src/renderer/utils/notifications/handlers/checkSuite.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import type {
1717
Notification,
1818
Subject,
1919
} from '../../../typesGitHub';
20-
import type { NotificationTypeHandler } from './types';
20+
import { DefaultHandler } from './default';
2121

22-
class CheckSuiteHandler implements NotificationTypeHandler {
22+
class CheckSuiteHandler extends DefaultHandler {
2323
readonly type = 'CheckSuite';
2424

2525
async enrich(

src/renderer/utils/notifications/handlers/commit.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import type {
1313
} from '../../../typesGitHub';
1414
import { getCommit, getCommitComment } from '../../api/client';
1515
import { isStateFilteredOut } from '../filters/filter';
16-
import type { NotificationTypeHandler } from './types';
16+
import { DefaultHandler } from './default';
1717
import { getSubjectUser } from './utils';
1818

19-
class CommitHandler implements NotificationTypeHandler {
19+
class CommitHandler extends DefaultHandler {
2020
readonly type = 'Commit';
2121

2222
async enrich(

src/renderer/utils/notifications/handlers/default.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { createSubjectMock } from '../../../__mocks__/notifications-mocks';
22
import { partialMockNotification } from '../../../__mocks__/partial-mocks';
33
import { mockSettings } from '../../../__mocks__/state-mocks';
4+
import { IconColor } from '../../../types';
5+
import type { StateType } from '../../../typesGitHub';
46
import { defaultHandler } from './default';
57

68
describe('renderer/utils/notifications/handlers/default.ts', () => {
@@ -26,4 +28,30 @@ describe('renderer/utils/notifications/handlers/default.ts', () => {
2628
'QuestionIcon',
2729
);
2830
});
31+
32+
describe('getIconColor', () => {
33+
const cases: Array<[StateType | null, IconColor]> = [
34+
['open' as StateType, IconColor.GREEN],
35+
['reopened' as StateType, IconColor.GREEN],
36+
['ANSWERED' as StateType, IconColor.GREEN],
37+
['success' as StateType, IconColor.GREEN],
38+
['closed' as StateType, IconColor.RED],
39+
['failure' as StateType, IconColor.RED],
40+
['completed' as StateType, IconColor.PURPLE],
41+
['RESOLVED' as StateType, IconColor.PURPLE],
42+
['merged' as StateType, IconColor.PURPLE],
43+
['not_planned' as StateType, IconColor.GRAY],
44+
['draft' as StateType, IconColor.GRAY],
45+
['skipped' as StateType, IconColor.GRAY],
46+
['cancelled' as StateType, IconColor.GRAY],
47+
['unknown' as StateType, IconColor.GRAY],
48+
[null, IconColor.GRAY],
49+
[undefined, IconColor.GRAY],
50+
];
51+
52+
it.each(cases)('returns correct color for state %s', (state, expected) => {
53+
const subject = createSubjectMock({ state });
54+
expect(defaultHandler.getIconColor(subject)).toBe(expected);
55+
});
56+
});
2957
});

src/renderer/utils/notifications/handlers/default.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import type { OcticonProps } from '@primer/octicons-react';
44
import { QuestionIcon } from '@primer/octicons-react';
55

66
import type { SettingsState } from '../../../types';
7+
import { IconColor } from '../../../types';
78
import type {
89
GitifySubject,
910
Notification,
1011
Subject,
1112
} from '../../../typesGitHub';
1213
import type { NotificationTypeHandler } from './types';
1314

14-
class DefaultHandler implements NotificationTypeHandler {
15+
export class DefaultHandler implements NotificationTypeHandler {
1516
async enrich(
1617
_notification: Notification,
1718
_settings: SettingsState,
@@ -22,6 +23,25 @@ class DefaultHandler implements NotificationTypeHandler {
2223
getIcon(_subject: Subject): FC<OcticonProps> | null {
2324
return QuestionIcon;
2425
}
26+
27+
getIconColor(subject: Subject): IconColor {
28+
switch (subject.state) {
29+
case 'open':
30+
case 'reopened':
31+
case 'ANSWERED':
32+
case 'success':
33+
return IconColor.GREEN;
34+
case 'closed':
35+
case 'failure':
36+
return IconColor.RED;
37+
case 'completed':
38+
case 'RESOLVED':
39+
case 'merged':
40+
return IconColor.PURPLE;
41+
default:
42+
return IconColor.GRAY;
43+
}
44+
}
2545
}
2646

2747
export const defaultHandler = new DefaultHandler();

src/renderer/utils/notifications/handlers/discussion.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import type {
2121
} from '../../../typesGitHub';
2222
import { getLatestDiscussion } from '../../api/client';
2323
import { isStateFilteredOut } from '../filters/filter';
24-
import type { NotificationTypeHandler } from './types';
24+
import { DefaultHandler } from './default';
2525

26-
class DiscussionHandler implements NotificationTypeHandler {
26+
class DiscussionHandler extends DefaultHandler {
2727
readonly type = 'Discussion';
2828

2929
async enrich(

src/renderer/utils/notifications/handlers/issue.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ import type {
1818
} from '../../../typesGitHub';
1919
import { getIssue, getIssueOrPullRequestComment } from '../../api/client';
2020
import { isStateFilteredOut } from '../filters/filter';
21-
import type { NotificationTypeHandler } from './types';
21+
import { DefaultHandler } from './default';
2222
import { getSubjectUser } from './utils';
2323

24-
class IssueHandler implements NotificationTypeHandler {
24+
class IssueHandler extends DefaultHandler {
2525
readonly type = 'Issue';
2626

2727
async enrich(

0 commit comments

Comments
 (0)