| 1 | "use client"; |
| 2 | |
| 3 | import Link from "next/link"; |
| 4 | import { usePathname, useSearchParams |
| 5 | import { Suspense, useState |
| 6 | import { CircleUser, Ghost |
| 7 | |
| 8 | import { NotificationBell } |
| 9 | import { NewRepositoryDialog } |
| 10 | import { SearchForm } |
| 11 | import { CustomThemeDialog, ThemeSubmenu |
| 12 | import { UserButton } |
| 13 | import { Button } |
| 14 | |
| 15 | const HEADER_SEARCH_CLASS = " |
| 16 | |
| 17 | export function AppHeader({ |
| 18 | username, |
| 19 | owners, |
| 20 | repository, |
| 21 | }: { |
| 22 | username: string |
| 23 | owners: string |
| 24 | /** Set on a repository's pages, where the search bar searches that repository's code. */ |
| 25 | repository?: { |
| 26 | }) { |
| 27 | const action = |
| 28 | ? `/${repository. |
| 29 | : "/search"; |
| 30 | const placeholder = |
| 31 | const [customThemeOpen, |
| 32 | |
| 33 | return ( |
| 34 | <header className="sticky top-0 z-40 border-b bg-background/80 backdrop-blur |
| 35 | <div className="mx-auto flex h-14 w-full max-w-6xl items-center gap-3 px-4 md:px-6 |
| 36 | <Link |
| 37 | href="/dashboard" |
| 38 | className="flex items-center gap-2 font-semibold tracking-tight" |
| 39 | > |
| 40 | <Ghost className=" |
| 41 | Ghost |
| 42 | </Link> |
| 43 | |
| 44 | <div className="ml-auto flex items-center gap-2 |
| 45 | {/* reading the URL opts a prerendered page out of static rendering up to the nearest boundary, so the bar gets its own */ |
| 46 | <Suspense |
| 47 | fallback={ |
| 48 | <SearchForm |
| 49 | action={action} |
| 50 | placeholder={placeholder} |
| 51 | className={HEADER_SEARCH_CLASS} |
| 52 | /> |
| 53 | } |
| 54 | > |
| 55 | <CurrentSearchForm action={ |
| 56 | </Suspense> |
| 57 | |
| 58 | {/* no room for the bar on a phone: the icon opens the search page, which has its own */ |
| 59 | <Button asChild variant |
| 60 | <Link href={ |
| 61 | <Search /> |
| 62 | </Link> |
| 63 | </Button> |
| 64 | |
| 65 | <NewRepositoryDialog owners={ |
| 66 | {/* square on a phone, where the label is hidden and the Button's inline-start padding (which outranks a plain override) would push the icon off centre */ |
| 67 | <Button |
| 68 | size="sm" |
| 69 | aria-label="New repository" |
| 70 | className="max-sm:w-7 max-sm:px-0!" |
| 71 | > |
| 72 | <Plus data-icon=" |
| 73 | <span className="hidden sm:inline |
| 74 | </Button> |
| 75 | </NewRepositoryDialog> |
| 76 | |
| 77 | {username && <NotificationBell |
| 78 | |
| 79 | <UserButton |
| 80 | size="icon" |
| 81 | align="end" |
| 82 | links={[ |
| 83 | { |
| 84 | label: "Your profile", |
| 85 | href: `/${username |
| 86 | icon: <CircleUser /> |
| 87 | visibility: "authenticated", |
| 88 | }, |
| 89 | <ThemeSubmenu |
| 90 | key="theme" |
| 91 | onCustomize={() => |
| 92 | />, |
| 93 | ]} |
| 94 | /> |
| 95 | </div> |
| 96 | </div> |
| 97 | |
| 98 | <CustomThemeDialog |
| 99 | open={customThemeOpen} |
| 100 | onOpenChange={setCustomThemeOpen} |
| 101 | /> |
| 102 | </header> |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | /** On the results page the bar holds the search being shown, and resubmitting keeps the tab. Anywhere else a `q` means something else, like the profile's repository filter. */ |
| 107 | function CurrentSearchForm({ |
| 108 | action, |
| 109 | placeholder, |
| 110 | }: { |
| 111 | action: string |
| 112 | placeholder: string |
| 113 | }) { |
| 114 | const pathname = |
| 115 | const params = |
| 116 | const onResults = |
| 117 | |
| 118 | return ( |
| 119 | <SearchForm |
| 120 | action={action} |
| 121 | placeholder={placeholder} |
| 122 | query={onResults ? |
| 123 | type={onResults ? |
| 124 | className={HEADER_SEARCH_CLASS} |
| 125 | /> |
| 126 | ); |
| 127 | } |