import type { ReviewDiffFileContentsInput } from "@t3tools/contracts"; import { splitPatch } from "../model/diffs.ts"; import type { MouseEvent, ScrollBoxRenderable } from "@opentui/core"; import type { RefObject } from "react"; import { isLeftClick, useHover } from "./Button.tsx"; import { colors, scrollbarOptions, useMarkdownStyle } from "./theme.ts"; export interface DiffState { readonly cwd: string; readonly title?: string; readonly source?: Pick; /** null to show every changed file. */ readonly path: string | null; readonly status: "loading" | "ready" | "error"; readonly patch?: string; readonly truncated?: boolean; readonly error?: string; } /** Working-tree or checkpoint changes through the same diff renderer: side by side when `split`, else unified. */ export function DiffView(props: { readonly diff: DiffState; readonly split: boolean; readonly scrollRef: RefObject; readonly selected: number; readonly expanded: Readonly>; readonly onFile: (index: number) => void }) { const { diff } = props; const markdownStyle = useMarkdownStyle(); const files = diff.patch ? splitPatch(diff.patch) : []; return ( {diff.title ?? "Uncommitted"} / {diff.path ?? `${files.length} ${files.length === 1 ? "file" : "files"}`} {diff.source ? "[/] file · x whole file / collapse · esc close" : "[/] file · esc close"} {diff.status === "loading" ? Loading diff… : null} {diff.status === "error" ? {`Could not load the diff: ${diff.error ?? "unknown error"}`} : null} {diff.status === "ready" && files.length === 0 ? No changes. : null} {diff.truncated ? The diff was too large and is cut short. : null} {files.map((file, index) => ( props.onFile(index)} /> {props.expanded[file.path] && !props.expanded[file.path]?.patch ? {props.expanded[file.path]?.error ?? "Loading full file…"} : null} ))} ); } /** A click selects the file; a click on the selected one shows its whole file, as x does. */ function FileHeader(props: { readonly path: string; readonly selected: boolean; readonly canExpand: boolean; readonly onPress: () => void }) { const [hover, hoverProps] = useHover(); return ( isLeftClick(event) && props.onPress()}> {`${props.selected ? "▸ " : " "}${props.path}`} {hover && props.selected && props.canExpand ? {" click for the whole file"} : null} ); } export { splitPatch } from "../model/diffs.ts"; const FILETYPES: Record = { 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" }; function filetypeOf(path: string): string | undefined { return FILETYPES[path.split(".").pop() ?? ""]; }