Add turn navigation, checkpoint diffs and confirmed rewind

This commit is contained in:
Callum Alpass committed 2026-11-02 20:42:55 +11:00
1 parent 43347d1d58
commit 7eb976f7ac
16 files changed
+634 -39

No files matched your search

+15 -16
View File
@@ -1,3 +1,5 @@
import type { ReviewDiffFileContentsInput } from "@t3tools/contracts";
import { splitPatch } from "../model/diffs.ts";
import type { ScrollBoxRenderable } from "@opentui/core";
import type { RefObject } from "react";
@@ -5,7 +7,9 @@ import { colors, getMarkdownStyle } from "./theme.ts";
export interface DiffState {
readonly cwd: string;
/** null for the whole working tree. */
readonly title?: string;
readonly source?: Pick<ReviewDiffFileContentsInput, "cwd" | "sourceKind" | "baseRef" | "headRef">;
/** null to show every changed file. */
readonly path: string | null;
readonly status: "loading" | "ready" | "error";
readonly patch?: string;
@@ -13,30 +17,31 @@ export interface DiffState {
readonly error?: string;
}
/** Uncommitted changes as unified diffs, one block per file, in place of the conversation. */
export function DiffView(props: { readonly diff: DiffState; readonly scrollRef: RefObject<ScrollBoxRenderable | null> }) {
/** Working-tree or checkpoint changes through the same unified diff renderer. */
export function DiffView(props: { readonly diff: DiffState; readonly scrollRef: RefObject<ScrollBoxRenderable | null>; readonly selected: number; readonly expanded: Readonly<Record<string, { patch?: string; error?: string }>> }) {
const { diff } = props;
const files = diff.patch ? splitPatch(diff.patch) : [];
return (
<box flexGrow={1} minHeight={0} flexDirection="column" paddingLeft={1} paddingRight={1}>
<box flexDirection="row" flexShrink={0} height={1}>
<text fg={colors.muted}>Uncommitted / </text>
<text fg={colors.muted}>{diff.title ?? "Uncommitted"} / </text>
<text fg={colors.text}>
<strong>{diff.path ?? `${files.length} ${files.length === 1 ? "file" : "files"}`}</strong>
</text>
<box flexGrow={1} />
<text fg={colors.faint}>esc close</text>
<text fg={colors.faint}>{diff.source ? "[/] file · x whole file / collapse · esc close" : "[/] file · esc close"}</text>
</box>
{diff.status === "loading" ? <text fg={colors.muted}>Loading diff…</text> : null}
{diff.status === "error" ? <text fg={colors.bad}>{`Could not load the diff: ${diff.error ?? "unknown error"}`}</text> : null}
{diff.status === "ready" && files.length === 0 ? <text fg={colors.muted}>No changes.</text> : null}
{diff.truncated ? <text fg={colors.warn}>The diff was too large and is cut short.</text> : null}
<scrollbox ref={props.scrollRef} flexGrow={1} minHeight={0}>
{files.map((file) => (
<box key={file.path} flexDirection="column" marginTop={1}>
{diff.path ? null : <text fg={colors.accent}>{file.path}</text>}
{files.map((file, index) => (
<box key={file.path} id={`diff-file-${index}`} flexDirection="column" marginTop={1}>
<text fg={index === props.selected ? colors.accent : colors.muted}>{`${index === props.selected ? "▸ " : " "}${file.path}`}</text>
{props.expanded[file.path] && !props.expanded[file.path]?.patch ? <text fg={props.expanded[file.path]?.error ? colors.bad : colors.muted}>{props.expanded[file.path]?.error ?? "Loading full file…"}</text> : null}
<diff
diff={file.patch}
diff={props.expanded[file.path]?.patch ?? file.patch}
view="unified"
{...(filetypeOf(file.path) ? { filetype: filetypeOf(file.path)! } : {})}
syntaxStyle={getMarkdownStyle()}
@@ -58,13 +63,7 @@ export function DiffView(props: { readonly diff: DiffState; readonly scrollRef:
);
}
/** Splits a multi-file patch at each `diff --git` header. */
export function splitPatch(patch: string): { path: string; patch: string }[] {
return patch
.split(/^(?=diff --git )/m)
.filter((chunk) => chunk.startsWith("diff --git "))
.map((chunk) => ({ path: /^diff --git a\/(.+?) b\//.exec(chunk)?.[1] ?? "file", patch: chunk }));
}
export { splitPatch } from "../model/diffs.ts";
const FILETYPES: Record<string, string> = { ts: "typescript", tsx: "tsx", js: "javascript", jsx: "javascript", json: "json", md: "markdown", rs: "rust", py: "python", go: "go", css: "css", html: "html", sh: "bash", yml: "yaml", yaml: "yaml", zig: "zig" };