-
-
Notifications
You must be signed in to change notification settings - Fork 130
Add basic search functionality #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7c572c2
Add basic search functionality
GreenMan36 3474894
Move browser router to app.tsx
GreenMan36 b91a02f
Consolidated snippet and search snippet list
GreenMan36 6a44981
Add description to snippetlist and limit title & desc to 1 line
GreenMan36 53be0bd
Remove straggler issue template file
GreenMan36 7031525
Refactor useSnippets with feedback and more
GreenMan36 5edc1b1
Add less history spammy but scuffed search
GreenMan36 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,64 @@ | ||
import { SearchIcon } from "./Icons"; | ||
import { useState, useCallback } from "react"; | ||
import { useSearchParams, useNavigate } from "react-router-dom"; | ||
import { useSearchParams, useNavigate, useLocation } from "react-router-dom"; | ||
|
||
const SearchInput = () => { | ||
const navigate = useNavigate(); | ||
const location = useLocation(); | ||
const [searchParams, setSearchParams] = useSearchParams(); | ||
const [searchValue, setSearchValue] = useState(searchParams.get("q") || ""); | ||
|
||
const debouncedSearch = useCallback( | ||
debounce((query: string) => { | ||
if (query) { | ||
setSearchParams({ q: query }); | ||
navigate(`/search?q=${encodeURIComponent(query.trim().toLowerCase())}`); | ||
} else { | ||
const navigateToSearch = useCallback( | ||
(query: string, isCompletedSearch = false) => { | ||
const trimmedQuery = query.trim().toLowerCase(); | ||
|
||
if (!trimmedQuery) { | ||
// Remove search params and navigate to home if query is empty | ||
setSearchParams({}); | ||
navigate("/"); | ||
navigate("/", { replace: true }); | ||
return; | ||
} | ||
}, 200), | ||
[setSearchParams, navigate] | ||
); | ||
|
||
const handleSearch = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
const value = e.target.value; | ||
setSearchValue(value); | ||
debouncedSearch(value); | ||
}; | ||
// Set the search params with the query | ||
// Use replace: true for keypresses (when isCompletedSearch is false) | ||
setSearchParams({ q: trimmedQuery }, { replace: !isCompletedSearch }); | ||
|
||
// Only navigate if we're not already on the search page | ||
if (location.pathname !== "/search") { | ||
navigate("/search", { | ||
replace: isCompletedSearch || location.pathname === "/search", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one should always be false There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll look at it next year, I've been going crazy for the past four hours trying to get this all to behave. |
||
}); | ||
} | ||
}, | ||
[navigate, location.pathname, setSearchParams] | ||
); | ||
|
||
return ( | ||
<div className="search-field"> | ||
<form | ||
onSubmit={(e) => { | ||
e.preventDefault(); | ||
navigateToSearch(searchValue, true); | ||
}} | ||
className="search-field" | ||
> | ||
<label htmlFor="search"> | ||
<SearchIcon /> | ||
</label> | ||
<input | ||
type="search" | ||
id="search" | ||
value={searchValue} | ||
onChange={handleSearch} | ||
onChange={(e) => { | ||
const newValue = e.target.value; | ||
setSearchValue(newValue); | ||
navigateToSearch(newValue, false); | ||
}} | ||
onBlur={() => navigateToSearch(searchValue, true)} | ||
placeholder="Search here..." | ||
autoComplete="off" | ||
/> | ||
</div> | ||
</form> | ||
); | ||
}; | ||
|
||
// Debounce utility function | ||
function debounce<T extends (...args: any[]) => any>( | ||
func: T, | ||
wait: number | ||
): (...args: Parameters<T>) => void { | ||
let timeout: ReturnType<typeof setTimeout>; | ||
return (...args: Parameters<T>) => { | ||
clearTimeout(timeout); | ||
timeout = setTimeout(() => func(...args), wait); | ||
}; | ||
} | ||
|
||
export default SearchInput; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one shouldn't replace