| 1 | "use client"; |
| 2 | |
| 3 | import Form from "next/form" |
| 4 | import Link from "next/link |
| 5 | import { usePathname, useSearchParams |
| 6 | import { Suspense } |
| 7 | import { CircleUser, Ghost |
| 8 | |
| 9 | import { NotificationBell } |
| 10 | import { NewRepositoryDialog } |
| 11 | import { ThemePicker } |
| 12 | import { UserButton } |
| 13 | import { Button } |
| 14 | import { |
| 15 | InputGroup, |
| 16 | InputGroupAddon, |
| 17 | InputGroupInput, |
| 18 | } from "@/components/ui/input-group"; |
| 19 | |
| 20 | export function AppHeader({ |
| 21 | username, |
| 22 | owners, |
| 23 | repository, |
| 24 | }: { |
| 25 | username: string |
| 26 | owners: string |
| 27 | /** Set on a repository's pages, where the search bar searches that repository's code. */ |
| 28 | repository?: { |
| 29 | }) { |
| 30 | const action = |
| 31 | ? `/${repository. |
| 32 | : "/search"; |
| 33 | const placeholder = |
| 34 | |
| 35 | return ( |
| 36 | <header className="sticky top-0 z-40 border-b bg-background/80 backdrop-blur |
| 37 | <div className="mx-auto flex h-14 w-full max-w-6xl items-center gap-3 px-4 md:px-6 |
| 38 | <Link |
| 39 | href="/dashboard" |
| 40 | className="flex items-center gap-2 font-semibold tracking-tight" |
| 41 | > |
| 42 | <Ghost className=" |
| 43 | Ghost |
| 44 | </Link> |
| 45 | |
| 46 | <div className="ml-auto flex items-center gap-2 |
| 47 | {/* reading the URL opts a prerendered page out of static rendering up to the nearest boundary, so the bar gets its own */ |
| 48 | <Suspense |
| 49 | fallback={<SearchForm |
| 50 | > |
| 51 | <CurrentSearchForm action={ |
| 52 | </Suspense> |
| 53 | |
| 54 | <NewRepositoryDialog owners={ |
| 55 | <Button size=" |
| 56 | <Plus data-icon=" |
| 57 | New |
| 58 | </Button> |
| 59 | </NewRepositoryDialog> |
| 60 | |
| 61 | {username && <NotificationBell |
| 62 | |
| 63 | <ThemePicker /> |
| 64 | |
| 65 | <UserButton |
| 66 | size="icon" |
| 67 | align="end" |
| 68 | links={[ |
| 69 | { |
| 70 | label: "Your profile", |
| 71 | href: `/${username |
| 72 | icon: <CircleUser /> |
| 73 | visibility: "authenticated", |
| 74 | }, |
| 75 | ]} |
| 76 | /> |
| 77 | </div> |
| 78 | </div> |
| 79 | </header> |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | /** 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. */ |
| 84 | function CurrentSearchForm({ |
| 85 | action, |
| 86 | placeholder, |
| 87 | }: { |
| 88 | action: string |
| 89 | placeholder: string |
| 90 | }) { |
| 91 | const pathname = |
| 92 | const params = |
| 93 | const onResults = |
| 94 | |
| 95 | return ( |
| 96 | <SearchForm |
| 97 | action={action} |
| 98 | placeholder={placeholder} |
| 99 | query={onResults ? |
| 100 | type={onResults ? |
| 101 | /> |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | function SearchForm({ |
| 106 | action, |
| 107 | placeholder, |
| 108 | query = "", |
| 109 | type = null, |
| 110 | }: { |
| 111 | action: string |
| 112 | placeholder: string |
| 113 | query?: string |
| 114 | type?: string |
| 115 | }) { |
| 116 | return ( |
| 117 | <Form action={ |
| 118 | {type && <input |
| 119 | <InputGroup className=" |
| 120 | <InputGroupAddon> |
| 121 | <Search /> |
| 122 | </InputGroupAddon> |
| 123 | <InputGroupInput |
| 124 | key={query} |
| 125 | type="search" |
| 126 | name="q" |
| 127 | required |
| 128 | defaultValue={query} |
| 129 | placeholder={placeholder} |
| 130 | aria-label={placeholder} |
| 131 | /> |
| 132 | </InputGroup> |
| 133 | </Form> |
| 134 | ); |
| 135 | } |