Skip to content

Commit a0958df

Browse files
committed
feat: avatar with fallback icons component
Signed-off-by: Adam Setch <[email protected]>
1 parent c275519 commit a0958df

File tree

9 files changed

+469
-121
lines changed

9 files changed

+469
-121
lines changed

src/renderer/components/avatars/AvatarWithFallback.test.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('renderer/components/avatars/AvatarWithFallback.tsx', () => {
1212
alt: 'gitify-app',
1313
name: '@gitify-app',
1414
size: Size.MEDIUM,
15-
isNonHuman: false,
15+
userType: 'User',
1616
};
1717

1818
it('should render avatar - human user', () => {
@@ -21,7 +21,23 @@ describe('renderer/components/avatars/AvatarWithFallback.tsx', () => {
2121
});
2222

2323
it('should render avatar - non-human user', () => {
24-
const tree = render(<AvatarWithFallback {...props} isNonHuman />);
24+
const tree = render(
25+
<AvatarWithFallback {...props} userType={'Organization'} />,
26+
);
27+
expect(tree).toMatchSnapshot();
28+
});
29+
30+
it('renders the fallback icon when no src url - human user', () => {
31+
const tree = render(<AvatarWithFallback {...props} src={null} />);
32+
33+
expect(tree).toMatchSnapshot();
34+
});
35+
36+
it('renders the fallback icon when no src url - non human user', () => {
37+
const tree = render(
38+
<AvatarWithFallback {...props} src={null} userType="Bot" />,
39+
);
40+
2541
expect(tree).toMatchSnapshot();
2642
});
2743

@@ -38,7 +54,7 @@ describe('renderer/components/avatars/AvatarWithFallback.tsx', () => {
3854
});
3955

4056
it('renders the fallback icon when the image fails to load (isBroken = true) - non human user', () => {
41-
render(<AvatarWithFallback {...props} isNonHuman />);
57+
render(<AvatarWithFallback {...props} userType={'Bot'} />);
4258

4359
// Find the avatar element by its alt text
4460
const avatar = screen.getByAltText('gitify-app') as HTMLImageElement;

src/renderer/components/avatars/AvatarWithFallback.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,35 @@ import { FeedPersonIcon, MarkGithubIcon } from '@primer/octicons-react';
55
import { Avatar, Stack, Text } from '@primer/react';
66

77
import { type Link, Size } from '../../types';
8+
import type { UserType } from '../../typesGitHub';
9+
import { isNonHumanUser } from '../../utils/helpers';
810

911
export interface IAvatarWithFallback {
10-
src: Link;
11-
alt: string;
12+
src?: Link;
13+
alt?: string;
1214
name?: string;
1315
size?: number;
14-
isNonHuman?: boolean;
16+
userType?: UserType;
1517
}
1618

1719
export const AvatarWithFallback: React.FC<IAvatarWithFallback> = ({
1820
src,
1921
alt,
2022
name,
2123
size = Size.MEDIUM,
22-
isNonHuman = false,
24+
userType = 'User',
2325
}) => {
2426
const [isBroken, setIsBroken] = useState(false);
2527

28+
const isNonHuman = isNonHumanUser(userType);
2629
return (
2730
<Stack
2831
direction="horizontal"
2932
align="center"
3033
gap="condensed"
3134
data-testid="avatar"
3235
>
33-
{isBroken ? (
36+
{!src || isBroken ? (
3437
isNonHuman ? (
3538
<MarkGithubIcon size={size} />
3639
) : (

src/renderer/components/avatars/__snapshots__/AvatarWithFallback.test.tsx.snap

Lines changed: 254 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/renderer/components/notifications/NotificationFooter.tsx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import type { FC, MouseEvent } from 'react';
22

3-
import { FeedPersonIcon, MarkGithubIcon } from '@primer/octicons-react';
43
import { Box, RelativeTime, Stack, Text } from '@primer/react';
54

6-
import { IconColor, Opacity, Size } from '../../types';
5+
import { Opacity, Size } from '../../types';
76
import type { Notification } from '../../typesGitHub';
87
import { cn } from '../../utils/cn';
9-
import { isNonHumanUser } from '../../utils/helpers';
108
import { openUserProfile } from '../../utils/links';
119
import { formatReason } from '../../utils/reason';
1210
import { AvatarWithFallback } from '../avatars/AvatarWithFallback';
@@ -43,18 +41,19 @@ export const NotificationFooter: FC<INotificationFooter> = ({
4341
src={notification.subject.user.avatar_url}
4442
alt={notification.subject.user.login}
4543
size={Size.SMALL}
46-
isNonHuman={isNonHumanUser(notification.subject.user.type)}
44+
userType={notification.subject.user.type}
4745
/>
4846
</Box>
4947
) : (
50-
<>
51-
{notification.subject.type === 'RepositoryDependabotAlertsThread' ||
52-
notification.subject.type === 'RepositoryVulnerabilityAlert' ? (
53-
<MarkGithubIcon size={Size.SMALL} />
54-
) : (
55-
<FeedPersonIcon size={Size.SMALL} className={IconColor.GRAY} />
56-
)}
57-
</>
48+
<AvatarWithFallback
49+
size={Size.SMALL}
50+
userType={
51+
notification.subject.type === 'RepositoryDependabotAlertsThread' ||
52+
notification.subject.type === 'RepositoryVulnerabilityAlert'
53+
? 'Bot'
54+
: 'User'
55+
}
56+
/>
5857
)}
5958

6059
<Stack direction="horizontal" gap="none">

0 commit comments

Comments
 (0)