|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import typesense, { ProjectHit } from "@/lib/typesense"; |
| 4 | +import { BlurImage, LoadingSpinner } from "@dub/ui"; |
| 5 | +import { cn } from "@dub/utils"; |
| 6 | +import { Command } from "cmdk"; |
| 7 | +import { Link2 } from "lucide-react"; |
| 8 | +import { useRouter, useSearchParams } from "next/navigation"; |
| 9 | +import { useEffect, useState } from "react"; |
| 10 | +import Highlighter from "react-highlight-words"; |
| 11 | +import { toast } from "sonner"; |
| 12 | +import { useDebounce } from "use-debounce"; |
| 13 | + |
| 14 | +export default function SearchBar() { |
| 15 | + const [loading, setLoading] = useState(false); |
| 16 | + const [items, setItems] = useState<ProjectHit[]>([]); |
| 17 | + |
| 18 | + const searchParams = useSearchParams(); |
| 19 | + const q = searchParams?.get("q") || ""; |
| 20 | + |
| 21 | + function search(query: string) { |
| 22 | + const params = new URLSearchParams(searchParams.toString()); |
| 23 | + if (query) { |
| 24 | + params.set("q", query); |
| 25 | + } else { |
| 26 | + params.delete("q"); |
| 27 | + } |
| 28 | + window.history.replaceState(null, "", `?${params.toString()}`); |
| 29 | + } |
| 30 | + |
| 31 | + const [debouncedQuery] = useDebounce(q, 150); |
| 32 | + |
| 33 | + useEffect(() => { |
| 34 | + const abortController = new AbortController(); |
| 35 | + |
| 36 | + if (!debouncedQuery) return setItems([]); |
| 37 | + const fetchAutocomplete = async (q: string) => { |
| 38 | + try { |
| 39 | + setLoading(true); |
| 40 | + const results = await typesense({ client: true }) |
| 41 | + .collections("projects") |
| 42 | + .documents() |
| 43 | + .search( |
| 44 | + { |
| 45 | + q, |
| 46 | + query_by: ["name", "description"], |
| 47 | + highlight_full_fields: ["name", "description"], |
| 48 | + sort_by: "stars:desc", |
| 49 | + num_typos: 1, |
| 50 | + limit: 8, |
| 51 | + exclude_fields: ["out_of", "search_time_ms"], |
| 52 | + infix: ["always", "off"], |
| 53 | + }, |
| 54 | + { |
| 55 | + abortSignal: abortController.signal, |
| 56 | + }, |
| 57 | + ); |
| 58 | + setItems(results.hits as ProjectHit[]); |
| 59 | + } catch (error) { |
| 60 | + toast.error(error); |
| 61 | + } |
| 62 | + setLoading(false); |
| 63 | + }; |
| 64 | + fetchAutocomplete(debouncedQuery); |
| 65 | + |
| 66 | + return () => { |
| 67 | + abortController.abort(); |
| 68 | + }; |
| 69 | + }, [debouncedQuery]); |
| 70 | + |
| 71 | + const router = useRouter(); |
| 72 | + |
| 73 | + return ( |
| 74 | + <Command className="peer relative w-full max-w-md" loop> |
| 75 | + <div className="absolute inset-y-0 left-3 mt-3 text-gray-400"> |
| 76 | + {loading ? ( |
| 77 | + <LoadingSpinner className="h-4" /> |
| 78 | + ) : ( |
| 79 | + <Link2 className="h-4" /> |
| 80 | + )} |
| 81 | + </div> |
| 82 | + <Command.Input |
| 83 | + name="query" |
| 84 | + id="query" |
| 85 | + className="block w-full rounded-md border-gray-200 pl-10 text-sm text-gray-900 placeholder-gray-400 shadow-lg focus:border-gray-500 focus:outline-none focus:ring-gray-500" |
| 86 | + placeholder="Search for a project" |
| 87 | + value={q} |
| 88 | + onValueChange={search} |
| 89 | + /> |
| 90 | + <Command.List |
| 91 | + className={cn( |
| 92 | + "absolute z-10 mt-2 h-[calc(var(--cmdk-list-height)+17px)] max-h-[300px] w-full overflow-auto rounded-md border border-gray-200 bg-white p-2 shadow-md transition-all duration-75", |
| 93 | + { |
| 94 | + hidden: items.length === 0, |
| 95 | + }, |
| 96 | + )} |
| 97 | + > |
| 98 | + {items.map((item) => { |
| 99 | + return ( |
| 100 | + <Command.Item |
| 101 | + key={item.document.id} |
| 102 | + value={item.document.name} |
| 103 | + onSelect={() => { |
| 104 | + router.push(`/projects/${item.document.slug}`); |
| 105 | + }} |
| 106 | + className="group flex cursor-pointer items-center justify-between rounded-md px-4 py-2 text-sm text-gray-900 hover:bg-gray-100 hover:text-gray-900 active:bg-gray-200 aria-disabled:cursor-not-allowed aria-disabled:opacity-75 aria-disabled:hover:bg-white aria-selected:bg-gray-100 aria-selected:text-gray-900" |
| 107 | + > |
| 108 | + <div className="flex items-center space-x-2"> |
| 109 | + <BlurImage |
| 110 | + src={item.document.logo} |
| 111 | + alt={item.document.name} |
| 112 | + className="h-6 w-6 rounded-full" |
| 113 | + width={16} |
| 114 | + height={16} |
| 115 | + /> |
| 116 | + <div className="flex flex-col space-y-0.5"> |
| 117 | + <Highlighter |
| 118 | + highlightClassName="underline bg-transparent text-purple-500" |
| 119 | + searchWords={ |
| 120 | + item.highlights.find((h) => h.field === "name") |
| 121 | + ?.matched_tokens || [] |
| 122 | + } |
| 123 | + autoEscape={true} |
| 124 | + textToHighlight={item.document.name} |
| 125 | + className="text-sm font-medium text-gray-600 group-aria-selected:text-purple-600 sm:group-hover:text-purple-600" |
| 126 | + /> |
| 127 | + <Highlighter |
| 128 | + highlightClassName="underline bg-transparent text-purple-500" |
| 129 | + searchWords={ |
| 130 | + item.highlights.find((h) => h.field === "description") |
| 131 | + ?.matched_tokens || [] |
| 132 | + } |
| 133 | + autoEscape={true} |
| 134 | + textToHighlight={item.document.description} |
| 135 | + className="line-clamp-1 text-xs text-gray-400" |
| 136 | + /> |
| 137 | + </div> |
| 138 | + </div> |
| 139 | + </Command.Item> |
| 140 | + ); |
| 141 | + })} |
| 142 | + </Command.List> |
| 143 | + </Command> |
| 144 | + ); |
| 145 | +} |
0 commit comments