Skip to content

Commit

Permalink
feat: avatar component
Browse files Browse the repository at this point in the history
  • Loading branch information
setchy committed Jun 19, 2024
1 parent 0027d84 commit e159718
Show file tree
Hide file tree
Showing 7 changed files with 331 additions and 28 deletions.
38 changes: 38 additions & 0 deletions src/components/Avatar.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { MarkGithubIcon } from '@primer/octicons-react';
import { render } from '@testing-library/react';
import { Avatar, type IAvatar } from './Avatar';

describe('components/Avatar.tsx', () => {
it('should render small avatar', () => {
const props: IAvatar = {
defaultIcon: MarkGithubIcon,
title: 'test',
url: 'https://avatars.githubusercontent.com/u/583231?v=4',
size: 'small',
};
const tree = render(<Avatar {...props} />);
expect(tree).toMatchSnapshot();
});

it('should render medium avatar', () => {
const props: IAvatar = {
defaultIcon: MarkGithubIcon,
title: 'test',
url: 'https://avatars.githubusercontent.com/u/583231?v=4',
size: 'medium',
};
const tree = render(<Avatar {...props} />);
expect(tree).toMatchSnapshot();
});

it('should render default icon when no url', () => {
const props: IAvatar = {
defaultIcon: MarkGithubIcon,
title: 'test',
url: null,
size: 'small',
};
const tree = render(<Avatar {...props} />);
expect(tree).toMatchSnapshot();
});
});
32 changes: 32 additions & 0 deletions src/components/Avatar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { Icon } from '@primer/octicons-react';
import type { FC } from 'react';
import { cn } from '../utils/cn';

export interface IAvatar {
title: string;
url: string | null;
size: 'small' | 'medium';
defaultIcon: Icon;
}

export const Avatar: FC<IAvatar> = (props: IAvatar) => {
if (props.url) {
return (
<img
className={cn(
'cursor-pointer rounded-full object-cover',
props.size === 'small' ? 'size-4' : 'size-5',
)}
src={props.url}
alt={`${props.title}'s avatar`}
/>
);
}

return (
<props.defaultIcon
size={props.size === 'small' ? 14 : 18}
className="text-gray-500 dark:text-gray-300"
/>
);
};
9 changes: 5 additions & 4 deletions src/components/NotificationRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
} from '../utils/icons';
import { openNotification, openUserProfile } from '../utils/links';
import { formatReason } from '../utils/reason';
import { Avatar } from './Avatar';
import { PillButton } from './buttons/PillButton';

interface INotificationRow {
Expand Down Expand Up @@ -141,11 +142,11 @@ export const NotificationRow: FC<INotificationRow> = ({
}}
className="flex-shrink-0"
>
<img
className="size-4 cursor-pointer rounded-full object-cover"
src={notification.subject.user.avatar_url}
<Avatar
title={notification.subject.user.login}
alt={`${notification.subject.user.login}'s avatar`}
url={notification.subject.user.avatar_url}
size="small"
defaultIcon={FeedPersonIcon}
/>
</button>
) : (
Expand Down
16 changes: 7 additions & 9 deletions src/components/RepositoryNotifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { type FC, useCallback, useContext, useState } from 'react';
import { AppContext } from '../context/App';
import type { Notification } from '../typesGitHub';
import { openRepository } from '../utils/links';
import { Avatar } from './Avatar';
import { NotificationRow } from './NotificationRow';

interface IRepositoryNotifications {
Expand Down Expand Up @@ -55,15 +56,12 @@ export const RepositoryNotifications: FC<IRepositoryNotifications> = ({
onClick={toggleRepositoryNotifications}
>
<div className="mt-0 flex flex-1 space-x-3 overflow-hidden overflow-ellipsis whitespace-nowrap text-sm font-medium">
{avatarUrl ? (
<img
className="size-5 rounded"
src={avatarUrl}
alt={`${repoName}'s avatar`}
/>
) : (
<MarkGithubIcon size={18} />
)}
<Avatar
title={repoName}
url={avatarUrl}
size="medium"
defaultIcon={MarkGithubIcon}
/>
<span
className="cursor-pointer truncate opacity-90"
onClick={() => openRepository(repoNotifications[0].repository)}
Expand Down
238 changes: 238 additions & 0 deletions src/components/__snapshots__/Avatar.test.tsx.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e159718

Please sign in to comment.