-
Notifications
You must be signed in to change notification settings - Fork 0
feat: search files #1
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
Conversation
Summary of ChangesHello @parthsali, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a significant enhancement to the academics section by adding a global file search capability. Users can now quickly locate specific academic files through an intuitive search dialog, which supports debounced queries and a convenient keyboard shortcut. This feature aims to improve the overall navigability and accessibility of academic resources. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
This pull request introduces a new file search feature, which is a great addition. It includes a new search component, API endpoint integration, and necessary UI components. The implementation is solid, but I have a few suggestions to improve robustness and maintainability, particularly regarding a race condition in the search logic, the use of magic numbers, and the placement of a helper function. Please see my detailed comments.
| useEffect(() => { | ||
| if (!searchQuery.trim()) { | ||
| setResults([]); | ||
| return; | ||
| } | ||
|
|
||
| setIsLoading(true); | ||
| const timer = setTimeout(async () => { | ||
| try { | ||
| const data = await searchFiles({ | ||
| query: searchQuery, | ||
| page: 1, | ||
| limit: 10, | ||
| }); | ||
|
|
||
| setResults(data.docs); | ||
| } catch (error) { | ||
| console.error("Search error:", error); | ||
| setResults([]); | ||
| } finally { | ||
| setIsLoading(false); | ||
| } | ||
| }, 300); // 300ms debounce | ||
|
|
||
| return () => clearTimeout(timer); | ||
| }, [searchQuery]); |
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 useEffect hook for debounced search has a potential race condition. If a user types quickly, multiple search requests can be initiated. If an earlier, slower request resolves after a later, faster one, the UI will display stale results. To fix this, you should ignore the results of stale requests. You can achieve this by using a flag that is checked before setting state, and toggled in the effect's cleanup function.
useEffect(() => {
if (!searchQuery.trim()) {
setResults([]);
return;
}
let isStale = false;
setIsLoading(true);
const timer = setTimeout(async () => {
try {
const data = await searchFiles({
query: searchQuery,
page: 1,
limit: 10,
});
if (!isStale) {
setResults(data.docs);
}
} catch (error) {
if (!isStale) {
console.error("Search error:", error);
setResults([]);
}
} finally {
if (!isStale) {
setIsLoading(false);
}
}
}, 300); // 300ms debounce
return () => {
clearTimeout(timer);
isStale = true;
};
}, [searchQuery]);
| limit: 10, | ||
| }); | ||
|
|
||
| setResults(data.docs); | ||
| } catch (error) { | ||
| console.error("Search error:", error); | ||
| setResults([]); | ||
| } finally { | ||
| setIsLoading(false); | ||
| } | ||
| }, 300); // 300ms debounce |
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.
The search result limit (10) on line 49 and the debounce delay (300) on line 59 are hardcoded. It's a good practice to extract these 'magic numbers' into named constants at the top of the file. This improves readability and makes them easier to modify in the future.
For example:
const SEARCH_RESULTS_LIMIT = 10;
const DEBOUNCE_DELAY_MS = 300;|
|
||
| setResults(data.docs); | ||
| } catch (error) { | ||
| console.error("Search error:", error); |
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.
Currently, search API errors are only logged to the console. For a better user experience, consider displaying a user-facing error message, for example, using a toast notification or showing an error message within the search dialog. This informs the user that something went wrong with their search.
| const getCategoryLabel = (category: string, unitNumber?: number) => { | ||
| if (category === "unit" && unitNumber) { | ||
| return `Unit ${unitNumber}`; | ||
| } | ||
| if (category === "endsem") return "Previous Year - End-Sem"; | ||
| if (category === "insem") return "Previous Year - In-Sem"; | ||
| if (category === "decode") return "Decodes"; | ||
| if (category === "book") return "Books"; | ||
| return category; | ||
| }; |
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.
The getCategoryLabel function is redefined on every render of the AcademicsFileSearch component. Since it's a pure function that doesn't rely on any component state or props, it can be defined outside the component scope. This prevents unnecessary re-creation and is a good practice for code organization and a slight performance improvement.
No description provided.