/** Create Pull Request, Submit Review and Checkout-by-number dialogs, rendered by one overlay. */ import { Fragment, useEffect, useState, type ReactNode } from "react"; import { create } from "zustand"; import type { ReviewEvent } from "@/platform/ipc"; import { call, commands } from "@/platform/ipc"; import { Checkbox, Dialog, Input, Select, T } from "@/ui"; import styles from "./Github.module.css"; interface Entry { id: number; node: ReactNode; } const useDialogs = create<{ stack: Entry[] }>(() => ({ stack: [] })); let seq = 0; function open(render: (done: (v: T | null) => void) => ReactNode): Promise { 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) => ( {e.node} ))} ); } export interface CreatePrInput { title: string; body: string; base: string; draft: boolean; push: boolean; } /** Asks for the new pull request's title, description, base branch and options. */ export function createPullRequestDialog(opts: { head: string; defaultBase: string | null; title: string; needsPush: boolean; }) { return open((done) => ); } function CreateDialog(props: { head: string; defaultBase: string | null; title: string; needsPush: boolean; onDone: (v: CreatePrInput | null) => void; }) { const [title, setTitle] = useState(props.title); const [body, setBody] = useState(""); const [base, setBase] = useState(props.defaultBase ?? ""); const [bases, setBases] = useState(props.defaultBase ? [props.defaultBase] : []); const [draft, setDraft] = useState(false); const [push, setPush] = useState(props.needsPush); useEffect(() => { let alive = true; // Base candidates: branches of the remotes (without `origin/`), the default first. call(commands.vcsBranches(null)) .then((branches) => { const names = new Set(); if (props.defaultBase) names.add(props.defaultBase); for (const b of branches) { if (!b.remote || b.name.endsWith("/HEAD")) continue; const short = b.name.slice(b.name.indexOf("/") + 1); if (short !== props.head) names.add(short); } if (alive) { setBases([...names]); setBase((cur) => cur || [...names][0] || ""); } }) .catch(() => undefined); return () => { alive = false; }; }, [props.defaultBase, props.head]); const ok = !!title.trim() && !!base; const submit = () => ok && props.onDone({ title: title.trim(), body, base, draft, push }); return ( props.onDone(null)} width={560} buttons={[ { label: draft ? "Create Draft" : "Create", role: "default", disabled: !ok, onClick: submit }, { label: "Cancel", role: "cancel" }, ]} >
setTitle(e.target.value)} />