import * as React from "react"; import { toast } from "sonner"; import { type Project, useProjectPullRequestsQuery, useRepoStateQuery, } from "@/features/projects/hooks"; import { useCreateProjectPullRequestMutation } from "@/features/projects/pullRequestMutations"; import { useProjectRepoSyncStatusQuery } from "@/features/projects/repoSyncHooks"; import { CreateProjectWorkItemDialog, type CreateProjectWorkItemDialogInput, } from "./CreateProjectWorkItemDialog"; export type CreatePullRequestDialogInput = CreateProjectWorkItemDialogInput; export function CreatePullRequestDialog({ initialProjectId, onCreated, onOpenChange, open, projects, reposDir, }: { initialProjectId?: string; onCreated: (project: Project, pullRequestId: string) => void | Promise; onOpenChange: (open: boolean) => void; open: boolean; projects: Project[]; reposDir?: string | null; }) { const initialProject = projects.find((project) => project.id === initialProjectId) ?? projects[0]; const [projectId, setProjectId] = React.useState(initialProject?.id ?? ""); const project = projects.find((candidate) => candidate.id === projectId) ?? initialProject; const repoStateQuery = useRepoStateQuery(project); const pullRequestsQuery = useProjectPullRequestsQuery(project); const initialSyncQuery = useProjectRepoSyncStatusQuery( project, reposDir, project?.defaultBranch, ); const branchOptions = React.useMemo(() => { const names = [ project?.defaultBranch, ...(repoStateQuery.data?.branches.map((branch) => branch.name) ?? []), initialSyncQuery.data?.localBranch, ].filter((name): name is string => Boolean(name)); return [...new Set(names)]; }, [ initialSyncQuery.data?.localBranch, project?.defaultBranch, repoStateQuery.data?.branches, ]); const [targetBranch, setTargetBranch] = React.useState( project?.defaultBranch ?? "", ); const [sourceBranch, setSourceBranch] = React.useState(""); const sourceSyncQuery = useProjectRepoSyncStatusQuery( project, reposDir, sourceBranch || null, targetBranch || null, ); const createMutation = useCreateProjectPullRequestMutation(project); React.useEffect(() => { if (!open) return; const nextProject = projects.find((candidate) => candidate.id === initialProjectId) ?? projects[0]; setProjectId(nextProject?.id ?? ""); }, [initialProjectId, open, projects]); React.useEffect(() => { if (!project) return; setTargetBranch(project.defaultBranch); setSourceBranch(""); }, [project]); React.useEffect(() => { if ( sourceBranch && branchOptions.includes(sourceBranch) && sourceBranch !== targetBranch ) { return; } setSourceBranch( branchOptions.find((branch) => branch !== targetBranch) ?? "", ); }, [branchOptions, sourceBranch, targetBranch]); const sourceCommit = repoStateQuery.data?.branches.find((branch) => branch.name === sourceBranch) ?.commit ?? (sourceSyncQuery.data?.remoteBranch === sourceBranch ? sourceSyncQuery.data.remoteHead : null); const hasOpenPullRequest = (pullRequestsQuery.data ?? []).some( (pullRequest) => (pullRequest.status === "Open" || pullRequest.status === "Draft") && pullRequest.branchName === sourceBranch && (pullRequest.targetBranch ?? project?.defaultBranch) === targetBranch, ); const selectionError = !project ? "Choose a repository." : !targetBranch ? "Choose a base branch." : !sourceBranch ? "Choose a compare branch." : sourceBranch === targetBranch ? "The base and compare branches must be different." : hasOpenPullRequest ? "An open pull request already compares these branches." : !sourceCommit ? "The compare branch must be pushed before opening a pull request." : null; const description = project && sourceBranch && targetBranch ? `${project.name}: ${sourceBranch} → ${targetBranch}${sourceCommit ? ` at ${sourceCommit.slice(0, 7)}` : ""}` : "Choose a repository and branches to compare."; async function handleCreate(input: CreatePullRequestDialogInput) { if (!project || !sourceCommit || selectionError) { throw new Error( selectionError ?? "Pull request branches are incomplete.", ); } const pullRequestId = await createMutation.mutateAsync({ ...input, branch: sourceBranch, targetBranch, commit: sourceCommit, mergeBase: sourceSyncQuery.data?.mergeBase ?? null, reviewers: [], }); toast.success("Pull request created"); await onCreated(project, pullRequestId); } return ( { if (!nextOpen && createMutation.isPending) return; onOpenChange(nextOpen); }} open={open} submitDisabled={Boolean(selectionError)} title="Open a pull request" titlePlaceholder="Describe the change" >
{selectionError ? (

{selectionError}

) : null}
); }