mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
204 lines
6.1 KiB
TypeScript
204 lines
6.1 KiB
TypeScript
import { useEffect, useState, type KeyboardEvent } from "react";
|
|
import type { Commit } from "@/bindings";
|
|
import { call, commands, nextRequestId } from "@/platform/ipc";
|
|
import { tx } from "@/platform/i18n";
|
|
import { Dialog, List, SearchField, Spinner, T, listNavigate, selectableText } from "@/ui";
|
|
import { openDialog } from "./dialogs";
|
|
import { annotateAtRevision } from "./annotateRevision";
|
|
import { openVcsDiff } from "./diffInput";
|
|
import { hasFileHistory } from "./fileRevisions";
|
|
import { useCommitDetails } from "./logDetails";
|
|
import { formatTime } from "./model";
|
|
import type { RepoCommit } from "./multiRoot";
|
|
import { repositoryPath } from "./repositoryPaths";
|
|
import { repoOf } from "./store";
|
|
import styles from "./FileRevisionChooser.module.css";
|
|
|
|
export function compareWithFileRevision(path: string | null | undefined) {
|
|
if (!path || !hasFileHistory(path)) return;
|
|
const repo = repoOf(path);
|
|
openDialog((close) => (
|
|
<FileRevisionChooser
|
|
path={path}
|
|
repo={repo}
|
|
onClose={close}
|
|
onChoose={(commit) => {
|
|
close();
|
|
if (repoOf(path) !== repo) return;
|
|
void openVcsDiff({
|
|
path,
|
|
left: {
|
|
type: "rev",
|
|
rev: commit.id,
|
|
label: commit.shortId,
|
|
path: commit.historyPath ? repositoryPath(repo, commit.historyPath) : path,
|
|
},
|
|
right: { type: "worktree" },
|
|
});
|
|
}}
|
|
/>
|
|
));
|
|
}
|
|
|
|
/** Annotate the file as of a revision the user picks: who last changed each line up to there. */
|
|
export function annotateFileRevision(path: string | null | undefined) {
|
|
// No `hasFileHistory` gate: the chooser lists the file's revisions itself, and a menu item
|
|
// that silently does nothing is worse than one that opens an empty list.
|
|
if (!path) return;
|
|
const repo = repoOf(path);
|
|
openDialog((close) => (
|
|
<FileRevisionChooser
|
|
path={path}
|
|
repo={repo}
|
|
onClose={close}
|
|
title={tx("Annotate at Revision…")}
|
|
onChoose={(commit) => {
|
|
close();
|
|
if (repoOf(path) !== repo) return;
|
|
void annotateAtRevision(path, commit);
|
|
}}
|
|
/>
|
|
));
|
|
}
|
|
|
|
function CommitMessage({ commit }: { commit: RepoCommit }) {
|
|
const { details } = useCommitDetails(commit, commit.messageTruncated === true);
|
|
return (
|
|
<div className={`${styles.message} ${selectableText}`}>
|
|
{details?.message ?? (commit.message || commit.summary)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function FileRevisionChooser({
|
|
path,
|
|
repo,
|
|
onClose,
|
|
onChoose,
|
|
title,
|
|
}: {
|
|
path: string;
|
|
repo: string | null;
|
|
onClose: () => void;
|
|
onChoose: (commit: Commit) => void;
|
|
/** "Compare with Revision…" unless the chooser asks for another sentence. */
|
|
title?: string;
|
|
}) {
|
|
const [commits, setCommits] = useState<Commit[] | null>(null);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [query, setQuery] = useState("");
|
|
const [active, setActive] = useState(0);
|
|
useEffect(() => {
|
|
let alive = true;
|
|
let pending = true;
|
|
const request = nextRequestId();
|
|
void call(
|
|
commands.vcsLog(repo, {
|
|
branch: null,
|
|
path,
|
|
author: null,
|
|
text: null,
|
|
skip: 0,
|
|
limit: 0,
|
|
follow: true,
|
|
summaryOnly: true,
|
|
request,
|
|
}),
|
|
)
|
|
.then((rows) => alive && setCommits(rows))
|
|
.catch((e) => alive && setError(String(e)))
|
|
.finally(() => {
|
|
pending = false;
|
|
});
|
|
return () => {
|
|
alive = false;
|
|
if (pending) void commands.cancelRequest(request).catch(() => undefined);
|
|
};
|
|
}, [path, repo]);
|
|
const q = query.trim().toLocaleLowerCase();
|
|
const items = (commits ?? []).filter((c) =>
|
|
`${c.id} ${formatTime(c.timeMs)} ${c.author} ${c.summary}`.toLocaleLowerCase().includes(q),
|
|
);
|
|
const idx = Math.min(active, items.length - 1);
|
|
const selected = items[idx];
|
|
const onKeyDown = (e: KeyboardEvent) => {
|
|
if (e.key === "Enter") {
|
|
e.preventDefault();
|
|
if (items[idx]) onChoose(items[idx]);
|
|
return;
|
|
}
|
|
const next = listNavigate(e.key, { items, active: idx, wrap: true });
|
|
if (next !== null) {
|
|
e.preventDefault();
|
|
setActive(next);
|
|
}
|
|
};
|
|
return (
|
|
<Dialog
|
|
open
|
|
title={title ?? "Compare with Revision…"}
|
|
onClose={onClose}
|
|
resizable="both"
|
|
initialSize={{ w: 600, h: 412 }}
|
|
minWidth={320}
|
|
minHeight={280}
|
|
className={styles.dialog}
|
|
>
|
|
<div className={styles.search}>
|
|
<SearchField
|
|
data-autofocus
|
|
value={query}
|
|
onChange={(value) => {
|
|
setQuery(value);
|
|
setActive(0);
|
|
}}
|
|
onKeyDown={onKeyDown}
|
|
placeholder="Search file revisions"
|
|
aria-label="Search file revisions"
|
|
variant="bare"
|
|
/>
|
|
</div>
|
|
<div className={styles.revisions}>
|
|
{error ? (
|
|
<div className={styles.error} role="alert">
|
|
{error}
|
|
</div>
|
|
) : commits === null ? (
|
|
<Spinner size={14} />
|
|
) : (
|
|
<List
|
|
items={items}
|
|
getKey={(c) => c.id}
|
|
activeIndex={idx}
|
|
onActiveChange={setActive}
|
|
onActivate={onChoose}
|
|
focusable={false}
|
|
hoverSelects
|
|
aria-label="File revisions"
|
|
empty={<T>No commits</T>}
|
|
className={styles.list}
|
|
renderItem={(c) => (
|
|
<span className={styles.row} title={`${c.shortId} ${formatTime(c.timeMs)} ${c.author} ${c.summary}`}>
|
|
<span className={styles.hash}>{c.shortId}</span>
|
|
<span className={styles.date}>{formatTime(c.timeMs)}</span>
|
|
<span className={styles.author}>{c.author}</span>
|
|
</span>
|
|
)}
|
|
/>
|
|
)}
|
|
</div>
|
|
<section
|
|
key={selected?.id}
|
|
className={styles.details}
|
|
aria-label={tx("Commit message")}
|
|
tabIndex={selected ? 0 : undefined}
|
|
>
|
|
<div className={styles.caption} aria-hidden="true">
|
|
<T>Commit message</T>
|
|
</div>
|
|
{selected && <CommitMessage commit={{ ...selected, repo }} />}
|
|
</section>
|
|
</Dialog>
|
|
);
|
|
}
|