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.

161 linesCodeBlameRaw
1/**
2 * References in chat and in g1t's answers, as compact chips: a work item
3 * or pull request (`acme/web#12`, `acme#12`, `#12` read as lib/refs.ts
4 * says, or a link to one) with its title and state in a hover card, a
5 * commit by its short hash, and any other page of the site by its path.
6 * One look everywhere a conversation shows them (components/chat/text.tsx
7 * for people's messages, components/ai-elements/response.tsx for g1t's
8 * answers).
9 */
10import { Bot, GitCommitHorizontal, GitPullRequest, Link2, ListTodo } from "lucide-react";
11import { type ReactNode, useEffect, useState } from "react";
12import { Link } from "react-router";
13
14import type { PullStatus } from "@g1t/contracts";
15
16import { ItemIcon, PullIcon } from "../work-icons";
17import { HoverCard, HoverCardContent, HoverCardTrigger } from "../ui/hover-card";
18import { Skeleton } from "../ui/skeleton";
19import { cn } from "../../lib/cn";
20import type { RefContext } from "../../lib/refs";
21import type { RefCard } from "../../routes/workspace/chat/ref";
22
23/** The chip's frame: a quiet pill that reads as part of the sentence. */
24const CHIP =
25 "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";
26
27const PULL_STATUSES: PullStatus[] = ["draft", "open", "merged", "closed"];
28
29/** Cards already asked for in this tab, by where they come from: a chip seen twice asks once. */
30const cards = new Map<string, Promise<RefCard | null>>();
31
32function cardAt(url: string): Promise<RefCard | null> {
33 let found = cards.get(url);
34 if (!found) {
35 found = fetch(url, { headers: { accept: "application/json" } })
36 .then((response) => (response.ok ? (response.json() as Promise<RefCard>) : null))
37 .catch(() => null);
38 cards.set(url, found);
39 }
40 return found;
41}
42
43/** Where `#number` is read, in words: `acme/web` or `acme`. */
44function scopeName(scope: RefContext): string {
45 return "repo" in scope ? `${scope.repo.namespace}/${scope.repo.name}` : scope.workspace;
46}
47
48/** Chat's card for `#number` read in `scope`, asked for in the workspace `slug` (routes/workspace/chat/ref.ts). */
49export function chatCardUrl(slug: string, scope: RefContext, number: number): string {
50 const where = "repo" in scope ? `repo=${encodeURIComponent(scopeName(scope))}` : `workspace=${encodeURIComponent(scope.workspace)}`;
51 return `/${slug}/-/chat/ref?${where}&number=${number}`;
52}
53
54/**
55 * The card itself, asked for from `url` once it opens: chat's
56 * (routes/workspace/chat/ref.ts) for chips there, a repository's
57 * (routes/repo/ref-card.ts) for references in its commits and timelines.
58 * `where` names the repository or workspace it was read in.
59 */
60export function RefCardBody({ url, where, number }: { url: string; where: string; number: number }) {
61 const [card, setCard] = useState<RefCard | null | "loading">("loading");
62 useEffect(() => {
63 let live = true;
64 void cardAt(url).then((found) => live && setCard(found));
65 return () => {
66 live = false;
67 };
68 }, [url]);
69 if (card === "loading") {
70 return (
71 <div className="space-y-2">
72 <Skeleton className="h-3 w-28" />
73 <Skeleton className="h-4 w-56" />
74 </div>
75 );
76 }
77 if (!card) return <p className="text-xs text-muted">Nothing you can see is numbered #{number} in {where}.</p>;
78 return (
79 <div className="flex gap-2.5">
80 <span className="mt-0.5">
81 {card.kind === "pull" ? (
82 <PullIcon status={PULL_STATUSES.find((s) => s === card.state) ?? "open"} size={16} />
83 ) : (
84 <ItemIcon item={{ state: card.state === "open" ? "open" : "closed", reason: card.state === "not planned" ? "not_planned" : null }} size={16} />
85 )}
86 </span>
87 <span className="min-w-0">
88 <span className="block text-xs text-muted">
89 {card.kind === "pull" ? `${card.repo} · Pull request` : `${card.workspace} · Work item`} #{card.number}
90 </span>
91 <span className="mt-0.5 block font-medium leading-snug text-fg">{card.title}</span>
92 <span className="mt-1 block text-xs text-faint capitalize">{card.state}</span>
93 </span>
94 </div>
95 );
96}
97
98/**
99 * `acme/web#12` or `#12`: a chip to the work item or pull request it names
100 * (`to`), its card on hover, read in `scope` and asked for in the
101 * workspace `slug`.
102 */
103export function RefChip({
104 slug,
105 scope,
106 number,
107 label,
108 to,
109 kind,
110}: {
111 slug: string;
112 scope: RefContext;
113 number: number;
114 label?: ReactNode;
115 to: string;
116 kind?: "item" | "pull";
117}) {
118 return (
119 <HoverCard>
120 <HoverCardTrigger asChild>
121 <Link to={to} className={CHIP} data-ref="ref">
122 {kind === "pull" ? <GitPullRequest size={12} className="text-faint" /> : <ListTodo size={12} className="text-faint" />}
123 <span className="truncate">{label ?? `${"repo" in scope ? scopeName(scope) : ""}#${number}`}</span>
124 </Link>
125 </HoverCardTrigger>
126 <HoverCardContent className="w-80">
127 <RefCardBody url={chatCardUrl(slug, scope, number)} where={scopeName(scope)} number={number} />
128 </HoverCardContent>
129 </HoverCard>
130 );
131}
132
133/** A commit, by its short hash. */
134export function CommitChip({ href, sha }: { href: string; sha: string }) {
135 return (
136 <Link to={href} className={cn(CHIP, "font-mono")} data-ref="commit">
137 <GitCommitHorizontal size={12} className="text-faint" />
138 {sha.slice(0, 7)}
139 </Link>
140 );
141}
142
143/** A session, by the end of its id, which is what tells sessions started together apart. */
144export function SessionChip({ href, id }: { href: string; id: string }) {
145 return (
146 <Link to={href} className={cn(CHIP, "font-mono")} data-ref="session">
147 <Bot size={12} className="text-faint" />
148 {id.length > 12 ? `${id.slice(0, 4)}…${id.slice(-6)}` : id}
149 </Link>
150 );
151}
152
153/** Another page of the site, linked by its path or a full link to it. */
154export function PageChip({ href, children }: { href: string; children: ReactNode }) {
155 return (
156 <Link to={href} className={CHIP}>
157 <Link2 size={12} className="text-faint" />
158 <span className="truncate">{children}</span>
159 </Link>
160 );
161}