| 55e9501 | | | 1 | "use client"; |
| 55e9501 | | | 2 | |
| 55e9501 | | | 3 | import { useEffect, useState } from "react"; |
| 55e9501 | | | 4 | import { useParams, useRouter } from "next/navigation"; |
| 55e9501 | | | 5 | import Link from "next/link"; |
| 55e9501 | | | 6 | import { useAuth } from "@/lib/auth"; |
| 55e9501 | | | 7 | import { repos, diffs, type Bookmark } from "@/lib/api"; |
| 55e9501 | | | 8 | import { Select } from "@/app/components/ui"; |
| 55e9501 | | | 9 | |
| 55e9501 | | | 10 | export default function NewDiffPage() { |
| 55e9501 | | | 11 | const { owner, repo } = useParams<{ owner: string; repo: string }>(); |
| 55e9501 | | | 12 | const { user, loading: authLoading } = useAuth(); |
| 55e9501 | | | 13 | const router = useRouter(); |
| 55e9501 | | | 14 | |
| 55e9501 | | | 15 | const [branches, setBranches] = useState<Bookmark[]>([]); |
| 55e9501 | | | 16 | const [defaultBranch, setDefaultBranch] = useState("main"); |
| 55e9501 | | | 17 | const [loadingBranches, setLoadingBranches] = useState(true); |
| 55e9501 | | | 18 | |
| 55e9501 | | | 19 | const [headBranch, setHeadBranch] = useState(""); |
| 55e9501 | | | 20 | const [title, setTitle] = useState(""); |
| 55e9501 | | | 21 | const [description, setDescription] = useState(""); |
| 55e9501 | | | 22 | const [creating, setCreating] = useState(false); |
| 55e9501 | | | 23 | const [error, setError] = useState(""); |
| 55e9501 | | | 24 | |
| 55e9501 | | | 25 | useEffect(() => { |
| 55e9501 | | | 26 | document.title = `New Diff \u00b7 ${repo}`; |
| 55e9501 | | | 27 | }, [repo]); |
| 55e9501 | | | 28 | |
| 55e9501 | | | 29 | useEffect(() => { |
| 55e9501 | | | 30 | if (!authLoading && !user) { |
| 6dd74de | | | 31 | router.push(`/login?redirect=${encodeURIComponent(window.location.pathname)}`); |
| 55e9501 | | | 32 | } |
| 55e9501 | | | 33 | }, [authLoading, user, router]); |
| 55e9501 | | | 34 | |
| 55e9501 | | | 35 | useEffect(() => { |
| 55e9501 | | | 36 | if (!owner || !repo) return; |
| 55e9501 | | | 37 | setLoadingBranches(true); |
| 55e9501 | | | 38 | Promise.all([ |
| 55e9501 | | | 39 | repos.get(owner, repo), |
| 55e9501 | | | 40 | repos.bookmarks(owner, repo), |
| 55e9501 | | | 41 | ]) |
| 55e9501 | | | 42 | .then(([repoData, bookmarkData]) => { |
| 55e9501 | | | 43 | setDefaultBranch(repoData.repo.default_branch || "main"); |
| 55e9501 | | | 44 | const allBranches = bookmarkData.bookmarks || repoData.branches || []; |
| 55e9501 | | | 45 | setBranches(allBranches); |
| 55e9501 | | | 46 | // Pre-select a non-default branch if one exists |
| 55e9501 | | | 47 | const nonDefault = allBranches.find( |
| 55e9501 | | | 48 | (b) => b.name !== (repoData.repo.default_branch || "main") |
| 55e9501 | | | 49 | ); |
| 55e9501 | | | 50 | if (nonDefault) { |
| 55e9501 | | | 51 | setHeadBranch(nonDefault.name); |
| 55e9501 | | | 52 | } |
| 55e9501 | | | 53 | }) |
| 55e9501 | | | 54 | .catch(() => {}) |
| 55e9501 | | | 55 | .finally(() => setLoadingBranches(false)); |
| 55e9501 | | | 56 | }, [owner, repo]); |
| 55e9501 | | | 57 | |
| 55e9501 | | | 58 | async function handleSubmit(e: React.FormEvent) { |
| 55e9501 | | | 59 | e.preventDefault(); |
| 55e9501 | | | 60 | if (!title.trim() || !headBranch) return; |
| 55e9501 | | | 61 | setCreating(true); |
| 55e9501 | | | 62 | setError(""); |
| 55e9501 | | | 63 | try { |
| 55e9501 | | | 64 | const branch = branches.find((b) => b.name === headBranch); |
| 55e9501 | | | 65 | if (!branch) { |
| 55e9501 | | | 66 | setError("Branch not found"); |
| 55e9501 | | | 67 | return; |
| 55e9501 | | | 68 | } |
| 55e9501 | | | 69 | const defaultBr = branches.find((b) => b.name === defaultBranch); |
| 55e9501 | | | 70 | const { diff } = await diffs.create(owner, repo, { |
| 55e9501 | | | 71 | title: title.trim(), |
| 55e9501 | | | 72 | description: description.trim() || undefined, |
| 55e9501 | | | 73 | head_commit: branch.commit_id, |
| 55e9501 | | | 74 | base_commit: defaultBr?.commit_id, |
| 55e9501 | | | 75 | }); |
| 55e9501 | | | 76 | router.push(`/${owner}/${repo}/diffs/${diff.number}`); |
| 55e9501 | | | 77 | } catch (err: unknown) { |
| 55e9501 | | | 78 | setError(err instanceof Error ? err.message : "Failed to create diff"); |
| 55e9501 | | | 79 | } finally { |
| 55e9501 | | | 80 | setCreating(false); |
| 55e9501 | | | 81 | } |
| 55e9501 | | | 82 | } |
| 55e9501 | | | 83 | |
| 55e9501 | | | 84 | if (authLoading || !user) return null; |
| 55e9501 | | | 85 | |
| 55e9501 | | | 86 | const nonDefaultBranches = branches.filter((b) => b.name !== defaultBranch); |
| 55e9501 | | | 87 | |
| 55e9501 | | | 88 | return ( |
| 55e9501 | | | 89 | <div className="max-w-xl mx-auto px-4 py-8"> |
| 55e9501 | | | 90 | <div className="text-sm mb-4"> |
| 55e9501 | | | 91 | <Link |
| 55e9501 | | | 92 | href={`/${owner}/${repo}/diffs`} |
| 55e9501 | | | 93 | style={{ color: "var(--text-muted)" }} |
| 55e9501 | | | 94 | className="hover:underline" |
| 55e9501 | | | 95 | > |
| 55e9501 | | | 96 | Diffs |
| 55e9501 | | | 97 | </Link> |
| 55e9501 | | | 98 | </div> |
| 55e9501 | | | 99 | <form |
| 55e9501 | | | 100 | onSubmit={handleSubmit} |
| 55e9501 | | | 101 | className="p-4 space-y-2.5" |
| 55e9501 | | | 102 | style={{ |
| 55e9501 | | | 103 | backgroundColor: "var(--bg-card)", |
| 55e9501 | | | 104 | border: "1px solid var(--border-subtle)", |
| 55e9501 | | | 105 | }} |
| 55e9501 | | | 106 | > |
| 55e9501 | | | 107 | <h1 className="text-base">New diff</h1> |
| 55e9501 | | | 108 | |
| 55e9501 | | | 109 | <label className="block text-xs" style={{ color: "var(--text-muted)" }}> |
| 55e9501 | | | 110 | Branch |
| 55e9501 | | | 111 | </label> |
| 55e9501 | | | 112 | {loadingBranches ? ( |
| 55e9501 | | | 113 | <div |
| 55e9501 | | | 114 | className="w-full px-3 py-2 text-sm" |
| 55e9501 | | | 115 | style={{ |
| 55e9501 | | | 116 | backgroundColor: "var(--bg-input)", |
| 55e9501 | | | 117 | border: "1px solid var(--border-subtle)", |
| 55e9501 | | | 118 | color: "var(--text-faint)", |
| 55e9501 | | | 119 | }} |
| 55e9501 | | | 120 | > |
| 55e9501 | | | 121 | Loading branches... |
| 55e9501 | | | 122 | </div> |
| 55e9501 | | | 123 | ) : nonDefaultBranches.length === 0 ? ( |
| 55e9501 | | | 124 | <div |
| 55e9501 | | | 125 | className="text-sm px-3 py-2" |
| 55e9501 | | | 126 | style={{ |
| 55e9501 | | | 127 | backgroundColor: "var(--bg-inset)", |
| 55e9501 | | | 128 | border: "1px solid var(--border-subtle)", |
| 55e9501 | | | 129 | color: "var(--text-muted)", |
| 55e9501 | | | 130 | }} |
| 55e9501 | | | 131 | > |
| 55e9501 | | | 132 | No branches to diff. Push a branch other than{" "} |
| 55e9501 | | | 133 | <code className="text-xs font-mono">{defaultBranch}</code> to create a diff. |
| 55e9501 | | | 134 | </div> |
| 55e9501 | | | 135 | ) : ( |
| 55e9501 | | | 136 | <Select |
| 55e9501 | | | 137 | value={headBranch} |
| 55e9501 | | | 138 | onChange={(e) => setHeadBranch(e.target.value)} |
| 55e9501 | | | 139 | options={nonDefaultBranches.map((b) => ({ |
| 55e9501 | | | 140 | value: b.name, |
| 55e9501 | | | 141 | label: `${b.name} \u2192 ${defaultBranch}`, |
| 55e9501 | | | 142 | }))} |
| 55e9501 | | | 143 | aria-label="Branch" |
| 55e9501 | | | 144 | /> |
| 55e9501 | | | 145 | )} |
| 55e9501 | | | 146 | |
| 55e9501 | | | 147 | <label className="block text-xs" style={{ color: "var(--text-muted)" }}> |
| 55e9501 | | | 148 | Title |
| 55e9501 | | | 149 | </label> |
| 55e9501 | | | 150 | <input |
| 55e9501 | | | 151 | value={title} |
| 55e9501 | | | 152 | onChange={(e) => setTitle(e.target.value)} |
| 55e9501 | | | 153 | className="w-full px-3 py-2 text-sm focus:outline-none" |
| 55e9501 | | | 154 | style={{ |
| 55e9501 | | | 155 | backgroundColor: "var(--bg-input)", |
| 55e9501 | | | 156 | border: "1px solid var(--border-subtle)", |
| 55e9501 | | | 157 | color: "var(--text-primary)", |
| 55e9501 | | | 158 | outline: "none", |
| 55e9501 | | | 159 | boxShadow: "none", |
| 55e9501 | | | 160 | }} |
| 55e9501 | | | 161 | placeholder="What does this change?" |
| 55e9501 | | | 162 | required |
| 55e9501 | | | 163 | autoFocus |
| 55e9501 | | | 164 | /> |
| 55e9501 | | | 165 | |
| 55e9501 | | | 166 | <label className="block text-xs" style={{ color: "var(--text-muted)" }}> |
| 55e9501 | | | 167 | Description |
| 55e9501 | | | 168 | </label> |
| 55e9501 | | | 169 | <textarea |
| 55e9501 | | | 170 | value={description} |
| 55e9501 | | | 171 | onChange={(e) => setDescription(e.target.value)} |
| 55e9501 | | | 172 | rows={3} |
| 55e9501 | | | 173 | className="w-full px-3 py-2 text-sm focus:outline-none resize-y" |
| 55e9501 | | | 174 | style={{ |
| 55e9501 | | | 175 | backgroundColor: "var(--bg-input)", |
| 55e9501 | | | 176 | border: "1px solid var(--border-subtle)", |
| 55e9501 | | | 177 | color: "var(--text-primary)", |
| 55e9501 | | | 178 | outline: "none", |
| 55e9501 | | | 179 | boxShadow: "none", |
| 55e9501 | | | 180 | }} |
| 55e9501 | | | 181 | placeholder="Optional description" |
| 55e9501 | | | 182 | /> |
| 55e9501 | | | 183 | |
| 55e9501 | | | 184 | {error && ( |
| 55e9501 | | | 185 | <div |
| 55e9501 | | | 186 | className="text-sm px-3 py-2" |
| 55e9501 | | | 187 | style={{ |
| 55e9501 | | | 188 | backgroundColor: "var(--error-bg)", |
| 55e9501 | | | 189 | border: "1px solid var(--error-border)", |
| 55e9501 | | | 190 | color: "var(--error-text)", |
| 55e9501 | | | 191 | }} |
| 55e9501 | | | 192 | > |
| 55e9501 | | | 193 | {error} |
| 55e9501 | | | 194 | </div> |
| 55e9501 | | | 195 | )} |
| 55e9501 | | | 196 | |
| 55e9501 | | | 197 | <div className="flex items-center gap-2"> |
| 55e9501 | | | 198 | <button |
| 55e9501 | | | 199 | type="submit" |
| 55e9501 | | | 200 | disabled={creating || !headBranch || !title.trim()} |
| 55e9501 | | | 201 | className="px-3 py-1.5 text-sm" |
| 55e9501 | | | 202 | style={{ |
| 55e9501 | | | 203 | backgroundColor: "var(--accent)", |
| 55e9501 | | | 204 | color: "var(--accent-text)", |
| 55e9501 | | | 205 | opacity: creating || !headBranch || !title.trim() ? 0.5 : 1, |
| 55e9501 | | | 206 | }} |
| 55e9501 | | | 207 | > |
| 55e9501 | | | 208 | {creating ? "Creating..." : "Create diff"} |
| 55e9501 | | | 209 | </button> |
| 55e9501 | | | 210 | <Link |
| 55e9501 | | | 211 | href={`/${owner}/${repo}/diffs`} |
| 55e9501 | | | 212 | className="px-3 py-1.5 text-sm" |
| 55e9501 | | | 213 | style={{ |
| 55e9501 | | | 214 | backgroundColor: "var(--bg-inset)", |
| 55e9501 | | | 215 | border: "1px solid var(--border-subtle)", |
| 55e9501 | | | 216 | color: "var(--text-secondary)", |
| 55e9501 | | | 217 | }} |
| 55e9501 | | | 218 | > |
| 55e9501 | | | 219 | Cancel |
| 55e9501 | | | 220 | </Link> |
| 55e9501 | | | 221 | </div> |
| 55e9501 | | | 222 | </form> |
| 55e9501 | | | 223 | </div> |
| 55e9501 | | | 224 | ); |
| 55e9501 | | | 225 | } |