Skip to content

Commit 86932c9

Browse files
committed
refactor(handlers): use notification instead of subject
Signed-off-by: Adam Setch <[email protected]>
1 parent fdbbad6 commit 86932c9

File tree

14 files changed

+171
-141
lines changed

14 files changed

+171
-141
lines changed

src/renderer/components/notifications/NotificationRow.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ export const NotificationRow: FC<INotificationRow> = ({
6363
};
6464

6565
const handler = createNotificationHandler(notification);
66-
const NotificationIcon = handler.iconType(notification);
67-
const iconColor = handler.iconColor(notification);
68-
const notificationType = handler.formattedNotificationType(notification);
69-
const notificationNumber = handler.formattedNotificationNumber(notification);
70-
const notificationTitle = handler.formattedNotificationTitle(notification);
66+
const NotificationIcon = handler.iconType();
67+
const iconColor = handler.iconColor();
68+
const notificationType = handler.formattedNotificationType();
69+
const notificationNumber = handler.formattedNotificationNumber();
70+
const notificationTitle = handler.formattedNotificationTitle();
7171

7272
const groupByDate = settings.groupBy === GroupBy.DATE;
7373

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@ import type {
1717
Notification,
1818
} from '../../../typesGitHub';
1919
import { DefaultHandler } from './default';
20+
import type { NotificationTypeHandler } from './types';
2021

2122
class CheckSuiteHandler extends DefaultHandler {
2223
readonly type = 'CheckSuite';
2324

24-
async enrich(
25-
notification: Notification,
26-
_settings: SettingsState,
27-
): Promise<GitifySubject> {
28-
const state = getCheckSuiteAttributes(notification)?.status;
25+
async enrich(_settings: SettingsState): Promise<GitifySubject> {
26+
const state = getCheckSuiteAttributes(this.notification)?.status;
2927

3028
if (state) {
3129
return {
@@ -37,8 +35,8 @@ class CheckSuiteHandler extends DefaultHandler {
3735
return null;
3836
}
3937

40-
iconType(notification: Notification): FC<OcticonProps> | null {
41-
switch (notification.subject.state) {
38+
iconType(): FC<OcticonProps> | null {
39+
switch (this.notification.subject.state) {
4240
case 'cancelled':
4341
return StopIcon;
4442
case 'failure':
@@ -53,7 +51,11 @@ class CheckSuiteHandler extends DefaultHandler {
5351
}
5452
}
5553

56-
export const checkSuiteHandler = new CheckSuiteHandler();
54+
export function createCheckSuiteHandler(
55+
notification: Notification,
56+
): NotificationTypeHandler {
57+
return new CheckSuiteHandler(notification);
58+
}
5759

5860
/**
5961
* Ideally we would be using a GitHub API to fetch the CheckSuite / WorkflowRun state,

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

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

1819
class CommitHandler extends DefaultHandler {
1920
readonly type = 'Commit';
2021

21-
async enrich(
22-
notification: Notification,
23-
settings: SettingsState,
24-
): Promise<GitifySubject> {
22+
async enrich(settings: SettingsState): Promise<GitifySubject> {
2523
const commitState: StateType = null; // Commit notifications are stateless
2624

2725
// Return early if this notification would be hidden by filters
@@ -31,18 +29,21 @@ class CommitHandler extends DefaultHandler {
3129

3230
let user: User;
3331

34-
if (notification.subject.latest_comment_url) {
32+
if (this.notification.subject.latest_comment_url) {
3533
const commitComment = (
3634
await getCommitComment(
37-
notification.subject.latest_comment_url,
38-
notification.account.token,
35+
this.notification.subject.latest_comment_url,
36+
this.notification.account.token,
3937
)
4038
).data;
4139

4240
user = commitComment.user;
4341
} else {
4442
const commit = (
45-
await getCommit(notification.subject.url, notification.account.token)
43+
await getCommit(
44+
this.notification.subject.url,
45+
this.notification.account.token,
46+
)
4647
).data;
4748

4849
user = commit.author;
@@ -54,9 +55,13 @@ class CommitHandler extends DefaultHandler {
5455
};
5556
}
5657

57-
iconType(_notification: Notification): FC<OcticonProps> | null {
58+
iconType(): FC<OcticonProps> | null {
5859
return GitCommitIcon;
5960
}
6061
}
6162

62-
export const commitHandler = new CommitHandler();
63+
export function createCommitHandler(
64+
notification: Notification,
65+
): NotificationTypeHandler {
66+
return new CommitHandler(notification);
67+
}

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

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,22 @@ import { formatForDisplay } from './utils';
1515

1616
export class DefaultHandler implements NotificationTypeHandler {
1717
type?: SubjectType;
18+
notification: Notification;
1819

19-
async enrich(
20-
_notification: Notification,
21-
_settings: SettingsState,
22-
): Promise<GitifySubject> {
20+
constructor(notification: Notification) {
21+
this.notification = notification;
22+
}
23+
24+
async enrich(_settings: SettingsState): Promise<GitifySubject> {
2325
return null;
2426
}
2527

26-
iconType(_notification: Notification): FC<OcticonProps> | null {
28+
iconType(): FC<OcticonProps> | null {
2729
return QuestionIcon;
2830
}
2931

30-
iconColor(notification: Notification): IconColor {
31-
switch (notification.subject.state) {
32+
iconColor(): IconColor {
33+
switch (this.notification.subject.state) {
3234
case 'open':
3335
case 'reopened':
3436
case 'ANSWERED':
@@ -46,25 +48,29 @@ export class DefaultHandler implements NotificationTypeHandler {
4648
}
4749
}
4850

49-
formattedNotificationType(notification: Notification): string {
51+
formattedNotificationType(): string {
5052
return formatForDisplay([
51-
notification.subject.state,
52-
notification.subject.type,
53+
this.notification.subject.state,
54+
this.notification.subject.type,
5355
]);
5456
}
55-
formattedNotificationNumber(notification: Notification): string {
56-
return notification.subject?.number
57-
? `#${notification.subject.number}`
57+
formattedNotificationNumber(): string {
58+
return this.notification.subject?.number
59+
? `#${this.notification.subject.number}`
5860
: '';
5961
}
60-
formattedNotificationTitle(notification: Notification): string {
61-
let title = notification.subject.title;
62+
formattedNotificationTitle(): string {
63+
let title = this.notification.subject.title;
6264

63-
if (notification.subject?.number) {
64-
title = `${title} [${this.formattedNotificationNumber(notification)}]`;
65+
if (this.notification.subject?.number) {
66+
title = `${title} [${this.formattedNotificationNumber()}]`;
6567
}
6668
return title;
6769
}
6870
}
6971

70-
export const defaultHandler = new DefaultHandler();
72+
export function createDefaultHandler(
73+
notification: Notification,
74+
): NotificationTypeHandler {
75+
return new DefaultHandler(notification);
76+
}

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

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

2526
class DiscussionHandler extends DefaultHandler {
2627
readonly type = 'Discussion';
2728

28-
async enrich(
29-
notification: Notification,
30-
settings: SettingsState,
31-
): Promise<GitifySubject> {
32-
const discussion = await getLatestDiscussion(notification);
29+
async enrich(settings: SettingsState): Promise<GitifySubject> {
30+
const discussion = await getLatestDiscussion(this.notification);
3331
let discussionState: DiscussionStateType = 'OPEN';
3432

3533
if (discussion) {
@@ -48,7 +46,7 @@ class DiscussionHandler extends DefaultHandler {
4846
}
4947

5048
const latestDiscussionComment = getClosestDiscussionCommentOrReply(
51-
notification,
49+
this.notification,
5250
discussion.comments.nodes,
5351
);
5452

@@ -76,8 +74,8 @@ class DiscussionHandler extends DefaultHandler {
7674
};
7775
}
7876

79-
iconType(notification: Notification): FC<OcticonProps> | null {
80-
switch (notification.subject.state) {
77+
iconType(): FC<OcticonProps> | null {
78+
switch (this.notification.subject.state) {
8179
case 'DUPLICATE':
8280
return DiscussionDuplicateIcon;
8381
case 'OUTDATED':
@@ -90,7 +88,11 @@ class DiscussionHandler extends DefaultHandler {
9088
}
9189
}
9290

93-
export const discussionHandler = new DiscussionHandler();
91+
export function createDiscussionHandler(
92+
notification: Notification,
93+
): NotificationTypeHandler {
94+
return new DiscussionHandler(notification);
95+
}
9496

9597
export function getClosestDiscussionCommentOrReply(
9698
notification: Notification,
Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,42 @@
11
import type { Notification } from '../../../typesGitHub';
2-
import { checkSuiteHandler } from './checkSuite';
3-
import { commitHandler } from './commit';
4-
import { defaultHandler } from './default';
5-
import { discussionHandler } from './discussion';
6-
import { issueHandler } from './issue';
7-
import { pullRequestHandler } from './pullRequest';
8-
import { releaseHandler } from './release';
9-
import { repositoryDependabotAlertsThreadHandler } from './repositoryDependabotAlertsThread';
10-
import { repositoryInvitationHandler } from './repositoryInvitation';
11-
import { repositoryVulnerabilityAlertHandler } from './repositoryVulnerabilityAlert';
2+
import { createCheckSuiteHandler } from './checkSuite';
3+
import { createCommitHandler } from './commit';
4+
import { createDefaultHandler } from './default';
5+
import { createDiscussionHandler } from './discussion';
6+
import { createIssueHandler } from './issue';
7+
import { createPullRequestHandler } from './pullRequest';
8+
import { createReleaseHandler } from './release';
9+
import { createRepositoryDependabotAlertsThreadHandler } from './repositoryDependabotAlertsThread';
10+
import { createRepositoryInvitationHandler } from './repositoryInvitation';
11+
import { createRepositoryVulnerabilityAlertHandler } from './repositoryVulnerabilityAlert';
1212
import type { NotificationTypeHandler } from './types';
13-
import { workflowRunHandler } from './workflowRun';
13+
import { createWorkflowRunHandler } from './workflowRun';
1414

1515
export function createNotificationHandler(
1616
notification: Notification,
1717
): NotificationTypeHandler {
1818
switch (notification.subject.type) {
1919
case 'CheckSuite':
20-
return checkSuiteHandler;
20+
return createCheckSuiteHandler(notification);
2121
case 'Commit':
22-
return commitHandler;
22+
return createCommitHandler(notification);
2323
case 'Discussion':
24-
return discussionHandler;
24+
return createDiscussionHandler(notification);
2525
case 'Issue':
26-
return issueHandler;
26+
return createIssueHandler(notification);
2727
case 'PullRequest':
28-
return pullRequestHandler;
28+
return createPullRequestHandler(notification);
2929
case 'Release':
30-
return releaseHandler;
30+
return createReleaseHandler(notification);
3131
case 'RepositoryDependabotAlertsThread':
32-
return repositoryDependabotAlertsThreadHandler;
32+
return createRepositoryDependabotAlertsThreadHandler(notification);
3333
case 'RepositoryInvitation':
34-
return repositoryInvitationHandler;
34+
return createRepositoryInvitationHandler(notification);
3535
case 'RepositoryVulnerabilityAlert':
36-
return repositoryVulnerabilityAlertHandler;
36+
return createRepositoryVulnerabilityAlertHandler(notification);
3737
case 'WorkflowRun':
38-
return workflowRunHandler;
38+
return createWorkflowRunHandler(notification);
3939
default:
40-
return defaultHandler;
40+
return createDefaultHandler(notification);
4141
}
4242
}
43-
44-
export const handlers = {
45-
checkSuiteHandler,
46-
commitHandler,
47-
discussionHandler,
48-
issueHandler,
49-
pullRequestHandler,
50-
releaseHandler,
51-
repositoryDependabotAlertsThreadHandler,
52-
repositoryInvitationHandler,
53-
repositoryVulnerabilityAlertHandler,
54-
workflowRunHandler,
55-
defaultHandler,
56-
};

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@ import type { GitifySubject, Notification, User } from '../../../typesGitHub';
1414
import { getIssue, getIssueOrPullRequestComment } from '../../api/client';
1515
import { isStateFilteredOut } from '../filters/filter';
1616
import { DefaultHandler } from './default';
17+
import type { NotificationTypeHandler } from './types';
1718
import { getSubjectUser } from './utils';
1819

1920
class IssueHandler extends DefaultHandler {
2021
readonly type = 'Issue';
2122

22-
async enrich(
23-
notification: Notification,
24-
settings: SettingsState,
25-
): Promise<GitifySubject> {
23+
async enrich(settings: SettingsState): Promise<GitifySubject> {
2624
const issue = (
27-
await getIssue(notification.subject.url, notification.account.token)
25+
await getIssue(
26+
this.notification.subject.url,
27+
this.notification.account.token,
28+
)
2829
).data;
2930

3031
const issueState = issue.state_reason ?? issue.state;
@@ -36,11 +37,11 @@ class IssueHandler extends DefaultHandler {
3637

3738
let issueCommentUser: User;
3839

39-
if (notification.subject.latest_comment_url) {
40+
if (this.notification.subject.latest_comment_url) {
4041
const issueComment = (
4142
await getIssueOrPullRequestComment(
42-
notification.subject.latest_comment_url,
43-
notification.account.token,
43+
this.notification.subject.latest_comment_url,
44+
this.notification.account.token,
4445
)
4546
).data;
4647
issueCommentUser = issueComment.user;
@@ -56,8 +57,8 @@ class IssueHandler extends DefaultHandler {
5657
};
5758
}
5859

59-
iconType(notification: Notification): FC<OcticonProps> | null {
60-
switch (notification.subject.state) {
60+
iconType(): FC<OcticonProps> | null {
61+
switch (this.notification.subject.state) {
6162
case 'draft':
6263
return IssueDraftIcon;
6364
case 'closed':
@@ -73,4 +74,8 @@ class IssueHandler extends DefaultHandler {
7374
}
7475
}
7576

76-
export const issueHandler = new IssueHandler();
77+
export function createIssueHandler(
78+
notification: Notification,
79+
): NotificationTypeHandler {
80+
return new IssueHandler(notification);
81+
}

0 commit comments

Comments
 (0)