Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ exports[`Section1 Component matches snapshot 1`] = `
alt=""
src="/assets/img/icons/calender1.svg"
/>
15th, 16th, & 17th January “2025”
15th, 16th, & 17th January “2025”
</a>
</li>
</ul>
Expand Down
19 changes: 16 additions & 3 deletions components/layout/TalksFilterBar.tsx
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, useRef, useState, useTransition } from "react";
import { motion } from "framer-motion";

interface TalksFilterBarProps {
Expand All @@ -21,13 +21,22 @@ 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);

// Update state when URL changes
useEffect(() => {
setSelectedTrack(searchParams.get("track") || "");
setSearchQuery(searchParams.get("q") || "");
}, [searchParams]);

useEffect(() => {
return () => {
if (searchTimeoutRef.current) {
clearTimeout(searchTimeoutRef.current);
}
};
}, []);

const updateFilters = useCallback(
(track: string, query: string) => {
const params = new URLSearchParams(searchParams.toString());
Expand Down Expand Up @@ -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);
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.

high

There's a potential race condition here. The setTimeout callback creates a closure over the selectedTrack value from the render it was created in. If the user changes the selected track by clicking a track button before the 300ms timeout completes, this callback will execute with a stale selectedTrack value, incorrectly reverting the user's track selection in the URL.

To fix this, you can use a useRef to ensure you always have access to the latest selectedTrack value inside the timeout callback.

  1. First, create a ref and an effect to keep it synchronized with the selectedTrack state. You can add this after your state declarations (around line 24):
const selectedTrackRef = useRef(selectedTrack);
useEffect(() => {
  selectedTrackRef.current = selectedTrack;
}, [selectedTrack]);
  1. Then, use the ref's current value when calling updateFilters inside the setTimeout:
Suggested change
updateFilters(selectedTrack, newQuery);
updateFilters(selectedTrackRef.current, newQuery);

}, 300);
return () => clearTimeout(timeoutId);
};

return (
Expand Down
Loading