Skip to content
116 linesCodeBlameRaw
1/**
2 * References in chat and in g1t's answers, as compact chips: an issue or
3 * pull request (`acme/web#12`, `#12` in a repository's context, or a link
4 * to one) with its title and state in a hover card, a commit by its short
5 * hash, and any other page of the site by its path. One look everywhere a
6 * conversation shows them (components/chat/text.tsx for people's messages,
7 * components/ai-elements/response.tsx for g1t's answers).
8 */
9import { CircleDot, GitCommitHorizontal, GitPullRequest, Link2 } from "lucide-react";
10import { type ReactNode, useEffect, useState } from "react";
11import { Link } from "react-router";
12
13import type { PullStatus } from "@g1t/contracts";
14
15import { PullIcon, IssueIcon } from "../work-icons";
16import { HoverCard, HoverCardContent, HoverCardTrigger } from "../ui/hover-card";
17import { Skeleton } from "../ui/skeleton";
18import { cn } from "../../lib/cn";
19import type { RefCard } from "../../routes/workspace/chat/ref";
20
21/** The chip's frame: a quiet pill that reads as part of the sentence. */
22const CHIP =
23 "inline-flex max-w-full items-baseline gap-1 rounded-md border border-line bg-surface px-1.5 py-px align-baseline text-[0.8125em] font-medium text-fg-soft no-underline transition-colors hover:border-accent/50 hover:bg-accent/10 hover:text-fg [&_svg]:self-center";
24
25const PULL_STATUSES: PullStatus[] = ["draft", "open", "merged", "closed"];
26
27/** Cards already asked for in this tab, by `repo#number`: a chip seen twice asks once. */
28const cards = new Map<string, Promise<RefCard | null>>();
29
30function cardFor(slug: string, repo: string, number: number): Promise<RefCard | null> {
31 const key = `${slug}:${repo}#${number}`;
32 let found = cards.get(key);
33 if (!found) {
34 found = fetch(`/${slug}/-/chat/ref?repo=${encodeURIComponent(repo)}&number=${number}`, { headers: { accept: "application/json" } })
35 .then((response) => (response.ok ? (response.json() as Promise<RefCard>) : null))
36 .catch(() => null);
37 cards.set(key, found);
38 }
39 return found;
40}
41
42function RefCardBody({ slug, repo, number }: { slug: string; repo: string; number: number }) {
43 const [card, setCard] = useState<RefCard | null | "loading">("loading");
44 useEffect(() => {
45 let live = true;
46 void cardFor(slug, repo, number).then((found) => live && setCard(found));
47 return () => {
48 live = false;
49 };
50 }, [slug, repo, number]);
51 if (card === "loading") {
52 return (
53 <div className="space-y-2">
54 <Skeleton className="h-3 w-28" />
55 <Skeleton className="h-4 w-56" />
56 </div>
57 );
58 }
59 if (!card) return <p className="text-xs text-muted">Nothing you can see is numbered #{number} in {repo}.</p>;
60 return (
61 <div className="flex gap-2.5">
62 <span className="mt-0.5">
63 {card.kind === "pull" ? (
64 <PullIcon status={PULL_STATUSES.find((s) => s === card.state) ?? "open"} size={16} />
65 ) : (
66 <IssueIcon issue={{ state: card.state === "open" ? "open" : "closed", reason: null }} size={16} />
67 )}
68 </span>
69 <span className="min-w-0">
70 <span className="block text-xs text-muted">
71 {card.repo} · {card.kind === "pull" ? "Pull request" : "Issue"} #{card.number}
72 </span>
73 <span className="mt-0.5 block font-medium leading-snug text-fg">{card.title}</span>
74 <span className="mt-1 block text-xs text-faint capitalize">{card.state}</span>
75 </span>
76 </div>
77 );
78}
79
80/** `acme/web#12`: a chip to the issue or pull request, its card on hover. */
81export function IssueChip({ slug, repo, number, label, to, kind }: { slug: string; repo: string; number: number; label?: ReactNode; to?: string; kind?: "issue" | "pull" }) {
82 const href = to ?? `/${repo}/issues/${number}`;
83 return (
84 <HoverCard>
85 <HoverCardTrigger asChild>
86 <Link to={href} className={CHIP} data-ref="issue">
87 {kind === "pull" ? <GitPullRequest size={12} className="text-faint" /> : <CircleDot size={12} className="text-faint" />}
88 <span className="truncate">{label ?? `${repo}#${number}`}</span>
89 </Link>
90 </HoverCardTrigger>
91 <HoverCardContent className="w-80">
92 <RefCardBody slug={slug} repo={repo} number={number} />
93 </HoverCardContent>
94 </HoverCard>
95 );
96}
97
98/** A commit, by its short hash. */
99export function CommitChip({ href, sha }: { href: string; sha: string }) {
100 return (
101 <Link to={href} className={cn(CHIP, "font-mono")} data-ref="commit">
102 <GitCommitHorizontal size={12} className="text-faint" />
103 {sha.slice(0, 7)}
104 </Link>
105 );
106}
107
108/** Another page of the site, linked by its path or a full link to it. */
109export function PageChip({ href, children }: { href: string; children: ReactNode }) {
110 return (
111 <Link to={href} className={CHIP}>
112 <Link2 size={12} className="text-faint" />
113 <span className="truncate">{children}</span>
114 </Link>
115 );
116}