-
-
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
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { useCategories } from "../hooks/useCategories"; | ||
import { useAppContext } from "../contexts/AppContext"; | ||
|
||
const SearchFilters = () => { | ||
const { category, setCategory } = useAppContext(); | ||
const { fetchedCategories } = useCategories(); | ||
|
||
return ( | ||
<div className="search-filters"> | ||
<select value={category} onChange={(e) => setCategory(e.target.value)}> | ||
<option value="">All Categories</option> | ||
{fetchedCategories.map((cat, idx) => ( | ||
<option key={idx} value={cat}> | ||
{cat} | ||
</option> | ||
))} | ||
</select> | ||
</div> | ||
); | ||
}; | ||
|
||
export default SearchFilters; |
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
Mathys-Gasnier marked this conversation as resolved.
Show resolved
Hide resolved
|
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { useState, useMemo } from "react"; | ||
import { SnippetType } from "../types"; | ||
import { useAppContext } from "../contexts/AppContext"; | ||
import { useSnippets } from "../hooks/useSnippets"; | ||
import SnippetModal from "./SnippetModal"; | ||
|
||
const SearchSnippetList = ({ query }: { query: string | null }) => { | ||
const { language, snippet, setSnippet } = useAppContext(); | ||
const { fetchedSnippets, loading } = useSnippets(); | ||
const [isModalOpen, setIsModalOpen] = useState(false); | ||
|
||
const filteredSnippets = useMemo(() => { | ||
if (!query) return []; | ||
return fetchedSnippets.filter((snippet) => | ||
snippet.title.toLowerCase().includes(query.toLowerCase()) | ||
); | ||
}, [fetchedSnippets, query]); | ||
|
||
const handleOpenModal = (activeSnippet: SnippetType) => { | ||
setIsModalOpen(true); | ||
setSnippet(activeSnippet); | ||
}; | ||
|
||
const handleCloseModal = () => { | ||
setIsModalOpen(false); | ||
setSnippet(null); | ||
}; | ||
|
||
if (loading) return <div>Searching...</div>; | ||
if (filteredSnippets.length === 0) | ||
return <div>No results found for "{query}"</div>; | ||
|
||
return ( | ||
<> | ||
<ul role="list" className="snippets"> | ||
{filteredSnippets.map((snippet, idx) => ( | ||
<li key={idx}> | ||
<button | ||
className="snippet | flow" | ||
data-flow-space="sm" | ||
onClick={() => handleOpenModal(snippet)} | ||
> | ||
<div className="snippet__preview"> | ||
<img src={language.icon} alt={language.lang} /> | ||
</div> | ||
<h3 className="snippet__title">{snippet.title}</h3> | ||
<p className="snippet__description">{snippet.description}</p> | ||
</button> | ||
</li> | ||
))} | ||
</ul> | ||
|
||
{isModalOpen && snippet && ( | ||
<SnippetModal | ||
snippet={snippet} | ||
handleCloseModal={handleCloseModal} | ||
language={language.lang} | ||
/> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
export default SearchSnippetList; |
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
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 |
---|---|---|
|
@@ -14,8 +14,10 @@ export const useSnippets = () => { | |
`/data/${slugify(language.lang)}.json` | ||
); | ||
|
||
const fetchedSnippets = data | ||
? data.find((item) => item.categoryName === category)?.snippets | ||
const fetchedSnippets: SnippetType[] = data | ||
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. instead of adding nested ternary operator, it would be better to populate it through a function. 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 did the thing 🫡 |
||
? category === "All snippets" | ||
? data.flatMap((item) => item.snippets) | ||
: (data.find((item) => item.categoryName === category)?.snippets ?? []) | ||
: []; | ||
|
||
return { fetchedSnippets, loading, error }; | ||
|
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
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { useAppContext } from "../contexts/AppContext"; | ||
import SnippetList from "../components/SnippetList"; | ||
import Sidebar from "../layouts/Sidebar"; | ||
|
||
const HomePage = () => { | ||
const { category } = useAppContext(); | ||
|
||
return ( | ||
<> | ||
<Sidebar /> | ||
<section className="flow"> | ||
<h2 className="section-title"> | ||
{category ? category : "Select a category"} | ||
</h2> | ||
<SnippetList /> | ||
</section> | ||
</> | ||
); | ||
}; | ||
|
||
export default HomePage; |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { useSearchParams } from "react-router-dom"; | ||
import SearchSnippetList from "../components/SearchSnippetList"; | ||
import Sidebar from "../layouts/Sidebar"; | ||
|
||
const SearchPage = () => { | ||
const [searchParams] = useSearchParams(); | ||
const query = searchParams.get("q"); | ||
|
||
return ( | ||
<> | ||
<Sidebar /> | ||
<section className="flow"> | ||
<h2 className="section-title">Search Results for: {query}</h2> | ||
<SearchSnippetList query={query} /> | ||
</section> | ||
</> | ||
); | ||
}; | ||
|
||
export default SearchPage; |
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.
I don't think navigating urls every debounced request is a good thing. I know it is debounced but still sometimes people pause to think while searching, and it will clutter the history.
You can maybe search on
Enter
, or show results while typing but navigate the URL on Enter or onBlurThere 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.
It's an SPA so it doesn't "refresh" the page, but yeah it will clutter the history, what you can do is use the replace feature of navigate here
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.
I think on Enter and onBlur is a great idea! I'll look into it.