refactor: Enhance auto-save functionality with debounced restore methods

This commit is contained in:
Danny Avila 2025-04-03 16:11:24 -04:00
parent 20ecdb4881
commit d2e4134d1f
No known key found for this signature in database
GPG key ID: BF31EEB2C5CA0956

View file

@ -1,6 +1,6 @@
import debounce from 'lodash/debounce';
import { SetterOrUpdater, useRecoilValue } from 'recoil';
import { useState, useEffect, useMemo, useCallback } from 'react';
import { useState, useEffect, useMemo, useCallback, useRef } from 'react';
import { LocalStorageKeys, TFile } from 'librechat-data-provider';
import type { ExtendedFile } from '~/common';
import { useChatFormContext } from '~/Providers';
@ -52,7 +52,7 @@ export const useAutoSave = ({
}
};
const restoreFiles = useCallback(
const restoreFilesRaw = useCallback(
(id: string) => {
const filesDraft = JSON.parse(
(localStorage.getItem(`${LocalStorageKeys.FILES_DRAFT}${id}`) ?? '') || '[]',
@ -92,7 +92,7 @@ export const useAutoSave = ({
[fileList, setFiles],
);
const restoreText = useCallback(
const restoreTextRaw = useCallback(
(id: string) => {
const savedDraft = (localStorage.getItem(`${LocalStorageKeys.TEXT_DRAFT}${id}`) ?? '') || '';
setValue('text', decodeBase64(savedDraft));
@ -100,6 +100,32 @@ export const useAutoSave = ({
[setValue],
);
const restoreFilesRef = useRef(
debounce((id: string) => {
restoreFilesRaw(id);
}, 500),
);
const restoreTextRef = useRef(
debounce((id: string) => {
restoreTextRaw(id);
}, 500),
);
const restoreFiles = useCallback(
(id: string) => {
restoreFilesRef.current(id);
},
[restoreFilesRef],
);
const restoreText = useCallback(
(id: string) => {
restoreTextRef.current(id);
},
[restoreTextRef],
);
const saveText = useCallback(
(id: string) => {
if (!textAreaRef?.current) {
@ -150,6 +176,16 @@ export const useAutoSave = ({
};
}, [conversationId, saveDrafts, textAreaRef]);
// Clean up debounced functions on unmount
useEffect(() => {
const filesRefCurrent = restoreFilesRef.current;
const textRefCurrent = restoreTextRef.current;
return () => {
filesRefCurrent.cancel();
textRefCurrent.cancel();
};
}, []);
useEffect(() => {
// This useEffect is responsible for saving the current conversation's draft and
// restoring the new conversation's draft when switching between conversations.
@ -171,6 +207,7 @@ export const useAutoSave = ({
saveText(currentConversationId);
}
// Call the debounced restore functions
restoreText(conversationId);
restoreFiles(conversationId);
} catch (e) {