import React, { useState } from "react"; /** * CreatePRButton — Claude-Code-on-Web parity PR creation action. * * When clicked, pushes session changes to a new branch and opens a PR. * Shows loading state and links to the created PR on GitHub. */ export default function CreatePRButton({ repo, sessionId, branch, defaultBranch, disabled, onPRCreated, }) { const [creating, setCreating] = useState(false); const [prUrl, setPrUrl] = useState(null); const [error, setError] = useState(null); const handleCreate = async () => { if (!repo || !branch || branch === defaultBranch) return; setCreating(true); setError(null); try { const token = localStorage.getItem("github_token"); const headers = { "Content-Type": "application/json", ...(token ? { Authorization: `Bearer ${token}` } : {}), }; const owner = repo.full_name?.split("/")[0] || repo.owner; const name = repo.full_name?.split("/")[1] || repo.name; const res = await fetch(`/api/repos/${owner}/${name}/pulls`, { method: "POST", headers, body: JSON.stringify({ title: `[GitPilot] Changes from session ${sessionId ? sessionId.slice(0, 8) : branch}`, head: branch, base: defaultBranch || "main", body: [ "## Summary", "", `Changes created by GitPilot AI assistant on branch \`${branch}\`.`, "", sessionId ? `Session ID: \`${sessionId}\`` : "", "", "---", "*This PR was generated by [GitPilot](https://github.com/ruslanmv/gitpilot).*", ] .filter(Boolean) .join("\n"), }), }); const data = await res.json(); if (!res.ok) throw new Error(data.detail || "Failed to create PR"); const url = data.html_url || data.url; setPrUrl(url); onPRCreated?.({ pr_url: url, pr_number: data.number, branch }); } catch (err) { setError(err.message); } finally { setCreating(false); } }; if (prUrl) { return ( View PR on GitHub → ); } return (
{error && (
{error}
)}
); } const styles = { btn: { display: "flex", alignItems: "center", gap: 6, height: 38, padding: "0 14px", borderRadius: 8, border: "1px solid rgba(16, 185, 129, 0.3)", background: "rgba(16, 185, 129, 0.08)", color: "#10B981", fontSize: 13, fontWeight: 600, cursor: "pointer", whiteSpace: "nowrap", transition: "background-color 0.15s", }, prLink: { display: "flex", alignItems: "center", gap: 6, height: 38, padding: "0 14px", borderRadius: 8, background: "rgba(16, 185, 129, 0.10)", color: "#10B981", fontSize: 13, fontWeight: 600, textDecoration: "none", whiteSpace: "nowrap", }, error: { fontSize: 11, color: "#EF4444", marginTop: 4, }, };