Skip to content

Commit

Permalink
Increase search debounce delay and move debouncing logic (#174)
Browse files Browse the repository at this point in the history
Signed-off-by: Damian Stasik <[email protected]>
  • Loading branch information
damianstasik authored Sep 10, 2024
1 parent e9beda6 commit bf96910
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
7 changes: 4 additions & 3 deletions frontend/src/components/Search/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ComboboxOptions,
} from "@headlessui/react";
import { useQuery } from "@tanstack/react-query";
import { useDeferredValue, useEffect, useMemo, useRef, useState } from "react";
import { useEffect, useMemo, useRef, useState } from "react";
import { useNavigate } from "react-router-dom";
import { getSearchQuery } from "../../q";
import { search } from "../../icons/search";
Expand All @@ -17,6 +17,7 @@ import { SearchProviderResult } from "./ProviderResult";
import { SearchOtherResult } from "./OtherResult";
import { definitions } from "@/api";
import clsx from "clsx";
import { useDebouncedValue } from "@/hooks/useDebouncedValue";

function getSearchResultType(value: string) {
switch (value) {
Expand Down Expand Up @@ -117,8 +118,8 @@ export function Search({
placeholder = "Search resources (Press / to focus)",
}: SearchProps) {
const [query, setQuery] = useState("");
const deferredQuery = useDeferredValue(query);
const { data, isLoading } = useQuery(getSearchQuery(deferredQuery));
const debouncedQuery = useDebouncedValue(query, 250);
const { data, isLoading } = useQuery(getSearchQuery(debouncedQuery));

const inputRef = useRef<HTMLInputElement | null>(null);
const navigate = useNavigate();
Expand Down
17 changes: 17 additions & 0 deletions frontend/src/hooks/useDebouncedValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useEffect, useState } from "react";

export function useDebouncedValue(value: string, delay: number) {
const [debouncedValue, setDebouncedValue] = useState(value);

useEffect(() => {
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);

return () => {
clearTimeout(handler);
};
}, [value, delay]);

return debouncedValue;
}
10 changes: 1 addition & 9 deletions frontend/src/q.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,7 @@ export const getSearchQuery = (query: string) =>
queryFn:
query.length > 0
? async ({ signal }) => {
// This is the lazy man's debounce, by waiting 100ms before making the request
// and utilizing the signal to cancel the request if the query changes
// we get a debounce effect
await (() => new Promise((resolve) => setTimeout(resolve, 100)))();
if (signal.aborted) {
return;
}

return await api(`search?q=${encodeURIComponent(query)}`, {
return api(`search?q=${encodeURIComponent(query)}`, {
signal,
}).json<definitions["SearchResultItem"][]>();
}
Expand Down

0 comments on commit bf96910

Please sign in to comment.