| 1 | import Link from "next/link"; |
| 2 | import { notFound, redirect |
| 3 | |
| 4 | import { CreatePullRequestForm } |
| 5 | import { DiffView } |
| 6 | import { createServerClient, getServerSession |
| 7 | import { API_URL } |
| 8 | |
| 9 | export default async function |
| 10 | params, |
| 11 | searchParams, |
| 12 | }: PageProps< |
| 13 | const { username, |
| 14 | const selected = |
| 15 | |
| 16 | const [session, |
| 17 | getServerSession(), |
| 18 | createServerClient(), |
| 19 | ]); |
| 20 | |
| 21 | if (!session?.user |
| 22 | redirect( |
| 23 | `/auth/sign-in?redirectTo=${encodeURIComponent(`/ |
| 24 | ); |
| 25 | } |
| 26 | |
| 27 | const repository = |
| 28 | params: { path: { |
| 29 | }); |
| 30 | |
| 31 | if (repository.response |
| 32 | if (!repository.data |
| 33 | |
| 34 | const viewerForkSlug = |
| 35 | const viewer = |
| 36 | |
| 37 | // Branches of this repository, and of the viewer's fork when they have one - a fork's branch is proposed as `owner:branch`, which is what the API resolves. |
| 38 | const [branches, |
| 39 | client.GET("/api/repositories/{username}/{slug}/branches |
| 40 | params: { path: { |
| 41 | }), |
| 42 | viewerForkSlug |
| 43 | ? client.GET( |
| 44 | params: { path: { |
| 45 | }) |
| 46 | : null, |
| 47 | ]); |
| 48 | |
| 49 | const own = |
| 50 | const heads = |
| 51 | ...own, |
| 52 | ...(forkBranches?.data |
| 53 | (branch) => |
| 54 | ), |
| 55 | ]; |
| 56 | |
| 57 | const base = |
| 58 | typeof selected.base |
| 59 | ? selected.base |
| 60 | : (branches.data |
| 61 | const head = |
| 62 | typeof selected.head |
| 63 | ? selected.head |
| 64 | : ""; |
| 65 | |
| 66 | const comparison = |
| 67 | base && head |
| 68 | ? await client. |
| 69 | params: { path: { |
| 70 | }) |
| 71 | : null; |
| 72 | |
| 73 | return ( |
| 74 | <div className="flex flex-col gap-4 |
| 75 | <div> |
| 76 | <h1 className="text-lg font-semibold |
| 77 | <p className="text-sm text-muted-foreground |
| 78 | Propose the commits on one branch for merging into another. |
| 79 | </p> |
| 80 | </div> |
| 81 | |
| 82 | {repository.data.parent |
| 83 | <p className="rounded-lg border border-dashed px-4 py-3 text-sm text-muted-foreground |
| 84 | Proposing these changes to{" "} |
| 85 | <Link |
| 86 | href={`/${repository |
| 87 | className="text-primary hover:underline" |
| 88 | > |
| 89 | {repository.data.parent |
| 90 | </Link>{" " |
| 91 | instead? Open the request there, and this fork's branches will be |
| 92 | offered as the source. |
| 93 | </p> |
| 94 | )} |
| 95 | |
| 96 | <CreatePullRequestForm |
| 97 | username={username} |
| 98 | repo={repo} |
| 99 | bases={own} |
| 100 | heads={heads} |
| 101 | defaultBase={base} |
| 102 | defaultHead={head} |
| 103 | /> |
| 104 | |
| 105 | {comparison?.data && |
| 106 | <DiffView |
| 107 | from={comparison.data |
| 108 | to={comparison.data |
| 109 | files={comparison.data |
| 110 | patchUrl={`${API_URL |
| 111 | /> |
| 112 | )} |
| 113 | |
| 114 | {comparison?.error && |
| 115 | <p className="rounded-lg border border-dashed px-4 py-3 text-sm text-muted-foreground |
| 116 | {comparison.error.message |
| 117 | </p> |
| 118 | )} |
| 119 | </div> |
| 120 | ); |
| 121 | } |