Skip to content

Commit 2e3f14b

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

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

src/lib/components/chat/Chat.svelte

Lines changed: 34 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 = '';
@@ -824,6 +848,7 @@
824848
825849
return () => {
826850
try {
851+
clearTimeout(controlsSaveTimer);
827852
if (chatIdProp && !$temporaryChatEnabled) {
828853
updateLastReadAt(chatIdProp);
829854
}
@@ -2723,6 +2748,15 @@
27232748
return _chatId;
27242749
};
27252750
2751+
const saveControlsHandler = async () => {
2752+
const _chatId = $chatId;
2753+
if (!_chatId || $temporaryChatEnabled || loading) return;
2754+
await updateChatById(localStorage.token, _chatId, {
2755+
params: params,
2756+
files: chatFiles
2757+
});
2758+
};
2759+
27262760
const saveChatHandler = async (_chatId, history) => {
27272761
if ($chatId == _chatId) {
27282762
if (!$temporaryChatEnabled) {

0 commit comments

Comments
 (0)