| 13a9fd1 | | | 1 | "use client"; |
| 13a9fd1 | | | 2 | |
| 13a9fd1 | | | 3 | import Link from "next/link"; |
| 13a9fd1 | | | 4 | import { useEffect, useState } from "react"; |
| 13a9fd1 | | | 5 | import { useRouter } from "next/navigation"; |
| 13a9fd1 | | | 6 | import { orgs as orgsApi, repos as reposApi, type Org } from "@/lib/api"; |
| 13a9fd1 | | | 7 | import { useAuth } from "@/lib/auth"; |
| 13a9fd1 | | | 8 | import { Skeleton } from "@/app/components/skeleton"; |
| 13a9fd1 | | | 9 | import { Select } from "@/app/components/ui"; |
| 13a9fd1 | | | 10 | |
| 13a9fd1 | | | 11 | export default function NewRepositoryPage() { |
| 549b8a6 | | | 12 | const cardStyle = { |
| 549b8a6 | | | 13 | backgroundColor: "var(--bg-card)", |
| 549b8a6 | | | 14 | border: "1px solid var(--border-subtle)", |
| 549b8a6 | | | 15 | minHeight: "19.5rem", |
| 549b8a6 | | | 16 | } as const; |
| 13a9fd1 | | | 17 | const { user, loading } = useAuth(); |
| 13a9fd1 | | | 18 | const router = useRouter(); |
| 13a9fd1 | | | 19 | const [orgList, setOrgList] = useState<Org[]>([]); |
| 13a9fd1 | | | 20 | const [orgsLoaded, setOrgsLoaded] = useState(false); |
| 13a9fd1 | | | 21 | const [repoName, setRepoName] = useState(""); |
| 13a9fd1 | | | 22 | const [repoDesc, setRepoDesc] = useState(""); |
| 13a9fd1 | | | 23 | const [repoOwner, setRepoOwner] = useState(""); |
| 13a9fd1 | | | 24 | const [creating, setCreating] = useState(false); |
| 13a9fd1 | | | 25 | const [error, setError] = useState(""); |
| 13a9fd1 | | | 26 | const [requestedOwner, setRequestedOwner] = useState(""); |
| 13a9fd1 | | | 27 | |
| 13a9fd1 | | | 28 | useEffect(() => { |
| 13a9fd1 | | | 29 | document.title = "New Repository"; |
| 13a9fd1 | | | 30 | }, []); |
| 13a9fd1 | | | 31 | |
| 13a9fd1 | | | 32 | useEffect(() => { |
| 13a9fd1 | | | 33 | const value = new URLSearchParams(window.location.search).get("owner"); |
| 13a9fd1 | | | 34 | setRequestedOwner(value?.trim() ?? ""); |
| 13a9fd1 | | | 35 | }, []); |
| 13a9fd1 | | | 36 | |
| 13a9fd1 | | | 37 | useEffect(() => { |
| 13a9fd1 | | | 38 | if (!loading && !user) { |
| 6dd74de | | | 39 | router.push(`/login?redirect=${encodeURIComponent(window.location.pathname + window.location.search)}`); |
| 13a9fd1 | | | 40 | } |
| 13a9fd1 | | | 41 | }, [loading, user, router]); |
| 13a9fd1 | | | 42 | |
| 13a9fd1 | | | 43 | useEffect(() => { |
| 13a9fd1 | | | 44 | if (!user) return; |
| 13a9fd1 | | | 45 | setOrgsLoaded(false); |
| 13a9fd1 | | | 46 | orgsApi |
| 13a9fd1 | | | 47 | .list() |
| 13a9fd1 | | | 48 | .then(({ orgs }) => setOrgList(orgs)) |
| 13a9fd1 | | | 49 | .catch(() => setOrgList([])) |
| 13a9fd1 | | | 50 | .finally(() => setOrgsLoaded(true)); |
| 13a9fd1 | | | 51 | }, [user]); |
| 13a9fd1 | | | 52 | |
| 13a9fd1 | | | 53 | useEffect(() => { |
| 13a9fd1 | | | 54 | if (!user || !requestedOwner) return; |
| 13a9fd1 | | | 55 | if (requestedOwner === user.username) { |
| 13a9fd1 | | | 56 | setRepoOwner(user.username); |
| 13a9fd1 | | | 57 | return; |
| 13a9fd1 | | | 58 | } |
| 13a9fd1 | | | 59 | if (orgList.some((org) => org.name === requestedOwner)) { |
| 13a9fd1 | | | 60 | setRepoOwner(requestedOwner); |
| 13a9fd1 | | | 61 | } |
| 13a9fd1 | | | 62 | }, [user, requestedOwner, orgList]); |
| 13a9fd1 | | | 63 | |
| 13a9fd1 | | | 64 | useEffect(() => { |
| 13a9fd1 | | | 65 | if (!user) return; |
| 13a9fd1 | | | 66 | setRepoOwner((prev) => prev || user.username); |
| 13a9fd1 | | | 67 | }, [user]); |
| 13a9fd1 | | | 68 | |
| 13a9fd1 | | | 69 | async function handleCreateRepo(e: React.FormEvent) { |
| 13a9fd1 | | | 70 | e.preventDefault(); |
| 13a9fd1 | | | 71 | if (!repoName.trim()) return; |
| 13a9fd1 | | | 72 | if (!user) return; |
| 13a9fd1 | | | 73 | setCreating(true); |
| 13a9fd1 | | | 74 | setError(""); |
| 13a9fd1 | | | 75 | try { |
| 13a9fd1 | | | 76 | const { repo } = await reposApi.create({ |
| 13a9fd1 | | | 77 | name: repoName.trim(), |
| 13a9fd1 | | | 78 | description: repoDesc.trim() || undefined, |
| 13a9fd1 | | | 79 | owner: repoOwner && repoOwner !== user.username ? repoOwner : undefined, |
| 13a9fd1 | | | 80 | }); |
| 13a9fd1 | | | 81 | router.push(`/${repo.owner_name}/${repo.name}`); |
| 13a9fd1 | | | 82 | } catch (err: unknown) { |
| 13a9fd1 | | | 83 | setError(err instanceof Error ? err.message : "Failed to create repository"); |
| 13a9fd1 | | | 84 | } finally { |
| 13a9fd1 | | | 85 | setCreating(false); |
| 13a9fd1 | | | 86 | } |
| 13a9fd1 | | | 87 | } |
| 13a9fd1 | | | 88 | |
| 13a9fd1 | | | 89 | if (loading || (user && !orgsLoaded)) { |
| 13a9fd1 | | | 90 | return ( |
| 13a9fd1 | | | 91 | <div className="max-w-xl mx-auto px-4 py-8"> |
| 13a9fd1 | | | 92 | <div |
| 549b8a6 | | | 93 | className="p-4 space-y-2.5" |
| 549b8a6 | | | 94 | style={cardStyle} |
| 13a9fd1 | | | 95 | > |
| 549b8a6 | | | 96 | <Skeleton width="7.5rem" height="1.5rem" /> |
| 549b8a6 | | | 97 | <Skeleton width="2.2rem" height="1rem" /> |
| 13a9fd1 | | | 98 | <Skeleton width="100%" height="2.25rem" /> |
| 549b8a6 | | | 99 | <Skeleton width="5.8rem" height="1rem" /> |
| 13a9fd1 | | | 100 | <Skeleton width="100%" height="2.25rem" /> |
| 549b8a6 | | | 101 | <Skeleton width="4.8rem" height="1rem" /> |
| 13a9fd1 | | | 102 | <Skeleton width="100%" height="2.25rem" /> |
| 13a9fd1 | | | 103 | <div className="flex items-center gap-2"> |
| 13a9fd1 | | | 104 | <Skeleton width="8.5rem" height="2rem" /> |
| 13a9fd1 | | | 105 | <Skeleton width="4.5rem" height="2rem" /> |
| 13a9fd1 | | | 106 | </div> |
| 13a9fd1 | | | 107 | </div> |
| 13a9fd1 | | | 108 | </div> |
| 13a9fd1 | | | 109 | ); |
| 13a9fd1 | | | 110 | } |
| 13a9fd1 | | | 111 | |
| 13a9fd1 | | | 112 | if (!user) return null; |
| 13a9fd1 | | | 113 | |
| 13a9fd1 | | | 114 | const backHref = requestedOwner ? `/${requestedOwner}` : "/"; |
| 13a9fd1 | | | 115 | |
| 13a9fd1 | | | 116 | return ( |
| 13a9fd1 | | | 117 | <div className="max-w-xl mx-auto px-4 py-8"> |
| 13a9fd1 | | | 118 | <form |
| 13a9fd1 | | | 119 | onSubmit={handleCreateRepo} |
| 13a9fd1 | | | 120 | className="p-4 space-y-2.5" |
| 549b8a6 | | | 121 | style={cardStyle} |
| 13a9fd1 | | | 122 | > |
| 13a9fd1 | | | 123 | <h1 className="text-base">New repository</h1> |
| 13a9fd1 | | | 124 | |
| 13a9fd1 | | | 125 | <label className="block text-xs" style={{ color: "var(--text-muted)" }}> |
| 13a9fd1 | | | 126 | Owner |
| 13a9fd1 | | | 127 | </label> |
| 13a9fd1 | | | 128 | <Select |
| 13a9fd1 | | | 129 | value={repoOwner} |
| 13a9fd1 | | | 130 | onChange={(e) => setRepoOwner(e.target.value)} |
| 13a9fd1 | | | 131 | options={[ |
| 13a9fd1 | | | 132 | { value: user.username, label: user.username }, |
| 13a9fd1 | | | 133 | ...orgList.map((org) => ({ |
| 13a9fd1 | | | 134 | value: org.name, |
| 13a9fd1 | | | 135 | label: org.display_name || org.name, |
| 13a9fd1 | | | 136 | })), |
| 13a9fd1 | | | 137 | ]} |
| 13a9fd1 | | | 138 | aria-label="Owner" |
| 13a9fd1 | | | 139 | /> |
| 13a9fd1 | | | 140 | |
| 13a9fd1 | | | 141 | <label className="block text-xs" style={{ color: "var(--text-muted)" }}> |
| 13a9fd1 | | | 142 | Repository name |
| 13a9fd1 | | | 143 | </label> |
| 13a9fd1 | | | 144 | <input |
| 13a9fd1 | | | 145 | value={repoName} |
| 13a9fd1 | | | 146 | onChange={(e) => setRepoName(e.target.value)} |
| 13a9fd1 | | | 147 | className="w-full px-3 py-2 text-sm focus:outline-none" |
| 13a9fd1 | | | 148 | style={{ |
| 13a9fd1 | | | 149 | backgroundColor: "var(--bg-input)", |
| 13a9fd1 | | | 150 | border: "1px solid var(--border-subtle)", |
| 13a9fd1 | | | 151 | color: "var(--text-primary)", |
| 13a9fd1 | | | 152 | outline: "none", |
| 13a9fd1 | | | 153 | boxShadow: "none", |
| 13a9fd1 | | | 154 | }} |
| 13a9fd1 | | | 155 | required |
| 13a9fd1 | | | 156 | autoFocus |
| 13a9fd1 | | | 157 | aria-label="Repository name" |
| 13a9fd1 | | | 158 | /> |
| 13a9fd1 | | | 159 | |
| 13a9fd1 | | | 160 | <label className="block text-xs" style={{ color: "var(--text-muted)" }}> |
| 13a9fd1 | | | 161 | Description |
| 13a9fd1 | | | 162 | </label> |
| 13a9fd1 | | | 163 | <input |
| 13a9fd1 | | | 164 | value={repoDesc} |
| 13a9fd1 | | | 165 | onChange={(e) => setRepoDesc(e.target.value)} |
| 13a9fd1 | | | 166 | className="w-full px-3 py-2 text-sm focus:outline-none" |
| 13a9fd1 | | | 167 | style={{ |
| 13a9fd1 | | | 168 | backgroundColor: "var(--bg-input)", |
| 13a9fd1 | | | 169 | border: "1px solid var(--border-subtle)", |
| 13a9fd1 | | | 170 | color: "var(--text-primary)", |
| 13a9fd1 | | | 171 | outline: "none", |
| 13a9fd1 | | | 172 | boxShadow: "none", |
| 13a9fd1 | | | 173 | }} |
| 13a9fd1 | | | 174 | aria-label="Description" |
| 13a9fd1 | | | 175 | /> |
| 13a9fd1 | | | 176 | |
| 13a9fd1 | | | 177 | {error && ( |
| 13a9fd1 | | | 178 | <div |
| 13a9fd1 | | | 179 | className="text-sm px-3 py-2" |
| 13a9fd1 | | | 180 | style={{ |
| 13a9fd1 | | | 181 | backgroundColor: "var(--error-bg)", |
| 13a9fd1 | | | 182 | border: "1px solid var(--error-border)", |
| 13a9fd1 | | | 183 | color: "var(--error-text)", |
| 13a9fd1 | | | 184 | }} |
| 13a9fd1 | | | 185 | > |
| 13a9fd1 | | | 186 | {error} |
| 13a9fd1 | | | 187 | </div> |
| 13a9fd1 | | | 188 | )} |
| 13a9fd1 | | | 189 | |
| 13a9fd1 | | | 190 | <div className="flex items-center gap-2"> |
| 13a9fd1 | | | 191 | <button |
| 13a9fd1 | | | 192 | type="submit" |
| 13a9fd1 | | | 193 | disabled={creating} |
| 13a9fd1 | | | 194 | className="px-3 py-1.5 text-sm" |
| 13a9fd1 | | | 195 | style={{ |
| 13a9fd1 | | | 196 | backgroundColor: "var(--accent)", |
| 13a9fd1 | | | 197 | color: "var(--accent-text)", |
| 13a9fd1 | | | 198 | opacity: creating ? 0.6 : 1, |
| 13a9fd1 | | | 199 | }} |
| 13a9fd1 | | | 200 | > |
| 13a9fd1 | | | 201 | {creating ? "Creating..." : "Create repository"} |
| 13a9fd1 | | | 202 | </button> |
| 13a9fd1 | | | 203 | <Link |
| 13a9fd1 | | | 204 | href={backHref} |
| 13a9fd1 | | | 205 | className="px-3 py-1.5 text-sm" |
| 13a9fd1 | | | 206 | style={{ |
| 13a9fd1 | | | 207 | backgroundColor: "var(--bg-inset)", |
| 13a9fd1 | | | 208 | border: "1px solid var(--border-subtle)", |
| 13a9fd1 | | | 209 | color: "var(--text-secondary)", |
| 13a9fd1 | | | 210 | }} |
| 13a9fd1 | | | 211 | > |
| 13a9fd1 | | | 212 | Cancel |
| 13a9fd1 | | | 213 | </Link> |
| 13a9fd1 | | | 214 | </div> |
| 13a9fd1 | | | 215 | </form> |
| 13a9fd1 | | | 216 | </div> |
| 13a9fd1 | | | 217 | ); |
| 13a9fd1 | | | 218 | } |