Skip to content
190 linesCodeBlameRaw
1import { Building2, Clock, GitCommitHorizontal, MapPin } from "lucide-react";
2import { type ReactElement, type ReactNode, useEffect, useState } from "react";
3import { Link, useParams } from "react-router";
4
5import { shownUsername } from "@g1t/contracts";
6
7import { type Card, type UserCard as UserCardData, cardHref, committedLabel } from "../lib/hovercard";
8import { localTime } from "../lib/time-zone";
9
10import { Avatar } from "./ui/avatar";
11import { HoverCard, HoverCardContent, HoverCardTrigger } from "./ui/hover-card";
12import { Skeleton } from "./ui/skeleton";
13
14/**
15 * Cards read this visit, by address: each is asked for once, whichever
16 * name or avatar opened it. A failure is forgotten, so the next hover asks
17 * again.
18 */
19const cards = new Map<string, Promise<Card | null>>();
20
21function readCard(href: string): Promise<Card | null> {
22 let found = cards.get(href);
23 if (!found) {
24 found = fetch(href, { headers: { accept: "application/json" } })
25 .then((response) => (response.ok ? (response.json() as Promise<Card>) : response.status === 404 ? null : Promise.reject(new Error(String(response.status)))))
26 .catch(() => {
27 cards.delete(href);
28 return null;
29 });
30 cards.set(href, found);
31 }
32 return found;
33}
34
35/** Whether a name can have a card: a username, and not a deleted account's. */
36export function hasCard(username: string | null | undefined): username is string {
37 return Boolean(username) && username !== "ghost" && /^[a-z0-9-]{1,39}$/i.test(username!);
38}
39
40/**
41 * A person's card, over their name or avatar (`children`, one element that
42 * takes a ref). It opens on hover and on keyboard focus after a moment,
43 * and is read only then. On a touch screen it never opens: a tap does what
44 * the name does. Inside a repository it says whether they committed there
45 * lately. Ghost, a deleted account, has none.
46 */
47export function UserCard({ username, children }: { username: string | null | undefined; children: ReactElement }) {
48 const params = useParams();
49 const [open, setOpen] = useState(false);
50 const [card, setCard] = useState<Card | null | undefined>(undefined);
51 const name = username?.toLowerCase() ?? "";
52 const repo = params.owner && params.repo ? `${params.owner}/${params.repo}` : null;
53 const g1t = name === "g1t";
54 const href = cardHref(name, repo);
55
56 useEffect(() => {
57 if (!open || g1t || !hasCard(name)) return;
58 let current = true;
59 void readCard(href).then((found) => {
60 if (current) setCard(found);
61 });
62 return () => {
63 current = false;
64 };
65 }, [open, g1t, name, href]);
66
67 if (!hasCard(name)) return children;
68 // Nobody by that name (a workspace mentioned, say): no card at all.
69 const body = g1t || card?.kind === "g1t" ? <G1tCard /> : card === undefined ? <Loading /> : card ? <PersonCard card={card} /> : null;
70 return (
71 <HoverCard open={open} onOpenChange={setOpen}>
72 <HoverCardTrigger asChild>{children}</HoverCardTrigger>
73 {body && <HoverCardContent aria-label={g1t ? "About g1t" : `About ${name}`}>{body}</HoverCardContent>}
74 </HoverCard>
75 );
76}
77
78/**
79 * A person's avatar that opens their card and links to their profile, as
80 * their name beside it does. g1t, ghost and names that are no account's
81 * are the avatar alone.
82 */
83export function UserAvatar({ username, size = 20, image }: { username: string; size?: number; image?: string | null }) {
84 const name = username.toLowerCase();
85 const face = <Avatar name={username} image={image} size={size} />;
86 if (name === "g1t" || !hasCard(name)) return face;
87 return (
88 <UserCard username={name}>
89 <Link to={`/u/${name}`} tabIndex={-1} aria-hidden className="inline-flex shrink-0 rounded-full">
90 {face}
91 </Link>
92 </UserCard>
93 );
94}
95
96function Loading() {
97 return (
98 <div aria-busy="true" className="flex items-center gap-3">
99 <Skeleton className="size-12 rounded-full" />
100 <div className="grow space-y-2">
101 <Skeleton className="block h-3.5 w-28" />
102 <Skeleton className="block h-3 w-20" />
103 </div>
104 </div>
105 );
106}
107
108function PersonCard({ card }: { card: UserCardData }) {
109 const profile = `/u/${card.username}`;
110 // Cards are only drawn in the browser, so this is the viewer's clock.
111 const time = localTime(card.timezone, Date.now());
112 return (
113 <div className="space-y-3">
114 <div className="flex items-start gap-3">
115 <Link to={profile} tabIndex={-1} className="shrink-0 rounded-full">
116 <Avatar name={card.username} image={card.avatar} size={48} />
117 </Link>
118 <div className="min-w-0 pt-0.5">
119 <Link to={profile} className="block leading-tight hover:underline">
120 {card.name ? (
121 <>
122 <span className="font-semibold wrap-anywhere">{card.name}</span>{" "}
123 <span className="font-mono text-[0.8125rem] text-muted">{shownUsername(card)}</span>
124 </>
125 ) : (
126 <span className="font-mono font-semibold">{shownUsername(card)}</span>
127 )}
128 </Link>
129 {card.pronouns && <p className="mt-0.5 text-xs text-faint">{card.pronouns}</p>}
130 </div>
131 </div>
132 {card.bio && <p className="leading-relaxed text-fg/90 wrap-anywhere">{card.bio}</p>}
133 {(card.location || time || card.workspaces.length > 0 || card.committed) && (
134 <ul className="space-y-1.5 text-[0.8125rem] text-muted">
135 {card.location && (
136 <Line icon={<MapPin size={14} />}>
137 <span className="wrap-anywhere">{card.location}</span>
138 </Line>
139 )}
140 {time && <Line icon={<Clock size={14} />}>{time} local time</Line>}
141 {card.workspaces.length > 0 && (
142 <Line icon={<Building2 size={14} />}>
143 Member of{" "}
144 {card.workspaces.map((workspace, index) => (
145 <span key={workspace.slug}>
146 {index > 0 && ", "}
147 <Link to={`/${workspace.slug}`} className="font-medium text-fg hover:underline">
148 {workspace.name}
149 </Link>
150 </span>
151 ))}
152 {card.more_workspaces > 0 && ` and ${card.more_workspaces} more`}
153 </Line>
154 )}
155 {card.committed && <Line icon={<GitCommitHorizontal size={14} />}>{committedLabel(card.committed)}</Line>}
156 </ul>
157 )}
158 </div>
159 );
160}
161
162function G1tCard() {
163 return (
164 <div className="space-y-3">
165 <div className="flex items-center gap-3">
166 <Avatar name="g1t" size={48} />
167 <div className="min-w-0">
168 <p className="flex items-baseline gap-1.5 leading-tight">
169 <span className="font-mono font-semibold">g1t</span>
170 <span className="rounded border border-line px-1 text-[0.625rem] leading-[1.35] font-medium text-muted">bot</span>
171 </p>
172 <p className="mt-0.5 text-xs text-muted">g1t itself</p>
173 </div>
174 </div>
175 <p className="leading-relaxed text-fg/90">
176 What g1t does on its own shows under this name: merges from the merge queue, automatic merges your settings allow,
177 and security updates.
178 </p>
179 </div>
180 );
181}
182
183function Line({ icon, children }: { icon: ReactElement; children: ReactNode }) {
184 return (
185 <li className="flex items-start gap-2">
186 <span className="mt-0.5 shrink-0 text-faint">{icon}</span>
187 <span className="min-w-0">{children}</span>
188 </li>
189 );
190}