Skip to content

feat: avatar with fallback icons component #1763

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 3 commits into from
Jan 23, 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
67 changes: 67 additions & 0 deletions src/renderer/components/avatars/AvatarWithFallback.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { render, screen } from '@testing-library/react';

import { type Link, Size } from '../../types';
import {
AvatarWithFallback,
type IAvatarWithFallback,
} from './AvatarWithFallback';

describe('renderer/components/avatars/AvatarWithFallback.tsx', () => {
const props: IAvatarWithFallback = {
src: 'https://avatars.githubusercontent.com/u/133795385?s=200&v=4' as Link,
alt: 'gitify-app',
name: '@gitify-app',
size: Size.MEDIUM,
userType: 'User',
};

it('should render avatar - human user', () => {
const tree = render(<AvatarWithFallback {...props} />);
expect(tree).toMatchSnapshot();
});

it('should render avatar - non-human user', () => {
const tree = render(
<AvatarWithFallback {...props} userType={'Organization'} />,
);
expect(tree).toMatchSnapshot();
});

it('renders the fallback icon when no src url - human user', () => {
const tree = render(<AvatarWithFallback {...props} src={null} />);

expect(tree).toMatchSnapshot();
});

it('renders the fallback icon when no src url - non human user', () => {
const tree = render(
<AvatarWithFallback {...props} src={null} userType="Bot" />,
);

expect(tree).toMatchSnapshot();
});

it('renders the fallback icon when the image fails to load (isBroken = true) - human user', () => {
render(<AvatarWithFallback {...props} />);

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

// Simulate an error event on the image element
avatar.dispatchEvent(new Event('error'));

expect(screen.getByTestId('avatar')).toMatchSnapshot();
});

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

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

// Simulate an error event on the image element
avatar.dispatchEvent(new Event('error'));

expect(screen.getByTestId('avatar')).toMatchSnapshot();
});
});
54 changes: 54 additions & 0 deletions src/renderer/components/avatars/AvatarWithFallback.tsx
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a huge fan of components like these -- nice work!

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import type React from 'react';
import { useState } from 'react';

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

import { type Link, Size } from '../../types';
import type { UserType } from '../../typesGitHub';
import { isNonHumanUser } from '../../utils/helpers';

export interface IAvatarWithFallback {
src?: Link;
alt?: string;
name?: string;
size?: number;
userType?: UserType;
}

export const AvatarWithFallback: React.FC<IAvatarWithFallback> = ({
src,
alt,
name,
size = Size.MEDIUM,
userType = 'User',
}) => {
const [isBroken, setIsBroken] = useState(false);

const isNonHuman = isNonHumanUser(userType);
return (
<Stack
direction="horizontal"
align="center"
gap="condensed"
data-testid="avatar"
>
{!src || isBroken ? (
isNonHuman ? (
<MarkGithubIcon size={size} />
) : (
<FeedPersonIcon size={size} />
)
) : (
<Avatar
src={src}
alt={alt}
size={size}
square={isNonHuman}
onError={() => setIsBroken(true)}
/>
)}
{name && <Text>{name}</Text>}
</Stack>
);
};
Loading
Loading