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