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