Skip to content

Commit 595beaa

Browse files
committed
fix(webview-ui): preserve chat draft when queued messages auto-send
1 parent f19ed7c commit 595beaa

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

webview-ui/src/components/chat/ChatView.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
176176
const textAreaRef = useRef<HTMLTextAreaElement>(null)
177177
const [sendingDisabled, setSendingDisabled] = useState(false)
178178
const [selectedImages, setSelectedImages] = useState<string[]>([])
179+
// Keep a stable ref for selected images to preserve user draft while queue drains
180+
const selectedImagesRef = useRef(selectedImages)
179181

180182
// we need to hold on to the ask because useEffect > lastMessage will always let us know when an ask comes in and handle it, but by the time handleMessage is called, the last message might not be the ask anymore (it could be a say that followed)
181183
const [clineAsk, setClineAsk] = useState<ClineAsk | undefined>(undefined)
@@ -224,6 +226,11 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
224226
inputValueRef.current = inputValue
225227
}, [inputValue])
226228

229+
// Keep selectedImagesRef in sync with selectedImages state
230+
useEffect(() => {
231+
selectedImagesRef.current = selectedImages
232+
}, [selectedImages])
233+
227234
useEffect(() => {
228235
isMountedRef.current = true
229236
return () => {
@@ -816,9 +823,16 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
816823
case "newChat":
817824
handleChatReset()
818825
break
819-
case "sendMessage":
826+
case "sendMessage": {
827+
// Preserve user's in-progress draft while a queued message is auto-sent
828+
const preservedInput = inputValueRef.current
829+
const preservedImages = selectedImagesRef.current
820830
handleSendMessage(message.text ?? "", message.images ?? [])
831+
// Restore the draft after programmatic send to avoid clearing while typing
832+
setInputValue(preservedInput)
833+
setSelectedImages(preservedImages)
821834
break
835+
}
822836
case "setChatBoxMessage":
823837
handleSetChatBoxMessage(message.text ?? "", message.images ?? [])
824838
break

0 commit comments

Comments
 (0)