| 1 | import type { Metadata } from "next"; |
| 2 | import { BookMarked, Code2 |
| 3 | |
| 4 | import { AppHeader } |
| 5 | import { CursorPagination } |
| 6 | import { RepositoryCard } |
| 7 | import { TabLink } |
| 8 | import { CodeSearchResults } |
| 9 | import { SearchForm } |
| 10 | import { |
| 11 | Empty, |
| 12 | EmptyDescription, |
| 13 | EmptyHeader, |
| 14 | EmptyMedia, |
| 15 | EmptyTitle, |
| 16 | } from "@/components/ui/empty"; |
| 17 | import { createServerClient, getServerSession |
| 18 | |
| 19 | const PAGE_SIZE = 20 |
| 20 | |
| 21 | type Kind |
| 22 | |
| 23 | export async function generateMetadata |
| 24 | searchParams, |
| 25 | }: PageProps< |
| 26 | const { q, |
| 27 | const query = |
| 28 | const title = |
| 29 | const image = |
| 30 | q: query, |
| 31 | type: type === |
| 32 | })}`; |
| 33 | |
| 34 | return { |
| 35 | title, |
| 36 | openGraph: { title, |
| 37 | twitter: { images: [ |
| 38 | }; |
| 39 | } |
| 40 | |
| 41 | export default async function |
| 42 | searchParams, |
| 43 | }: PageProps< |
| 44 | const { q, |
| 45 | const query = |
| 46 | const kind: |
| 47 | const pageCursor = |
| 48 | |
| 49 | const session = |
| 50 | const viewer = |
| 51 | |
| 52 | const tabs = |
| 53 | { value: "repositories", |
| 54 | { value: "code", |
| 55 | ] as const; |
| 56 | |
| 57 | return ( |
| 58 | <div className="flex min-h-full flex-col |
| 59 | <AppHeader username={ |
| 60 | |
| 61 | <main className="mx-auto flex w-full max-w-6xl flex-1 flex-col gap-6 px-4 py-8 md:px-6 |
| 62 | <SearchForm |
| 63 | action="/search" |
| 64 | placeholder="Search Ghost" |
| 65 | query={query} |
| 66 | type={kind} |
| 67 | autoFocus={!query} |
| 68 | /> |
| 69 | |
| 70 | <nav className="flex gap-1 border-b |
| 71 | {tabs.map(( |
| 72 | <TabLink |
| 73 | key={value} |
| 74 | href={`/search?${new |
| 75 | active={kind === |
| 76 | > |
| 77 | <Icon className=" |
| 78 | {label} |
| 79 | </TabLink> |
| 80 | ))} |
| 81 | </nav> |
| 82 | |
| 83 | {kind === "code |
| 84 | <CodeResults query={ |
| 85 | ) : ( |
| 86 | <RepositoryResults query={ |
| 87 | )} |
| 88 | </main> |
| 89 | </div> |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | async function RepositoryResults( |
| 94 | query, |
| 95 | cursor, |
| 96 | }: { |
| 97 | query: string |
| 98 | cursor?: string |
| 99 | }) { |
| 100 | const client = |
| 101 | const { data |
| 102 | params: { query: { |
| 103 | }); |
| 104 | if (!data) |
| 105 | |
| 106 | return ( |
| 107 | <> |
| 108 | {data.repositories.length |
| 109 | <div className="flex flex-col border-t |
| 110 | {data.repositories.map |
| 111 | <RepositoryCard |
| 112 | key={repository.id |
| 113 | showOwner |
| 114 | repository={{ |
| 115 | name: repository.name |
| 116 | slug: repository.slug |
| 117 | owner: repository.owner |
| 118 | description: repository.description |
| 119 | visibility: "public", |
| 120 | updatedAt: repository.lastPushedAt |
| 121 | }} |
| 122 | /> |
| 123 | ))} |
| 124 | </div> |
| 125 | ) : ( |
| 126 | <Empty className=" |
| 127 | <EmptyHeader> |
| 128 | <EmptyMedia variant=" |
| 129 | <BookMarked /> |
| 130 | </EmptyMedia> |
| 131 | <EmptyTitle>No repositories found</EmptyTitle |
| 132 | <EmptyDescription> |
| 133 | {query |
| 134 | ? `No public repository has “${query} |
| 135 | : "There are no public repositories yet."} |
| 136 | </EmptyDescription> |
| 137 | </EmptyHeader> |
| 138 | </Empty> |
| 139 | )} |
| 140 | |
| 141 | <CursorPagination |
| 142 | pathname="/search" |
| 143 | params={{ q: |
| 144 | cursor={cursor} |
| 145 | nextCursor={data.nextCursor |
| 146 | /> |
| 147 | </> |
| 148 | ); |
| 149 | } |
| 150 | |
| 151 | async function CodeResults( |
| 152 | if (!query) |
| 153 | return ( |
| 154 | <Empty className=" |
| 155 | <EmptyHeader> |
| 156 | <EmptyMedia variant=" |
| 157 | <Search /> |
| 158 | </EmptyMedia> |
| 159 | <EmptyTitle>Search code</EmptyTitle |
| 160 | <EmptyDescription> |
| 161 | Type in the search bar above to search the code of public |
| 162 | repositories. |
| 163 | </EmptyDescription> |
| 164 | </EmptyHeader> |
| 165 | </Empty> |
| 166 | ); |
| 167 | } |
| 168 | |
| 169 | const client = |
| 170 | const { data, |
| 171 | params: { query: { |
| 172 | }); |
| 173 | |
| 174 | return <CodeSearchResults files |
| 175 | } |