-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Webui - change setText command from parent window to also send the message. #13309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
e2ec22f
67687b7
6e29fdc
d9e2249
7db7a63
7c3d763
d361212
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,4 +1,4 @@ | ||||||||||||||||||||
| import { useEffect, useState } from 'react'; | ||||||||||||||||||||
| import { useEffect, useRef, useState } from 'react'; | ||||||||||||||||||||
| import { MessageExtraContext } from './types'; | ||||||||||||||||||||
| import { ChatTextareaApi } from '../components/useChatTextarea.ts'; | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
@@ -15,14 +15,25 @@ interface SetTextEvData { | |||||||||||||||||||
| * window.postMessage({ command: 'setText', text: 'Spot the syntax error', context: 'def test()\n return 123' }, '*'); | ||||||||||||||||||||
| */ | ||||||||||||||||||||
|
|
||||||||||||||||||||
| export const useVSCodeContext = (textarea: ChatTextareaApi) => { | ||||||||||||||||||||
| const [extraContext, setExtraContext] = useState<MessageExtraContext | null>( | ||||||||||||||||||||
| export const useVSCodeContext = ( | ||||||||||||||||||||
| textarea: ChatTextareaApi, | ||||||||||||||||||||
| onSend?: () => void | ||||||||||||||||||||
| ) => { | ||||||||||||||||||||
| const [extraContext, _setExtraContext] = useState<MessageExtraContext | null>( | ||||||||||||||||||||
| null | ||||||||||||||||||||
| ); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Use ref to store the latest value | ||||||||||||||||||||
| const extraContextRef = useRef(extraContext); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const setExtraContext = (value: MessageExtraContext | null) => { | ||||||||||||||||||||
| extraContextRef.current = value; | ||||||||||||||||||||
| _setExtraContext(value); | ||||||||||||||||||||
| }; | ||||||||||||||||||||
| // Accept setText message from a parent window and set inputMsg and extraContext | ||||||||||||||||||||
| useEffect(() => { | ||||||||||||||||||||
| const handleMessage = (event: MessageEvent) => { | ||||||||||||||||||||
| if (event.source !== window.parent) return; | ||||||||||||||||||||
| if (event.data?.command === 'setText') { | ||||||||||||||||||||
| const data: SetTextEvData = event.data; | ||||||||||||||||||||
| textarea.setValue(data?.text); | ||||||||||||||||||||
|
|
@@ -33,12 +44,18 @@ export const useVSCodeContext = (textarea: ChatTextareaApi) => { | |||||||||||||||||||
| }); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| textarea.focus(); | ||||||||||||||||||||
| if (onSend && data?.text) { | ||||||||||||||||||||
| // Use setTimeout to ensure state updates are processed | ||||||||||||||||||||
| setTimeout(() => { | ||||||||||||||||||||
| onSend(); | ||||||||||||||||||||
| }, 50); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
||||||||||||||||||||
| textarea.focus(); | |
| if (onSend && data?.text) { | |
| // Use setTimeout to ensure state updates are processed | |
| setTimeout(() => { | |
| onSend(); | |
| }, 50); | |
| } | |
| textarea.focus(); | |
| textarea.onSubmit(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. It is cleaner.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need
useCallbackhere. We can simply updatetextarea.onSubmit = sendNewMessageeach time