Skip to content

Commit cb9fd72

Browse files
fix dropdown bug
1 parent 41b8806 commit cb9fd72

1 file changed

Lines changed: 51 additions & 4 deletions

File tree

frontend/src/components/search/SearchBar.tsx

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,33 @@ export default function SearchBar() {
1919
const searchBarRef = useRef<HTMLDivElement>(null);
2020
const fetchInProgress = useRef(false);
2121
const initialRender = useRef(true);
22+
const previousPath = useRef(location.pathname);
23+
const debounceTimer = useRef<NodeJS.Timeout | null>(null);
24+
25+
// Immediately hide dropdown when location changes
26+
// This runs before any other effects
27+
useEffect(() => {
28+
// Hide dropdown immediately on any location change
29+
setShowResults(false);
30+
31+
// If the path has changed (not including search params)
32+
if (location.pathname !== previousPath.current) {
33+
// Clear the search input and results
34+
setQuery('');
35+
setDebouncedQuery('');
36+
setUserResults([]);
37+
setContentResults([]);
38+
39+
// Clear any pending debounce timers
40+
if (debounceTimer.current) {
41+
clearTimeout(debounceTimer.current);
42+
debounceTimer.current = null;
43+
}
44+
45+
// Update the previous path
46+
previousPath.current = location.pathname;
47+
}
48+
}, [location]);
2249

2350
// Handle outside clicks to close the dropdown
2451
useEffect(() => {
@@ -42,18 +69,32 @@ export default function SearchBar() {
4269
return;
4370
}
4471

45-
const handler = setTimeout(() => {
72+
// Clear any existing timer
73+
if (debounceTimer.current) {
74+
clearTimeout(debounceTimer.current);
75+
}
76+
77+
// Don't set up a new timer if we're on the search results page
78+
if (location.pathname === '/search') {
79+
return;
80+
}
81+
82+
debounceTimer.current = setTimeout(() => {
4683
if (query.trim().length >= 3) {
4784
setDebouncedQuery(query);
4885
} else {
4986
setShowResults(false);
5087
}
88+
debounceTimer.current = null;
5189
}, 500);
5290

5391
return () => {
54-
clearTimeout(handler);
92+
if (debounceTimer.current) {
93+
clearTimeout(debounceTimer.current);
94+
debounceTimer.current = null;
95+
}
5596
};
56-
}, [query]);
97+
}, [query, location.pathname]);
5798

5899
// Fetch search results when debounced query changes
59100
useEffect(() => {
@@ -83,6 +124,12 @@ export default function SearchBar() {
83124
console.log(`SearchBar: Fetching dropdown results for "${debouncedQuery}"`);
84125
const response = await SearchService.search(debouncedQuery, "all", 5, 0);
85126

127+
// Check if the path has changed since we started the fetch
128+
if (location.pathname !== previousPath.current) {
129+
// Don't update state if we've navigated away
130+
return;
131+
}
132+
86133
if (!(response instanceof Error)) {
87134
// Explicitly type the response to ensure type safety
88135
const users: SearchUser[] = response.users;
@@ -147,7 +194,7 @@ export default function SearchBar() {
147194
</button>
148195
</form>
149196

150-
{showResults && (
197+
{showResults && query.trim().length >= 3 && (
151198
<div className="search-dropdown">
152199
{loading ? (
153200
<div className="search-loading">Loading...</div>

0 commit comments

Comments
 (0)