Feature/search bar - #11
Conversation
|
|
||
| const nextConfig: NextConfig = { | ||
| /* config options here */ | ||
| async rewrites() { |
There was a problem hiding this comment.
Why do we need this? Initially danswer plan to be public and we can just use public URL
There was a problem hiding this comment.
Sorry, this was something I tried to set up initially, and I forgot to remove it later.
|
|
||
| import SearchBar from "./SearchBar"; | ||
|
|
||
| export default function Header() { |
There was a problem hiding this comment.
nit: let's use arrow functions over export default to be consistent over codebase
There was a problem hiding this comment.
Okay, I'll update this and in SearchBar.tsx as well for consistency in the code.
| @@ -0,0 +1,15 @@ | |||
| import { useEffect, useState } from "react"; | |||
|
|
|||
| export function useDebounce<T>(value: T, delay: number): T { | |||
There was a problem hiding this comment.
Do you know what T means here?
There was a problem hiding this comment.
Yes, is a reference type for our function. It's the same type that we should have on the return. We are doing this for more reusability in our code.
| setError(null); | ||
|
|
||
| try { | ||
| const response = await fetch( |
There was a problem hiding this comment.
Let's abstract the logic of usage danswer.
What do you think about moving it to the new service like I did for notion? You can check notion.service
There was a problem hiding this comment.
I moved the search logic to the fetchDanswerData function in the srs/services folder as you requested. However, I couldn't find notion.service, so I implemented everything based on my understanding.
| })); | ||
|
|
||
| setSearchResults(results); | ||
| } catch (err: any) { |
There was a problem hiding this comment.
By default all errors in typescript is unknown and it's a better type rather than any. Do you know why?
| } catch (err: any) { | |
| } catch (err) { |
There was a problem hiding this comment.
I had thought about this before. I know that we should avoid using the 'any' type because we won't have control over the object type that we get. But somehow I left it as is. I'll redo it now.
| const [isLoading, setIsLoading] = useState(false); | ||
| const [error, setError] = useState<string | null>(null); | ||
|
|
||
| const debouncedQuery = useDebounce(searchQuery, 500); |
There was a problem hiding this comment.
Let's not use magic numbers. Let's move this to a variable outside of hook (because it static) and made it const
FYI: A magic number/string is a normal string or number that doesn't have a descriptive name and stands as a symbol.
There was a problem hiding this comment.
I added a constant:
const DEBOUNCE_DELAY = 500;
outside the hook.
| throw new Error(`No results found for "${searchQuery}"`); | ||
| } | ||
| const data = await response.json(); | ||
| const results = searchResultSchema.parse(data).documents.map((doc) => ({ |
There was a problem hiding this comment.
Move this logic of mapping in helper as it could simplify logic of hook
There was a problem hiding this comment.
Also I suggest to do maping and parsing in 2 different steps, this simplify reading of the code
There was a problem hiding this comment.
I created a file named mapDocuments.ts and placed it in the /helpers folder.
I implemented the .map function there with the proper types and added this helper function to useSearch.ts.
| } | ||
| }; | ||
|
|
||
| if (debouncedQuery !== previousQuery.current) { |
There was a problem hiding this comment.
Why do we need this if we already debounce query?
There was a problem hiding this comment.
Regarding your comment, we have this logic inside if (debouncedQuery !== previousQuery.current) because, previously, we used useEffect for triggering the search when the query changed, but it was requested to remove it. By moving this logic here, we ensure that the search only triggers when the debounced query changes, and we still avoid unnecessary renders that would occur if we used useEffect. It's essentially a manual debounce implementation, which ensures better performance and avoids unnecessary re-renders while keeping the debounce logic. I hope that clears it up!
|
|
||
| return { | ||
| searchQuery, | ||
| setSearchQuery, |
There was a problem hiding this comment.
I suggest to you now to share "setSeachQuery" and "setSearchResults" but instead share logical functions like "onClear" and "onQueryChange". In the end when you use this hook you need:
- get query for search
- perform change of query
- receive results
- have possibility to clear results
… service and helper functions; updated Zod schema
Optimize search functionality and add spiner to SearchBar