Skip to content

Commit 72b724b

Browse files
authored
Merge pull request #19 from wafflestudio/11-feature-profile-info
[PR] 프로필 정보 영역 컴포넌트 생성
2 parents ac6b70b + 6ad3eb0 commit 72b724b

File tree

6 files changed

+182
-0
lines changed

6 files changed

+182
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import * as React from 'react'
2+
3+
import { Button } from '@/shared/ui/button'
4+
import { cn } from '@/shared/lib/utils'
5+
6+
type FollowButtonProps = {
7+
className?: string
8+
defaultIsFollowing?: boolean
9+
onToggle?: (nextIsFollowing: boolean) => Promise<void> | void
10+
}
11+
12+
const FOLLOW_BUTTON_LABEL = {
13+
follow: '팔로우',
14+
following: '팔로잉',
15+
}
16+
17+
const FOLLOW_BUTTON_PRIMARY_CLASSNAME =
18+
'bg-[#0095f6] text-white hover:bg-[#0095f6]/90'
19+
20+
export function FollowButton({
21+
className,
22+
defaultIsFollowing = false,
23+
onToggle,
24+
}: FollowButtonProps) {
25+
const [isFollowing, setIsFollowing] = React.useState(defaultIsFollowing)
26+
const [isPending, startTransition] = React.useTransition()
27+
28+
const handleClick = () => {
29+
const nextIsFollowing = !isFollowing
30+
31+
startTransition(() => {
32+
void (async () => {
33+
try {
34+
await onToggle?.(nextIsFollowing)
35+
setIsFollowing(nextIsFollowing)
36+
} catch {
37+
return
38+
}
39+
})()
40+
})
41+
}
42+
43+
const label = isFollowing
44+
? FOLLOW_BUTTON_LABEL.following
45+
: FOLLOW_BUTTON_LABEL.follow
46+
47+
return (
48+
<Button
49+
type="button"
50+
aria-pressed={isFollowing}
51+
disabled={isPending}
52+
onClick={handleClick}
53+
className={cn(
54+
'h-8 rounded-md px-4 text-sm font-semibold',
55+
isFollowing
56+
? 'border border-gray-300 bg-white text-gray-900 hover:bg-gray-50'
57+
: FOLLOW_BUTTON_PRIMARY_CLASSNAME,
58+
className
59+
)}
60+
>
61+
{label}
62+
</Button>
63+
)
64+
}

src/shared/lib/numberFormat.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const LOCALE_KO_KR = 'ko-KR'
2+
3+
export function numberFormat(value: number, locale: string = LOCALE_KO_KR) {
4+
return value.toLocaleString(locale)
5+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { cn } from '@/shared/lib/utils'
2+
3+
import { AVATAR_SIZE_CLASSNAME } from './constants'
4+
5+
type ProfileAvatarProps = {
6+
avatarUrl?: string | null
7+
nickname: string
8+
}
9+
10+
export function ProfileAvatar({ avatarUrl, nickname }: ProfileAvatarProps) {
11+
const initial = nickname.trim().slice(0, 1).toUpperCase()
12+
13+
if (avatarUrl) {
14+
return (
15+
<img
16+
src={avatarUrl}
17+
alt={`${nickname} 프로필 이미지`}
18+
className={cn(AVATAR_SIZE_CLASSNAME, 'rounded-full object-cover')}
19+
/>
20+
)
21+
}
22+
23+
return (
24+
<div
25+
aria-label={`${nickname} 프로필 이미지`}
26+
className={cn(
27+
AVATAR_SIZE_CLASSNAME,
28+
'flex items-center justify-center rounded-full bg-gray-200 text-5xl font-semibold text-gray-500'
29+
)}
30+
>
31+
{initial || '?'}
32+
</div>
33+
)
34+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { FollowButton } from '@/features/follow-user/ui/FollowButton'
2+
import { cn } from '@/shared/lib/utils'
3+
import { PROFILE_CONTAINER_CLASSNAME } from './constants'
4+
import { ProfileAvatar } from './ProfileAvatar'
5+
import { ProfileStat } from './ProfileStat'
6+
7+
interface ProfileHeaderProps {
8+
className?: string
9+
avatarUrl?: string | null
10+
nickname: string
11+
bio?: string | null
12+
postsCount: number
13+
followersCount: number
14+
followingCount: number
15+
defaultIsFollowing?: boolean
16+
onFollowToggle?: (nextIsFollowing: boolean) => Promise<void> | void
17+
}
18+
19+
export function ProfileHeader({
20+
className,
21+
avatarUrl,
22+
nickname,
23+
bio,
24+
postsCount,
25+
followersCount,
26+
followingCount,
27+
defaultIsFollowing = false,
28+
onFollowToggle,
29+
}: ProfileHeaderProps) {
30+
return (
31+
<section className={cn(PROFILE_CONTAINER_CLASSNAME, className)}>
32+
<div className="flex gap-8 md:gap-16">
33+
<div className="flex w-[180px] justify-center">
34+
<ProfileAvatar avatarUrl={avatarUrl} nickname={nickname} />
35+
</div>
36+
37+
<div className="min-w-0 flex-1">
38+
<div className="flex flex-wrap items-center gap-4">
39+
<h1 className="text-xl font-normal text-gray-900">{nickname}</h1>
40+
<FollowButton
41+
defaultIsFollowing={defaultIsFollowing}
42+
onToggle={onFollowToggle}
43+
/>
44+
</div>
45+
46+
<div className="mt-6 flex gap-10 text-base">
47+
<ProfileStat label="게시물" value={postsCount} />
48+
<ProfileStat label="팔로워" value={followersCount} />
49+
<ProfileStat label="팔로잉" value={followingCount} />
50+
</div>
51+
52+
{bio ? (
53+
<p className="mt-5 text-sm leading-relaxed whitespace-pre-line text-gray-900">
54+
{bio}
55+
</p>
56+
) : null}
57+
</div>
58+
</div>
59+
</section>
60+
)
61+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { numberFormat } from '@/shared/lib/numberFormat'
2+
3+
type ProfileStatProps = {
4+
label: string
5+
value: number
6+
}
7+
8+
export function ProfileStat({ label, value }: ProfileStatProps) {
9+
return (
10+
<div className="flex items-baseline gap-1">
11+
<span className="font-semibold text-gray-900">{numberFormat(value)}</span>
12+
<span className="text-gray-600">{label}</span>
13+
</div>
14+
)
15+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const PROFILE_CONTAINER_CLASSNAME =
2+
'mx-auto w-full max-w-[935px] px-4 py-8'
3+
export const AVATAR_SIZE_CLASSNAME = 'size-[150px]'

0 commit comments

Comments
 (0)