/** * The commits and changes of the new pull request (`GHPRCreateComponentFactory.changesPanel`): * "Changes" / "Changes from" with the commit chooser ("{n} commits", "All commits (n)"), * the selected commit's details (Show / Hide details) and the changes tree, whose * Show Diff opens the files in the diff viewer as one chain. */ import { useMemo, useRef, useState, type MouseEvent, type ReactNode } from "react"; import { buildFileTree, dirKeys, type FileTreeNode } from "@/features/vcs/fileTree"; import { openDiff, withChain } from "@/features/vcs/diffInput"; import { KIND_COLOR, fileCount, renameFrom, type EffectiveKind } from "@/features/vcs/model"; import type { Commit } from "@/platform/ipc"; import { tx } from "@/platform/i18n"; import { Icon, IconButton, Spinner, T, Tree, fileIconName, showContextMenu, type MenuItem } from "@/ui"; import { ChooserPopup } from "../ui/ChooserPopup"; import { prettyDate } from "../ui/prettyDate"; import { commitDescription } from "./model"; import { selectCommit, useCreatePr, type CreateChange } from "./store"; import styles from "./Create.module.css"; function Centered({ children, error }: { children: ReactNode; error?: boolean }) { return
{children}
; } export function ChangesPanel() { const commits = useCreatePr((s) => s.commits); if (!commits) return ( Select pull request base and head to view changes ); if (commits.state === "loading") return ( ); if (commits.state === "error") return (
Failed to load commits
{commits.error}
); if (!commits.value.length) return ( Pull request does not contain any commits ); return ; } function CommitsAndChanges({ commits }: { commits: Commit[] }) { const selectedId = useCreatePr((s) => s.selectedCommit); const index = commits.findIndex((c) => c.id === selectedId); const selected = index >= 0 ? commits[index]! : null; return (
{commits.length === 1 ? tx("Changes") : tx("Changes from")} {commits.length > 1 && selected && ( <> selectCommit(commits[index + 1]!.id)} /> selectCommit(commits[index - 1]!.id)} /> )}
{selected && }
); } function CommitChooser({ commits, selected }: { commits: Commit[]; selected: Commit | null }) { const ref = useRef(null); const [open, setOpen] = useState(false); const items = useMemo>(() => [null, ...commits], [commits]); return ( <> {open && ( mode="single" anchor={ref} items={items} getKey={(c) => c?.id ?? "all"} present={(c) => c ? { text: c.summary, detail: `${c.author}, ${c.timeMs ? prettyDate(c.timeMs) : ""}`, icon: selected?.id === c.id ? "check" : "commit", } : { text: tx`All commits (${commits.length})`, icon: selected ? "commit" : "check" } } placeholder="Search commits" aria-label="Commits" width={360} onChoose={(c) => selectCommit(c?.id ?? null)} onClose={() => setOpen(false)} /> )} ); } /** `CodeReviewDetailsCommitInfoComponentFactory`: the subject, and with the details the body, author and date. */ function CommitDetails({ commit }: { commit: Commit }) { const [details, setDetails] = useState(false); const description = commitDescription(commit.message); return (
{commit.summary}
{details && description &&
{description}
} {details && (
{commit.author}, {commit.timeMs ? prettyDate(commit.timeMs) : ""}
)}
); } const kindOf = (c: CreateChange): EffectiveKind => (c.kind === "untracked" ? "added" : c.kind); /** Show Diff: `first` with every file of the tree in the chain (F7 moves along). */ function showDiff(all: readonly CreateChange[], first: CreateChange) { void openDiff( withChain( all.map((c) => c.diff), Math.max(0, all.indexOf(first)), ), ); } function ChangesTree() { const changes = useCreatePr((s) => s.changes); const files = changes?.state === "ok" ? changes.value : null; const roots = useMemo(() => (files ? buildFileTree(files, (f) => f.path) : []), [files]); const expanded = useMemo(() => dirKeys(roots), [roots]); const ordered = useMemo(() => { const out: CreateChange[] = []; const walk = (ns: readonly FileTreeNode[]) => ns.forEach((n) => (n.file ? out.push(n.file) : walk(n.children))); walk(roots); return out; }, [roots]); const [selection, setSelection] = useState>(new Set()); if (!changes || changes.state === "loading") return ( ); if (changes.state === "error") return (
Failed to load changes
{changes.error}
); const selectedFiles = (keys: ReadonlySet) => ordered.filter((f) => keys.has(`f:${f.path}`)); const menu = (node: FileTreeNode | null, keys: Set, e: MouseEvent) => { const picked = selectedFiles(keys); const first = picked[0] ?? node?.file ?? null; const items: MenuItem[] = [ { label: "Show Diff", icon: "diff", disabled: !first, run: () => first && showDiff(ordered, first) }, ]; showContextMenu(e, items); }; return ( > key={changes.value.length ? "files" : "empty"} className={styles.tree} roots={roots} getKey={(n) => n.key} getLabel={(n) => n.name} getChildren={(n) => n.children} isExpandable={(n) => !n.file} defaultExpanded={expanded} selected={selection} onSelectionChange={setSelection} multiSelect rowHeight={22} aria-label="Changes" speedSearch onActivate={(n) => n.file && showDiff(ordered, n.file)} onContextMenu={menu} empty={ Commit does not contain any changes } renderNode={(n) => } /> ); } function ChangeRow({ node }: { node: FileTreeNode }) { if (!node.file) return ( {node.name} {fileCount(node.count)} ); const f = node.file; return ( {node.name} {f.oldPath && {renameFrom(f.path, f.oldPath)}} ); }