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.

g1t.sh
130 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 { Bot, 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 where they come from: a chip seen twice asks once. */
28const cards = new Map<string, Promise<RefCard | null>>();
29
30function cardAt(url: string): Promise<RefCard | null> {
31 let found = cards.get(url);
32 if (!found) {
33 found = fetch(url, { headers: { accept: "application/json" } })
34 .then((response) => (response.ok ? (response.json() as Promise<RefCard>) : null))
35 .catch(() => null);
36 cards.set(url, found);
37 }
38 return found;
39}
40
41/**
42 * The card itself, asked for from `url` once it opens: chat's
43 * (routes/workspace/chat/ref.ts) for chips there, a repository's
44 * (routes/repo/ref-card.ts) for references in its commits and timelines.
45 */
46export function RefCardBody({ url, repo, number }: { url: string; repo: string; number: number }) {
47 const [card, setCard] = useState<RefCard | null | "loading">("loading");
48 useEffect(() => {
49 let live = true;
50 void cardAt(url).then((found) => live && setCard(found));
51 return () => {
52 live = false;
53 };
54 }, [url]);
55 if (card === "loading") {
56 return (
57 <div className="space-y-2">
58 <Skeleton className="h-3 w-28" />
59 <Skeleton className="h-4 w-56" />
60 </div>
61 );
62 }
63 if (!card) return <p className="text-xs text-muted">Nothing you can see is numbered #{number} in {repo}.</p>;
64 return (
65 <div className="flex gap-2.5">
66 <span className="mt-0.5">
67 {card.kind === "pull" ? (
68 <PullIcon status={PULL_STATUSES.find((s) => s === card.state) ?? "open"} size={16} />
69 ) : (
70 <IssueIcon issue={{ state: card.state === "open" ? "open" : "closed", reason: null }} size={16} />
71 )}
72 </span>
73 <span className="min-w-0">
74 <span className="block text-xs text-muted">
75 {card.repo} · {card.kind === "pull" ? "Pull request" : "Issue"} #{card.number}
76 </span>
77 <span className="mt-0.5 block font-medium leading-snug text-fg">{card.title}</span>
78 <span className="mt-1 block text-xs text-faint capitalize">{card.state}</span>
79 </span>
80 </div>
81 );
82}
83
84/** `acme/web#12`: a chip to the issue or pull request, its card on hover. */
85export function IssueChip({ slug, repo, number, label, to, kind }: { slug: string; repo: string; number: number; label?: ReactNode; to?: string; kind?: "issue" | "pull" }) {
86 const href = to ?? `/${repo}/issues/${number}`;
87 return (
88 <HoverCard>
89 <HoverCardTrigger asChild>
90 <Link to={href} className={CHIP} data-ref="issue">
91 {kind === "pull" ? <GitPullRequest size={12} className="text-faint" /> : <CircleDot size={12} className="text-faint" />}
92 <span className="truncate">{label ?? `${repo}#${number}`}</span>
93 </Link>
94 </HoverCardTrigger>
95 <HoverCardContent className="w-80">
96 <RefCardBody url={`/${slug}/-/chat/ref?repo=${encodeURIComponent(repo)}&number=${number}`} repo={repo} number={number} />
97 </HoverCardContent>
98 </HoverCard>
99 );
100}
101
102/** A commit, by its short hash. */
103export function CommitChip({ href, sha }: { href: string; sha: string }) {
104 return (
105 <Link to={href} className={cn(CHIP, "font-mono")} data-ref="commit">
106 <GitCommitHorizontal size={12} className="text-faint" />
107 {sha.slice(0, 7)}
108 </Link>
109 );
110}
111
112/** A session, by the end of its id, which is what tells sessions started together apart. */
113export function SessionChip({ href, id }: { href: string; id: string }) {
114 return (
115 <Link to={href} className={cn(CHIP, "font-mono")} data-ref="session">
116 <Bot size={12} className="text-faint" />
117 {id.length > 12 ? `${id.slice(0, 4)}…${id.slice(-6)}` : id}
118 </Link>
119 );
120}
121
122/** Another page of the site, linked by its path or a full link to it. */
123export function PageChip({ href, children }: { href: string; children: ReactNode }) {
124 return (
125 <Link to={href} className={CHIP}>
126 <Link2 size={12} className="text-faint" />
127 <span className="truncate">{children}</span>
128 </Link>
129 );
130}