Skip to content

Commit cbb9e9e

Browse files
committed
refactor: handler enhancements
Signed-off-by: Adam Setch <[email protected]>
1 parent c3a80f0 commit cbb9e9e

24 files changed

+106
-77
lines changed

src/renderer/components/notifications/NotificationRow.tsx

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { GroupBy, Opacity, Size } from '../../types';
88
import type { Notification } from '../../typesGitHub';
99
import { cn } from '../../utils/cn';
1010
import { isMarkAsDoneFeatureSupported } from '../../utils/features';
11-
import { formatForDisplay } from '../../utils/helpers';
1211
import { openNotification } from '../../utils/links';
1312
import { createNotificationHandler } from '../../utils/notifications/handlers';
1413
import { HoverButton } from '../primitives/HoverButton';
@@ -72,21 +71,11 @@ export const NotificationRow: FC<INotificationRow> = ({
7271

7372
const handler = createNotificationHandler(notification);
7473

75-
const NotificationIcon = handler.getIcon(notification.subject);
76-
const iconColor = handler.getIconColor(notification.subject);
77-
78-
const notificationType = formatForDisplay([
79-
notification.subject.state,
80-
notification.subject.type,
81-
]);
82-
83-
const notificationNumber = notification.subject?.number
84-
? `#${notification.subject.number}`
85-
: '';
86-
87-
const notificationTitle = notificationNumber
88-
? `${notification.subject.title} [${notificationNumber}]`
89-
: notification.subject.title;
74+
const NotificationIcon = handler.iconType(notification.subject);
75+
const iconColor = handler.iconColor(notification.subject);
76+
const notificationType = handler.formattedNotificationType(notification);
77+
const notificationNumber = handler.formattedNotificationNumber(notification);
78+
const notificationTitle = handler.formattedNotificationTitle(notification);
9079

9180
const groupByDate = settings.groupBy === GroupBy.DATE;
9281

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,15 @@ describe('renderer/utils/notifications/handlers/checkSuite.ts', () => {
136136
});
137137
});
138138

