Skip to content

Commit d2e4134

Browse files
committed
refactor: Enhance auto-save functionality with debounced restore methods
1 parent 20ecdb4 commit d2e4134

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

client/src/hooks/Input/useAutoSave.ts

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import debounce from 'lodash/debounce';
22
import { SetterOrUpdater, useRecoilValue } from 'recoil';
3-
import { useState, useEffect, useMemo, useCallback } from 'react';
3+
import { useState, useEffect, useMemo, useCallback, useRef } from 'react';
44
import { LocalStorageKeys, TFile } from 'librechat-data-provider';
55
import type { ExtendedFile } from '~/common';
66
import { useChatFormContext } from '~/Providers';
@@ -52,7 +52,7 @@ export const useAutoSave = ({
5252
}
5353
};
5454

55-
const restoreFiles = useCallback(
55+
const restoreFilesRaw = useCallback(
5656
(id: string) => {
5757
const filesDraft = JSON.parse(
5858
(localStorage.getItem(`${LocalStorageKeys.FILES_DRAFT}${id}`) ?? '') || '[]',
@@ -92,14 +92,40 @@ export const useAutoSave = ({
9292
[fileList, setFiles],
9393
);
9494

95-
const restoreText = useCallback(
95+
const restoreTextRaw = useCallback(
9696
(id: string) => {
9797
const savedDraft = (localStorage.getItem(`${LocalStorageKeys.TEXT_DRAFT}${id}`) ?? '') || '';
9898
setValue('text', decodeBase64(savedDraft));
9999
},
100100
[setValue],
101101
);
102102

103+
const restoreFilesRef = useRef(
104+
debounce((id: string) => {
105+
restoreFilesRaw(id);
106+
}, 500),
107+
);
108+
109+
const restoreTextRef = useRef(
110+
debounce((id: string) => {
111+
restoreTextRaw(id);
112+
}, 500),
113+
);
114+
115+
const restoreFiles = useCallback(
116+
(id: string) => {
117+
restoreFilesRef.current(id);
118+
},
119+
[restoreFilesRef],
120+
);
121+
122+
const restoreText = useCallback(
123+
(id: string) => {
124+
restoreTextRef.current(id);
125+
},
126+
[restoreTextRef],
127+
);
128+
103129
const saveText = useCallback(
104130
(id: string) => {
105131
if (!textAreaRef?.current) {
@@ -150,6 +176,16 @@ export const useAutoSave = ({
150176
};
151177
}, [conversationId, saveDrafts, textAreaRef]);
152178

179+
// Clean up debounced functions on unmount
180+
useEffect(() => {
181+
const filesRefCurrent = restoreFilesRef.current;
182+
const textRefCurrent = restoreTextRef.current;
183+
return () => {
184+
filesRefCurrent.cancel();
185+
textRefCurrent.cancel();
186+
};
187+
}, []);
188+
153189
useEffect(() => {
154190
// This useEffect is responsible for saving the current conversation's draft and
155191
// restoring the new conversation's draft when switching between conversations.
@@ -171,6 +207,7 @@ export const useAutoSave = ({
171207
saveText(currentConversationId);
172208
}
173209

210+
// Call the debounced restore functions
174211
restoreText(conversationId);
175212
restoreFiles(conversationId);
176213
} catch (e) {

0 commit comments

Comments
 (0)