import { useEffect, useState } from "react"; import { parseUnifiedDiff } from "../lib/diff"; import { useUiStore } from "../lib/store"; import { age } from "../lib/ui"; import type { CommitInfo, FileDiff } from "../types"; import { ScaleIn } from "./amicro/scale-in"; import { LoadingField } from "./common"; import { DiffViewer } from "./DiffViewer"; import { useFlow } from "./flow"; const firstLine = (s: string) => { const l = (s ?? "").split("\n").find((x) => x.trim()) ?? ""; return l.length > 110 ? l.slice(0, 110) + "…" : l; }; /** * Full-screen modal that renders a single commit's diff by sha. Opened from * agent-run cards (the commit an agent pushed) and from the activity feed's * "{user} pushed {hash}" events. Diff + metadata come straight from the GitHub * API, so the commit must be on the remote (every agent push and timeline * commit is). One repo per window, so the active flow's client is the right one. */ export function CommitDiffModal() { const view = useUiStore((s) => s.commitView); const close = useUiStore((s) => s.closeCommit); const { ctx } = useFlow(); const [files, setFiles] = useState(null); const [meta, setMeta] = useState(null); const [err, setErr] = useState(""); const repo = view?.repo; const sha = view?.sha; useEffect(() => { if (!repo || !sha) return; setFiles(null); setMeta(null); setErr(""); let cancelled = false; void (async () => { try { const [diff, info] = await Promise.all([ ctx.gh.getCommitDiff(repo, sha), ctx.gh.getCommit(repo, sha).catch(() => null), ]); if (cancelled) return; setFiles(parseUnifiedDiff(diff)); if (info) setMeta(info); } catch (e) { if (!cancelled) setErr(e instanceof Error ? e.message : String(e)); } })(); return () => { cancelled = true; }; }, [repo, sha, ctx.gh]); useEffect(() => { if (!view) return; const onKey = (e: KeyboardEvent) => { if (e.key === "Escape") close(); }; document.addEventListener("keydown", onKey); return () => document.removeEventListener("keydown", onKey); }, [view, close]); if (!view) return null; return (
e.stopPropagation()}>
{view.sha.slice(0, 10)} {meta?.message && {firstLine(meta.message)}} {meta?.url && ( ↗ )}
{meta && (
{meta.author && {meta.author}} {meta.date ? ` committed ${age(meta.date)}` : ""} {" · "} +{meta.additions} −{meta.deletions} {" · "} {meta.filesChanged} file{meta.filesChanged === 1 ? "" : "s"}
)}
{err &&

{err}

} {!files && !err && } {files && files.length === 0 && !err && (

No file changes in this commit (it may be empty or a merge commit).

)} {files && files.length > 0 && }
); }