Skip to content

Commit cbf8cad

Browse files
authored
webui: Make textarea uncontrolled to eliminate devastating lag
1 parent 1e2f78a commit cbf8cad

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

examples/server/webui/src/components/ChatScreen.tsx

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,15 @@ export default function ChatScreen() {
9999
canvasData,
100100
replaceMessageAndGenerate,
101101
} = useAppContext();
102-
const [inputMsg, setInputMsg] = useState(prefilledMsg.content());
103102
const inputRef = useRef<HTMLTextAreaElement>(null);
104103

105104
const { extraContext, clearExtraContext } = useVSCodeContext(
106105
inputRef,
107-
setInputMsg
106+
(msg) => {
107+
if (inputRef?.current) {
108+
inputRef.current.value = msg;
109+
}
110+
}
108111
);
109112
// TODO: improve this when we have "upload file" feature
110113
const currExtra: Message['extra'] = extraContext ? [extraContext] : undefined;
@@ -135,9 +138,14 @@ export default function ChatScreen() {
135138
};
136139

137140
const sendNewMessage = async () => {
138-
if (inputMsg.trim().length === 0 || isGenerating(currConvId ?? '')) return;
139-
const lastInpMsg = inputMsg;
140-
setInputMsg('');
141+
if (
142+
!inputRef.current ||
143+
inputRef.current.value.trim().length === 0 ||
144+
isGenerating(currConvId ?? '')
145+
)
146+
return;
147+
const inputValue = inputRef.current.value;
148+
inputRef.current.value = '';
141149
scrollToBottom(false);
142150
setCurrNodeId(-1);
143151
// get the last message node
@@ -146,13 +154,13 @@ export default function ChatScreen() {
146154
!(await sendMessage(
147155
currConvId,
148156
lastMsgNodeId,
149-
inputMsg,
157+
inputValue,
150158
currExtra,
151159
onChunk
152160
))
153161
) {
154162
// restore the input message if failed
155-
setInputMsg(lastInpMsg);
163+
inputRef.current.value = inputValue;
156164
}
157165
// OK
158166
clearExtraContext();
@@ -259,8 +267,7 @@ export default function ChatScreen() {
259267
className="textarea textarea-bordered w-full"
260268
placeholder="Type a message (Shift+Enter to add a new line)"
261269
ref={inputRef}
262-
value={inputMsg}
263-
onChange={(e) => setInputMsg(e.target.value)}
270+
defaultValue={prefilledMsg.content()}
264271
onKeyDown={(e) => {
265272
if (e.nativeEvent.isComposing || e.keyCode === 229) return;
266273
if (e.key === 'Enter' && e.shiftKey) return;
@@ -280,11 +287,7 @@ export default function ChatScreen() {
280287
Stop
281288
</button>
282289
) : (
283-
<button
284-
className="btn btn-primary ml-2"
285-
onClick={sendNewMessage}
286-
disabled={inputMsg.trim().length === 0}
287-
>
290+
<button className="btn btn-primary ml-2" onClick={sendNewMessage}>
288291
Send
289292
</button>
290293
)}

0 commit comments

Comments
 (0)