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