/**
* The "New Pull Request at {repo}" tab (IntelliJ's `GHPRCreateComponentFactory`): a
* vertical splitter with the merge direction and the commits and changes above, and the
* title and description, reviewers / assignees / labels, the status line and the
* "Create Pull Request" option button (with "Create Draft Pull Request") below.
*
* Contract: `CreatePanel()` takes no props and renders inside the New Pull Request tab
* while the tool window is connected. Its state (`store.ts`) lives as long as the tab.
*/
import { useEffect, useRef, useState } from "react";
import { useVcs } from "@/features/vcs/store";
import { tx } from "@/platform/i18n";
import {
Button,
Icon,
IconButton,
Input,
Menu,
Popup,
SplitGroup,
SplitHandle,
SplitPanel,
Spinner,
T,
type MenuItem,
} from "@/ui";
import { openPullRequest } from "../toolwindow/open";
import { ChangesPanel } from "./ChangesPanel";
import { DirectionSelector } from "./DirectionSelector";
import { MetadataGrid } from "./MetadataGrid";
import {
canCreate,
createFromForm,
initCreate,
onRepositoryChanged,
refreshBranchesState,
resetCreate,
setDescription,
setTitle,
useCreatePr,
} from "./store";
import styles from "./Create.module.css";
export function CreatePanel() {
const ready = useCreatePr((s) => s.ready);
const initError = useCreatePr((s) => s.initError);
useEffect(() => {
void initCreate();
}, []);
// Commits follow the repository (a new commit, a push, a fetch).
useEffect(
() =>
useVcs.subscribe((s, prev) => {
if (s.version !== prev.version) void onRepositoryChanged();
}),
[],
);
if (initError)
return (
{initError}
);
if (!ready)
return (
);
return (
);
}
/** `CodeReviewTitleDescriptionComponentFactory`: the title and description editors. */
function TextPanel() {
const title = useCreatePr((s) => s.title);
const description = useCreatePr((s) => s.description);
const templateLoading = useCreatePr((s) => s.templateLoading);
return (
setTitle(e.target.value)}
/>
);
}
/** "Details": the error in a popup above the link. */
function ErrorDetails({ title, error }: { title: string; error: string }) {
const ref = useRef(null);
const [open, setOpen] = useState(false);
return (
<>
{open && (
setOpen(false)}
role="dialog"
aria-label={title}
className={styles.errorPopup}
>
{title}
{error}
)}
>
);
}
function ErrorLabel({ text }: { text: string }) {
return (
{text}
);
}
function LoadingLabel({ text }: { text: string }) {
return (
{text}
);
}
/** The branches check, then the creation progress (`statusPanel`). */
function StatusLine() {
const check = useCreatePr((s) => s.check);
const creation = useCreatePr((s) => s.creation);
let checkLine = null;
if (check?.state === "loading") checkLine = ;
else if (check?.state === "error")
checkLine = (
<>
void refreshBranchesState()} />
>
);
else if (check?.state === "ok" && check.value.kind === "alreadyExists") {
const n = check.value.number;
checkLine = (
<>
>
);
} else if (check?.state === "ok" && check.value.kind === "noChanges")
checkLine = ;
let progressLine = null;
if (creation?.kind === "pushing") progressLine = ;
else if (creation?.kind === "creating") progressLine = ;
else if (creation?.kind === "metadata") progressLine = ;
else if (creation?.kind === "error")
progressLine = (
<>
>
);
if (!checkLine && !progressLine) return null;
return (