Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions src/components/Searchbar/searchbar-child.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,26 @@ function SearchBarChild({
filtersNotPulled?: () => void;
}) {
const router = useRouter();
const [count, setCount] = useState(0);
const [searchText, setSearchText] = useState("");
const [suggestions, setSuggestions] = useState<string[]>([]);
const [subjectCounts, setSubjectCounts] = useState<Record<string, number>>({});
const suggestionsRef = useRef<HTMLUListElement | null>(null);
const fuzzy = new Fuse(initialSubjects);

const fetchPaperCount = async (subjectName: string) => {
try {
const cleanSubject = subjectName.replace(/^"|"$/g, "");
const encodedSubject = encodeURIComponent(cleanSubject);

const response = await axios.get(`/api/papers/count?subject=${encodedSubject}`);
Copy link
Collaborator

@abhitrueprogrammer abhitrueprogrammer Jul 21, 2025

Choose a reason for hiding this comment

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

  1. Put in the interface of your response like:

axios.get<{count:int}(/api/papers/count?subject=${encodedSubject});

  1. put in the server uri like this: const { data } = await axios.get<ICourses[]>(${process.env.SERVER_URL}/api/course-list);

  2. install eslint plugin and ensure no errors remain. Also do pnpm build and if any errors come up, fix them


return response.data.count ?? 0;
} catch (error) {
console.error("Error fetching count for", subjectName, error);
return 0;
}
};

const handleSearchChange = async (e: React.ChangeEvent<HTMLInputElement>) => {
const text = e.target.value;
setSearchText(text);
Expand All @@ -35,8 +49,24 @@ function SearchBarChild({
.slice(0, 10);

setSuggestions(filteredSuggestions);

// Fetch counts in parallel for each suggestion
const counts = await Promise.all(
filteredSuggestions.map(async (subject) => {
const count = await fetchPaperCount(subject);
return { subject, count };
})
);

const countsMap = counts.reduce((acc, { subject, count }) => {
acc[subject] = count;
return acc;
}, {} as Record<string, number>);

setSubjectCounts(countsMap);
} else {
setSuggestions([]);
setSubjectCounts({});
}
};

Expand Down Expand Up @@ -98,9 +128,10 @@ function SearchBarChild({
<li
key={index}
onClick={() => handleSelectSuggestion(suggestion)}
className="cursor-pointer truncate p-2 hover:bg-gray-100 dark:hover:bg-gray-800"
className="flex items-center rounded cursor-pointer truncate p-2 hover:bg-gray-100 dark:hover:bg-gray-800"
>
{suggestion}
<div id="paper_count" className="bg-[#171720] w-10 h-10 flex items-center justify-center rounded-md text-white text-sm font-semibold mr-4">{subjectCounts[suggestion] ?? "0"}</div>
<span id="subject" className="text-white items-center text-sm sm:text-base tracking-wide">{suggestion}</span>
Comment on lines 132 to +134
Copy link
Collaborator

Choose a reason for hiding this comment

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

do add images while making pull requests. Makes it easier to review frontend

</li>
))}
</ul>
Expand Down