Skip to content

Commit 10c6fda

Browse files
authored
refactor: user type utils (#1820)
* refactor: user type utils Signed-off-by: Adam Setch <[email protected]> * refactor: user type utils Signed-off-by: Adam Setch <[email protected]> --------- Signed-off-by: Adam Setch <[email protected]>
1 parent ea7c82b commit 10c6fda

File tree

8 files changed

+60
-20
lines changed

8 files changed

+60
-20
lines changed

src/renderer/components/avatars/AvatarWithFallback.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import type React from 'react';
22
import { useState } from 'react';
33

4-
import { FeedPersonIcon, MarkGithubIcon } from '@primer/octicons-react';
54
import { Avatar, Stack, Truncate } from '@primer/react';
65

76
import { type Link, Size } from '../../types';
87
import type { UserType } from '../../typesGitHub';
9-
import { isNonHumanUser } from '../../utils/helpers';
8+
import { getDefaultUserIcon } from '../../utils/icons';
9+
import { isNonHumanUser } from '../../utils/notifications/filters/userType';
1010

1111
export interface IAvatarWithFallback {
1212
src?: Link;
@@ -26,7 +26,7 @@ export const AvatarWithFallback: React.FC<IAvatarWithFallback> = ({
2626
const [isBroken, setIsBroken] = useState(false);
2727

2828
const isNonHuman = isNonHumanUser(userType);
29-
const DefaultUserIcon = isNonHuman ? MarkGithubIcon : FeedPersonIcon;
29+
const DefaultUserIcon = getDefaultUserIcon(userType);
3030

3131
// TODO explore using AnchoredOverlay component (https://primer.style/components/anchored-overlay/react/alpha) to render Avatar Card on hover
3232
return (

src/renderer/utils/helpers.test.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
getChevronDetails,
2424
getPlatformFromHostname,
2525
isEnterpriseServerHost,
26-
isNonHumanUser,
2726
} from './helpers';
2827

2928
describe('renderer/utils/helpers.ts', () => {
@@ -553,12 +552,4 @@ describe('renderer/utils/helpers.ts', () => {
553552
});
554553
});
555554
});
556-
557-
it('isNonHumanUser', () => {
558-
expect(isNonHumanUser('User')).toBe(false);
559-
expect(isNonHumanUser('EnterpriseUserAccount')).toBe(false);
560-
expect(isNonHumanUser('Bot')).toBe(true);
561-
expect(isNonHumanUser('Organization')).toBe(true);
562-
expect(isNonHumanUser('Mannequin')).toBe(true);
563-
});
564555
});

src/renderer/utils/helpers.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66

77
import { logError, logWarn } from '../../shared/logger';
88
import type { Chevron, Hostname, Link } from '../types';
9-
import type { Notification, UserType } from '../typesGitHub';
9+
import type { Notification } from '../typesGitHub';
1010
import { getHtmlUrl, getLatestDiscussion } from './api/client';
1111
import type { PlatformType } from './auth/types';
1212
import { Constants } from './constants';
@@ -210,7 +210,3 @@ export function getChevronDetails(
210210
label: `Show ${type} notifications`,
211211
};
212212
}
213-
214-
export function isNonHumanUser(type: UserType): boolean {
215-
return type === 'Bot' || type === 'Organization' || type === 'Mannequin';
216-
}

