mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-18 09:20:15 +01:00
refactor: Enhance auto-save functionality with debounced restore methods
This commit is contained in:
parent
20ecdb4881
commit
d2e4134d1f
1 changed files with 40 additions and 3 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
import debounce from 'lodash/debounce';
|
import debounce from 'lodash/debounce';
|
||||||
import { SetterOrUpdater, useRecoilValue } from 'recoil';
|
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 { LocalStorageKeys, TFile } from 'librechat-data-provider';
|
||||||
import type { ExtendedFile } from '~/common';
|
import type { ExtendedFile } from '~/common';
|
||||||
import { useChatFormContext } from '~/Providers';
|
import { useChatFormContext } from '~/Providers';
|
||||||
|
|
@ -52,7 +52,7 @@ export const useAutoSave = ({
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const restoreFiles = useCallback(
|
const restoreFilesRaw = useCallback(
|
||||||
(id: string) => {
|
(id: string) => {
|
||||||
const filesDraft = JSON.parse(
|
const filesDraft = JSON.parse(
|
||||||
(localStorage.getItem(`${LocalStorageKeys.FILES_DRAFT}${id}`) ?? '') || '[]',
|
(localStorage.getItem(`${LocalStorageKeys.FILES_DRAFT}${id}`) ?? '') || '[]',
|
||||||
|
|
@ -92,7 +92,7 @@ export const useAutoSave = ({
|
||||||
[fileList, setFiles],
|
[fileList, setFiles],
|
||||||
);
|
);
|
||||||
|
|
||||||
const restoreText = useCallback(
|
const restoreTextRaw = useCallback(
|
||||||
(id: string) => {
|
(id: string) => {
|
||||||
const savedDraft = (localStorage.getItem(`${LocalStorageKeys.TEXT_DRAFT}${id}`) ?? '') || '';
|
const savedDraft = (localStorage.getItem(`${LocalStorageKeys.TEXT_DRAFT}${id}`) ?? '') || '';
|
||||||
setValue('text', decodeBase64(savedDraft));
|
setValue('text', decodeBase64(savedDraft));
|
||||||
|
|
@ -100,6 +100,32 @@ export const useAutoSave = ({
|
||||||
[setValue],
|
[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(
|
const saveText = useCallback(
|
||||||
(id: string) => {
|
(id: string) => {
|
||||||
if (!textAreaRef?.current) {
|
if (!textAreaRef?.current) {
|
||||||
|
|
@ -150,6 +176,16 @@ export const useAutoSave = ({
|
||||||
};
|
};
|
||||||
}, [conversationId, saveDrafts, textAreaRef]);
|
}, [conversationId, saveDrafts, textAreaRef]);
|
||||||
|
|
||||||
|
// Clean up debounced functions on unmount
|
||||||
|
useEffect(() => {
|
||||||
|
const filesRefCurrent = restoreFilesRef.current;
|
||||||
|
const textRefCurrent = restoreTextRef.current;
|
||||||
|
return () => {
|
||||||
|
filesRefCurrent.cancel();
|
||||||
|
textRefCurrent.cancel();
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// This useEffect is responsible for saving the current conversation's draft and
|
// This useEffect is responsible for saving the current conversation's draft and
|
||||||
// restoring the new conversation's draft when switching between conversations.
|
// restoring the new conversation's draft when switching between conversations.
|
||||||
|
|
@ -171,6 +207,7 @@ export const useAutoSave = ({
|
||||||
saveText(currentConversationId);
|
saveText(currentConversationId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Call the debounced restore functions
|
||||||
restoreText(conversationId);
|
restoreText(conversationId);
|
||||||
restoreFiles(conversationId);
|
restoreFiles(conversationId);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue