|
1 | 1 | import { SearchIcon } from "./Icons"; |
2 | 2 | import { useState, useCallback } from "react"; |
3 | | -import { useSearchParams, useNavigate } from "react-router-dom"; |
| 3 | +import { useSearchParams, useNavigate, useLocation } from "react-router-dom"; |
4 | 4 |
|
5 | 5 | const SearchInput = () => { |
6 | 6 | const navigate = useNavigate(); |
| 7 | + const location = useLocation(); |
7 | 8 | const [searchParams, setSearchParams] = useSearchParams(); |
8 | 9 | const [searchValue, setSearchValue] = useState(searchParams.get("q") || ""); |
9 | 10 |
|
10 | | - const debouncedSearch = useCallback( |
11 | | - debounce((query: string) => { |
12 | | - if (query) { |
13 | | - setSearchParams({ q: query }); |
14 | | - navigate(`/search?q=${encodeURIComponent(query.trim().toLowerCase())}`); |
15 | | - } else { |
| 11 | + const navigateToSearch = useCallback( |
| 12 | + (query: string, isCompletedSearch = false) => { |
| 13 | + const trimmedQuery = query.trim().toLowerCase(); |
| 14 | + |
| 15 | + if (!trimmedQuery) { |
| 16 | + // Remove search params and navigate to home if query is empty |
16 | 17 | setSearchParams({}); |
17 | | - navigate("/"); |
| 18 | + navigate("/", { replace: true }); |
| 19 | + return; |
18 | 20 | } |
19 | | - }, 200), |
20 | | - [setSearchParams, navigate] |
21 | | - ); |
22 | 21 |
|
23 | | - const handleSearch = (e: React.ChangeEvent<HTMLInputElement>) => { |
24 | | - const value = e.target.value; |
25 | | - setSearchValue(value); |
26 | | - debouncedSearch(value); |
27 | | - }; |
| 22 | + // Set the search params with the query |
| 23 | + // Use replace: true for keypresses (when isCompletedSearch is false) |
| 24 | + setSearchParams({ q: trimmedQuery }, { replace: !isCompletedSearch }); |
| 25 | + |
| 26 | + // Only navigate if we're not already on the search page |
| 27 | + if (location.pathname !== "/search") { |
| 28 | + navigate("/search", { |
| 29 | + replace: isCompletedSearch || location.pathname === "/search", |
| 30 | + }); |
| 31 | + } |
| 32 | + }, |
| 33 | + [navigate, location.pathname, setSearchParams] |
| 34 | + ); |
28 | 35 |
|
29 | 36 | return ( |
30 | | - <div className="search-field"> |
| 37 | + <form |
| 38 | + onSubmit={(e) => { |
| 39 | + e.preventDefault(); |
| 40 | + navigateToSearch(searchValue, true); |
| 41 | + }} |
| 42 | + className="search-field" |
| 43 | + > |
31 | 44 | <label htmlFor="search"> |
32 | 45 | <SearchIcon /> |
33 | 46 | </label> |
34 | 47 | <input |
35 | 48 | type="search" |
36 | 49 | id="search" |
37 | 50 | value={searchValue} |
38 | | - onChange={handleSearch} |
| 51 | + onChange={(e) => { |
| 52 | + const newValue = e.target.value; |
| 53 | + setSearchValue(newValue); |
| 54 | + navigateToSearch(newValue, false); |
| 55 | + }} |
| 56 | + onBlur={() => navigateToSearch(searchValue, true)} |
39 | 57 | placeholder="Search here..." |
40 | 58 | autoComplete="off" |
41 | 59 | /> |
42 | | - </div> |
| 60 | + </form> |
43 | 61 | ); |
44 | 62 | }; |
45 | 63 |
|
46 | | -// Debounce utility function |
47 | | -function debounce<T extends (...args: any[]) => any>( |
48 | | - func: T, |
49 | | - wait: number |
50 | | -): (...args: Parameters<T>) => void { |
51 | | - let timeout: ReturnType<typeof setTimeout>; |
52 | | - return (...args: Parameters<T>) => { |
53 | | - clearTimeout(timeout); |
54 | | - timeout = setTimeout(() => func(...args), wait); |
55 | | - }; |
56 | | -} |
57 | | - |
58 | 64 | export default SearchInput; |
0 commit comments