Files
openidea/src/features/github/ui/dialogs.tsx
T
imacross 059fd8a69b feat(github): pull request tool window, timeline and review flow (#477)
Replaces the Pull Requests tool window with a full review workflow on top of gh's authentication.

- List with search, quick filters and paging; a tab per pull request and a New Pull Request tab.
- Details: commit picker, changes tree with viewed state, CI and reviewer status, merge, close and reopen.
- Timeline editor: comments, review threads, reactions and suggested changes.
- Review in the diff viewer and, for the checked-out branch, in the editor (review mode).
- New generic vcs extension points: registerDiffViewerProvider, lineInlays, reviewGutter, branch widget extensions; new vcs_merge_base and gh_api commands.
- CSP: img-src allows https://avatars.githubusercontent.com for avatars.

Fixed while testing against a real repository: remotes behind an SSH host alias, mergeability stuck on UNKNOWN, Apply Locally staying disabled after a checkout, relative links navigating the app's page, and review inlays wider than the pane.

Co-authored-by: imacross <30753807+imacross@users.noreply.github.com>
2026-10-31 07:50:43 +08:00

134 lines
4.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Promise-based dialogs of the GitHub feature, rendered by one overlay (`GithubDialogHost`):
*
* const message = await mergeCommitMessageDialog({ title: "Merge Review", message });
*
* `openDialog` lets other parts add their own (it resolves with what `done` is given,
* null when cancelled). Included: IntelliJ's merge commit message dialog, the pull request
* number prompt of Git › GitHub › Checkout Pull Request….
*/
import { Fragment, useState, type ReactNode } from "react";
import { create } from "zustand";
import { tx } from "@/platform/i18n";
import { Dialog, Input, T } from "@/ui";
import styles from "./ui.module.css";
interface Entry {
id: number;
node: ReactNode;
}
const useDialogs = create<{ stack: Entry[] }>(() => ({ stack: [] }));
let seq = 0;
/** Shows `render(done)` until `done` is called; resolves with its value (null: cancelled). */
export function openDialog<T>(render: (done: (v: T | null) => void) => ReactNode): Promise<T | null> {
return new Promise((resolve) => {
const id = ++seq;
const done = (v: T | null) => {
useDialogs.setState((s) => ({ stack: s.stack.filter((e) => e.id !== id) }));
resolve(v);
};
useDialogs.setState((s) => ({ stack: [...s.stack, { id, node: render(done) }] }));
});
}
export function GithubDialogHost() {
const stack = useDialogs((s) => s.stack);
return (
<>
{stack.map((e) => (
<Fragment key={e.id}>{e.node}</Fragment>
))}
</>
);
}
// ---------------------------------------------------------------------------
// Merge commit message (`ReviewMergeCommitMessageDialog`)
/**
* "Merge Review" / "Squash and Merge Review": the commit message to merge with (subject,
* blank line, body); resolves with the edited message.
*/
export function mergeCommitMessageDialog(opts: { title: string; message: string }) {
return openDialog<string>((done) => <MergeDialog {...opts} onDone={done} />);
}
function MergeDialog(props: { title: string; message: string; onDone: (v: string | null) => void }) {
const [message, setMessage] = useState(props.message);
return (
<Dialog
open
title={props.title}
onClose={() => props.onDone(null)}
width={540}
buttons={[
{ label: "Merge", role: "default", disabled: !message.trim(), onClick: () => props.onDone(message) },
{ label: "Cancel", role: "cancel" },
]}
>
<div className={styles.form}>
<label className={styles.fieldLabel}>
<T>Merge commit message:</T>
</label>
<textarea
data-autofocus
aria-label={tx("Merge commit message")}
className={styles.textarea}
style={{ minHeight: 85 }}
rows={6}
value={message}
onChange={(e) => setMessage(e.target.value)}
/>
</div>
</Dialog>
);
}
// ---------------------------------------------------------------------------
// Pull request number
/** Asks for a pull request number (Git › GitHub › Checkout Pull Request…). */
export function numberDialog(title: string) {
return openDialog<number>((done) => <NumberDialog title={title} onDone={done} />);
}
function NumberDialog(props: { title: string; onDone: (v: number | null) => void }) {
const [value, setValue] = useState("");
const n = Number.parseInt(value.replace(/^#/, ""), 10);
const ok = Number.isInteger(n) && n > 0;
return (
<Dialog
open
title={props.title}
onClose={() => props.onDone(null)}
width={360}
buttons={[
{ label: "OK", role: "default", disabled: !ok, onClick: () => props.onDone(n) },
{ label: "Cancel", role: "cancel" },
]}
>
<form
className={styles.form}
onSubmit={(e) => {
e.preventDefault();
if (ok) props.onDone(n);
}}
>
<label className={styles.fieldLabel}>
<T>Pull request number:</T>
</label>
<Input
data-autofocus
mono
aria-label="Pull request number"
placeholder="#123"
value={value}
onChange={(e) => setValue(e.target.value)}
/>
</form>
</Dialog>
);
}