| 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 | */ |
| 10 | import { Bot, GitCommitHorizontal, GitPullRequest, Link2, ListTodo } from "lucide-react"; |
| 11 | import { type ReactNode, useEffect, useState } from "react"; |
| 12 | import { Link } from "react-router"; |
| 13 | |
| 14 | import type { PullStatus } from "@g1t/contracts"; |
| 15 | |
| 16 | import { ItemIcon, PullIcon } from "../work-icons"; |
| 17 | import { HoverCard, HoverCardContent, HoverCardTrigger } from "../ui/hover-card"; |
| 18 | import { Skeleton } from "../ui/skeleton"; |
| 19 | import { cn } from "../../lib/cn"; |
| 20 | import type { RefContext } from "../../lib/refs"; |
| 21 | import type { RefCard } from "../../routes/workspace/chat/ref"; |
| 22 | |
| 23 | /** The chip's frame: a quiet pill that reads as part of the sentence. */ |
| 24 | const 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 | |
| 27 | const 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. */ |
| 30 | const cards = new Map<string, Promise<RefCard | null>>(); |
| 31 | |
| 32 | function 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`. */ |
| 44 | function 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). */ |
| 49 | export 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 | */ |
| 60 | export 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) |
| 78 | return ( |
| 79 | <p className="text-xs text-muted"> |
| 80 | Nothing you can see is numbered #{number} in {where}. |
| 81 | </p> |
| 82 | ); |
| 83 | return ( |
| 84 | <div className="flex gap-2.5"> |
| 85 | <span className="mt-0.5"> |
| 86 | {card.kind === "pull" ? ( |
| 87 | <PullIcon status={PULL_STATUSES.find((s) => s === card.state) ?? "open"} size={16} /> |
| 88 | ) : ( |
| 89 | <ItemIcon item={{ state: card.state === "open" ? "open" : "closed", reason: card.state === "not planned" ? "not_planned" : null }} size={16} /> |
| 90 | )} |
| 91 | </span> |
| 92 | <span className="min-w-0"> |
| 93 | <span className="block text-xs text-muted"> |
| 94 | {card.kind === "pull" ? `${card.repo} · Pull request` : `${card.workspace} · Work item`} #{card.number} |
| 95 | </span> |
| 96 | <span className="mt-0.5 block font-medium leading-snug text-fg">{card.title}</span> |
| 97 | <span className="mt-1 block text-xs text-faint capitalize">{card.state}</span> |
| 98 | </span> |
| 99 | </div> |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * `acme/web#12` or `#12`: a chip to the work item or pull request it names |
| 105 | * (`to`), its card on hover, read in `scope` and asked for in the |
| 106 | * workspace `slug`. |
| 107 | */ |
| 108 | export function RefChip({ |
| 109 | slug, |
| 110 | scope, |
| 111 | number, |
| 112 | label, |
| 113 | to, |
| 114 | kind, |
| 115 | }: { |
| 116 | slug: string; |
| 117 | scope: RefContext; |
| 118 | number: number; |
| 119 | label?: ReactNode; |
| 120 | to: string; |
| 121 | kind?: "item" | "pull"; |
| 122 | }) { |
| 123 | return ( |
| 124 | <HoverCard> |
| 125 | <HoverCardTrigger asChild> |
| 126 | <Link to={to} className={CHIP} data-ref="ref"> |
| 127 | {kind === "pull" ? <GitPullRequest size={12} className="text-faint" /> : <ListTodo size={12} className="text-faint" />} |
| 128 | <span className="truncate">{label ?? `${"repo" in scope ? scopeName(scope) : ""}#${number}`}</span> |
| 129 | </Link> |
| 130 | </HoverCardTrigger> |
| 131 | <HoverCardContent className="w-80"> |
| 132 | <RefCardBody url={chatCardUrl(slug, scope, number)} where={scopeName(scope)} number={number} /> |
| 133 | </HoverCardContent> |
| 134 | </HoverCard> |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | /** A commit, by its short hash. */ |
| 139 | export function CommitChip({ href, sha }: { href: string; sha: string }) { |
| 140 | return ( |
| 141 | <Link to={href} className={cn(CHIP, "font-mono")} data-ref="commit"> |
| 142 | <GitCommitHorizontal size={12} className="text-faint" /> |
| 143 | {sha.slice(0, 7)} |
| 144 | </Link> |
| 145 | ); |
| 146 | } |
| 147 | |
| 148 | /** A session, by the end of its id, which is what tells sessions started together apart. */ |
| 149 | export function SessionChip({ href, id }: { href: string; id: string }) { |
| 150 | return ( |
| 151 | <Link to={href} className={cn(CHIP, "font-mono")} data-ref="session"> |
| 152 | <Bot size={12} className="text-faint" /> |
| 153 | {id.length > 12 ? `${id.slice(0, 4)}…${id.slice(-6)}` : id} |
| 154 | </Link> |
| 155 | ); |
| 156 | } |
| 157 | |
| 158 | /** Another page of the site, linked by its path or a full link to it. */ |
| 159 | export function PageChip({ href, children }: { href: string; children: ReactNode }) { |
| 160 | return ( |
| 161 | <Link to={href} className={CHIP}> |
| 162 | <Link2 size={12} className="text-faint" /> |
| 163 | <span className="truncate">{children}</span> |
| 164 | </Link> |
| 165 | ); |
| 166 | } |