From 72f830e474b03d16b55b5d9ef0dc2388b5fa548d Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Mon, 15 Jan 2024 15:49:44 +0100 Subject: [PATCH] #730 - Verify if update is done after debounce update --- .../components/Fields/TextField.tsx | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/panelWebView/components/Fields/TextField.tsx b/src/panelWebView/components/Fields/TextField.tsx index 933599f0..f69400d5 100644 --- a/src/panelWebView/components/Fields/TextField.tsx +++ b/src/panelWebView/components/Fields/TextField.tsx @@ -12,6 +12,8 @@ import * as l10n from '@vscode/l10n'; import { LocalizationKey } from '../../../localization'; import { useDebounce } from '../../../hooks/useDebounce'; +const DEBOUNCE_TIME = 300; + export interface ITextFieldProps extends BaseFieldProps { singleLine: boolean | undefined; wysiwyg: boolean | undefined; @@ -38,12 +40,14 @@ export const TextField: React.FunctionComponent = ({ required }: React.PropsWithChildren) => { const [, setRequiredFields] = useRecoilState(RequiredFieldsAtom); - const [text, setText] = React.useState(value); + const [text, setText] = React.useState(undefined); const [loading, setLoading] = React.useState(false); - const debouncedValue = useDebounce(text, 500); + const [lastUpdated, setLastUpdated] = React.useState(0); + const debouncedText = useDebounce(text, DEBOUNCE_TIME); const onTextChange = (txtValue: string) => { setText(txtValue); + setLastUpdated(Date.now()); }; let isValid = true; @@ -117,16 +121,16 @@ export const TextField: React.FunctionComponent = ({ }, [settings?.aiEnabled, name]); useEffect(() => { - if (text !== value) { - setText(value); + if (text !== value && (Date.now() - DEBOUNCE_TIME) > lastUpdated) { + setText(value || null); } }, [value]); useEffect(() => { - if (debouncedValue !== value) { - onChange(debouncedValue || ''); + if (debouncedText !== undefined) { + onChange(debouncedText || ''); } - }, [debouncedValue]); + }, [debouncedText]); return (