| 1 | "use client"; |
| 2 | |
| 3 | import { |
| 4 | BookMarked, |
| 5 | ChevronRight, |
| 6 | CircleDot, |
| 7 | Code2, |
| 8 | GitBranch, |
| 9 | GitFork, |
| 10 | GitPullRequest, |
| 11 | Settings, |
| 12 | } from "lucide-react"; |
| 13 | import Link from "next/link |
| 14 | import { usePathname, useRouter |
| 15 | |
| 16 | import { AppHeader } |
| 17 | import { WatchButton } |
| 18 | import { TabLink } |
| 19 | import { ClonePopover } |
| 20 | import { StarButton } |
| 21 | import { Badge } |
| 22 | import { Button } |
| 23 | import { |
| 24 | Select, |
| 25 | SelectContent, |
| 26 | SelectGroup, |
| 27 | SelectItem, |
| 28 | SelectTrigger, |
| 29 | SelectValue, |
| 30 | } from "@/components/ui/select"; |
| 31 | |
| 32 | import { API_URL, sshCloneUrlFor |
| 33 | import { atLeast } |
| 34 | import type { RepositoryFrameProps |
| 35 | |
| 36 | // pages under the code tab that are not a view of the tree, and so have no branch to pick |
| 37 | const OWN_HEADER_VIEWS = new |
| 38 | |
| 39 | export function RepositoryFrame({ |
| 40 | viewer, |
| 41 | viewerRole, |
| 42 | username, |
| 43 | slug, |
| 44 | name, |
| 45 | description, |
| 46 | visibility, |
| 47 | defaultBranch, |
| 48 | branches, |
| 49 | starCount, |
| 50 | viewerHasStarred, |
| 51 | watchLevel, |
| 52 | forkCount, |
| 53 | openPullRequestCount, |
| 54 | openIssueCount, |
| 55 | parent, |
| 56 | sidebar, |
| 57 | children, |
| 58 | }: RepositoryFrameProps) |
| 59 | const router = |
| 60 | const pathname = |
| 61 | const [view, |
| 62 | .split("/") |
| 63 | .slice(3) |
| 64 | .filter(Boolean) |
| 65 | .map(decodeURIComponent); |
| 66 | |
| 67 | const hasRev = |
| 68 | const rev = |
| 69 | const segments = |
| 70 | const onBranch = |
| 71 | |
| 72 | const base = |
| 73 | const treeBase = |
| 74 | |
| 75 | const tabs = |
| 76 | { value: "code", |
| 77 | { |
| 78 | value: "issues", |
| 79 | label: "Issues", |
| 80 | icon: CircleDot, |
| 81 | href: `${base} |
| 82 | count: openIssueCount ?? |
| 83 | }, |
| 84 | { |
| 85 | value: "pulls", |
| 86 | label: "Pull requests", |
| 87 | icon: GitPullRequest, |
| 88 | href: `${base} |
| 89 | count: openPullRequestCount ?? |
| 90 | }, |
| 91 | ...(atLeast(viewerRole |
| 92 | ? [ |
| 93 | { |
| 94 | value: "settings", |
| 95 | label: "Settings", |
| 96 | icon: Settings, |
| 97 | href: `${base} |
| 98 | count: null, |
| 99 | }, |
| 100 | ] |
| 101 | : []), |
| 102 | ]; |
| 103 | const activeTab = |
| 104 | view === "pulls" |
| 105 | ? view |
| 106 | : "code"; |
| 107 | // the root of the repository only: a file or a subdirectory has nothing to say about the repository as a whole |
| 108 | const showSidebar = |
| 109 | |
| 110 | return ( |
| 111 | <div className="flex min-h-full flex-col |
| 112 | <AppHeader |
| 113 | username={viewer} |
| 114 | owners={viewer ? |
| 115 | repository={{ owner: |
| 116 | /> |
| 117 | |
| 118 | <div className="border-b bg-muted/30 |
| 119 | <div className="mx-auto flex w-full max-w-6xl flex-col gap-4 px-4 pt-6 md:px-6 |
| 120 | {/* on a phone this is one column: the icon stays beside the name |
| 121 | rather than stranded on a line of its own, and the actions get a |
| 122 | full-width row under the description */} |
| 123 | <div className="flex flex-col gap-3 sm:flex-row sm:items-start |
| 124 | <div className="flex min-w-0 flex-1 gap-3 |
| 125 | <div className="flex size-9 shrink-0 items-center justify-center rounded-lg border bg-background |
| 126 | <BookMarked className=" |
| 127 | </div> |
| 128 | |
| 129 | {/* at least as tall as the icon and centred on it, so the name alone, or the name with its fork line, lines up with it */ |
| 130 | <div className="flex min-h-9 min-w-0 flex-col justify-center gap-1 |
| 131 | <div className="flex flex-wrap items-center gap-2 |
| 132 | <h1 className="flex min-w-0 flex-wrap items-center gap-1.5 text-lg leading-none |
| 133 | <Link |
| 134 | href={`/${username |
| 135 | className="truncate text-muted-foreground hover:text-foreground hover:underline" |
| 136 | > |
| 137 | {username} |
| 138 | </Link> |
| 139 | <span className="text-muted-foreground/60 |
| 140 | <Link |
| 141 | href={base} |
| 142 | className="truncate font-semibold hover:underline" |
| 143 | > |
| 144 | {name} |
| 145 | </Link> |
| 146 | </h1> |
| 147 | |
| 148 | <Badge variant=" |
| 149 | {visibility} |
| 150 | </Badge> |
| 151 | </div> |
| 152 | |
| 153 | {parent && ( |
| 154 | <p className="text-xs text-muted-foreground |
| 155 | forked from{" "} |
| 156 | <Link |
| 157 | href={`/${parent |
| 158 | className="hover:text-foreground hover:underline" |
| 159 | > |
| 160 | {parent.username}/ |
| 161 | </Link> |
| 162 | </p> |
| 163 | )} |
| 164 | |
| 165 | {description && ( |
| 166 | <p className="max-w-2xl text-sm text-muted-foreground |
| 167 | {description} |
| 168 | </p> |
| 169 | )} |
| 170 | </div> |
| 171 | </div> |
| 172 | |
| 173 | <div className="flex shrink-0 gap-2 sm:ml-auto |
| 174 | {watchLevel && ( |
| 175 | <WatchButton |
| 176 | username={username} |
| 177 | repo={slug} |
| 178 | level={watchLevel} |
| 179 | /> |
| 180 | )} |
| 181 | <StarButton |
| 182 | username={username} |
| 183 | slug={slug} |
| 184 | starCount={starCount} |
| 185 | viewerHasStarred={viewerHasStarred} |
| 186 | signedIn={Boolean(viewer |
| 187 | /> |
| 188 | <Button variant=" |
| 189 | <Link href={ |
| 190 | <GitFork data-icon=" |
| 191 | Fork |
| 192 | <span className="ml-1 text-muted-foreground tabular-nums |
| 193 | {forkCount} |
| 194 | </span> |
| 195 | </Link> |
| 196 | </Button> |
| 197 | </div> |
| 198 | </div> |
| 199 | |
| 200 | <nav className="-mb-px flex gap-1 overflow-x-auto |
| 201 | {tabs.map(( |
| 202 | <TabLink key={ |
| 203 | <Icon className=" |
| 204 | {label} |
| 205 | {count !== null |
| 206 | <span className="rounded-full bg-muted px-1.5 py-0.5 text-xs tabular-nums |
| 207 | {count} |
| 208 | </span> |
| 209 | )} |
| 210 | </TabLink> |
| 211 | ))} |
| 212 | </nav> |
| 213 | </div> |
| 214 | </div> |
| 215 | |
| 216 | <main className="mx-auto w-full max-w-6xl flex-1 px-4 py-6 md:px-6 |
| 217 | <div className="flex flex-col gap-6 lg:flex-row lg:items-start |
| 218 | <div className="flex min-w-0 flex-1 flex-col gap-4 |
| 219 | {activeTab === "code |
| 220 | // one row on a phone too: the select gives way first, and the count drops its word |
| 221 | <div className="flex items-center gap-3 |
| 222 | <Select |
| 223 | value={onBranch ? |
| 224 | onValueChange={(branch) |
| 225 | router.push(`${ |
| 226 | } |
| 227 | > |
| 228 | <SelectTrigger className=" |
| 229 | <SelectValue |
| 230 | placeholder={ |
| 231 | // a sha is shortened; a tag reads in full |
| 232 | onBranch |
| 233 | ? "Select a branch" |
| 234 | : /^[0-9a-f]{40}$ |
| 235 | ? rev.slice( |
| 236 | : rev |
| 237 | } |
| 238 | /> |
| 239 | </SelectTrigger> |
| 240 | <SelectContent> |
| 241 | <SelectGroup> |
| 242 | {(branches ?? []) |
| 243 | <SelectItem key={ |
| 244 | {item} |
| 245 | </SelectItem> |
| 246 | ))} |
| 247 | </SelectGroup> |
| 248 | </SelectContent> |
| 249 | </Select> |
| 250 | <Link |
| 251 | href={`${base |
| 252 | className="flex shrink-0 items-center gap-1 text-sm text-muted-foreground hover:text-foreground hover:underline" |
| 253 | > |
| 254 | <GitBranch className=" |
| 255 | <span className="font-medium tabular-nums |
| 256 | {branches?.length ?? |
| 257 | </span> |
| 258 | <span className="max-sm:sr-only |
| 259 | {branches?.length === |
| 260 | </span> |
| 261 | </Link> |
| 262 | <div className="ml-auto flex items-center gap-2 |
| 263 | <ClonePopover |
| 264 | cloneUrl={`${API_URL |
| 265 | sshCloneUrl={sshCloneUrlFor(username |
| 266 | /> |
| 267 | </div> |
| 268 | </div> |
| 269 | )} |
| 270 | {segments.length > |
| 271 | <nav className="flex flex-wrap items-center gap-1 text-sm |
| 272 | <Link href={ |
| 273 | {name} |
| 274 | </Link> |
| 275 | {segments.map(( |
| 276 | const isLast = |
| 277 | const href = |
| 278 | .slice(0, index |
| 279 | .map(encodeURIComponent) |
| 280 | .join("/") |
| 281 | |
| 282 | return ( |
| 283 | <span key={href |
| 284 | <ChevronRight className=" |
| 285 | {isLast ? ( |
| 286 | <span className="font-semibold |
| 287 | ) : ( |
| 288 | <Link |
| 289 | href={href} |
| 290 | className="text-primary hover:underline" |
| 291 | > |
| 292 | {segment} |
| 293 | </Link> |
| 294 | )} |
| 295 | </span> |
| 296 | ); |
| 297 | })} |
| 298 | </nav> |
| 299 | )} |
| 300 | |
| 301 | {children} |
| 302 | </div> |
| 303 | |
| 304 | {sidebar && showSidebar |
| 305 | <aside className="flex w-full flex-col gap-6 lg:w-72 lg:shrink-0 |
| 306 | {sidebar} |
| 307 | </aside> |
| 308 | ) : null} |
| 309 | </div> |
| 310 | </main> |
| 311 | </div> |
| 312 | ); |
| 313 | } |