Skip to content

refactor: user type utils #1820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/renderer/components/avatars/AvatarWithFallback.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type React from 'react';
import { useState } from 'react';

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

import { type Link, Size } from '../../types';
import type { UserType } from '../../typesGitHub';
import { isNonHumanUser } from '../../utils/helpers';
import { getDefaultUserIcon } from '../../utils/icons';
import { isNonHumanUser } from '../../utils/notifications/filters/userType';

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

const isNonHuman = isNonHumanUser(userType);
const DefaultUserIcon = isNonHuman ? MarkGithubIcon : FeedPersonIcon;
const DefaultUserIcon = getDefaultUserIcon(userType);

// TODO explore using AnchoredOverlay component (https://primer.style/components/anchored-overlay/react/alpha) to render Avatar Card on hover
return (
Expand Down
9 changes: 0 additions & 9 deletions src/renderer/utils/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
getChevronDetails,
getPlatformFromHostname,
isEnterpriseServerHost,
isNonHumanUser,
} from './helpers';

describe('renderer/utils/helpers.ts', () => {
Expand Down Expand Up @@ -553,12 +552,4 @@ describe('renderer/utils/helpers.ts', () => {
});
});
});

it('isNonHumanUser', () => {
expect(isNonHumanUser('User')).toBe(false);
expect(isNonHumanUser('EnterpriseUserAccount')).toBe(false);
expect(isNonHumanUser('Bot')).toBe(true);
expect(isNonHumanUser('Organization')).toBe(true);
expect(isNonHumanUser('Mannequin')).toBe(true);
});
});
6 changes: 1 addition & 5 deletions src/renderer/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {

import { logError, logWarn } from '../../shared/logger';
import type { Chevron, Hostname, Link } from '../types';
import type { Notification, UserType } from '../typesGitHub';
import type { Notification } from '../typesGitHub';
import { getHtmlUrl, getLatestDiscussion } from './api/client';
import type { PlatformType } from './auth/types';
import { Constants } from './constants';
Expand Down Expand Up @@ -210,7 +210,3 @@ export function getChevronDetails(
label: `Show ${type} notifications`,
};
}

export function isNonHumanUser(type: UserType): boolean {
return type === 'Bot' || type === 'Organization' || type === 'Mannequin';
}
18 changes: 17 additions & 1 deletion src/renderer/utils/icons.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { CheckIcon, CommentIcon, FileDiffIcon } from '@primer/octicons-react';
import {
CheckIcon,
CommentIcon,
FeedPersonIcon,
FileDiffIcon,
MarkGithubIcon,
OrganizationIcon,
} from '@primer/octicons-react';
import { IconColor } from '../types';
import type {
GitifyPullRequestReview,
Expand All @@ -8,6 +15,7 @@ import type {
} from '../typesGitHub';
import {
getAuthMethodIcon,
getDefaultUserIcon,
getNotificationTypeIcon,
getNotificationTypeIconColor,
getPlatformIcon,
Expand Down Expand Up @@ -412,6 +420,14 @@ describe('renderer/utils/icons.ts', () => {

expect(getPlatformIcon('GitHub Enterprise Server')).toMatchSnapshot();
});

describe('getDefaultUserIcon', () => {
expect(getDefaultUserIcon('Bot')).toBe(MarkGithubIcon);
expect(getDefaultUserIcon('EnterpriseUserAccount')).toBe(FeedPersonIcon);
expect(getDefaultUserIcon('Mannequin')).toBe(MarkGithubIcon);
expect(getDefaultUserIcon('Organization')).toBe(OrganizationIcon);
expect(getDefaultUserIcon('User')).toBe(FeedPersonIcon);
});
});

function createSubjectMock(mocks: {
Expand Down
20 changes: 19 additions & 1 deletion src/renderer/utils/icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
DiscussionClosedIcon,
DiscussionDuplicateIcon,
DiscussionOutdatedIcon,
FeedPersonIcon,
FileDiffIcon,
GitCommitIcon,
GitMergeIcon,
Expand All @@ -21,6 +22,7 @@ import {
MailIcon,
MarkGithubIcon,
type OcticonProps,
OrganizationIcon,
PersonIcon,
QuestionIcon,
RocketIcon,
Expand All @@ -32,7 +34,11 @@ import {
} from '@primer/octicons-react';
import type { FC } from 'react';
import { IconColor, type PullRequestApprovalIcon } from '../types';
import type { GitifyPullRequestReview, Subject } from '../typesGitHub';
import type {
GitifyPullRequestReview,
Subject,
UserType,
} from '../typesGitHub';
import type { AuthMethod, PlatformType } from './auth/types';

export function getNotificationTypeIcon(subject: Subject): FC<OcticonProps> {
Expand Down Expand Up @@ -180,3 +186,15 @@ export function getPlatformIcon(
return MarkGithubIcon;
}
}

export function getDefaultUserIcon(userType: UserType) {
switch (userType) {
case 'Bot':
case 'Mannequin':
return MarkGithubIcon;
case 'Organization':
return OrganizationIcon;
default:
return FeedPersonIcon;
}
}
15 changes: 15 additions & 0 deletions src/renderer/utils/notifications/filters/userType.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { isNonHumanUser } from './userType';

describe('renderer/utils/notifications/filters/userType.ts', () => {
afterEach(() => {
jest.clearAllMocks();
});

it('isNonHumanUser', () => {
expect(isNonHumanUser('User')).toBe(false);
expect(isNonHumanUser('EnterpriseUserAccount')).toBe(false);
expect(isNonHumanUser('Bot')).toBe(true);
expect(isNonHumanUser('Organization')).toBe(true);
expect(isNonHumanUser('Mannequin')).toBe(true);
});
});
4 changes: 4 additions & 0 deletions src/renderer/utils/notifications/filters/userType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,7 @@ export function filterNotificationByUserType(

return notification.subject?.user?.type === userType;
}

export function isNonHumanUser(type: UserType): boolean {
return type === 'Bot' || type === 'Organization' || type === 'Mannequin';
}
2 changes: 1 addition & 1 deletion src/renderer/utils/subject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ export async function getLatestReviewForReviewers(

// Find the most recent review for each reviewer
const latestReviews: PullRequestReview[] = [];
const sortedReviews = prReviews.data.reverse();
const sortedReviews = prReviews.data.reverse();
for (const prReview of sortedReviews) {
const reviewerFound = latestReviews.find(
(review) => review.user.login === prReview.user.login,
Expand Down