From 8b2e1c60883a8d40b84c2bc6f7a56210f50ad09a Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Fri, 19 Sep 2025 13:19:38 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9D=20fix:=20Prevent=20Deletion=20of?= =?UTF-8?q?=20User=20Input=20During=20AI=20Generation=20(#9719)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * bug: updating so text doesn't delete * fix: preserve user input when typing during AI response Previously, form.reset() would clear any text the user started typing while waiting for AI response. Now checks if textarea has new content before resetting, preventing text loss and improving UX. * chore: revert useSubmitMessage changes * fix: reduce debounce time for input handling in auto-save * fix: improve debounce handling for auto-save input to enhance user experience --------- Co-authored-by: Megan Greenberg --- client/src/hooks/Input/useAutoSave.ts | 29 ++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/client/src/hooks/Input/useAutoSave.ts b/client/src/hooks/Input/useAutoSave.ts index 597ab6ea52..c8c6fa8336 100644 --- a/client/src/hooks/Input/useAutoSave.ts +++ b/client/src/hooks/Input/useAutoSave.ts @@ -105,11 +105,33 @@ export const useAutoSave = ({ return; } - const handleInput = debounce((value: string) => setDraft({ id: conversationId, value }), 750); + /** Use shorter debounce for saving text (65ms) to capture rapid typing */ + const handleInputFast = debounce( + (value: string) => setDraft({ id: conversationId, value }), + 65, + ); + + /** Use longer debounce for clearing empty values (850ms) to prevent accidental draft loss */ + const handleInputSlow = debounce( + (value: string) => setDraft({ id: conversationId, value }), + 850, + ); const eventListener = (e: Event) => { const target = e.target as HTMLTextAreaElement; - handleInput(target.value); + const value = target.value; + + /** Cancel any pending operations to avoid conflicts */ + handleInputFast.cancel(); + handleInputSlow.cancel(); + + /** If empty, use long delay to prevent accidental clearing + * Otherwise use short delay to capture rapid typing */ + if (value === '') { + handleInputSlow(value); + } else { + handleInputFast(value); + } }; const textArea = textAreaRef?.current; @@ -121,7 +143,8 @@ export const useAutoSave = ({ if (textArea) { textArea.removeEventListener('input', eventListener); } - handleInput.cancel(); + handleInputFast.cancel(); + handleInputSlow.cancel(); }; }, [conversationId, saveDrafts, textAreaRef]);