|
| 1 | +import { Loader2Icon, SearchIcon, XIcon } from "lucide-react"; |
| 2 | +import { useEffect, useMemo } from "react"; |
| 3 | + |
| 4 | +import { Kbd } from "@hypr/ui/components/ui/kbd"; |
| 5 | +import { useCmdKeyPressed } from "@hypr/ui/hooks/use-cmd-key-pressed"; |
| 6 | +import { cn } from "@hypr/utils"; |
| 7 | + |
| 8 | +import { useSearch } from "~/search/contexts/ui"; |
| 9 | +import { useTabs } from "~/store/zustand/tabs"; |
| 10 | + |
| 11 | +export function SidebarSearchInput() { |
| 12 | + const { |
| 13 | + query, |
| 14 | + setQuery, |
| 15 | + inputRef, |
| 16 | + setFocusImpl, |
| 17 | + isSearching, |
| 18 | + isIndexing, |
| 19 | + selectedIndex, |
| 20 | + setSelectedIndex, |
| 21 | + results, |
| 22 | + } = useSearch(); |
| 23 | + const isCmdPressed = useCmdKeyPressed(); |
| 24 | + const openNew = useTabs((state) => state.openNew); |
| 25 | + |
| 26 | + const flatResults = useMemo(() => { |
| 27 | + if (!results) return []; |
| 28 | + return results.groups.flatMap((g) => g.results); |
| 29 | + }, [results]); |
| 30 | + |
| 31 | + useEffect(() => { |
| 32 | + setFocusImpl(() => { |
| 33 | + inputRef.current?.focus(); |
| 34 | + }); |
| 35 | + }, [setFocusImpl, inputRef]); |
| 36 | + |
| 37 | + const showLoading = isSearching || isIndexing; |
| 38 | + const showShortcut = isCmdPressed && !query; |
| 39 | + |
| 40 | + return ( |
| 41 | + <div className="relative flex h-8 shrink-0 items-center px-2"> |
| 42 | + {showLoading ? ( |
| 43 | + <Loader2Icon |
| 44 | + className={cn([ |
| 45 | + "absolute left-5 h-4 w-4 animate-spin text-neutral-400", |
| 46 | + ])} |
| 47 | + /> |
| 48 | + ) : ( |
| 49 | + <SearchIcon |
| 50 | + className={cn(["absolute left-5 h-4 w-4 text-neutral-400"])} |
| 51 | + /> |
| 52 | + )} |
| 53 | + <input |
| 54 | + ref={inputRef} |
| 55 | + type="text" |
| 56 | + placeholder="Search anything..." |
| 57 | + value={query} |
| 58 | + onChange={(e) => setQuery(e.target.value)} |
| 59 | + onKeyDown={(e) => { |
| 60 | + if (e.key === "Escape") { |
| 61 | + if (query.trim()) { |
| 62 | + setQuery(""); |
| 63 | + setSelectedIndex(-1); |
| 64 | + } else { |
| 65 | + e.currentTarget.blur(); |
| 66 | + } |
| 67 | + } |
| 68 | + if (e.key === "Enter" && (e.metaKey || e.ctrlKey) && query.trim()) { |
| 69 | + e.preventDefault(); |
| 70 | + openNew({ |
| 71 | + type: "search", |
| 72 | + state: { |
| 73 | + selectedTypes: null, |
| 74 | + initialQuery: query.trim(), |
| 75 | + }, |
| 76 | + }); |
| 77 | + setQuery(""); |
| 78 | + e.currentTarget.blur(); |
| 79 | + } |
| 80 | + if (e.key === "ArrowDown" && flatResults.length > 0) { |
| 81 | + e.preventDefault(); |
| 82 | + setSelectedIndex( |
| 83 | + Math.min(selectedIndex + 1, flatResults.length - 1), |
| 84 | + ); |
| 85 | + } |
| 86 | + if (e.key === "ArrowUp" && flatResults.length > 0) { |
| 87 | + e.preventDefault(); |
| 88 | + setSelectedIndex(Math.max(selectedIndex - 1, -1)); |
| 89 | + } |
| 90 | + if ( |
| 91 | + e.key === "Enter" && |
| 92 | + !e.metaKey && |
| 93 | + !e.ctrlKey && |
| 94 | + selectedIndex >= 0 && |
| 95 | + selectedIndex < flatResults.length |
| 96 | + ) { |
| 97 | + e.preventDefault(); |
| 98 | + const item = flatResults[selectedIndex]; |
| 99 | + if (item.type === "session") { |
| 100 | + openNew({ type: "sessions", id: item.id }); |
| 101 | + } else if (item.type === "human") { |
| 102 | + openNew({ |
| 103 | + type: "contacts", |
| 104 | + state: { |
| 105 | + selected: { type: "person", id: item.id }, |
| 106 | + }, |
| 107 | + }); |
| 108 | + } else if (item.type === "organization") { |
| 109 | + openNew({ |
| 110 | + type: "contacts", |
| 111 | + state: { |
| 112 | + selected: { type: "organization", id: item.id }, |
| 113 | + }, |
| 114 | + }); |
| 115 | + } |
| 116 | + e.currentTarget.blur(); |
| 117 | + } |
| 118 | + }} |
| 119 | + className={cn([ |
| 120 | + "text-sm placeholder:text-sm placeholder:text-neutral-400", |
| 121 | + "h-full w-full pl-8", |
| 122 | + query ? "pr-8" : showShortcut ? "pr-14" : "pr-4", |
| 123 | + "rounded-lg bg-neutral-100", |
| 124 | + "focus:bg-neutral-200 focus:outline-hidden", |
| 125 | + ])} |
| 126 | + /> |
| 127 | + {query && ( |
| 128 | + <button |
| 129 | + onClick={() => setQuery("")} |
| 130 | + className={cn([ |
| 131 | + "absolute right-5", |
| 132 | + "h-4 w-4", |
| 133 | + "text-neutral-400 hover:text-neutral-600", |
| 134 | + "transition-colors", |
| 135 | + ])} |
| 136 | + aria-label="Clear search" |
| 137 | + > |
| 138 | + <XIcon className="h-4 w-4" /> |
| 139 | + </button> |
| 140 | + )} |
| 141 | + {showShortcut && ( |
| 142 | + <div className="absolute top-1 right-4"> |
| 143 | + <Kbd>⌘ K</Kbd> |
| 144 | + </div> |
| 145 | + )} |
| 146 | + </div> |
| 147 | + ); |
| 148 | +} |
0 commit comments