|
1 |
| -import { useEffect, useMemo, useState } from 'react'; |
| 1 | +import {useEffect, useMemo, useRef, useState} from 'react'; |
2 | 2 | import { CallbackGeneratedChunk, useAppContext } from '../utils/app.context';
|
3 | 3 | import ChatMessage from './ChatMessage';
|
4 | 4 | import { CanvasType, Message, PendingMessage } from '../utils/types';
|
@@ -81,6 +81,33 @@ export default function ChatScreen() {
|
81 | 81 | replaceMessageAndGenerate,
|
82 | 82 | } = useAppContext();
|
83 | 83 | const [inputMsg, setInputMsg] = useState('');
|
| 84 | + const inputRef = useRef<HTMLTextAreaElement>(null); |
| 85 | + |
| 86 | + // Accept setText message from a parent window and set inputMsg and extraContext |
| 87 | + useEffect(() => { |
| 88 | + const handleMessage = (event: MessageEvent) => { |
| 89 | + if (event.data?.command === 'setText') { |
| 90 | + setInputMsg(event.data?.text); |
| 91 | + StorageUtils.setExtraContext(event.data?.context) |
| 92 | + inputRef.current?.focus(); |
| 93 | + } |
| 94 | + }; |
| 95 | + |
| 96 | + window.addEventListener('message', handleMessage); |
| 97 | + return () => window.removeEventListener('message', handleMessage); |
| 98 | + }, []); |
| 99 | + |
| 100 | + // Add a keydown listener that sends the "escapePressed" message to the parent window |
| 101 | + useEffect(() => { |
| 102 | + const handleKeyDown = (event: KeyboardEvent) => { |
| 103 | + if (event.key === 'Escape') { |
| 104 | + window.parent.postMessage({ command: 'escapePressed' }, '*'); |
| 105 | + } |
| 106 | + }; |
| 107 | + |
| 108 | + window.addEventListener('keydown', handleKeyDown); |
| 109 | + return () => window.removeEventListener('keydown', handleKeyDown); |
| 110 | + }, []); |
84 | 111 |
|
85 | 112 | // keep track of leaf node for rendering
|
86 | 113 | const [currNodeId, setCurrNodeId] = useState<number>(-1);
|
@@ -203,6 +230,7 @@ export default function ChatScreen() {
|
203 | 230 | <textarea
|
204 | 231 | className="textarea textarea-bordered w-full"
|
205 | 232 | placeholder="Type a message (Shift+Enter to add a new line)"
|
| 233 | + ref={inputRef} |
206 | 234 | value={inputMsg}
|
207 | 235 | onChange={(e) => setInputMsg(e.target.value)}
|
208 | 236 | onKeyDown={(e) => {
|
|
0 commit comments