Skip to content

Commit c0bf5f8

Browse files
authored
refactor(reason): separate into own file and use Record for mappings (#1036)
1 parent 9d1614a commit c0bf5f8

File tree

8 files changed

+224
-228
lines changed

8 files changed

+224
-228
lines changed

src/components/NotificationRow.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ import { AppContext } from '../context/App';
1717
import type { Notification } from '../typesGithub';
1818
import { openExternalLink } from '../utils/comms';
1919
import {
20-
formatReason,
2120
getNotificationTypeIcon,
2221
getNotificationTypeIconColor,
2322
} from '../utils/github-api';
2423
import { formatForDisplay, openInBrowser } from '../utils/helpers';
24+
import { formatReason } from '../utils/reason';
2525

2626
interface IProps {
2727
hostname: string;
@@ -128,7 +128,7 @@ export const NotificationRow: FC<IProps> = ({ notification, hostname }) => {
128128
</span>
129129
)}
130130
<span className="ml-1" title={reason.description}>
131-
{reason.type}
131+
{reason.title}
132132
</span>
133133
<span className="ml-1">{updatedAt}</span>
134134
</span>

src/types.ts

+5
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,8 @@ export type ErrorType =
8888
| 'NETWORK'
8989
| 'RATE_LIMITED'
9090
| 'UNKNOWN';
91+
92+
export interface FormattedReason {
93+
title: string;
94+
description: string;
95+
}

src/utils/__snapshots__/github-api.test.ts.snap

-112
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils/__snapshots__/reason.test.ts.snap

+113
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils/github-api.test.ts

+1-21
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,10 @@
1-
import type { Reason, StateType, Subject, SubjectType } from '../typesGithub';
1+
import type { StateType, Subject, SubjectType } from '../typesGithub';
22
import {
3-
formatReason,
43
getNotificationTypeIcon,
54
getNotificationTypeIconColor,
65
} from './github-api';
76

87
describe('utils/github-api.ts', () => {
9-
it('formatReason - should format the notification reason', () => {
10-
expect(formatReason('approval_requested')).toMatchSnapshot();
11-
expect(formatReason('assign')).toMatchSnapshot();
12-
expect(formatReason('author')).toMatchSnapshot();
13-
expect(formatReason('ci_activity')).toMatchSnapshot();
14-
expect(formatReason('comment')).toMatchSnapshot();
15-
expect(formatReason('invitation')).toMatchSnapshot();
16-
expect(formatReason('manual')).toMatchSnapshot();
17-
expect(formatReason('member_feature_requested')).toMatchSnapshot();
18-
expect(formatReason('mention')).toMatchSnapshot();
19-
expect(formatReason('review_requested')).toMatchSnapshot();
20-
expect(formatReason('security_advisory_credit')).toMatchSnapshot();
21-
expect(formatReason('security_alert')).toMatchSnapshot();
22-
expect(formatReason('state_change')).toMatchSnapshot();
23-
expect(formatReason('subscribed')).toMatchSnapshot();
24-
expect(formatReason('team_mention')).toMatchSnapshot();
25-
expect(formatReason('something_else_unknown' as Reason)).toMatchSnapshot();
26-
});
27-
288
describe('getNotificationTypeIcon - should get the notification type icon', () => {
299
expect(
3010
getNotificationTypeIcon(

src/utils/github-api.ts

+1-93
Original file line numberDiff line numberDiff line change
@@ -24,99 +24,7 @@ import {
2424
XIcon,
2525
} from '@primer/octicons-react';
2626
import type { FC } from 'react';
27-
import type { Reason, Subject } from '../typesGithub';
28-
29-
const DESCRIPTIONS = {
30-
APPROVAL_REQUESTED: 'You were requested to review and approve a deployment.',
31-
ASSIGN: 'You were assigned to the issue.',
32-
AUTHOR: 'You created the thread.',
33-
CI_ACTIVITY:
34-
'A GitHub Actions workflow run was triggered for your repository',
35-
COMMENT: 'You commented on the thread.',
36-
INVITATION: 'You accepted an invitation to contribute to the repository.',
37-
MANUAL: 'You subscribed to the thread (via an issue or pull request).',
38-
MEMBER_FEATURE_REQUESTED:
39-
'Organization members have requested to enable a feature such as Draft Pull Requests or CoPilot.',
40-
MENTION: 'You were specifically @mentioned in the content.',
41-
REVIEW_REQUESTED:
42-
"You, or a team you're a member of, were requested to review a pull request.",
43-
SECURITY_ADVISORY_CREDIT:
44-
'You were credited for contributing to a security advisory.',
45-
SECURITY_ALERT:
46-
'GitHub discovered a security vulnerability in your repository.',
47-
STATE_CHANGE:
48-
'You changed the thread state (for example, closing an issue or merging a pull request).',
49-
SUBSCRIBED: "You're watching the repository.",
50-
TEAM_MENTION: 'You were on a team that was mentioned.',
51-
UNKNOWN: 'The reason for this notification is not supported by the app.',
52-
};
53-
54-
export function formatReason(reason: Reason): {
55-
type: string;
56-
description: string;
57-
} {
58-
switch (reason) {
59-
case 'approval_requested':
60-
return {
61-
type: 'Approval Requested',
62-
description: DESCRIPTIONS.APPROVAL_REQUESTED,
63-
};
64-
case 'assign':
65-
return { type: 'Assigned', description: DESCRIPTIONS.ASSIGN };
66-
case 'author':
67-
return { type: 'Authored', description: DESCRIPTIONS.AUTHOR };
68-
case 'ci_activity':
69-
return {
70-
type: 'Workflow Run Completed',
71-
description: DESCRIPTIONS.CI_ACTIVITY,
72-
};
73-
case 'comment':
74-
return { type: 'Commented', description: DESCRIPTIONS.COMMENT };
75-
case 'invitation':
76-
return {
77-
type: 'Invitation Received',
78-
description: DESCRIPTIONS.INVITATION,
79-
};
80-
case 'manual':
81-
return { type: 'Updated', description: DESCRIPTIONS.MANUAL };
82-
case 'member_feature_requested':
83-
return {
84-
type: 'Member Feature Requested',
85-
description: DESCRIPTIONS.MEMBER_FEATURE_REQUESTED,
86-
};
87-
case 'mention':
88-
return { type: 'Mentioned', description: DESCRIPTIONS.MENTION };
89-
case 'review_requested':
90-
return {
91-
type: 'Review Requested',
92-
description: DESCRIPTIONS.REVIEW_REQUESTED,
93-
};
94-
case 'security_advisory_credit':
95-
return {
96-
type: 'Security Advisory Credit Recevied',
97-
description: DESCRIPTIONS.SECURITY_ADVISORY_CREDIT,
98-
};
99-
case 'security_alert':
100-
return {
101-
type: 'Security Alert Received',
102-
description: DESCRIPTIONS.SECURITY_ALERT,
103-
};
104-
case 'state_change':
105-
return {
106-
type: 'State Changed',
107-
description: DESCRIPTIONS.STATE_CHANGE,
108-
};
109-
case 'subscribed':
110-
return { type: 'Updated', description: DESCRIPTIONS.SUBSCRIBED };
111-
case 'team_mention':
112-
return {
113-
type: 'Team Mentioned',
114-
description: DESCRIPTIONS.TEAM_MENTION,
115-
};
116-
default:
117-
return { type: 'Unknown', description: DESCRIPTIONS.UNKNOWN };
118-
}
119-
}
27+
import type { Subject } from '../typesGithub';
12028

12129
export function getNotificationTypeIcon(subject: Subject): FC<OcticonProps> {
12230
switch (subject.type) {

0 commit comments

Comments
 (0)