|
| 1 | +import * as React from 'react' |
| 2 | +import { cn } from '@explainer/ui' |
| 3 | +import { Icon } from '@iconify/react' |
| 4 | + |
| 5 | +interface PagefindResult { |
| 6 | + url: string |
| 7 | + meta: { title: string } |
| 8 | + excerpt: string |
| 9 | +} |
| 10 | + |
| 11 | +let pagefindInstance: any = null |
| 12 | +let pagefindFailed = false |
| 13 | + |
| 14 | +async function getPagefind() { |
| 15 | + if (pagefindInstance) return pagefindInstance |
| 16 | + if (pagefindFailed) return null |
| 17 | + try { |
| 18 | + const module = await new Function("return import('/pagefind/pagefind.js')")() |
| 19 | + await module.init() |
| 20 | + pagefindInstance = module |
| 21 | + return pagefindInstance |
| 22 | + } catch { |
| 23 | + pagefindFailed = true |
| 24 | + return null |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +export function Search() { |
| 29 | + const [query, setQuery] = React.useState('') |
| 30 | + const [results, setResults] = React.useState<PagefindResult[]>([]) |
| 31 | + const [open, setOpen] = React.useState(false) |
| 32 | + const [loading, setLoading] = React.useState(false) |
| 33 | + const [unavailable, setUnavailable] = React.useState(false) |
| 34 | + const containerRef = React.useRef<HTMLDivElement>(null) |
| 35 | + |
| 36 | + React.useEffect(() => { |
| 37 | + const handler = (e: MouseEvent) => { |
| 38 | + if (containerRef.current && !containerRef.current.contains(e.target as Node)) { |
| 39 | + setOpen(false) |
| 40 | + } |
| 41 | + } |
| 42 | + document.addEventListener('mousedown', handler) |
| 43 | + return () => document.removeEventListener('mousedown', handler) |
| 44 | + }, []) |
| 45 | + |
| 46 | + React.useEffect(() => { |
| 47 | + if (!query.trim()) { |
| 48 | + setResults([]) |
| 49 | + setOpen(false) |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + const timeout = setTimeout(async () => { |
| 54 | + setLoading(true) |
| 55 | + const pagefind = await getPagefind() |
| 56 | + if (!pagefind) { |
| 57 | + setLoading(false) |
| 58 | + setUnavailable(true) |
| 59 | + setOpen(true) |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + const search = await pagefind.search(query) |
| 64 | + const data = await Promise.all(search.results.slice(0, 8).map((r: any) => r.data())) |
| 65 | + setResults(data) |
| 66 | + setOpen(true) |
| 67 | + setLoading(false) |
| 68 | + }, 200) |
| 69 | + |
| 70 | + return () => clearTimeout(timeout) |
| 71 | + }, [query]) |
| 72 | + |
| 73 | + return ( |
| 74 | + <div ref={containerRef} className="relative px-3"> |
| 75 | + <div className="relative"> |
| 76 | + <Icon |
| 77 | + icon="lucide:search" |
| 78 | + className="absolute left-2.5 top-1/2 -translate-y-1/2 size-4 text-muted-foreground" |
| 79 | + /> |
| 80 | + <input |
| 81 | + type="text" |
| 82 | + placeholder="Search docs..." |
| 83 | + value={query} |
| 84 | + onChange={(e) => setQuery(e.target.value)} |
| 85 | + onFocus={() => results.length > 0 && setOpen(true)} |
| 86 | + className={cn( |
| 87 | + 'w-full rounded-md border bg-background px-3 py-2 pl-9 text-sm', |
| 88 | + 'placeholder:text-muted-foreground', |
| 89 | + 'focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 focus:ring-offset-background', |
| 90 | + )} |
| 91 | + /> |
| 92 | + {loading && ( |
| 93 | + <Icon |
| 94 | + icon="lucide:loader-2" |
| 95 | + className="absolute right-2.5 top-1/2 -translate-y-1/2 size-4 text-muted-foreground animate-spin" |
| 96 | + /> |
| 97 | + )} |
| 98 | + </div> |
| 99 | + |
| 100 | + {open && results.length > 0 && ( |
| 101 | + <div className="absolute left-3 right-3 top-full z-50 mt-1 max-h-80 overflow-y-auto rounded-md border bg-popover p-1 shadow-md"> |
| 102 | + {results.map((result, i) => ( |
| 103 | + <a |
| 104 | + key={i} |
| 105 | + href={result.url} |
| 106 | + className="block rounded-sm px-3 py-2 text-sm hover:bg-accent hover:text-accent-foreground transition-colors" |
| 107 | + onClick={() => setOpen(false)} |
| 108 | + > |
| 109 | + <div className="font-medium">{result.meta.title}</div> |
| 110 | + {result.excerpt && ( |
| 111 | + <div |
| 112 | + className="text-xs text-muted-foreground mt-0.5 line-clamp-2" |
| 113 | + dangerouslySetInnerHTML={{ __html: result.excerpt }} |
| 114 | + /> |
| 115 | + )} |
| 116 | + </a> |
| 117 | + ))} |
| 118 | + </div> |
| 119 | + )} |
| 120 | + |
| 121 | + {open && query.trim() && results.length === 0 && !loading && ( |
| 122 | + <div className="absolute left-3 right-3 top-full z-50 mt-1 rounded-md border bg-popover p-3 shadow-md"> |
| 123 | + <p className="text-sm text-muted-foreground text-center"> |
| 124 | + {unavailable ? 'Search is available after building the site' : 'No results found'} |
| 125 | + </p> |
| 126 | + </div> |
| 127 | + )} |
| 128 | + </div> |
| 129 | + ) |
| 130 | +} |
0 commit comments