|
| 1 | +import { useQuery } from "@tanstack/react-query"; |
| 2 | +import { Link } from "@tanstack/react-router"; |
| 3 | +import { IdCardIcon, RotateCwIcon, UserRoundXIcon } from "lucide-react"; |
| 4 | +import React from "react"; |
| 5 | +import { useFormatter, useTranslations } from "use-intl"; |
| 6 | + |
| 7 | +import { useSessionQuery } from "@/tanstack/auth/session-query"; |
| 8 | +import { RouteMessages } from "@/tanstack/i18n/route-messages"; |
| 9 | +import { Reputation } from "@/tanstack/reactions/reputation"; |
| 10 | +import { userPresenceQueryOptions } from "@/views/presence/presence-query"; |
| 11 | +import { userCoverStyle } from "@/views/profile/images/cover-style"; |
| 12 | +import { |
| 13 | + isProfileNotFound, |
| 14 | + type UserProfile, |
| 15 | + userProfileQueryOptions, |
| 16 | +} from "@/views/profile/profile-query"; |
| 17 | + |
| 18 | +import { Avatar } from "./avatar"; |
| 19 | +import { RoleFormat } from "./role-format"; |
| 20 | +import { Button } from "./ui/button"; |
| 21 | +import { |
| 22 | + Empty, |
| 23 | + EmptyContent, |
| 24 | + EmptyDescription, |
| 25 | + EmptyHeader, |
| 26 | + EmptyMedia, |
| 27 | + EmptyTitle, |
| 28 | +} from "./ui/empty"; |
| 29 | +import { UserCoverImage } from "./user-cover-image"; |
| 30 | +import { UserPresenceStatus } from "./user-presence-status"; |
| 31 | +import { UserPreviewSkeleton } from "./user-preview-skeleton"; |
| 32 | +import { userProfileHref } from "./user-profile-href"; |
| 33 | + |
| 34 | +const USER_PREVIEW_NAMESPACES = ["core.global", "core.profile"] as const; |
| 35 | +const SHOWN_SECONDARY_ROLES = 2; |
| 36 | + |
| 37 | +const PreviewPresence = ({ userId }: { userId: number }) => { |
| 38 | + const t = useTranslations("core.profile"); |
| 39 | + const { data: session, isPending: isSessionPending } = useSessionQuery(); |
| 40 | + const { data } = useQuery({ |
| 41 | + ...userPresenceQueryOptions({ |
| 42 | + userId, |
| 43 | + viewerId: session?.user?.id ?? null, |
| 44 | + }), |
| 45 | + enabled: !isSessionPending, |
| 46 | + }); |
| 47 | + |
| 48 | + if (!data) return null; |
| 49 | + |
| 50 | + return ( |
| 51 | + <li className="flex min-w-0 items-center"> |
| 52 | + <span className="sr-only">{t("presence")}: </span> |
| 53 | + <UserPresenceStatus presence={data} variant="full" /> |
| 54 | + </li> |
| 55 | + ); |
| 56 | +}; |
| 57 | + |
| 58 | +const PreviewRoles = ({ user }: { user: UserProfile }) => { |
| 59 | + const t = useTranslations("core.profile"); |
| 60 | + const tGlobal = useTranslations("core.global"); |
| 61 | + const shown = user.secondaryRoles.slice(0, SHOWN_SECONDARY_ROLES); |
| 62 | + const hidden = user.secondaryRoles.length - shown.length; |
| 63 | + |
| 64 | + return ( |
| 65 | + <div className="flex min-w-0 flex-col gap-1"> |
| 66 | + <span className="sr-only">{t("role")}</span> |
| 67 | + <ul className="flex min-w-0 flex-wrap items-center gap-x-3 gap-y-1 text-sm"> |
| 68 | + <li className="min-w-0"> |
| 69 | + <RoleFormat role={user.role} /> |
| 70 | + </li> |
| 71 | + {shown.map(role => ( |
| 72 | + <li className="min-w-0" key={role.id}> |
| 73 | + <RoleFormat className="font-normal" role={role} /> |
| 74 | + </li> |
| 75 | + ))} |
| 76 | + {hidden > 0 ? ( |
| 77 | + <li className="text-muted-foreground"> |
| 78 | + {tGlobal("more_count", { count: hidden })} |
| 79 | + </li> |
| 80 | + ) : null} |
| 81 | + </ul> |
| 82 | + </div> |
| 83 | + ); |
| 84 | +}; |
| 85 | + |
| 86 | +const PreviewProfile = ({ user }: { user: UserProfile }) => { |
| 87 | + const t = useTranslations("core.profile"); |
| 88 | + const format = useFormatter(); |
| 89 | + const joinedAt = new Date(user.createdAt); |
| 90 | + |
| 91 | + return ( |
| 92 | + <div className="flex flex-col"> |
| 93 | + <div |
| 94 | + className="bg-muted h-20 w-full" |
| 95 | + data-slot="user-preview-cover" |
| 96 | + style={userCoverStyle(user.avatarColor)} |
| 97 | + > |
| 98 | + <UserCoverImage url={user.coverUrl} /> |
| 99 | + </div> |
| 100 | + |
| 101 | + <div className="flex flex-col gap-3 px-4 pb-4"> |
| 102 | + <div className="-mt-8 flex items-end justify-between gap-3"> |
| 103 | + <Avatar |
| 104 | + alt="" |
| 105 | + className="border-popover size-16 shrink-0 rounded-full border-4" |
| 106 | + size={64} |
| 107 | + user={user} |
| 108 | + /> |
| 109 | + {typeof user.reputation === "number" ? ( |
| 110 | + <Reputation value={user.reputation} /> |
| 111 | + ) : null} |
| 112 | + </div> |
| 113 | + |
| 114 | + <div className="flex min-w-0 flex-col"> |
| 115 | + <Link |
| 116 | + className="text-foreground focus-visible:ring-ring/50 truncate rounded-sm text-base font-semibold underline-offset-4 outline-none hover:underline focus-visible:ring-3" |
| 117 | + to={userProfileHref(user.nameCode)} |
| 118 | + > |
| 119 | + {user.name} |
| 120 | + </Link> |
| 121 | + <p className="text-muted-foreground truncate text-sm"> |
| 122 | + @{user.nameCode} |
| 123 | + </p> |
| 124 | + </div> |
| 125 | + |
| 126 | + {user.headline ? ( |
| 127 | + <p className="line-clamp-2 text-sm leading-relaxed text-pretty"> |
| 128 | + <span className="sr-only">{t("headline")}: </span> |
| 129 | + {user.headline} |
| 130 | + </p> |
| 131 | + ) : null} |
| 132 | + |
| 133 | + <PreviewRoles user={user} /> |
| 134 | + |
| 135 | + <ul className="text-muted-foreground flex flex-wrap items-center gap-x-4 gap-y-1 text-sm"> |
| 136 | + <li> |
| 137 | + {t("preview.joined", { |
| 138 | + date: format.dateTime(joinedAt, { |
| 139 | + month: "short", |
| 140 | + year: "numeric", |
| 141 | + }), |
| 142 | + })} |
| 143 | + </li> |
| 144 | + <PreviewPresence userId={user.id} /> |
| 145 | + </ul> |
| 146 | + </div> |
| 147 | + </div> |
| 148 | + ); |
| 149 | +}; |
| 150 | + |
| 151 | +const PreviewBody = ({ nameCode }: { nameCode: string }) => { |
| 152 | + const t = useTranslations("core.profile"); |
| 153 | + const query = useQuery(userProfileQueryOptions({ nameCode })); |
| 154 | + |
| 155 | + if (query.isPending) return <UserPreviewSkeleton />; |
| 156 | + |
| 157 | + if (query.isError) { |
| 158 | + const notFound = isProfileNotFound(query.error); |
| 159 | + |
| 160 | + return ( |
| 161 | + <Empty className="p-6" role="alert"> |
| 162 | + <EmptyHeader> |
| 163 | + <EmptyMedia variant="icon"> |
| 164 | + {notFound ? ( |
| 165 | + <UserRoundXIcon aria-hidden="true" /> |
| 166 | + ) : ( |
| 167 | + <IdCardIcon aria-hidden="true" /> |
| 168 | + )} |
| 169 | + </EmptyMedia> |
| 170 | + <EmptyTitle> |
| 171 | + {notFound ? t("notFound.title") : t("preview.load_error.title")} |
| 172 | + </EmptyTitle> |
| 173 | + <EmptyDescription> |
| 174 | + {notFound ? t("notFound.desc") : t("preview.load_error.desc")} |
| 175 | + </EmptyDescription> |
| 176 | + </EmptyHeader> |
| 177 | + {notFound ? null : ( |
| 178 | + <EmptyContent> |
| 179 | + <Button |
| 180 | + isLoading={query.isFetching} |
| 181 | + onClick={() => void query.refetch()} |
| 182 | + size="sm" |
| 183 | + variant="outline" |
| 184 | + > |
| 185 | + <RotateCwIcon aria-hidden="true" /> |
| 186 | + {t("preview.retry")} |
| 187 | + </Button> |
| 188 | + </EmptyContent> |
| 189 | + )} |
| 190 | + </Empty> |
| 191 | + ); |
| 192 | + } |
| 193 | + |
| 194 | + return <PreviewProfile user={query.data} />; |
| 195 | +}; |
| 196 | + |
| 197 | +/** |
| 198 | + * The user preview card's content: loads its messages and the member's public |
| 199 | + * profile, showing a skeleton until both arrive. Lazy-loaded by |
| 200 | + * `UserPreview` when the card opens. |
| 201 | + */ |
| 202 | +export const UserPreviewCard = ({ nameCode }: { nameCode: string }) => ( |
| 203 | + <React.Suspense fallback={<UserPreviewSkeleton />}> |
| 204 | + <RouteMessages namespaces={USER_PREVIEW_NAMESPACES}> |
| 205 | + <PreviewBody nameCode={nameCode} /> |
| 206 | + </RouteMessages> |
| 207 | + </React.Suspense> |
| 208 | +); |
0 commit comments