Skip to content

Commit 3b3507f

Browse files
committed
try to fix a chat bug that was reported that I can't reproduce at all
- reported bug: responding to chat and hitting shift+enter causes the message to send but a new draft to get created with the same content
1 parent c9ee8f5 commit 3b3507f

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/packages/frontend/chat/input.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ export default function ChatInput({
9292
const lastSavedRef = useRef<string | null>(null);
9393
const saveChat = useDebouncedCallback(
9494
(input) => {
95-
if (!isMountedRef.current || syncdb == null) return;
95+
if (!isMountedRef.current || syncdb == null || !saveOnUnmountRef.current)
96+
return;
9697
onChange(input);
9798
lastSavedRef.current = input;
9899
// also save to syncdb, so we have undo, etc.
@@ -129,8 +130,12 @@ export default function ChatInput({
129130
},
130131
);
131132

133+
const saveOnUnmountRef = useRef<boolean>(true);
132134
useEffect(() => {
133135
return () => {
136+
if (!saveOnUnmountRef.current) {
137+
return;
138+
}
134139
// save before unmounting. This is very important since if a new reply comes in,
135140
// then the input component gets unmounted, then remounted BELOW the reply.
136141
// Note: it is still slightly annoying, due to loss of focus... however, data
@@ -148,7 +153,6 @@ export default function ChatInput({
148153
) {
149154
return;
150155
}
151-
152156
syncdb.set({
153157
event: "draft",
154158
sender_id,
@@ -206,6 +210,9 @@ export default function ChatInput({
206210
saveChat(input);
207211
}}
208212
onShiftEnter={(input) => {
213+
// no need to save on unmount, since we are saving
214+
// the correct state below.
215+
saveOnUnmountRef.current = false;
209216
setInput(input);
210217
saveChat(input);
211218
saveChat.cancel();

src/packages/frontend/chat/message.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -610,22 +610,19 @@ export default function Message(props: Readonly<Props>) {
610610
edited_message_ref.current = value;
611611
}}
612612
/>
613-
<div style={{ marginTop: "10px" }}>
613+
<div style={{ marginTop: "10px", display: "flex" }}>
614614
<Button
615-
type="primary"
616615
style={{ marginRight: "5px" }}
617-
onClick={saveEditedMessage}
618-
>
619-
<Icon name="save" /> Save Edited Message
620-
</Button>
621-
<Button
622616
onClick={() => {
623617
props.actions?.set_editing(message, false);
624618
props.actions?.delete_draft(date);
625619
}}
626620
>
627621
Cancel
628622
</Button>
623+
<Button type="primary" onClick={saveEditedMessage}>
624+
<Icon name="save" /> Save Edited Message
625+
</Button>
629626
</div>
630627
</div>
631628
);

0 commit comments

Comments
 (0)