Skip to content

Commit 34acce3

Browse files
committed
fix some chat input bugs
1 parent 55ad3ab commit 34acce3

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/packages/frontend/chat/input.tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,18 @@ export default function ChatInput({
8787
return input;
8888
});
8989
const currentInputRef = useRef<string>(input);
90+
const saveOnUnmountRef = useRef<boolean>(true);
9091

9192
const isMountedRef = useIsMountedRef();
92-
const lastSavedRef = useRef<string | null>(null);
93+
const lastSavedRef = useRef<string>(input);
9394
const saveChat = useDebouncedCallback(
9495
(input) => {
95-
if (!isMountedRef.current || syncdb == null || !saveOnUnmountRef.current)
96+
if (
97+
syncdb == null ||
98+
(!isMountedRef.current && !saveOnUnmountRef.current)
99+
) {
96100
return;
101+
}
97102
onChange(input);
98103
lastSavedRef.current = input;
99104
// also save to syncdb, so we have undo, etc.
@@ -130,10 +135,9 @@ export default function ChatInput({
130135
},
131136
);
132137

133-
const saveOnUnmountRef = useRef<boolean>(true);
134138
useEffect(() => {
135139
return () => {
136-
if (!saveOnUnmountRef.current) {
140+
if (!isMountedRef.current && !saveOnUnmountRef.current) {
137141
return;
138142
}
139143
// save before unmounting. This is very important since if a new reply comes in,
@@ -174,10 +178,10 @@ export default function ChatInput({
174178
date,
175179
});
176180
const input = x?.get("input");
177-
if (input != null && input !== lastSavedRef.current) {
181+
if (input != null && input != lastSavedRef.current) {
178182
setInput(input);
179183
currentInputRef.current = input;
180-
lastSavedRef.current = null;
184+
lastSavedRef.current = input;
181185
}
182186
};
183187
syncdb.on("change", onSyncdbChange);
@@ -213,6 +217,7 @@ export default function ChatInput({
213217
// no need to save on unmount, since we are saving
214218
// the correct state below.
215219
saveOnUnmountRef.current = false;
220+
lastSavedRef.current = input;
216221
setInput(input);
217222
saveChat(input);
218223
saveChat.cancel();

0 commit comments

Comments
 (0)