feat: Cache file content

This commit is contained in:
Felipe Hernandez 2025-04-18 11:38:15 +02:00
parent 88f97372d9
commit 14fa7d16f9

View File

@ -86,10 +86,13 @@
let selectedFileId = null; let selectedFileId = null;
let selectedFileContent = ''; let selectedFileContent = '';
// Add cache object
let fileContentCache = new Map();
$: if (selectedFileId) { $: if (selectedFileId) {
const file = (knowledge?.files ?? []).find((file) => file.id === selectedFileId); const file = (knowledge?.files ?? []).find((file) => file.id === selectedFileId);
if (file) { if (file) {
handleFileClick(file); fileSelectHandler(file);
} else { } else {
selectedFile = null; selectedFile = null;
} }
@ -394,7 +397,10 @@
const updateFileContentHandler = async () => { const updateFileContentHandler = async () => {
const fileId = selectedFile.id; const fileId = selectedFile.id;
const content = selectedFileContent const content = selectedFileContent;
// Clear the cache for this file since we're updating it
fileContentCache.delete(fileId);
const res = updateFileDataContentById(localStorage.token, fileId, content).catch((e) => { const res = updateFileDataContentById(localStorage.token, fileId, content).catch((e) => {
toast.error(`${e}`); toast.error(`${e}`);
@ -450,12 +456,21 @@
} }
}; };
const handleFileClick = async (file) => { const fileSelectHandler = async (file) => {
try { try {
selectedFile = file; selectedFile = file;
// Check cache first
if (fileContentCache.has(file.id)) {
selectedFileContent = fileContentCache.get(file.id);
return;
}
const response = await getFileById(localStorage.token, file.id); const response = await getFileById(localStorage.token, file.id);
if (response) { if (response) {
selectedFileContent = response.data.content; selectedFileContent = response.data.content;
// Cache the content
fileContentCache.set(file.id, response.data.content);
} else { } else {
toast.error($i18n.t('No content found in file.')); toast.error($i18n.t('No content found in file.'));
} }