-
-
Notifications
You must be signed in to change notification settings - Fork 129
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
Add basic search functionality #34
Conversation
I don't have much experience with react so my architecture and code is likely not so clean. |
✅ Deploy Preview for quicksnip ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
I agree that moving the state to the URL should be something we work on, as it also allows to go back and forth in your history of visited pages |
As you said you aren't really familiar with react router and all, if you want I can contribute to this PR improving the way it's used and handled |
I'd really appreciate that! I could figure it out, but learning React Router while working on a public repo sounds like more effort than I want to put in right now. |
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.
For me it looks good, let's wait on another review since it's a pretty big change
src/hooks/useSnippets.ts
Outdated
@@ -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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
I did the thing 🫡
src/components/SearchInput.tsx
Outdated
debounce((query: string) => { | ||
if (query) { | ||
setSearchParams({ q: query }); | ||
navigate(`/search?q=${encodeURIComponent(query.trim().toLowerCase())}`); |
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 onBlur
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.
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.
@@ -1,29 +1,24 @@ | |||
import { useAppContext } from "./contexts/AppContext"; | |||
|
|||
import { Routes, Route, BrowserRouter } from "react-router-dom"; |
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.
react-router-dom
is not used anywhere in the project, it was just added in package.json
. So I think, it will be better to use the latest version.
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'm not sure where you're seeing it being added to the package.json. react-router-dom
was part of the initial commit.
If you wish to have the dependencies updated, I recommend making a new PR for it.
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.
Can you update it in your PR ?
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.
It was added in the initial commit, but its not being used anywhere. Its just there.
Since you are using it in this PR, you can just make it like its being added for the first time and update it to the latest version.
Instead of using ternary operators, I made a pure function with early returns for easier readability and maintainability. Also moved the endpoint to a const for clarity/simplicity.
I rewrote this five times today, all with similar issues. Either you write too much to history, or you overwrite history too much. I'm pretty sure in the current implementation, we don't even need /search as a page
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.
Apart from the two little comments i made, it seems about right
setSearchParams({}); | ||
navigate("/"); | ||
navigate("/", { replace: true }); |
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
// 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 comment
The 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 comment
The 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.
Oh and there is a weird flicker when you go from the search page to the hompage... I think one way to make this easier would be to remove the separation between the search and hompage... |
I originally intended to make a different design, in my latest commit message I also conclude the same as you. |
When using quicksnip, I find that this is the feature I am missing most. I'm not sure about the current state of development on this PR, but I wanted to help a little, so I finished the search feature based on the work already completed and feedback within this task (addressing some of the comments and smaller errors also). @GreenMan36 If you are currently working on this and are nearing a solution please feel free to ignore, as a lot of my work is based on yours anyway. You are more than welcome to have a look and use any code from the patch file I will attach to this comment if you find it helpful. Here is a demo. I'm also attaching the patch file so the work can be applied to this branch. I started from a more recent version of the |
Awesome work! I just shut down my PC but I guess imma boot it back up haha! Give me a few minutes! I didn't share in the public channels but; https://lucaong.github.io/minisearch/ This might be a neat project to integrate. |
Yikes, I merged main to mine to be up-to-date and due to all the commit hooks I had to run lint and format but that seems to have changed a couple of things? |
minisearch might be a nince addition in the future once we get the basic search up and running for sure. |
2604786
to
5edc1b1
Compare
Yes, quite a lot of things have changed. Definitely make sure that all of the packages are up to date as well. Linting and prettier formatting might require you to change some things as this PR is based on older code. |
Yeah I'm having major issues with Prettier, ESlint and GIT all messing with formatting and line styles and whatnot. I think this branch and all my other are now broken. |
Maybe the easiest thing to do might be to keep this PR active for now, create a new branch from latest and get everything working, apply the patch from above then make any changes you need from this branch? Maybe create a new PR as well if you need to more easily compare the changes. |
I did a hard reset of my branch on main, and its still broken. I recommend you make a new branch with your changes, make a new PR and you can mention this PR. Your changes look good to me. |
@GreenMan36 The new PR is here #159. Please feel free to leave a review, or suggest/make any changes that you think I've missed. Thanks |
Closing this PR. |
It's pretty scuffed as its been a while since I worked with React.
I added the
react-router-dom
for theBrowserRouter
so I could useuseSearchParams
anduseNavigate
.I'd like to move more of the state to the url, this is great as it allows you to share your search queries and what snippet you have open. However, that would be a major refactor, so I'll leave the idea here.
It's been like a year but WDS had a video on it and I Theo as well. I think, I didn't re-watch to check.