pr_01m47d15m3e54sn21z27rpy5n9/apps/web/app/components/blame-view.tsx
| 1 | import { |
| 2 | ArrowUpRight, |
| 3 | CircleDot, |
| 4 | GitCommitHorizontal, |
| 5 | GitPullRequest, |
| 6 | MessagesSquare, |
| 7 | Sparkles, |
| 8 | Terminal, |
| 9 | X, |
| 10 | } from "lucide-react"; |
| 11 | import { Fragment, useEffect, useMemo, useState } from "react"; |
| 12 | import { Link, useFetcher } from "react-router"; |
| 13 | |
| 14 | import type { Blame, Commit } from "@g1t/contracts"; |
| 15 | |
| 16 | import type { loader as whyLoader } from "../routes/repo/why"; |
| 17 | import { repoAt } from "../lib/markdown-plugins"; |
| 18 | import { Markdown } from "./markdown"; |
| 19 | import { Avatar, TimeAgo } from "./ui"; |
| 20 | |
| 21 | /** |
| 22 | * A file with, beside each run of lines, the commit that last changed it. |
| 23 | * Picking a line opens why it is the way it is: the commit, the pull |
| 24 | * request and issue it came from, and what the agent was thinking. |
| 25 | */ |
| 26 | export function BlameView({ |
| 27 | base, |
| 28 | path, |
| 29 | lines, |
| 30 | html, |
| 31 | blame, |
| 32 | }: { |
| 33 | base: string; |
| 34 | path: string; |
| 35 | lines: string[]; |
| 36 | /** Highlighted HTML per line, when the language is known. */ |
| 37 | html: string[] | null; |
| 38 | blame: Blame; |
| 39 | }) { |
| 40 | const commits = useMemo( |
| 41 | () => new Map<string, Commit>(blame.commits.map((commit) => [commit.hash, commit])), |
| 42 | [blame.commits], |
| 43 | ); |
| 44 | // Newer commits are brighter: rank them by when they were written. |
| 45 | const age = useMemo(() => { |
| 46 | const sorted = [...blame.commits].sort((a, b) => b.authoredAt.localeCompare(a.authoredAt)); |
| 47 | const rank = new Map(sorted.map((commit, index) => [commit.hash, index])); |
| 48 | return (hash: string) => 1 - (rank.get(hash) ?? 0) / Math.max(sorted.length, 1); |
| 49 | }, [blame.commits]); |
| 50 | // The run of lines picked, and the line clicked, under which the |
| 51 | // explanation opens. |
| 52 | const [picked, setPicked] = useState<{ commit: string; start: number; end: number; line: number } | null>( |
| 53 | null, |
| 54 | ); |
| 55 | const [hovered, setHovered] = useState<string | null>(null); |
| 56 | const why = useFetcher<typeof whyLoader>(); |
| 57 | |
| 58 | useEffect(() => { |
| 59 | if (!picked) return; |
| 60 | why.load(`${base}/why/${picked.commit}?path=${encodeURIComponent(path)}`); |
| 61 | // `why` changes identity as it loads; the pick is what matters. |
| 62 | // eslint-disable-next-line react-hooks/exhaustive-deps |
| 63 | }, [picked?.commit, base, path]); |
| 64 | |
| 65 | // A line named in the address is picked on arrival: `#L12`. |
| 66 | useEffect(() => { |
| 67 | const match = /^#L(\d+)$/.exec(window.location.hash); |
| 68 | if (!match) return; |
| 69 | const line = Number(match[1]); |
| 70 | const range = blame.ranges.find((r) => r.start <= line && line <= r.end); |
| 71 | if (range) { |
| 72 | setPicked({ ...range, line }); |
| 73 | document.getElementById(`L${line}`)?.scrollIntoView({ block: "center" }); |
| 74 | } |
| 75 | }, [blame.ranges]); |
| 76 | |
| 77 | const pick = (start: number, end: number, commit: string, line: number) => { |
| 78 | // Clicking the open line again closes it. |
| 79 | if (picked?.line === line) { |
| 80 | close(); |
| 81 | return; |
| 82 | } |
| 83 | setPicked({ commit, start, end, line }); |
| 84 | window.history.replaceState(null, "", `#L${line}`); |
| 85 | }; |
| 86 | const close = () => { |
| 87 | setPicked(null); |
| 88 | window.history.replaceState(null, "", window.location.pathname + window.location.search); |
| 89 | }; |
| 90 | |
| 91 | return ( |
| 92 | <div> |
| 93 | <p className="flex items-center gap-2 border-b border-line px-4 py-2 text-xs text-muted"> |
| 94 | <Sparkles size={13} className="text-merged" /> |
| 95 | Pick any line to see why it is the way it is: the commit, the pull request and issue it |
| 96 | came from, and what the agent was thinking. |
| 97 | </p> |
| 98 | <div className="overflow-x-auto"> |
| 99 | <table className="w-full border-collapse font-mono text-[0.8125rem] leading-6"> |
| 100 | <tbody> |
| 101 | {blame.ranges.map((range) => { |
| 102 | const commit = commits.get(range.commit); |
| 103 | const selected = picked?.commit === range.commit; |
| 104 | const lit = hovered === range.commit; |
| 105 | const bright = age(range.commit); |
| 106 | const rows = []; |
| 107 | for (let line = range.start; line <= range.end; line++) { |
| 108 | const first = line === range.start; |
| 109 | rows.push( |
| 110 | <tr |
| 111 | key={line} |
| 112 | id={`L${line}`} |
| 113 | onMouseEnter={() => setHovered(range.commit)} |
| 114 | onMouseLeave={() => setHovered(null)} |
| 115 | onClick={() => pick(range.start, range.end, range.commit, line)} |
| 116 | className={`cursor-pointer transition-colors ${ |
| 117 | selected ? "bg-accent/[0.07]" : lit ? "bg-raised/50" : "" |
| 118 | } ${first ? "border-t border-line" : ""}`} |
| 119 | > |
| 120 | <td className="w-72 max-w-72 border-r border-line px-3 align-top font-sans"> |
| 121 | {first && commit && ( |
| 122 | <span className="flex items-center gap-2 py-0.5 text-xs"> |
| 123 | <Avatar name={commit.author.name} size={16} /> |
| 124 | <span className={`min-w-0 grow truncate ${selected ? "text-fg" : "text-muted"}`}> |
| 125 | {commit.message.split("\n")[0]} |
| 126 | </span> |
| 127 | <span className="shrink-0 text-faint"> |
| 128 | <TimeAgo at={commit.authoredAt} /> |
| 129 | </span> |
| 130 | </span> |
| 131 | )} |
| 132 | </td> |
| 133 | <td className="w-1 p-0" aria-hidden="true"> |
| 134 | <span |
| 135 | className="block h-6 w-0.5 bg-accent" |
| 136 | style={{ opacity: 0.15 + bright * 0.85 }} |
| 137 | /> |
| 138 | </td> |
| 139 | <td className="w-12 px-2 text-right text-faint select-none">{line}</td> |
| 140 | <td className="pr-4 pl-2 whitespace-pre"> |
| 141 | {html?.[line - 1] != null ? ( |
| 142 | <span dangerouslySetInnerHTML={{ __html: html[line - 1] || " " }} /> |
| 143 | ) : ( |
| 144 | lines[line - 1] || " " |
| 145 | )} |
| 146 | </td> |
| 147 | </tr>, |
| 148 | ); |
| 149 | // The explanation opens right under the line clicked. |
| 150 | if (picked?.line === line) { |
| 151 | rows.push( |
| 152 | <tr key={`why-${line}`}> |
| 153 | <td colSpan={4} className="border-y border-line bg-bg/60 px-3 py-3 font-sans whitespace-normal md:pl-80"> |
| 154 | <div className="max-w-2xl overflow-hidden rounded-xl border border-merged/30 bg-surface shadow-xl shadow-black/40"> |
| 155 | <WhyPanel |
| 156 | base={base} |
| 157 | picked={picked} |
| 158 | loading={why.state === "loading"} |
| 159 | data={why.data} |
| 160 | onClose={close} |
| 161 | /> |
| 162 | </div> |
| 163 | </td> |
| 164 | </tr>, |
| 165 | ); |
| 166 | } |
| 167 | } |
| 168 | return <Fragment key={range.start}>{rows}</Fragment>; |
| 169 | })} |
| 170 | </tbody> |
| 171 | </table> |
| 172 | {blame.partial && ( |
| 173 | <p className="border-t border-line px-4 py-2 text-xs text-faint"> |
| 174 | This file's history is long; its oldest lines are credited to the oldest commit read. |
| 175 | </p> |
| 176 | )} |
| 177 | </div> |
| 178 | </div> |
| 179 | ); |
| 180 | } |
| 181 | |
| 182 | type WhyData = Awaited<ReturnType<typeof whyLoader>>; |
| 183 | |
| 184 | function WhyPanel({ |
| 185 | base, |
| 186 | picked, |
| 187 | loading, |
| 188 | data, |
| 189 | onClose, |
| 190 | }: { |
| 191 | base: string; |
| 192 | picked: { commit: string; start: number; end: number; line: number }; |
| 193 | loading: boolean; |
| 194 | data: WhyData | undefined; |
| 195 | onClose: () => void; |
| 196 | }) { |
| 197 | const current = data && data.commit.hash === picked.commit ? data : null; |
| 198 | return ( |
| 199 | <div> |
| 200 | <header className="flex items-center gap-2 border-b border-line px-4 py-3"> |
| 201 | <Sparkles size={15} className="text-merged" /> |
| 202 | <span className="grow text-sm font-medium"> |
| 203 | Why line{picked.start === picked.end ? ` ${picked.start}` : `s ${picked.start}–${picked.end}`} |
| 204 | </span> |
| 205 | <button |
| 206 | type="button" |
| 207 | onClick={onClose} |
| 208 | aria-label="Close" |
| 209 | className="rounded p-1 text-faint hover:bg-raised hover:text-fg" |
| 210 | > |
| 211 | <X size={14} /> |
| 212 | </button> |
| 213 | </header> |
| 214 | {!current ? ( |
| 215 | <div className="space-y-3 p-4" aria-busy={loading}> |
| 216 | {[70, 90, 55].map((width) => ( |
| 217 | <span key={width} className="block h-3 animate-pulse rounded bg-raised" style={{ width: `${width}%` }} /> |
| 218 | ))} |
| 219 | </div> |
| 220 | ) : ( |
| 221 | <div className="divide-y divide-line text-sm"> |
| 222 | <section className="p-4"> |
| 223 | <p className="flex items-center gap-1.5 text-xs text-faint"> |
| 224 | <GitCommitHorizontal size={13} /> |
| 225 | Last changed in |
| 226 | </p> |
| 227 | <Link |
| 228 | to={`${base}/commit/${current.commit.hash}`} |
| 229 | className="mt-1.5 block font-medium hover:text-accent" |
| 230 | > |
| 231 | {current.commit.message.split("\n")[0]} |
| 232 | </Link> |
| 233 | <p className="mt-1.5 flex items-center gap-2 text-xs text-muted"> |
| 234 | <Avatar name={current.commit.author.name} size={14} /> |
| 235 | {current.commit.author.name} · <TimeAgo at={current.commit.authoredAt} /> ·{" "} |
| 236 | <span className="font-mono">{current.commit.hash.slice(0, 7)}</span> |
| 237 | </p> |
| 238 | </section> |
| 239 | |
| 240 | {current.pull ? ( |
| 241 | <section className="p-4"> |
| 242 | <p className="flex items-center gap-1.5 text-xs text-faint"> |
| 243 | <GitPullRequest size={13} /> |
| 244 | Arrived in |
| 245 | </p> |
| 246 | <Link |
| 247 | to={`${base}/pull/${current.pull.number}`} |
| 248 | className="mt-1.5 block font-medium hover:text-accent" |
| 249 | > |
| 250 | {current.pull.title} <span className="font-normal text-faint">#{current.pull.number}</span> |
| 251 | </Link> |
| 252 | <p className="mt-1.5 flex flex-wrap items-center gap-x-2 gap-y-1 text-xs text-muted"> |
| 253 | {current.pull.runtime === "hosted" ? ( |
| 254 | <span className="flex items-center gap-1 text-merged"> |
| 255 | <Sparkles size={11} /> |
| 256 | written by {current.pull.agent} |
| 257 | </span> |
| 258 | ) : ( |
| 259 | <span>by {current.pull.author} with {current.pull.agent}</span> |
| 260 | )} |
| 261 | {current.pull.mergedBy && <span>· merged by {current.pull.mergedBy}</span>} |
| 262 | </p> |
| 263 | </section> |
| 264 | ) : ( |
| 265 | <section className="p-4 text-xs text-muted"> |
| 266 | Pushed straight to the branch, not through a pull request, so there is no |
| 267 | conversation behind it. |
| 268 | </section> |
| 269 | )} |
| 270 | |
| 271 | {current.issue && ( |
| 272 | <section className="p-4"> |
| 273 | <p className="flex items-center gap-1.5 text-xs text-faint"> |
| 274 | <CircleDot size={13} /> |
| 275 | What was asked for |
| 276 | </p> |
| 277 | <Link |
| 278 | to={`${base}/issues/${current.issue.number}`} |
| 279 | className="mt-1.5 block font-medium hover:text-accent" |
| 280 | > |
| 281 | {current.issue.title} <span className="font-normal text-faint">#{current.issue.number}</span> |
| 282 | </Link> |
| 283 | {current.issue.body && ( |
| 284 | <div className="mt-2 max-h-48 overflow-hidden text-muted [&_*]:text-[0.8125rem]! [&_*]:leading-5! mask-[linear-gradient(to_bottom,black_70%,transparent)]"> |
| 285 | <Markdown source={current.issue.body} repo={repoAt(base)} /> |
| 286 | </div> |
| 287 | )} |
| 288 | </section> |
| 289 | )} |
| 290 | |
| 291 | {current.steps.length > 0 && current.pull && ( |
| 292 | <section className="p-4"> |
| 293 | <p className="flex items-center gap-1.5 text-xs text-faint"> |
| 294 | <MessagesSquare size={13} /> |
| 295 | In the agent's words |
| 296 | </p> |
| 297 | {current.steps |
| 298 | .filter((step) => step.kind === "message") |
| 299 | .map((step) => ( |
| 300 | <blockquote |
| 301 | key={step.seq} |
| 302 | className="mt-3 border-l-2 border-merged/50 pl-3 text-[0.8125rem] whitespace-pre-line text-fg/90" |
| 303 | > |
| 304 | {step.text} |
| 305 | </blockquote> |
| 306 | ))} |
| 307 | {current.steps.some((step) => step.kind === "tool_call") && ( |
| 308 | <details className="group mt-3"> |
| 309 | <summary className="cursor-pointer text-xs text-muted hover:text-fg"> |
| 310 | What it ran ({current.steps.filter((step) => step.kind === "tool_call").length}) |
| 311 | </summary> |
| 312 | <ol className="mt-2 space-y-1.5"> |
| 313 | {current.steps |
| 314 | .filter((step) => step.kind === "tool_call") |
| 315 | .map((step) => ( |
| 316 | <li key={step.seq} className="flex items-start gap-1.5 font-mono text-xs text-muted"> |
| 317 | <Terminal size={12} className="mt-0.5 shrink-0" /> |
| 318 | <span className="line-clamp-2 break-all"> |
| 319 | <span className="text-fg">{step.tool}</span> {step.text.split("\n")[0]} |
| 320 | </span> |
| 321 | </li> |
| 322 | ))} |
| 323 | </ol> |
| 324 | </details> |
| 325 | )} |
| 326 | <Link |
| 327 | to={`${base}/pull/${current.pull.number}?tab=session`} |
| 328 | className="mt-4 inline-flex items-center gap-1 text-xs text-accent hover:underline" |
| 329 | > |
| 330 | The whole session |
| 331 | <ArrowUpRight size={12} /> |
| 332 | </Link> |
| 333 | </section> |
| 334 | )} |
| 335 | </div> |
| 336 | )} |
| 337 | </div> |
| 338 | ); |
| 339 | } |