|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { liteClient as algoliasearch } from "algoliasearch/lite"; |
| 4 | +import { Search } from "lucide-react"; |
| 5 | +import { useEffect, useState } from "react"; |
| 6 | +import { |
| 7 | + Highlight, |
| 8 | + Hits, |
| 9 | + InstantSearch, |
| 10 | + SearchBox, |
| 11 | + useInstantSearch, |
| 12 | +} from "react-instantsearch"; |
| 13 | + |
| 14 | +type HitRecord = { |
| 15 | + objectID: string; |
| 16 | + title?: string; |
| 17 | + description?: string; |
| 18 | + url?: string; |
| 19 | +}; |
| 20 | + |
| 21 | +const appId = process.env.NEXT_PUBLIC_ALGOLIA_APP_ID; |
| 22 | +const searchKey = process.env.NEXT_PUBLIC_ALGOLIA_SEARCH_API_KEY; |
| 23 | +const indexName = process.env.NEXT_PUBLIC_ALGOLIA_INDEX_NAME; |
| 24 | + |
| 25 | +const searchClient = |
| 26 | + appId && searchKey ? algoliasearch(appId, searchKey) : null; |
| 27 | + |
| 28 | +function safeHref(url: string | undefined): string { |
| 29 | + if (!url) { |
| 30 | + return "/"; |
| 31 | + } |
| 32 | + if ( |
| 33 | + url.startsWith("https://") || |
| 34 | + (url.startsWith("/") && !url.startsWith("//")) |
| 35 | + ) { |
| 36 | + return url; |
| 37 | + } |
| 38 | + return "/"; |
| 39 | +} |
| 40 | + |
| 41 | +function SearchHit({ hit }: { hit: HitRecord }) { |
| 42 | + return ( |
| 43 | + <a |
| 44 | + className="block rounded-lg px-4 py-3 hover:bg-neutral-100 dark:hover:bg-white/5" |
| 45 | + href={safeHref(hit.url)} |
| 46 | + > |
| 47 | + <div className="truncate text-sm font-medium text-foreground"> |
| 48 | + <Highlight |
| 49 | + attribute="title" |
| 50 | + hit={hit as Parameters<typeof Highlight>[0]["hit"]} |
| 51 | + /> |
| 52 | + </div> |
| 53 | + {hit.description && ( |
| 54 | + <div className="mt-0.5 truncate text-xs text-muted-foreground"> |
| 55 | + <Highlight |
| 56 | + attribute="description" |
| 57 | + hit={hit as Parameters<typeof Highlight>[0]["hit"]} |
| 58 | + /> |
| 59 | + </div> |
| 60 | + )} |
| 61 | + </a> |
| 62 | + ); |
| 63 | +} |
| 64 | + |
| 65 | +function EmptyQuery() { |
| 66 | + const { indexUiState } = useInstantSearch(); |
| 67 | + if (indexUiState.query) { |
| 68 | + return null; |
| 69 | + } |
| 70 | + return ( |
| 71 | + <p className="px-4 py-8 text-center text-sm text-muted-foreground"> |
| 72 | + Start typing to search the docs… |
| 73 | + </p> |
| 74 | + ); |
| 75 | +} |
| 76 | + |
| 77 | +function NoResults() { |
| 78 | + const { results } = useInstantSearch(); |
| 79 | + if (!results?.query || results.nbHits > 0) { |
| 80 | + return null; |
| 81 | + } |
| 82 | + return ( |
| 83 | + <p className="px-4 py-8 text-center text-sm text-muted-foreground"> |
| 84 | + No results for{" "} |
| 85 | + <strong className="text-foreground">"{results.query}"</strong> |
| 86 | + </p> |
| 87 | + ); |
| 88 | +} |
| 89 | + |
| 90 | +function SearchUnavailable() { |
| 91 | + return ( |
| 92 | + <p className="px-4 py-8 text-center text-sm text-muted-foreground"> |
| 93 | + Add <code className="text-xs">NEXT_PUBLIC_ALGOLIA_APP_ID</code>,{" "} |
| 94 | + <code className="text-xs">NEXT_PUBLIC_ALGOLIA_SEARCH_API_KEY</code>, and{" "} |
| 95 | + <code className="text-xs">NEXT_PUBLIC_ALGOLIA_INDEX_NAME</code> to your |
| 96 | + environment to enable search. |
| 97 | + </p> |
| 98 | + ); |
| 99 | +} |
| 100 | + |
| 101 | +export function AlgoliaSearch() { |
| 102 | + const [isOpen, setIsOpen] = useState(false); |
| 103 | + |
| 104 | + useEffect(() => { |
| 105 | + const handler = (e: KeyboardEvent) => { |
| 106 | + if ((e.metaKey || e.ctrlKey) && e.key === "k") { |
| 107 | + e.preventDefault(); |
| 108 | + setIsOpen((prev) => !prev); |
| 109 | + } |
| 110 | + if (e.key === "Escape") { |
| 111 | + setIsOpen(false); |
| 112 | + } |
| 113 | + }; |
| 114 | + window.addEventListener("keydown", handler); |
| 115 | + return () => window.removeEventListener("keydown", handler); |
| 116 | + }, []); |
| 117 | + |
| 118 | + return ( |
| 119 | + <> |
| 120 | + <button |
| 121 | + aria-label="Search docs" |
| 122 | + className="algolia-search-button flex w-56 items-center gap-2 rounded-lg border border-neutral-200 bg-neutral-100 px-3 py-1.5 text-sm text-muted-foreground transition-colors hover:bg-neutral-200 dark:border-white/10 dark:bg-white/5 dark:hover:bg-white/10" |
| 123 | + onClick={() => setIsOpen(true)} |
| 124 | + type="button" |
| 125 | + > |
| 126 | + <Search className="size-3.5 shrink-0" /> |
| 127 | + <span className="flex-1 text-left">Search</span> |
| 128 | + <kbd className="flex items-center gap-0.5 rounded border border-border px-1 text-xs text-muted-foreground"> |
| 129 | + <span>⌘</span>K |
| 130 | + </kbd> |
| 131 | + </button> |
| 132 | + |
| 133 | + {isOpen && ( |
| 134 | + <div |
| 135 | + aria-label="Search" |
| 136 | + aria-modal="true" |
| 137 | + className="fixed inset-0 z-50 flex items-start justify-center px-4 pt-[10vh]" |
| 138 | + role="dialog" |
| 139 | + > |
| 140 | + <button |
| 141 | + aria-label="Close search" |
| 142 | + className="fixed inset-0 bg-black/30 backdrop-blur-sm dark:bg-black/50" |
| 143 | + onClick={() => setIsOpen(false)} |
| 144 | + type="button" |
| 145 | + /> |
| 146 | + <div className="relative z-10 w-full max-w-2xl overflow-hidden rounded-xl border border-border bg-popover shadow-2xl"> |
| 147 | + {searchClient && indexName ? ( |
| 148 | + <InstantSearch indexName={indexName} searchClient={searchClient}> |
| 149 | + <div className="flex items-center border-b border-border px-4"> |
| 150 | + <Search className="size-4 shrink-0 text-muted-foreground" /> |
| 151 | + <SearchBox |
| 152 | + autoFocus |
| 153 | + classNames={{ |
| 154 | + form: "flex flex-1", |
| 155 | + input: |
| 156 | + "w-full bg-transparent px-3 py-4 text-sm text-foreground placeholder:text-muted-foreground outline-none", |
| 157 | + loadingIndicator: "hidden", |
| 158 | + reset: "hidden", |
| 159 | + root: "flex-1", |
| 160 | + submit: "hidden", |
| 161 | + }} |
| 162 | + placeholder="Search docs…" |
| 163 | + /> |
| 164 | + </div> |
| 165 | + <div className="max-h-[60vh] overflow-y-auto p-2"> |
| 166 | + <EmptyQuery /> |
| 167 | + <NoResults /> |
| 168 | + <Hits |
| 169 | + classNames={{ item: "", list: "space-y-0.5", root: "" }} |
| 170 | + hitComponent={({ hit }) => ( |
| 171 | + <SearchHit hit={hit as unknown as HitRecord} /> |
| 172 | + )} |
| 173 | + /> |
| 174 | + </div> |
| 175 | + </InstantSearch> |
| 176 | + ) : ( |
| 177 | + <SearchUnavailable /> |
| 178 | + )} |
| 179 | + </div> |
| 180 | + </div> |
| 181 | + )} |
| 182 | + </> |
| 183 | + ); |
| 184 | +} |
0 commit comments