Diff Timeline
What an agent changed in a pull request, edit by edit: added and removed lines as two growing curves, a diff bar per file, and the edit under the pointer shown as a diff.
"use client"
import * as React from "react"
import { FileCode2, GitPullRequest } from "lucide-react"
import {
AgentGlyph,
AiKitStyles,
AiStyle,
cx,
Roll,
type AiTone,
} from "@/lib/ai-kit"
export interface DiffLine {
kind: "+" | "-" | " "
text: string
}
export interface DiffEdit {
/** Seconds into the run. */
at: number
/** Path of the file, e.g. "components/hero.tsx". */
file: string
added: number
removed: number
/** A few lines of the edit, shown on hover. */
lines?: readonly DiffLine[]
}
export interface DiffTimelineProps {
agent: { name: string; tone: AiTone }
/** Pull request label, e.g. "PR #214". */
pr: string
edits: readonly DiffEdit[]
/** Length of the run in seconds. */
length: number
/** Playhead in seconds: edits after it have not happened yet. */
time: number
className?: string
}
const W = 1000
const H = 260
const CSS = `
.aik-df{width:min(100%,1000px);container-type:inline-size}
.aik-df-card{--aik-r-shell:24px;--aik-rim:5px}
.aik-df-in{padding:20px 22px 12px}
.aik-df-head{display:flex;align-items:center;gap:14px;padding-bottom:20px}
.aik-df-tile{display:grid;place-items:center;width:40px;height:40px;flex:none;border-radius:12px;background:color-mix(in oklab,var(--c) 13%,var(--aik-card));box-shadow:inset 0 0 0 1px color-mix(in oklab,var(--c) 26%,transparent)}
.aik-df-who{display:grid;gap:2px;min-width:0;margin-right:auto}
.aik-df-who b{display:flex;align-items:center;gap:8px;font-weight:600;font-size:16px;white-space:nowrap}
.aik-df-who b span{font-weight:400;color:var(--aik-text-3);display:inline-flex;align-items:center;gap:5px}
.aik-df-who small{font-size:14px;color:var(--aik-text-3);white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.aik-df-who small strong{font-weight:500;color:var(--aik-text)}
.aik-df-chip{height:28px;padding:0 9px;display:inline-flex;align-items:center;border-radius:8px;font:500 14px var(--aik-mono);font-variant-numeric:tabular-nums}
.aik-df-chip[data-k="+"]{color:var(--aik-green);background:color-mix(in oklab,var(--aik-green) 15%,transparent)}
.aik-df-chip[data-k="-"]{color:var(--aik-red);background:color-mix(in oklab,var(--aik-red) 14%,transparent)}
.aik-df-files{font-size:14.5px;color:var(--aik-text-3);white-space:nowrap}
.aik-df-chart{display:grid;grid-template-columns:44px minmax(0,1fr);grid-template-rows:${H}px 30px}
.aik-df-y{position:relative}
.aik-df-y span{position:absolute;right:10px;translate:0 -50%;font:12.5px var(--aik-mono);color:var(--aik-text-3)}
.aik-df-plot{position:relative;cursor:crosshair}
.aik-df-svg{position:absolute;inset:0;width:100%;height:100%;overflow:visible}
.aik-df-svg .grid{stroke:var(--aik-line-2);stroke-width:1;stroke-dasharray:2 5;vector-effect:non-scaling-stroke}
.aik-df-svg .base{stroke:var(--aik-line-3);stroke-width:1;vector-effect:non-scaling-stroke}
.aik-df-svg .add{fill:none;stroke:var(--aik-green);stroke-width:2.25;stroke-linejoin:round;vector-effect:non-scaling-stroke}
.aik-df-svg .rem{fill:none;stroke:var(--aik-red);stroke-width:2.25;stroke-linejoin:round;vector-effect:non-scaling-stroke}
.aik-df-svg .net{fill:none;stroke:var(--aik-text-3);stroke-width:1.5;stroke-dasharray:4 4;vector-effect:non-scaling-stroke}
.aik-df-svg .guide{stroke:var(--aik-text-3);stroke-width:1;stroke-dasharray:2 3;vector-effect:non-scaling-stroke}
.aik-df-head-dot{position:absolute;width:11px;height:11px;margin:-5.5px 0 0 -5.5px;border-radius:50%;background:var(--c);box-shadow:0 0 0 3px var(--aik-card),0 0 0 4.5px color-mix(in oklab,var(--c) 40%,transparent),0 0 16px var(--c);pointer-events:none;transition:top 360ms var(--aik-ease-move)}
.aik-df-head-dot[data-k="net"]{width:7px;height:7px;margin:-3.5px 0 0 -3.5px;box-shadow:none}
.aik-df-x{position:relative;grid-column:2}
.aik-df-x span{position:absolute;top:9px;translate:-50% 0;font:12.5px var(--aik-mono);color:var(--aik-text-3)}
.aik-df-x span:first-child{translate:0 0}
.aik-df-x span:last-child{translate:-100% 0}
.aik-df-tip{position:absolute;z-index:2;width:330px;padding:12px 0 8px;border-radius:13px;background:var(--aik-card);box-shadow:var(--aik-sh-pop);pointer-events:none;animation:aik-df-tip 200ms var(--aik-ease-out) both}
.aik-df-tip header{display:flex;align-items:center;gap:8px;padding:0 13px 9px;font-size:13.5px;border-bottom:1px solid var(--aik-line)}
.aik-df-tip header b{font-weight:500;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;margin-right:auto}
.aik-df-tip header span{font:12.5px var(--aik-mono)}
.aik-df-tip pre{margin:6px 0 0;font:12.5px/20px var(--aik-mono);overflow:hidden}
.aik-df-tip pre div{padding:0 13px;white-space:pre;overflow:hidden;text-overflow:ellipsis}
.aik-df-tip pre div[data-k="+"]{color:var(--aik-green);background:color-mix(in oklab,var(--aik-green) 10%,transparent)}
.aik-df-tip pre div[data-k="-"]{color:var(--aik-red);background:color-mix(in oklab,var(--aik-red) 9%,transparent)}
.aik-df-tip pre div[data-k=" "]{color:var(--aik-text-3)}
.aik-df-list{display:grid;margin-top:12px}
.aik-df-row{display:grid;grid-template-columns:minmax(0,1.2fr) minmax(0,1fr) 92px;align-items:center;gap:18px;height:48px;padding:0 12px;border-radius:11px;transition:background var(--aik-d-panel)}
.aik-df-row[data-on]{background:var(--aik-raised)}
.aik-df-row[data-wait]{opacity:.45}
.aik-df-name{display:flex;align-items:center;gap:10px;min-width:0;font-size:15px;white-space:nowrap}
.aik-df-name svg{color:var(--aik-blue);flex:none}
.aik-df-row[data-on] .aik-df-name svg{animation:aik-df-blink 1s ease-in-out infinite}
.aik-df-name span{overflow:hidden;text-overflow:ellipsis;color:var(--aik-text-3)}
.aik-df-name span b{font-weight:500;color:var(--aik-text)}
.aik-df-bars{position:relative;height:8px}
.aik-df-bars i{position:absolute;left:0;top:0;width:100%;height:8px;border-radius:3px;transform-origin:left center;transition:transform 520ms var(--aik-ease-move)}
.aik-df-bars i:first-child{background:color-mix(in oklab,var(--aik-green) 75%,transparent)}
.aik-df-bars i:last-child{background:color-mix(in oklab,var(--aik-red) 75%,transparent)}
.aik-df-count{display:flex;justify-content:flex-end;gap:10px;font:500 13.5px var(--aik-mono);font-variant-numeric:tabular-nums}
.aik-df-count span:first-child{color:var(--aik-green)}
.aik-df-count span:last-child{color:var(--aik-red)}
@keyframes aik-df-tip{from{opacity:0;transform:translateY(4px)}}
@keyframes aik-df-blink{50%{opacity:.35}}
@container (max-width:620px){
.aik-df-in{padding:16px 14px 10px}
.aik-df-head{flex-wrap:wrap}
.aik-df-files{display:none}
.aik-df-chart{grid-template-columns:34px minmax(0,1fr);grid-template-rows:200px 30px}
.aik-df-row{grid-template-columns:minmax(0,1fr) 80px;gap:10px;padding:0 6px}
.aik-df-bars{display:none}
.aik-df-tip{width:250px}
}
@media (prefers-reduced-motion:reduce){.aik-df-row[data-on] .aik-df-name svg{animation:none}}
`
function clock(s: number) {
const v = Math.max(0, Math.round(s))
return `${Math.floor(v / 60)}:${String(v % 60).padStart(2, "0")}`
}
function split(path: string) {
const i = path.lastIndexOf("/")
return i < 0 ? ["", path] : [path.slice(0, i + 1), path.slice(i + 1)]
}
/**
* What an agent changed in a pull request, edit by edit: lines added and
* removed as two curves growing over the run, a diff bar per file, and the
* diff of whichever edit sits under the pointer.
*/
export function DiffTimeline({
agent,
pr,
edits,
length,
time,
className,
}: DiffTimelineProps) {
const plot = React.useRef<HTMLDivElement>(null)
const [hover, setHover] = React.useState<number | null>(null)
const sorted = [...edits].sort((a, b) => a.at - b.at)
const done = sorted.filter((e) => e.at <= time)
const totalAdd = sorted.reduce((a, e) => a + e.added, 0)
const max = Math.max(100, Math.ceil((totalAdd * 1.25) / 100) * 100)
const x = (t: number) => (t / length) * W
const y = (v: number) => H - (v / max) * H
// Cumulative series with a short ramp into each edit, so steps read as motion.
const series = (pick: (e: DiffEdit) => number) => {
let v = 0
let d = `M0 ${y(0)}`
for (const e of done) {
const a = Math.max(0, x(e.at) - 14)
d += `H${a}`
v += pick(e)
d += `C${a + 7} ${y(v - pick(e))} ${x(e.at) - 7} ${y(v)} ${x(e.at)} ${y(v)}`
}
d += `H${x(time)}`
return { d, v }
}
const add = series((e) => e.added)
const rem = series((e) => e.removed)
const net = series((e) => e.added - e.removed)
const area = (d: string) => `${d}V${H}H0Z`
const files = Array.from(new Set(sorted.map((e) => e.file)))
const perFile = files.map((file) => {
const mine = done.filter((e) => e.file === file)
return {
file,
added: mine.reduce((a, e) => a + e.added, 0),
removed: mine.reduce((a, e) => a + e.removed, 0),
started: mine.length > 0,
}
})
const biggest = Math.max(1, ...perFile.map((f) => f.added + f.removed))
const current = done.at(-1)
const shown = hover !== null ? done[hover] : undefined
const guideT = shown ? shown.at : time
const onMove = (event: React.PointerEvent) => {
if (!plot.current || !done.length) return
const box = plot.current.getBoundingClientRect()
const t = ((event.clientX - box.left) / box.width) * length
let best = 0
done.forEach((e, i) => {
if (Math.abs(e.at - t) < Math.abs(done[best].at - t)) best = i
})
setHover(best)
}
const valueAt = (t: number, pick: (e: DiffEdit) => number) =>
done.filter((e) => e.at <= t).reduce((a, e) => a + pick(e), 0)
return (
<div
className={cx("aik aik-df", className)}
style={{ "--c": `var(--aik-f-${agent.tone})` } as React.CSSProperties}
>
<AiKitStyles />
<AiStyle id="diff-timeline" css={CSS} />
<div className="aik-shell aik-df-card">
<div className="aik-surface aik-df-in">
<div className="aik-df-head">
<span className="aik-df-tile">
<AgentGlyph
seed={agent.name}
tone={agent.tone}
size={18}
live={time < length}
/>
</span>
<span className="aik-df-who">
<b>
{agent.name}
<span>
<GitPullRequest size={14} aria-hidden="true" />
{pr}
</span>
</b>
<small>
{current ? (
<Roll>{`Editing ${current.file}`}</Roll>
) : (
"Reading the codebase"
)}
</small>
</span>
<span className="aik-df-chip" data-k="+">
+<Roll>{String(add.v)}</Roll>
</span>
<span className="aik-df-chip" data-k="-">
ā<Roll>{String(rem.v)}</Roll>
</span>
<span className="aik-df-files">across {files.length} files</span>
</div>
<div className="aik-df-chart">
<div className="aik-df-y" aria-hidden="true">
{[0, 0.25, 0.5, 0.75, 1].map((f) => (
<span key={f} style={{ top: `${(1 - f) * 100}%` }}>
{Math.round(max * f)}
</span>
))}
</div>
<div
ref={plot}
className="aik-df-plot"
onPointerMove={onMove}
onPointerLeave={() => setHover(null)}
>
<svg
className="aik-df-svg"
viewBox={`0 0 ${W} ${H}`}
preserveAspectRatio="none"
aria-hidden="true"
>
<defs>
<linearGradient id="aik-df-ga" x1="0" x2="0" y1="0" y2="1">
<stop
offset="0"
stopColor="var(--aik-green)"
stopOpacity=".22"
/>
<stop
offset="1"
stopColor="var(--aik-green)"
stopOpacity="0"
/>
</linearGradient>
<linearGradient id="aik-df-gr" x1="0" x2="0" y1="0" y2="1">
<stop
offset="0"
stopColor="var(--aik-red)"
stopOpacity=".2"
/>
<stop
offset="1"
stopColor="var(--aik-red)"
stopOpacity="0"
/>
</linearGradient>
</defs>
{[0.25, 0.5, 0.75, 1].map((f) => (
<line
key={f}
className="grid"
x1={0}
x2={W}
y1={y(max * f)}
y2={y(max * f)}
/>
))}
<line className="base" x1={0} x2={W} y1={H} y2={H} />
<path d={area(add.d)} fill="url(#aik-df-ga)" />
<path d={area(rem.d)} fill="url(#aik-df-gr)" />
<path className="net" d={net.d} />
<path className="add" d={add.d} />
<path className="rem" d={rem.d} />
<line
className="guide"
x1={x(guideT)}
x2={x(guideT)}
y1={0}
y2={H}
/>
</svg>
{(
[
["add", "var(--aik-green)", (e: DiffEdit) => e.added],
[
"net",
"var(--aik-text-3)",
(e: DiffEdit) => e.added - e.removed,
],
["rem", "var(--aik-red)", (e: DiffEdit) => e.removed],
] as const
).map(([k, c, pick]) => (
<span
key={k}
className="aik-df-head-dot"
data-k={k}
style={
{
left: `${(guideT / length) * 100}%`,
top: `${(y(valueAt(guideT, pick)) / H) * 100}%`,
"--c": c,
} as React.CSSProperties
}
/>
))}
{shown ? (
<div
className="aik-df-tip"
style={{
left: `clamp(0px, calc(${(shown.at / length) * 100}% + 16px), calc(100% - 330px))`,
top: 6,
}}
>
<header>
<FileCode2 size={14} aria-hidden="true" />
<b>{shown.file}</b>
<span style={{ color: "var(--aik-green)" }}>
+{shown.added}
</span>
<span style={{ color: "var(--aik-red)" }}>
ā{shown.removed}
</span>
</header>
<pre>
{(shown.lines ?? []).map((line, i) => (
<div key={i} data-k={line.kind}>
{line.kind} {line.text}
</div>
))}
</pre>
</div>
) : null}
</div>
<div className="aik-df-x" aria-hidden="true">
{Array.from(
{ length: Math.floor(length / 60) + 1 },
(_, i) => i * 60
).map((t) => (
<span key={t} style={{ left: `${(t / length) * 100}%` }}>
{clock(t)}
</span>
))}
</div>
</div>
<div className="aik-df-list">
{perFile.map((f) => {
const [dir, name] = split(f.file)
return (
<div
key={f.file}
className="aik-df-row"
data-on={(shown ?? current)?.file === f.file ? "" : undefined}
data-wait={f.started ? undefined : ""}
>
<span className="aik-df-name">
<FileCode2 size={17} aria-hidden="true" />
<span>
{dir}
<b>{name}</b>
</span>
</span>
<span className="aik-df-bars" aria-hidden="true">
{/* Both bars span the track and scale from the left; the
removed bar is pushed past the added one with translate,
so growth is a transform, never a width change. */}
<i style={{ transform: `scaleX(${f.added / biggest})` }} />
<i
style={{
transform: `translateX(calc(${(f.added / biggest) * 100}% + ${f.added ? 3 : 0}px)) scaleX(${f.removed / biggest})`,
}}
/>
</span>
<span className="aik-df-count">
<span>+{f.added}</span>
<span>ā{f.removed}</span>
</span>
</div>
)
})}
</div>
</div>
</div>
</div>
)
}
Installation
One command adds diff-timeline and the shared AI Kit layer it is built on (tokens, faces, marks and brand icons). No animation library, light and dark built in.
$npx shadcn@latest add https://tweenui.dev/r/diff-timeline.json