Skip to content

Commit 826a4e6

Browse files
committed
fix chat and markdown input state bug
1 parent d43a725 commit 826a4e6

File tree

4 files changed

+54
-29
lines changed

4 files changed

+54
-29
lines changed

src/packages/frontend/chat/actions.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -287,14 +287,21 @@ export class ChatActions extends Actions<ChatState> {
287287
this.syncdb.set(message);
288288
if (!reply_to) {
289289
this.delete_draft(0);
290+
// NOTE: we also clear search, since it's confusing to send a message and not
291+
// even see it (if it doesn't match search). We do NOT clear the hashtags though,
292+
// since by default the message you are sending has those tags.
293+
// Also, only do this clearing when not replying.
294+
// For replies search find full threads not individual messages.
295+
this.setState({
296+
input: "",
297+
search: "",
298+
});
299+
} else {
300+
// TODO: but until we fix search, do this:
301+
this.setState({
302+
search: "",
303+
});
290304
}
291-
// NOTE: we also clear search, since it's confusing to send a message and not
292-
// even see it (if it doesn't match search). We do NOT clear the hashtags though,
293-
// since by default the message you are sending has those tags.
294-
this.setState({
295-
input: "",
296-
search: "",
297-
});
298305
this.ensureDraftStartsWithHashtags(false);
299306

300307
if (this.store) {
@@ -558,7 +565,7 @@ export class ChatActions extends Actions<ChatState> {
558565
public scrollToBottom(index: number = -1) {
559566
if (this.syncdb == null) return;
560567
// this triggers scroll behavior in the chat-log component.
561-
this.setState({ scrollToBottom: null }); // noop, but necessary to trigger a change
568+
this.setState({ scrollToBottom: null }); // no-op, but necessary to trigger a change
562569
this.setState({ scrollToBottom: index });
563570
}
564571

src/packages/frontend/chat/input.tsx

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import { CSSProperties, useEffect, useMemo, useRef, useState } from "react";
77
import { useDebouncedCallback } from "use-debounce";
8-
98
import {
109
CSS,
1110
redux,
@@ -61,6 +60,10 @@ export default function ChatInput({
6160
submitMentionsRef,
6261
syncdb,
6362
}: Props) {
63+
const onSendRef = useRef<Function>(on_send);
64+
useEffect(() => {
65+
onSendRef.current = on_send;
66+
}, [on_send]);
6467
const { project_id, path, actions } = useFrameContext();
6568
const fontSize = useRedux(["font_size"], project_id, path);
6669
if (font_size == null) {
@@ -213,16 +216,7 @@ export default function ChatInput({
213216
setInput(input);
214217
saveChat(input);
215218
}}
216-
onShiftEnter={(input) => {
217-
// no need to save on unmount, since we are saving
218-
// the correct state below.
219-
saveOnUnmountRef.current = false;
220-
lastSavedRef.current = input;
221-
setInput(input);
222-
saveChat(input);
223-
saveChat.cancel();
224-
on_send(input);
225-
}}
219+
onShiftEnter={on_send}
226220
height={height}
227221
placeholder={getPlaceholder(project_id, placeholder)}
228222
extraHelp={

src/packages/frontend/chat/message.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -628,10 +628,12 @@ export default function Message(props: Readonly<Props>) {
628628
);
629629
}
630630

631-
function sendReply() {
631+
function sendReply(reply?: string) {
632632
if (props.actions == null) return;
633633
setReplying(false);
634-
const reply = replyMentionsRef.current?.() ?? replyMessageRef.current;
634+
if (!reply) {
635+
reply = replyMentionsRef.current?.() ?? replyMessageRef.current;
636+
}
635637
props.actions.send_reply({ message: message.toJS(), reply });
636638
props.actions.scrollToBottom(props.index);
637639
}
@@ -680,7 +682,12 @@ export default function Message(props: Readonly<Props>) {
680682
>
681683
Cancel
682684
</Button>
683-
<Button onClick={sendReply} type="primary">
685+
<Button
686+
onClick={() => {
687+
sendReply();
688+
}}
689+
type="primary"
690+
>
684691
<Icon name="paper-plane" /> Send
685692
</Button>
686693
<LLMCostEstimationChat

src/packages/frontend/editors/markdown-input/multimode.tsx

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,19 @@ export default function MultiMarkdownInput(props: Props) {
189189
path,
190190
} = useFrameContext();
191191

192+
// We use refs for shiftEnter and onChange to be absolutely
193+
// 100% certain that if either of these functions is changed,
194+
// then the new function is used, even if the components
195+
// implementing our markdown editor mess up somehow and hang on.
196+
const onShiftEnterRef = useRef<any>(onShiftEnter);
197+
useEffect(() => {
198+
onShiftEnterRef.current = onShiftEnter;
199+
}, [onShiftEnter]);
200+
const onChangeRef = useRef<any>(onChange);
201+
useEffect(() => {
202+
onChangeRef.current = onChange;
203+
}, [onChange]);
204+
192205
const editBar2 = useRef<JSX.Element | undefined>(undefined);
193206

194207
function getCache() {
@@ -380,7 +393,9 @@ export default function MultiMarkdownInput(props: Props) {
380393
divRef={editorDivRef}
381394
selectionRef={selectionRef}
382395
value={value}
383-
onChange={onChange}
396+
onChange={(value) => {
397+
onChangeRef.current?.(value);
398+
}}
384399
saveDebounceMs={saveDebounceMs}
385400
getValueRef={getValueRef}
386401
project_id={project_id}
@@ -389,7 +404,9 @@ export default function MultiMarkdownInput(props: Props) {
389404
onUploadStart={onUploadStart}
390405
onUploadEnd={onUploadEnd}
391406
enableMentions={enableMentions}
392-
onShiftEnter={onShiftEnter}
407+
onShiftEnter={(value) => {
408+
onShiftEnterRef.current?.(value);
409+
}}
393410
placeholder={placeholder ?? "Type markdown..."}
394411
fontSize={fontSize}
395412
cmOptions={cmOptions}
@@ -400,7 +417,7 @@ export default function MultiMarkdownInput(props: Props) {
400417
extraHelp={extraHelp}
401418
hideHelp={hideHelp}
402419
onBlur={(value) => {
403-
onChange?.(value);
420+
onChangeRef.current?.(value);
404421
if (!ignoreBlur.current) {
405422
onBlur?.();
406423
}
@@ -463,14 +480,14 @@ export default function MultiMarkdownInput(props: Props) {
463480
getValueRef={getValueRef}
464481
actions={{
465482
set_value: (value) => {
466-
onChange?.(value);
483+
onChangeRef.current?.(value);
467484
},
468485
shiftEnter: (value) => {
469-
onChange?.(value);
470-
onShiftEnter?.(value);
486+
onChangeRef.current?.(value);
487+
onShiftEnterRef.current?.(value);
471488
},
472489
altEnter: (value) => {
473-
onChange?.(value);
490+
onChangeRef.current?.(value);
474491
setMode("markdown");
475492
},
476493
set_cursor_locs: onCursors,

0 commit comments

Comments
 (0)