Skip to content

Commit c267638

Browse files
committed
feat(profile): ✨ add hover profile preview card for usernames and avatars
Hovering or focusing a linked username or avatar opens a card with the member's cover, avatar, name, group, headline, reputation, join date and presence. The card only receives the nameCode and loads the public profile itself when it opens (shared cache with the profile page), with a skeleton while it loads and an empty state for missing or failed profiles. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CnAiFbA6iCLzpJ2F55jPdT
1 parent 5f6b84e commit c267638

15 files changed

Lines changed: 536 additions & 33 deletions

File tree

34 KB
Loading

‎apps/web/content/docs/ui/user-format.mdx‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ description: Show a member's name in their role color, with the role's icon or e
44
icon: UserCheck
55
---
66

7+
import { ImgDocs } from "@/components/fumadocs/img";
8+
import previewCard from "./user-format-preview.webp";
9+
710
`UserFormat` prints a member's name the way the rest of the site does. Use it
811
anywhere a name shows up: article bylines, tables, comments, notifications.
912

@@ -89,6 +92,45 @@ Leave `link` off when the name sits inside another link or a button, such as a
8992
table row that already opens something. Nested links are invalid HTML and
9093
confuse screen readers.
9194

95+
## Profile preview
96+
97+
A linked name opens a profile preview card when someone rests the pointer on it
98+
or tabs to it: cover, avatar, name, `@nameCode`, role, headline, reputation,
99+
join date and online status. On touch screens, a tap just opens the profile.
100+
101+
<ImgDocs alt="A profile preview card under a linked username, with cover, avatar, name, role, reputation and join date" src={previewCard} />
102+
103+
You don't pass the card any data. When it opens, it loads the member's public
104+
profile from `GET /users/profile/{nameCode}` and shows a skeleton until the
105+
answer arrives. The card shares its cache with the profile page, so opening a
106+
card and then clicking the name renders the profile right away. Nothing is
107+
fetched for names nobody hovers.
108+
109+
The preview is on by default for every `UserFormat` with `link`. Turn it off
110+
with `preview={false}` when the name already sits inside another hover card:
111+
112+
```tsx
113+
<UserFormat format link preview={false} user={user} />
114+
```
115+
116+
### Preview on an avatar
117+
118+
Wrap any element in `UserPreview` to give it the same card. It renders a link
119+
to the profile, so only pass the `nameCode`:
120+
121+
```tsx
122+
import { Avatar } from '@vitnode/core/components/avatar'
123+
import { UserPreview } from '@vitnode/core/components/user-preview'
124+
125+
<UserPreview decorative nameCode={user.nameCode}>
126+
<Avatar alt="" size={32} user={user} />
127+
</UserPreview>
128+
```
129+
130+
Add `decorative` when a linked name sits right next to the avatar. It keeps the
131+
duplicate link out of the tab order and away from screen readers, while mouse
132+
users still get the card.
133+
92134
## Names inside a sentence
93135

94136
`UserFormat` is an `inline-flex` row, which keeps the icon centered on the
@@ -111,6 +153,7 @@ words around it:
111153
| `avatarSize` | `number` | No | `20` | The avatar's width and height in pixels |
112154
| `format` | `boolean` | No | `false` | Adds the role color and prefix |
113155
| `link` | `boolean` | No | `false` | Links to `/users/<nameCode>` when it is set |
156+
| `preview` | `boolean` | No | `true` | With `link`, opens the profile preview card on hover or focus |
114157
| `className` | `string` | No | - | Extra classes on the outer element |
115158

116159
Other `<span>` props go to the outer `<span>`. With `link`, only `className`,

‎packages/vitnode/src/components/user-format.tsx‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,26 @@ import { cn } from "cn";
44

55
import { Avatar } from "./avatar";
66
import { EmojiIcon } from "./ui/emoji-icon";
7+
import { UserPreview } from "./user-preview";
8+
import { userProfileHref } from "./user-profile-href";
79

8-
const FALLBACK_AVATAR_COLOR = "64748b";
10+
export { userProfileHref } from "./user-profile-href";
911

10-
/** The path of a user's public profile page. */
11-
export const userProfileHref = (nameCode: string): string =>
12-
`/users/${encodeURIComponent(nameCode)}`;
12+
const FALLBACK_AVATAR_COLOR = "64748b";
1313

1414
/**
1515
* A user's name, optionally styled by their role, led by their avatar and
1616
* linked to their profile. Shows `user.fullName` in place of the nickname when
17-
* the API sends one.
17+
* the API sends one. A linked name opens the member's preview card on hover or
18+
* focus, which fetches its own data.
1819
*/
1920
export const UserFormat = ({
2021
user,
2122
avatar = false,
2223
avatarSize = 20,
2324
format,
2425
link,
26+
preview = true,
2527
className,
2628
style,
2729
...props
@@ -38,6 +40,12 @@ export const UserFormat = ({
3840
format?: boolean;
3941
/** Links to the profile page; needs `user.nameCode`. */
4042
link?: boolean;
43+
/**
44+
* With `link`, opens the member's preview card (cover, avatar, group) when
45+
* the link is hovered or focused. Turn it off inside another hover card.
46+
* Defaults to `true`.
47+
*/
48+
preview?: boolean;
4149
user: {
4250
/** The letter avatar's background, a hex color without `#`. */
4351
avatarColor?: string;
@@ -83,6 +91,19 @@ export const UserFormat = ({
8391
</>
8492
);
8593

94+
if (link && user.nameCode && preview) {
95+
return (
96+
<UserPreview
97+
className={cn(classes, "underline-offset-4 hover:underline")}
98+
nameCode={user.nameCode}
99+
style={colorStyle}
100+
title={props.title}
101+
>
102+
{content}
103+
</UserPreview>
104+
);
105+
}
106+
86107
if (link && user.nameCode) {
87108
return (
88109
<Link
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
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+
);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { useTranslations } from "use-intl";
2+
3+
import { Skeleton } from "./ui/skeleton";
4+
5+
/** The user preview card's loading state, shaped like the loaded card. */
6+
export const UserPreviewSkeleton = () => {
7+
const t = useTranslations("core.global");
8+
9+
return (
10+
<div aria-busy="true" className="flex flex-col">
11+
<span className="sr-only" role="status">
12+
{t("loading")}
13+
</span>
14+
<Skeleton className="h-20 w-full rounded-none" />
15+
<div className="flex flex-col gap-3 px-4 pb-4">
16+
<Skeleton className="border-popover bg-muted -mt-8 size-16 rounded-full border-4" />
17+
<div className="flex flex-col gap-2">
18+
<Skeleton className="h-5 w-2/5" />
19+
<Skeleton className="h-4 w-1/4" />
20+
</div>
21+
<Skeleton className="h-4 w-4/5" />
22+
<div className="flex gap-2">
23+
<Skeleton className="h-5 w-20 rounded-full" />
24+
<Skeleton className="h-5 w-24 rounded-full" />
25+
</div>
26+
</div>
27+
</div>
28+
);
29+
};

0 commit comments

Comments
 (0)