139-
it('getIcon', () => {
139+
it('iconType', () => {
140140
expect(
141-
checkSuiteHandler.getIcon(
141+
checkSuiteHandler.iconType(
142142
createSubjectMock({ type: 'CheckSuite', state: null }),
143143
).displayName,
144144
).toBe('RocketIcon');
145145

146146
expect(
147-
checkSuiteHandler.getIcon(
147+
checkSuiteHandler.iconType(
148148
createSubjectMock({
149149
type: 'CheckSuite',
150150
state: 'cancelled',
@@ -153,7 +153,7 @@ describe('renderer/utils/notifications/handlers/checkSuite.ts', () => {
153153
).toBe('StopIcon');
154154

155155
expect(
156-
checkSuiteHandler.getIcon(
156+
checkSuiteHandler.iconType(
157157
createSubjectMock({
158158
type: 'CheckSuite',
159159
state: 'failure',
@@ -162,7 +162,7 @@ describe('renderer/utils/notifications/handlers/checkSuite.ts', () => {
162162
).toBe('XIcon');
163163

164164
expect(
165-
checkSuiteHandler.getIcon(
165+
checkSuiteHandler.iconType(
166166
createSubjectMock({
167167
type: 'CheckSuite',
168168
state: 'skipped',
@@ -171,7 +171,7 @@ describe('renderer/utils/notifications/handlers/checkSuite.ts', () => {
171171
).toBe('SkipIcon');
172172

173173
expect(
174-
checkSuiteHandler.getIcon(
174+
checkSuiteHandler.iconType(
175175
createSubjectMock({
176176
type: 'CheckSuite',
177177
state: 'success',

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class CheckSuiteHandler extends DefaultHandler {
3838
return null;
3939
}
4040

41-
getIcon(subject: Subject): FC<OcticonProps> | null {
41+
iconType(subject: Subject): FC<OcticonProps> | null {
4242
switch (subject.state) {
4343
case 'cancelled':
4444
return StopIcon;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ describe('renderer/utils/notifications/handlers/commit.ts', () => {
9797
});
9898
});
9999

100-
it('getIcon', () => {
100+
it('iconType', () => {
101101
expect(
102-
commitHandler.getIcon(createSubjectMock({ type: 'Commit' })).displayName,
102+
commitHandler.iconType(createSubjectMock({ type: 'Commit' })).displayName,
103103
).toBe('GitCommitIcon');
104104
});
105105
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class CommitHandler extends DefaultHandler {
5555
};
5656
}
5757

58-
getIcon(_subject: Subject): FC<OcticonProps> | null {
58+
iconType(_subject: Subject): FC<OcticonProps> | null {
5959
return GitCommitIcon;
6060
}
6161
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ describe('renderer/utils/notifications/handlers/default.ts', () => {
2323
});
2424
});
2525

26-
it('getIcon', () => {
27-
expect(defaultHandler.getIcon(createSubjectMock({})).displayName).toBe(
26+
it('iconType', () => {
27+
expect(defaultHandler.iconType(createSubjectMock({})).displayName).toBe(
2828
'QuestionIcon',
2929
);
3030
});
3131

32-
describe('getIconColor', () => {
32+
describe('iconColor', () => {
3333
const cases: Array<[StateType | null, IconColor]> = [
3434
['open' as StateType, IconColor.GREEN],
3535
['reopened' as StateType, IconColor.GREEN],
@@ -51,7 +51,7 @@ describe('renderer/utils/notifications/handlers/default.ts', () => {
5151

5252
it.each(cases)('returns correct color for state %s', (state, expected) => {
5353
const subject = createSubjectMock({ state });
54-
expect(defaultHandler.getIconColor(subject)).toBe(expected);
54+
expect(defaultHandler.iconColor(subject)).toBe(expected);
5555
});
5656
});
5757
});

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,26 @@ import type {
99
GitifySubject,
1010
Notification,
1111
Subject,
12+
SubjectType,
1213
} from '../../../typesGitHub';
14+
import { formatForDisplay } from '../../helpers';
1315
import type { NotificationTypeHandler } from './types';
1416

1517
export class DefaultHandler implements NotificationTypeHandler {
18+
type?: SubjectType;
19+
1620
async enrich(
1721
_notification: Notification,
1822
_settings: SettingsState,
1923
): Promise<GitifySubject> {
2024
return null;
2125
}
2226

23-
getIcon(_subject: Subject): FC<OcticonProps> | null {
27+
iconType(_subject: Subject): FC<OcticonProps> | null {
2428
return QuestionIcon;
2529
}
2630

27-
getIconColor(subject: Subject): IconColor {
31+
iconColor(subject: Subject): IconColor {
2832
switch (subject.state) {
2933
case 'open':
3034
case 'reopened':
@@ -42,6 +46,26 @@ export class DefaultHandler implements NotificationTypeHandler {
4246
return IconColor.GRAY;
4347
}
4448
}
49+
50+
formattedNotificationType(notification: Notification): string {
51+
return formatForDisplay([
52+
notification.subject.state,
53+
notification.subject.type,
54+
]);
55+
}
56+
formattedNotificationNumber(notification: Notification): string {
57+
return notification.subject?.number
58+
? `#${notification.subject.number}`
59+
: '';
60+
}
61+
formattedNotificationTitle(notification: Notification): string {
62+
let title = notification.subject.title;
63+
64+
if (notification.subject?.number) {
65+
title = `${title} [${this.formattedNotificationNumber(notification)}]`;
66+
}
67+
return title;
68+
}
4569
}
4670

4771
export const defaultHandler = new DefaultHandler();

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,26 +279,26 @@ describe('renderer/utils/notifications/handlers/discussion.ts', () => {
279279
});
280280
});
281281

282-
it('getIcon', () => {
282+
it('iconType', () => {
283283
expect(
284-
discussionHandler.getIcon(createSubjectMock({ type: 'Discussion' }))
284+
discussionHandler.iconType(createSubjectMock({ type: 'Discussion' }))
285285
.displayName,
286286
).toBe('CommentDiscussionIcon');
287287

288288
expect(
289-
discussionHandler.getIcon(
289+
discussionHandler.iconType(
290290
createSubjectMock({ type: 'Discussion', state: 'DUPLICATE' }),
291291
).displayName,
292292
).toBe('DiscussionDuplicateIcon');
293293

294294
expect(
295-
discussionHandler.getIcon(
295+
discussionHandler.iconType(
296296
createSubjectMock({ type: 'Discussion', state: 'OUTDATED' }),
297297
).displayName,
298298
).toBe('DiscussionOutdatedIcon');
299299

300300
expect(
301-
discussionHandler.getIcon(
301+
discussionHandler.iconType(
302302
createSubjectMock({ type: 'Discussion', state: 'RESOLVED' }),
303303
).displayName,
304304
).toBe('DiscussionClosedIcon');

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class DiscussionHandler extends DefaultHandler {
7777
};
7878
}
7979

80-
getIcon(subject: Subject): FC<OcticonProps> | null {
80+
iconType(subject: Subject): FC<OcticonProps> | null {
8181
switch (subject.state) {
8282
case 'DUPLICATE':
8383
return DiscussionDuplicateIcon;

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -294,18 +294,19 @@ describe('renderer/utils/notifications/handlers/issue.ts', () => {
294294
});
295295
});
296296

297-
it('getIcon', () => {
297+
it('iconType', () => {
298298
expect(
299-
issueHandler.getIcon(createSubjectMock({ type: 'Issue' })).displayName,
299+
issueHandler.iconType(createSubjectMock({ type: 'Issue' })).displayName,
300300
).toBe('IssueOpenedIcon');
301301

302302
expect(
303-
issueHandler.getIcon(createSubjectMock({ type: 'Issue', state: 'draft' }))
304-
.displayName,
303+
issueHandler.iconType(
304+
createSubjectMock({ type: 'Issue', state: 'draft' }),
305+
).displayName,
305306
).toBe('IssueDraftIcon');
306307

307308
expect(
308-
issueHandler.getIcon(
309+
issueHandler.iconType(
309310
createSubjectMock({
310311
type: 'Issue',
311312
state: 'closed',
@@ -314,7 +315,7 @@ describe('renderer/utils/notifications/handlers/issue.ts', () => {
314315
).toBe('IssueClosedIcon');
315316

316317
expect(
317-
issueHandler.getIcon(
318+
issueHandler.iconType(
318319
createSubjectMock({
319320
type: 'Issue',
320321
state: 'completed',
@@ -323,7 +324,7 @@ describe('renderer/utils/notifications/handlers/issue.ts', () => {
323324
).toBe('IssueClosedIcon');
324325

325326
expect(
326-
issueHandler.getIcon(
327+
issueHandler.iconType(
327328
createSubjectMock({
328329
type: 'Issue',
329330
state: 'not_planned',
@@ -332,7 +333,7 @@ describe('renderer/utils/notifications/handlers/issue.ts', () => {
332333
).toBe('SkipIcon');
333334

334335
expect(
335-
issueHandler.getIcon(
336+
issueHandler.iconType(
336337
createSubjectMock({
337338
type: 'Issue',
338339
state: 'reopened',

0 commit comments

Comments
 (0)