Skip to content
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

fix: Avatar에서 에러와 긴 이미지 대응 #276

Merged
merged 2 commits into from
Apr 29, 2024
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
Binary file added src/assets/images/default-profile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions src/components/atoms/avatar/Avatar.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,18 @@ export const SizeSpecificationWithImage: Story = {
size: 50,
},
};

export const WithLongImage: Story = {
args: {
profileImage:
'https://mblogthumb-phinf.pstatic.net/MjAyMjEyMjZfMTgg/MDAxNjcyMDM3NzQ3ODM2.Sbq1QfmZo0iF88TxMv-pTcvxWQ-9Bygpl_xc7-y8jDIg.juEk5qCAI9YLs6koopdMyPoXnWdTsXdDA7ptSqMhZyUg.JPEG.planet-keepers/5269_20221226134032608.jpg?type=w800',
size: 40,
},
};

export const WithError: Story = {
args: {
profileImage: 'asdf',
size: 40,
},
};
34 changes: 22 additions & 12 deletions src/components/atoms/avatar/Avatar.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
import Image from 'next/image';
import type { SyntheticEvent } from 'react';
import Link from 'next/link';

import UserIcon from '@/assets/icons/user.svg';
import DefaultProfileImage from '@/assets/images/default-profile.png';

interface AvatarProps {
size?: number;
profileImage?: string;
href?: string;
}

export const Avatar = ({ size = 24, profileImage }: AvatarProps) => {
export const Avatar = ({ size = 24, profileImage, href }: AvatarProps) => {
const handleAddDefaultImg = (e: SyntheticEvent<HTMLImageElement, Event>) => {
e.currentTarget.src = DefaultProfileImage.src;
};

return (
<div
style={{ width: size, height: size }}
className="flex justify-center items-center rounded-xl overflow-hidden bg-white"
>
{!profileImage ? (
<UserIcon width={`calc(${size} / 1.5)`} height={`calc(${size} / 1.5)`} />
) : (
<Image src={profileImage} width={size} height={size} alt="profile_image" />
)}
</div>
<Link href={{ pathname: href }}>
<div
style={{ width: size, height: size }}
className="flex justify-center items-center rounded-xl overflow-hidden bg-white"
>
{!profileImage ? (
<UserIcon width={`calc(${size} / 1.5)`} height={`calc(${size} / 1.5)`} />
) : (
// eslint-disable-next-line @next/next/no-img-element
<img src={profileImage} width={size} height={size} alt="profile_image" onError={handleAddDefaultImg} />
)}
</div>
</Link>
Comment on lines +27 to +30
Copy link
Member Author

Choose a reason for hiding this comment

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

next/image를 사용하면 onError 하나 때문에 state로 말아서 넣어줘야 해서.. img를 택했는데 다들 의견이 궁금해유~

사용법 관련 블로그

Copy link
Member

Choose a reason for hiding this comment

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

이미지로 해도 좋을듯! 어처피 아바타에 들어가는거니까 괜춘치 않읆까~?

);
};
7 changes: 2 additions & 5 deletions src/components/molecules/comment/Comment.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use client';

import Image from 'next/image';
import Link from 'next/link';
import { m } from 'framer-motion';

import TrashIcon from '@/assets/icons/trash-icon.svg';
import { Typography } from '@/components';
import { Avatar, Typography } from '@/components';
import { convertTimeToElapsedTime } from '@/utils/date';

interface CommentProps {
Expand All @@ -23,9 +22,7 @@ interface CommentProps {
export const Comment = ({ commenter, content, writtenAt, isDeletable, onDelete }: CommentProps) => {
return (
<m.div className="flex gap-4xs" {...animate}>
<Link href={{ pathname: `/home/${commenter.username}` }}>
<Image src={commenter.image} width={50} height={50} alt="user_profile_image" className="rounded-full" />
</Link>
<Avatar profileImage={commenter.image} size={50} href={`/home/${commenter.username}`} />
<div className="w-full flex flex-col justify-between">
<div className="w-full flex justify-between">
<div className="flex gap-6xs items-center">
Expand Down
Loading