Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

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