@@ -92,7 +92,8 @@ export default function ChatInput({
92
92
const lastSavedRef = useRef < string | null > ( null ) ;
93
93
const saveChat = useDebouncedCallback (
94
94
( input ) => {
95
- if ( ! isMountedRef . current || syncdb == null ) return ;
95
+ if ( ! isMountedRef . current || syncdb == null || ! saveOnUnmountRef . current )
96
+ return ;
96
97
onChange ( input ) ;
97
98
lastSavedRef . current = input ;
98
99
// also save to syncdb, so we have undo, etc.
@@ -129,8 +130,12 @@ export default function ChatInput({
129
130
} ,
130
131
) ;
131
132
133
+ const saveOnUnmountRef = useRef < boolean > ( true ) ;
132
134
useEffect ( ( ) => {
133
135
return ( ) => {
136
+ if ( ! saveOnUnmountRef . current ) {
137
+ return ;
138
+ }
134
139
// save before unmounting. This is very important since if a new reply comes in,
135
140
// then the input component gets unmounted, then remounted BELOW the reply.
136
141
// Note: it is still slightly annoying, due to loss of focus... however, data
@@ -148,7 +153,6 @@ export default function ChatInput({
148
153
) {
149
154
return ;
150
155
}
151
-
152
156
syncdb . set ( {
153
157
event : "draft" ,
154
158
sender_id,
@@ -206,6 +210,9 @@ export default function ChatInput({
206
210
saveChat ( input ) ;
207
211
} }
208
212
onShiftEnter = { ( input ) => {
213
+ // no need to save on unmount, since we are saving
214
+ // the correct state below.
215
+ saveOnUnmountRef . current = false ;
209
216
setInput ( input ) ;
210
217
saveChat ( input ) ;
211
218
saveChat . cancel ( ) ;
0 commit comments