Skip to content

Commit 52c31a2

Browse files
committed
better approach
1 parent e3bae75 commit 52c31a2

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed
16 Bytes
Binary file not shown.

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,19 @@ export interface MessageDisplay {
2222
* If the current URL contains "?m=...", prefill the message input with the value.
2323
* If the current URL contains "?p=...", prefill and SEND the message.
2424
*/
25-
let prefilledMessage = '';
26-
let sendPrefilledMessage = false;
27-
{
28-
const url = new URL(window.location.href);
29-
prefilledMessage =
30-
url.searchParams.get('m') ?? url.searchParams.get('p') ?? '';
31-
sendPrefilledMessage = url.searchParams.has('p');
32-
cleanCurrentUrl(['m', 'p']);
33-
}
25+
const prefilledMsg = {
26+
content() {
27+
const url = new URL(window.location.href);
28+
return url.searchParams.get('m') ?? url.searchParams.get('q') ?? '';
29+
},
30+
shouldSend() {
31+
const url = new URL(window.location.href);
32+
return url.searchParams.has('q');
33+
},
34+
clear() {
35+
cleanCurrentUrl(['m', 'q']);
36+
},
37+
};
3438

3539
function getListMessageDisplay(
3640
msgs: Readonly<Message[]>,
@@ -95,7 +99,7 @@ export default function ChatScreen() {
9599
canvasData,
96100
replaceMessageAndGenerate,
97101
} = useAppContext();
98-
const [inputMsg, setInputMsg] = useState(prefilledMessage);
102+
const [inputMsg, setInputMsg] = useState(prefilledMsg.content());
99103
const inputRef = useRef<HTMLTextAreaElement>(null);
100104

101105
const { extraContext, clearExtraContext } = useVSCodeContext(
@@ -187,10 +191,8 @@ export default function ChatScreen() {
187191
const hasCanvas = !!canvasData;
188192

189193
useEffect(() => {
190-
prefilledMessage = ''; // clear the prefilled message as it has been set to inputMsg
191-
if (sendPrefilledMessage) {
194+
if (prefilledMsg.shouldSend()) {
192195
// send the prefilled message if needed
193-
sendPrefilledMessage = false;
194196
sendNewMessage();
195197
} else {
196198
// otherwise, focus on the input and move the cursor to the end
@@ -199,6 +201,7 @@ export default function ChatScreen() {
199201
inputRef.current.selectionStart = inputRef.current.value.length;
200202
}
201203
}
204+
prefilledMsg.clear();
202205
// no need to keep track of sendNewMessage
203206
// eslint-disable-next-line react-hooks/exhaustive-deps
204207
}, [inputRef]);

0 commit comments

Comments
 (0)