Profile.tsx79 lines, 2.4 KB
| 1 | import { useInfiniteQuery, useQuery } from '@tanstack/react-query' |
| 2 | import { useParams } from 'react-router' |
| 3 | import { Avatar } from '../../components/ui/Avatar' |
| 4 | import { Failed, Notice, NotFound } from '../../components/ui/Notice' |
| 5 | import { |
| 6 | ApiError, |
| 7 | getSession, |
| 8 | request, |
| 9 | type Profile as P, |
| 10 | } from '../../lib/api' |
| 11 | import { month } from '../../lib/time' |
| 12 | import { PostList } from '../feed/PostList' |
| 13 | import { postsOptions } from '../feed/posts' |
| 14 | import { Settings } from '../../components/Settings' |
| 15 | import { FollowButton } from './FollowButton' |
| 16 | |
| 17 | export function Profile() { |
| 18 | const { handle = '' } = useParams() |
| 19 | const mine = getSession()?.handle === handle |
| 20 | const user = useQuery({ |
| 21 | queryKey: ['user', handle], |
| 22 | queryFn: () => request<P>(`/users/${handle}`), |
| 23 | }) |
| 24 | const posts = useInfiniteQuery( |
| 25 | postsOptions(`/users/${handle}/posts`, `@${handle}`), |
| 26 | ) |
| 27 | if (user.error instanceof ApiError && user.error.status === 404) { |
| 28 | return <NotFound /> |
| 29 | } |
| 30 | if (user.isError) { |
| 31 | return <Failed error={user.error} retry={user.refetch} /> |
| 32 | } |
| 33 | const p = user.data |
| 34 | const counts = p && [ |
| 35 | [p.posts, 'posts'], |
| 36 | [p.followers, 'followers'], |
| 37 | [p.following, 'following'], |
| 38 | ] |
| 39 | |
| 40 | return ( |
| 41 | <> |
| 42 | <h1 className="bar">Profile</h1> |
| 43 | <header className="border-b border-line px-4 py-6"> |
| 44 | <div className="flex items-center gap-4"> |
| 45 | <Avatar handle={handle} size={64} /> |
| 46 | <div className="min-w-0 flex-1"> |
| 47 | <p className="truncate text-xl font-semibold">@{handle}</p> |
| 48 | <p className="h-5 text-sm text-mute"> |
| 49 | {p && `Joined ${month(p.joined)}`} |
| 50 | </p> |
| 51 | </div> |
| 52 | {p && !mine && ( |
| 53 | <FollowButton |
| 54 | handle={handle} |
| 55 | following={p.followed_by_me} |
| 56 | /> |
| 57 | )} |
| 58 | </div> |
| 59 | <ul className="mt-5 flex h-5 gap-5 text-sm text-mute"> |
| 60 | {counts?.map(([n, label]) => ( |
| 61 | <li key={label}> |
| 62 | <b className="font-semibold text-ink tabular-nums"> |
| 63 | {n.toLocaleString('en')} |
| 64 | </b>{' '} |
| 65 | {label} |
| 66 | </li> |
| 67 | ))} |
| 68 | </ul> |
| 69 | {mine && ( |
| 70 | <Settings |
| 71 | className="mt-5 flex gap-2 sm:hidden" |
| 72 | button="btn-quiet flex items-center gap-2" |
| 73 | /> |
| 74 | )} |
| 75 | </header> |
| 76 | <PostList query={posts} empty={<Notice title="No posts yet" />} /> |
| 77 | </> |
| 78 | ) |
| 79 | } |