Skip to content

Commit b2d2f9a

Browse files
committed
fix: autosave chat controls on change
1 parent 6368cae commit b2d2f9a

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

src/lib/components/chat/Chat.svelte

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,21 @@
175175
let chatFiles = [];
176176
let files = [];
177177
let params = {};
178+
let oldParams = {};
179+
let oldChatFiles = [];
180+
let controlsSaveTimer: ReturnType<typeof setTimeout> | null = null;
181+
182+
$: if (!equal(params, oldParams) || !equal(chatFiles, oldChatFiles)) {
183+
oldParams = structuredClone(params);
184+
oldChatFiles = structuredClone(chatFiles);
185+
if (!loading && !$temporaryChatEnabled && $chatId) {
186+
clearTimeout(controlsSaveTimer);
187+
controlsSaveTimer = setTimeout(() => {
188+
controlsSaveTimer = null;
189+
saveControlsHandler().catch((err) => console.error('[saveControlsHandler]', err));
190+
}, 400);
191+
}
192+
}
178193
179194
$: if (chatIdProp) {
180195
navigateHandler();
@@ -187,6 +202,9 @@
187202
updateLastReadAt($chatId);
188203
}
189204
205+
clearTimeout(controlsSaveTimer);
206+
controlsSaveTimer = null;
207+
190208
loading = true;
191209
192210
prompt = '';
@@ -824,6 +842,7 @@
824842
825843
return () => {
826844
try {
845+
clearTimeout(controlsSaveTimer);
827846
if (chatIdProp && !$temporaryChatEnabled) {
828847
updateLastReadAt(chatIdProp);
829848
}
@@ -2723,6 +2742,15 @@
27232742
return _chatId;
27242743
};
27252744
2745+
const saveControlsHandler = async () => {
2746+
const _chatId = $chatId;
2747+
if (!_chatId || $temporaryChatEnabled || loading) return;
2748+
await updateChatById(localStorage.token, _chatId, {
2749+
params: params,
2750+
files: chatFiles
2751+
});
2752+
};
2753+
27262754
const saveChatHandler = async (_chatId, history) => {
27272755
if ($chatId == _chatId) {
27282756
if (!$temporaryChatEnabled) {

0 commit comments

Comments
 (0)