Skip to content

Commit efa139f

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

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 = '';
@@ -824,6 +848,11 @@
824848
825849
return () => {
826850
try {
851+
if (controlsSaveTimer !== null) {
852+
clearTimeout(controlsSaveTimer);
853+
controlsSaveTimer = null;
854+
saveControlsHandler().catch((err) => console.error('[saveControlsHandler]', err));
855+
}
827856
if (chatIdProp && !$temporaryChatEnabled) {
828857
updateLastReadAt(chatIdProp);
829858
}
@@ -2723,6 +2752,15 @@
27232752
return _chatId;
27242753
};
27252754
2755+
const saveControlsHandler = async () => {
2756+
const _chatId = $chatId;
2757+
if (!_chatId || $temporaryChatEnabled || loading) return;
2758+
await updateChatById(localStorage.token, _chatId, {
2759+
params: params,
2760+
files: chatFiles
2761+
});
2762+
};
2763+
27262764
const saveChatHandler = async (_chatId, history) => {
27272765
if ($chatId == _chatId) {
27282766
if (!$temporaryChatEnabled) {

0 commit comments

Comments
 (0)