Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Fix the search box debounce and guard precedence

Rafi9160aed9037f
diff --git a/apps/web/ui/shared/search-box.tsx b/apps/web/ui/shared/search-box.tsx
index 2a2fc25e76..05828cb5f5 100644
--- a/apps/web/ui/shared/search-box.tsx
+++ b/apps/web/ui/shared/search-box.tsx
@@ -96,6 +96,8 @@ export const SearchBox = forwardRef(
{showClearButton && value.length > 0 && (
<button
onClick={() => {
+ // Or a pending keystroke lands after this and restores the term.
+ debounced.cancel();
onChange("");
onChangeDebounced?.("");
}}
@@ -127,7 +129,9 @@ export function SearchBoxPersisted({
// Set URL param when debounced value changes
useEffect(() => {
- if (searchParams.get(urlParam) ?? "" !== debouncedValue)
+ // Parens matter: `!==` binds tighter than `??`, so without them this reads
+ // `get() ?? ("" !== debouncedValue)` and never skips.
+ if ((searchParams.get(urlParam) ?? "") !== debouncedValue)
queryParams(
debouncedValue === ""
? { del: [urlParam, "page", ...resetParamsOnChange] }
@@ -142,7 +146,8 @@ export function SearchBoxPersisted({
useEffect(() => {
const search = searchParams.get(urlParam);
// Only update if the value and debouncedValue are synced (the user isn't actively typing)
- if ((search ?? "" !== value) && value === debouncedValue)
+ // Same precedence trap.
+ if ((search ?? "") !== value && value === debouncedValue)
setValue(search ?? "");
}, [searchParams.get(urlParam)]);