src/renderer/utils/icons.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { CheckIcon, CommentIcon, FileDiffIcon } from '@primer/octicons-react';
1+
import {
2+
CheckIcon,
3+
CommentIcon,
4+
FeedPersonIcon,
5+
FileDiffIcon,
6+
MarkGithubIcon,
7+
OrganizationIcon,
8+
} from '@primer/octicons-react';
29
import { IconColor } from '../types';
310
import type {
411
GitifyPullRequestReview,
@@ -8,6 +15,7 @@ import type {
815
} from '../typesGitHub';
916
import {
1017
getAuthMethodIcon,
18+
getDefaultUserIcon,
1119
getNotificationTypeIcon,
1220
getNotificationTypeIconColor,
1321
getPlatformIcon,
@@ -412,6 +420,14 @@ describe('renderer/utils/icons.ts', () => {
412420

413421
expect(getPlatformIcon('GitHub Enterprise Server')).toMatchSnapshot();
414422
});
423+
424+
describe('getDefaultUserIcon', () => {
425+
expect(getDefaultUserIcon('Bot')).toBe(MarkGithubIcon);
426+
expect(getDefaultUserIcon('EnterpriseUserAccount')).toBe(FeedPersonIcon);
427+
expect(getDefaultUserIcon('Mannequin')).toBe(MarkGithubIcon);
428+
expect(getDefaultUserIcon('Organization')).toBe(OrganizationIcon);
429+
expect(getDefaultUserIcon('User')).toBe(FeedPersonIcon);
430+
});
415431
});
416432

417433
function createSubjectMock(mocks: {

src/renderer/utils/icons.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
DiscussionClosedIcon,
88
DiscussionDuplicateIcon,
99
DiscussionOutdatedIcon,
10+
FeedPersonIcon,
1011
FileDiffIcon,
1112
GitCommitIcon,
1213
GitMergeIcon,
@@ -21,6 +22,7 @@ import {
2122
MailIcon,
2223
MarkGithubIcon,
2324
type OcticonProps,
25+
OrganizationIcon,
2426
PersonIcon,
2527
QuestionIcon,
2628
RocketIcon,
@@ -32,7 +34,11 @@ import {
3234
} from '@primer/octicons-react';
3335
import type { FC } from 'react';
3436
import { IconColor, type PullRequestApprovalIcon } from '../types';
35-
import type { GitifyPullRequestReview, Subject } from '../typesGitHub';
37+
import type {
38+
GitifyPullRequestReview,
39+
Subject,
40+
UserType,
41+
} from '../typesGitHub';
3642
import type { AuthMethod, PlatformType } from './auth/types';
3743

3844
export function getNotificationTypeIcon(subject: Subject): FC<OcticonProps> {
@@ -180,3 +186,15 @@ export function getPlatformIcon(
180186
return MarkGithubIcon;
181187
}
182188
}
189+
190+
export function getDefaultUserIcon(userType: UserType) {
191+
switch (userType) {
192+
case 'Bot':
193+
case 'Mannequin':
194+
return MarkGithubIcon;
195+
case 'Organization':
196+
return OrganizationIcon;
197+
default:
198+
return FeedPersonIcon;
199+
}
200+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { isNonHumanUser } from './userType';
2+
3+
describe('renderer/utils/notifications/filters/userType.ts', () => {
4+
afterEach(() => {
5+
jest.clearAllMocks();
6+
});
7+
8+
it('isNonHumanUser', () => {
9+
expect(isNonHumanUser('User')).toBe(false);
10+
expect(isNonHumanUser('EnterpriseUserAccount')).toBe(false);
11+
expect(isNonHumanUser('Bot')).toBe(true);
12+
expect(isNonHumanUser('Organization')).toBe(true);
13+
expect(isNonHumanUser('Mannequin')).toBe(true);
14+
});
15+
});

src/renderer/utils/notifications/filters/userType.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,7 @@ export function filterNotificationByUserType(
6060

6161
return notification.subject?.user?.type === userType;
6262
}
63+
64+
export function isNonHumanUser(type: UserType): boolean {
65+
return type === 'Bot' || type === 'Organization' || type === 'Mannequin';
66+
}

src/renderer/utils/subject.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ export async function getLatestReviewForReviewers(
297297

298298
// Find the most recent review for each reviewer
299299
const latestReviews: PullRequestReview[] = [];
300-
const sortedReviews = prReviews.data.reverse();
300+
const sortedReviews = prReviews.data.reverse();
301301
for (const prReview of sortedReviews) {
302302
const reviewerFound = latestReviews.find(
303303
(review) => review.user.login === prReview.user.login,

0 commit comments

Comments
 (0)