@@ -87,13 +87,18 @@ export default function ChatInput({
87
87
return input ;
88
88
} ) ;
89
89
const currentInputRef = useRef < string > ( input ) ;
90
+ const saveOnUnmountRef = useRef < boolean > ( true ) ;
90
91
91
92
const isMountedRef = useIsMountedRef ( ) ;
92
- const lastSavedRef = useRef < string | null > ( null ) ;
93
+ const lastSavedRef = useRef < string > ( input ) ;
93
94
const saveChat = useDebouncedCallback (
94
95
( input ) => {
95
- if ( ! isMountedRef . current || syncdb == null || ! saveOnUnmountRef . current )
96
+ if (
97
+ syncdb == null ||
98
+ ( ! isMountedRef . current && ! saveOnUnmountRef . current )
99
+ ) {
96
100
return ;
101
+ }
97
102
onChange ( input ) ;
98
103
lastSavedRef . current = input ;
99
104
// also save to syncdb, so we have undo, etc.
@@ -130,10 +135,9 @@ export default function ChatInput({
130
135
} ,
131
136
) ;
132
137
133
- const saveOnUnmountRef = useRef < boolean > ( true ) ;
134
138
useEffect ( ( ) => {
135
139
return ( ) => {
136
- if ( ! saveOnUnmountRef . current ) {
140
+ if ( ! isMountedRef . current && ! saveOnUnmountRef . current ) {
137
141
return ;
138
142
}
139
143
// save before unmounting. This is very important since if a new reply comes in,
@@ -174,10 +178,10 @@ export default function ChatInput({
174
178
date,
175
179
} ) ;
176
180
const input = x ?. get ( "input" ) ;
177
- if ( input != null && input !== lastSavedRef . current ) {
181
+ if ( input != null && input != lastSavedRef . current ) {
178
182
setInput ( input ) ;
179
183
currentInputRef . current = input ;
180
- lastSavedRef . current = null ;
184
+ lastSavedRef . current = input ;
181
185
}
182
186
} ;
183
187
syncdb . on ( "change" , onSyncdbChange ) ;
@@ -213,6 +217,7 @@ export default function ChatInput({
213
217
// no need to save on unmount, since we are saving
214
218
// the correct state below.
215
219
saveOnUnmountRef . current = false ;
220
+ lastSavedRef . current = input ;
216
221
setInput ( input ) ;
217
222
saveChat ( input ) ;
218
223
saveChat . cancel ( ) ;
0 commit comments