Skip to content

Feature/search bar - #11

Open
N3k1ta wants to merge 5 commits into
mainfrom
feature/search-bar
Open

Feature/search bar#11
N3k1ta wants to merge 5 commits into
mainfrom
feature/search-bar

Conversation

@N3k1ta

@N3k1ta N3k1ta commented Dec 12, 2024

Copy link
Copy Markdown

Optimize search functionality and add spiner to SearchBar

Comment thread next.config.ts Outdated

const nextConfig: NextConfig = {
/* config options here */
async rewrites() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this? Initially danswer plan to be public and we can just use public URL

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, this was something I tried to set up initially, and I forgot to remove it later.

Comment thread src/components/Header.tsx Outdated

import SearchBar from "./SearchBar";

export default function Header() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: let's use arrow functions over export default to be consistent over codebase

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I'll update this and in SearchBar.tsx as well for consistency in the code.

Comment thread src/hooks/useDebounce.ts
@@ -0,0 +1,15 @@
import { useEffect, useState } from "react";

export function useDebounce<T>(value: T, delay: number): T {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know what T means here?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/hooks/useSearch.ts Outdated
setError(null);

try {
const response = await fetch(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/hooks/useSearch.ts Outdated
}));

setSearchResults(results);
} catch (err: any) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default all errors in typescript is unknown and it's a better type rather than any. Do you know why?

Suggested change
} catch (err: any) {
} catch (err) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/hooks/useSearch.ts Outdated
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<string | null>(null);

const debouncedQuery = useDebounce(searchQuery, 500);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a constant:
const DEBOUNCE_DELAY = 500;
outside the hook.

Comment thread src/hooks/useSearch.ts Outdated
throw new Error(`No results found for "${searchQuery}"`);
}
const data = await response.json();
const results = searchResultSchema.parse(data).documents.map((doc) => ({

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this logic of mapping in helper as it could simplify logic of hook

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I suggest to do maping and parsing in 2 different steps, this simplify reading of the code

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/hooks/useSearch.ts
}
};

if (debouncedQuery !== previousQuery.current) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this if we already debounce query?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

Comment thread src/hooks/useSearch.ts Outdated

return {
searchQuery,
setSearchQuery,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

… service and helper functions; updated Zod schema
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants