| 1 | import { GitMerge, GitPullRequest, GitPullRequestClosed } from "lucide-react"; |
| 2 | import type { Metadata |
| 3 | import { Suspense } |
| 4 | import { notFound } |
| 5 | |
| 6 | import { UserLink } |
| 7 | import { FromNowHoverCard } |
| 8 | import { EditableField } |
| 9 | import { branchLabel } |
| 10 | import { |
| 11 | ThreadSubscription, |
| 12 | ThreadSubscriptionSkeleton, |
| 13 | } from "@/components/notifications/thread-subscription"; |
| 14 | import { Badge } |
| 15 | import { |
| 16 | createServerClient, |
| 17 | getServerSession, |
| 18 | getViewerRole, |
| 19 | } from "@/lib/api/server"; |
| 20 | import { atLeast } |
| 21 | import { cn } |
| 22 | |
| 23 | import { PullRequestNav } |
| 24 | |
| 25 | export async function generateMetadata |
| 26 | params, |
| 27 | }: LayoutProps< |
| 28 | const { username, |
| 29 | const client = |
| 30 | const { data |
| 31 | "/api/repositories/{username}/{repo}/pulls/{number}", |
| 32 | { params: { path: |
| 33 | ); |
| 34 | |
| 35 | const title = |
| 36 | ? `${data.title |
| 37 | : `Pull request #${number} |
| 38 | |
| 39 | return { title, |
| 40 | } |
| 41 | |
| 42 | export default async function |
| 43 | params, |
| 44 | children, |
| 45 | }: LayoutProps< |
| 46 | const { username, |
| 47 | |
| 48 | const [session, |
| 49 | getServerSession(), |
| 50 | createServerClient(), |
| 51 | getViewerRole(username, repo |
| 52 | ]); |
| 53 | const pull = |
| 54 | "/api/repositories/{username}/{repo}/pulls/{number}", |
| 55 | { params: { path: |
| 56 | ); |
| 57 | |
| 58 | if (pull.response |
| 59 | if (!pull.data |
| 60 | throw new Error( |
| 61 | } |
| 62 | |
| 63 | const { state, |
| 64 | const viewer = |
| 65 | // the author, or whoever can write to the base repository |
| 66 | const canEdit = |
| 67 | Boolean(viewer) && |
| 68 | (viewer === pull |
| 69 | const Icon = |
| 70 | state === "merged" |
| 71 | ? GitMerge |
| 72 | : state === " |
| 73 | ? GitPullRequestClosed |
| 74 | : GitPullRequest; |
| 75 | |
| 76 | return ( |
| 77 | <div className="flex flex-col gap-4 |
| 78 | <div className="flex flex-col gap-2 |
| 79 | <EditableField |
| 80 | username={username} |
| 81 | repo={repo} |
| 82 | number={pull.data |
| 83 | noun="pull request" |
| 84 | field="title" |
| 85 | value={pull.data |
| 86 | canEdit={canEdit} |
| 87 | > |
| 88 | <h1 className="text-xl font-semibold |
| 89 | {pull.data.title |
| 90 | <span className="font-normal text-muted-foreground |
| 91 | #{pull.data.number |
| 92 | </span> |
| 93 | </h1> |
| 94 | </EditableField> |
| 95 | |
| 96 | <div className="flex flex-wrap items-center gap-2 text-sm text-muted-foreground |
| 97 | <Badge |
| 98 | className={cn( |
| 99 | "gap-1.5 rounded-full capitalize", |
| 100 | state === "merged" |
| 101 | state === "closed" |
| 102 | state === "open" |
| 103 | )} |
| 104 | > |
| 105 | <Icon className=" |
| 106 | {state} |
| 107 | </Badge> |
| 108 | {pull.data.draft |
| 109 | <Badge variant=" |
| 110 | Draft |
| 111 | </Badge> |
| 112 | )} |
| 113 | |
| 114 | <span> |
| 115 | <UserLink |
| 116 | username={pull.data |
| 117 | image={pull.data |
| 118 | avatar="xs" |
| 119 | className="align-middle" |
| 120 | />{" "} |
| 121 | wants to merge {pull.data.commitCount |
| 122 | {pull.data.commitCount |
| 123 | <code className="rounded bg-muted px-1.5 py-0.5 text-xs |
| 124 | {branchLabel(base, |
| 125 | </code>{" "} |
| 126 | from{" "} |
| 127 | <code className="rounded bg-muted px-1.5 py-0.5 text-xs |
| 128 | {branchLabel(head, |
| 129 | </code> |
| 130 | </span> |
| 131 | |
| 132 | <span> |
| 133 | · opened <FromNowHoverCard date={ |
| 134 | </span> |
| 135 | |
| 136 | <div className="ml-auto |
| 137 | <Suspense fallback={ |
| 138 | <ThreadSubscription |
| 139 | username={username} |
| 140 | repo={repo} |
| 141 | number={Number(number |
| 142 | /> |
| 143 | </Suspense> |
| 144 | </div> |
| 145 | </div> |
| 146 | </div> |
| 147 | |
| 148 | {head.username === |
| 149 | <p className="rounded-lg border border-dashed p-4 text-sm text-muted-foreground |
| 150 | The repository this pull request came from was deleted, and its |
| 151 | changes with it. |
| 152 | </p> |
| 153 | )} |
| 154 | |
| 155 | <PullRequestNav |
| 156 | base={`/${username |
| 157 | commitCount={pull.data |
| 158 | changedFiles={pull.data |
| 159 | additions={pull.data |
| 160 | deletions={pull.data |
| 161 | /> |
| 162 | |
| 163 | {children} |
| 164 | </div> |
| 165 | ); |
| 166 | } |