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