Skip to content

Commit 2aa7ef9

Browse files
committed
fix: autosave chat controls on change
1 parent 496b805 commit 2aa7ef9

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

src/lib/components/chat/Chat.svelte

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,24 @@
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) => {
190+
console.error('[saveControlsHandler]', err);
191+
toast.error($i18n.t('Failed to save chat controls'));
192+
});
193+
}, 400);
194+
}
195+
}
178196
179197
$: if (chatIdProp) {
180198
navigateHandler();
@@ -187,6 +205,12 @@
187205
updateLastReadAt($chatId);
188206
}
189207
208+
if (controlsSaveTimer !== null) {
209+
clearTimeout(controlsSaveTimer);
210+
controlsSaveTimer = null;
211+
await saveControlsHandler().catch((err) => console.error('[saveControlsHandler]', err));
212+
}
213+
190214
loading = true;
191215
192216
prompt = '';
@@ -821,6 +845,11 @@
821845
822846
return () => {
823847
try {
848+
if (controlsSaveTimer !== null) {
849+
clearTimeout(controlsSaveTimer);
850+
controlsSaveTimer = null;
851+
saveControlsHandler().catch((err) => console.error('[saveControlsHandler]', err));
852+
}
824853
if (chatIdProp && !$temporaryChatEnabled) {
825854
updateLastReadAt(chatIdProp);
826855
}
@@ -2716,6 +2745,15 @@
27162745
return _chatId;
27172746
};
27182747
2748+
const saveControlsHandler = async () => {
2749+
const _chatId = $chatId;
2750+
if (!_chatId || $temporaryChatEnabled || loading) return;
2751+
await updateChatById(localStorage.token, _chatId, {
2752+
params: params,
2753+
files: chatFiles
2754+
});
2755+
};
2756+
27192757
const saveChatHandler = async (_chatId, history) => {
27202758
if ($chatId == _chatId) {
27212759
if (!$temporaryChatEnabled) {

0 commit comments

Comments
 (0)