Debounce vendor search query with usehooks-ts

Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
Émile Ré
2026-05-11 16:26:27 +04:00
parent bf8c622bcd
commit 6c2037e013

View File

@@ -22,9 +22,10 @@ import {
IconPlusLarge,
useDialogRef,
} from "@probo/ui";
import { type ReactNode, Suspense, useState } from "react";
import { type ReactNode, Suspense, useEffect, useRef, useState, useCallback } from "react";
import { useQueryLoader } from "react-relay";
import { graphql, readInlineData } from "relay-runtime";
import { useDebounceCallback } from "usehooks-ts";
import type { CommonThirdPartyComboboxQuery } from "#/__generated__/core/CommonThirdPartyComboboxQuery.graphql";
import type { CreateVendorDialog_commonThirdParty$key } from "#/__generated__/core/CreateVendorDialog_commonThirdParty.graphql";
@@ -115,10 +116,21 @@ export function CreateVendorDialog({
});
};
const debouncedLoadQuery = useDebounceCallback(
useCallback(
(name: string) => {
loadQuery({ name });
},
[loadQuery],
),
500,
);
const handleSearch = (name: string) => {
setSearchQuery(name);
if (name.trim().length >= 2) {
loadQuery({ name: name.trim() });
const trimmed = name.trim();
if (trimmed.length >= 2) {
debouncedLoadQuery(trimmed);
}
};