|
| 1 | + |
| 2 | +import { useState } from "react"; |
| 3 | +import { Search, X } from "lucide-react"; |
| 4 | +import { useSearch } from "./useSearch"; |
| 5 | +import type { SearchItemData } from "./types"; |
| 6 | +import SearchItem from "./SearchItem"; |
| 7 | + |
| 8 | +interface Props { |
| 9 | + onSearch: (query: string) => Promise<SearchItemData[]>; |
| 10 | +} |
| 11 | + |
| 12 | +export default function SearchBar({ onSearch }: Props) { |
| 13 | + const [open, setOpen] = useState(false); |
| 14 | + |
| 15 | + const [recent, setRecent] = useState<SearchItemData[]>([ |
| 16 | + { id: 1, name: "Mayra S.", meta: "128k Followers" }, |
| 17 | + { id: 2, name: "Vihan Singh", meta: "112k Followers" }, |
| 18 | + ]); |
| 19 | + |
| 20 | + const { query, setQuery, results, loading } = useSearch(onSearch); |
| 21 | + |
| 22 | + const handleSelect = (item: SearchItemData) => { |
| 23 | + setRecent((prev) => |
| 24 | + [item, ...prev.filter((i) => i.id !== item.id)].slice(0, 5) |
| 25 | + ); |
| 26 | + setOpen(false); |
| 27 | + setQuery(""); |
| 28 | + }; |
| 29 | + |
| 30 | + return ( |
| 31 | + <div className="relative w-full max-w-lg"> |
| 32 | + <div |
| 33 | + onClick={() => setOpen(true)} |
| 34 | + className="flex items-center gap-2 px-4 py-2.5 rounded-xl |
| 35 | + bg-[#0b1330] border border-white/10 |
| 36 | + focus-within:ring-2 focus-within:ring-blue-500/70 |
| 37 | + transition" |
| 38 | + > |
| 39 | + <Search className="w-4 h-4 text-white/60" /> |
| 40 | + |
| 41 | + <input |
| 42 | + value={query} |
| 43 | + onChange={(e) => setQuery(e.target.value)} |
| 44 | + placeholder="Search for something..." |
| 45 | + className="flex-1 bg-transparent outline-none |
| 46 | + text-sm text-white placeholder-white/50" |
| 47 | + /> |
| 48 | + |
| 49 | + {query && ( |
| 50 | + <button |
| 51 | + type="button" |
| 52 | + onClick={() => setQuery("")} |
| 53 | + className="text-white/50 hover:text-white transition" |
| 54 | + > |
| 55 | + <X className="w-4 h-4" /> |
| 56 | + </button> |
| 57 | + )} |
| 58 | + </div> |
| 59 | + {open && ( |
| 60 | + <div |
| 61 | + className="absolute z-50 mt-2 w-full rounded-2xl |
| 62 | + bg-[#0a1130] border border-white/10 |
| 63 | + shadow-2xl overflow-hidden" |
| 64 | + > |
| 65 | + <div |
| 66 | + className="flex items-center justify-between px-4 py-3 |
| 67 | + border-b border-white/10" |
| 68 | + > |
| 69 | + <span className="text-xs font-medium text-white/70"> |
| 70 | + {query ? "Search results" : "Recent"} |
| 71 | + </span> |
| 72 | + |
| 73 | + {!query && recent.length > 0 && ( |
| 74 | + <button |
| 75 | + type="button" |
| 76 | + onClick={() => setRecent([])} |
| 77 | + className="text-white/40 hover:text-white transition" |
| 78 | + title="Clear all" |
| 79 | + > |
| 80 | + <X className="w-4 h-4" /> |
| 81 | + </button> |
| 82 | + )} |
| 83 | + </div> |
| 84 | + <div className="p-2 max-h-72 overflow-y-auto"> |
| 85 | + {loading && ( |
| 86 | + <p className="text-center text-xs text-white/50 py-4"> |
| 87 | + Searching… |
| 88 | + </p> |
| 89 | + )} |
| 90 | + |
| 91 | + {!loading && query && results.length === 0 && ( |
| 92 | + <p className="text-center text-xs text-white/40 py-4"> |
| 93 | + No results found |
| 94 | + </p> |
| 95 | + )} |
| 96 | + |
| 97 | + {!query && |
| 98 | + recent.map((item) => ( |
| 99 | + <SearchItem |
| 100 | + key={item.id} |
| 101 | + item={item} |
| 102 | + onSelect={handleSelect} |
| 103 | + /> |
| 104 | + ))} |
| 105 | + |
| 106 | + {query && |
| 107 | + results.map((item) => ( |
| 108 | + <SearchItem |
| 109 | + key={item.id} |
| 110 | + item={item} |
| 111 | + onSelect={handleSelect} |
| 112 | + /> |
| 113 | + ))} |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + )} |
| 117 | + </div> |
| 118 | + ); |
| 119 | + } |
0 commit comments