-
Notifications
You must be signed in to change notification settings - Fork 0
β‘ Bolt: Fix search debounce to prevent redundant URL updates and renders #156
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| "use client"; | ||
|
|
||
| import { usePathname, useRouter, useSearchParams } from "next/navigation"; | ||
| import { useCallback, useEffect, useState, useTransition } from "react"; | ||
| import { useCallback, useEffect, useState, useTransition, useRef } from "react"; | ||
| import { motion } from "framer-motion"; | ||
|
|
||
| interface TalksFilterBarProps { | ||
|
|
@@ -21,6 +21,15 @@ export default function TalksFilterBar({ tracks, year: _year }: TalksFilterBarPr | |
|
|
||
| const [selectedTrack, setSelectedTrack] = useState<string>(searchParams.get("track") || ""); | ||
| const [searchQuery, setSearchQuery] = useState<string>(searchParams.get("q") || ""); | ||
| const searchTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| return () => { | ||
| if (searchTimeoutRef.current) { | ||
| clearTimeout(searchTimeoutRef.current); | ||
| } | ||
| }; | ||
| }, []); | ||
|
|
||
| // Update state when URL changes | ||
| useEffect(() => { | ||
|
|
@@ -60,11 +69,15 @@ export default function TalksFilterBar({ tracks, year: _year }: TalksFilterBarPr | |
| const handleSearchChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
| const newQuery = e.target.value; | ||
| setSearchQuery(newQuery); | ||
|
|
||
| if (searchTimeoutRef.current) { | ||
| clearTimeout(searchTimeoutRef.current); | ||
| } | ||
|
|
||
| // Debounce the URL update for search | ||
| const timeoutId = setTimeout(() => { | ||
| searchTimeoutRef.current = setTimeout(() => { | ||
| updateFilters(selectedTrack, newQuery); | ||
| }, 300); | ||
|
Comment on lines
+78
to
80
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This debounced call to Additionally, there is a significant UX issue: when To fix the race condition, you should clear
Comment on lines
+73
to
80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1. Stale track in debounce A pending debounced search timer can call updateFilters with a stale selectedTrack if the user clicks a track pill within the 300ms debounce window, reverting the URL (and rendered results) back to the old track. This happens because handleTrackClick doesnβt cancel the pending search timeout and the timeout callback closes over selectedTrack from the earlier render. Agent Prompt
Comment on lines
+78
to
80
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. π§© Analysis chainπ Script executed: #!/bin/bash
# Verify stale-closure pattern is present in debounce callback
rg -nP --type=tsx 'setTimeout\(\(\)\s*=>\s*\{\s*updateFilters\(selectedTrack,\s*newQuery\)' components/layout/TalksFilterBar.tsxRepository: anyulled/devbcn-nextjs Length of output: 93 π Script executed: cat -n components/layout/TalksFilterBar.tsx | head -100Repository: anyulled/devbcn-nextjs Length of output: 3819 π Script executed: # Check the rest of the file to understand full context around lines 78-80
tail -n +75 components/layout/TalksFilterBar.tsx | head -20Repository: anyulled/devbcn-nextjs Length of output: 714 Debounced callback captures stale On Line 79, the timeout callback uses the render-time π‘ Proposed fix const [selectedTrack, setSelectedTrack] = useState<string>(searchParams.get("track") || "");
const [searchQuery, setSearchQuery] = useState<string>(searchParams.get("q") || "");
const searchTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
+ const selectedTrackRef = useRef(selectedTrack);
+
+ useEffect(() => {
+ selectedTrackRef.current = selectedTrack;
+ }, [selectedTrack]);
const handleTrackClick = (track: string) => {
const newTrack = track === selectedTrack ? "" : track;
+ selectedTrackRef.current = newTrack;
+ if (searchTimeoutRef.current) {
+ clearTimeout(searchTimeoutRef.current);
+ }
setSelectedTrack(newTrack);
updateFilters(newTrack, searchQuery);
};
searchTimeoutRef.current = setTimeout(() => {
- updateFilters(selectedTrack, newQuery);
+ updateFilters(selectedTrackRef.current, newQuery);
}, 300);π€ Prompt for AI Agents |
||
| return () => clearTimeout(timeoutId); | ||
| }; | ||
|
|
||
| return ( | ||
|
|
||
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 action item should be expanded to mention clearing the timeout in other event handlers that perform immediate updates to the same state or URL (e.g., track selection) to prevent race conditions where a debounced update overwrites a more recent immediate update.