feat: chat history incognito mode

This commit is contained in:
Timothy J. Baek 2024-01-06 02:33:00 -08:00
parent 77a4c4ce3e
commit 13d9e6675e
4 changed files with 332 additions and 262 deletions

View file

@ -238,6 +238,11 @@
const toggleSaveChatHistory = async () => { const toggleSaveChatHistory = async () => {
saveChatHistory = !saveChatHistory; saveChatHistory = !saveChatHistory;
console.log(saveChatHistory);
if (saveChatHistory === false) {
await goto('/');
}
saveSettings({ saveChatHistory: saveChatHistory }); saveSettings({ saveChatHistory: saveChatHistory });
}; };

View file

@ -6,7 +6,7 @@
import { goto, invalidateAll } from '$app/navigation'; import { goto, invalidateAll } from '$app/navigation';
import { page } from '$app/stores'; import { page } from '$app/stores';
import { user, chats, showSettings, chatId } from '$lib/stores'; import { user, chats, settings, showSettings, chatId } from '$lib/stores';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import { deleteChatById, getChatList, updateChatById } from '$lib/apis/chats'; import { deleteChatById, getChatList, updateChatById } from '$lib/apis/chats';
@ -49,6 +49,12 @@
await deleteChatById(localStorage.token, id); await deleteChatById(localStorage.token, id);
await chats.set(await getChatList(localStorage.token)); await chats.set(await getChatList(localStorage.token));
}; };
const saveSettings = async (updated) => {
await settings.set({ ...$settings, ...updated });
localStorage.setItem('settings', JSON.stringify($settings));
location.href = '/';
};
</script> </script>
<div <div
@ -159,6 +165,48 @@
</div> </div>
{/if} {/if}
<div class="relative flex flex-col flex-1 overflow-y-auto">
{#if !($settings.saveChatHistory ?? true)}
<div class="absolute z-40 w-full h-full bg-black/90 flex justify-center">
<div class=" text-left px-5 py-2">
<div class=" font-medium">Chat History is off for this browser.</div>
<div class="text-xs mt-2">
When history is turned off, new chats on this browser won't appear in your history on
any of your devices. <span class=" font-semibold"
>This setting does not sync across browsers or devices.</span
>
</div>
<div class="mt-3">
<button
class="flex justify-center items-center space-x-1.5 px-3 py-2.5 rounded-lg text-xs bg-gray-200 hover:bg-gray-300 transition text-gray-800 font-medium w-full"
type="button"
on:click={() => {
saveSettings({
saveChatHistory: true
});
}}
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 16 16"
fill="currentColor"
class="w-3 h-3"
>
<path
fill-rule="evenodd"
d="M8 1a.75.75 0 0 1 .75.75v6.5a.75.75 0 0 1-1.5 0v-6.5A.75.75 0 0 1 8 1ZM4.11 3.05a.75.75 0 0 1 0 1.06 5.5 5.5 0 1 0 7.78 0 .75.75 0 0 1 1.06-1.06 7 7 0 1 1-9.9 0 .75.75 0 0 1 1.06 0Z"
clip-rule="evenodd"
/>
</svg>
<div>Enable Chat History</div>
</button>
</div>
</div>
</div>
{/if}
<div class="px-2.5 mt-1 mb-2 flex justify-center space-x-2"> <div class="px-2.5 mt-1 mb-2 flex justify-center space-x-2">
<div class="flex w-full" id="chat-search"> <div class="flex w-full" id="chat-search">
<div class="self-center pl-3 py-2 rounded-l bg-gray-950"> <div class="self-center pl-3 py-2 rounded-l bg-gray-950">
@ -407,6 +455,7 @@
</div> </div>
{/each} {/each}
</div> </div>
</div>
<div class="px-2.5"> <div class="px-2.5">
<hr class=" border-gray-900 mb-1 w-full" /> <hr class=" border-gray-900 mb-1 w-full" />

View file

@ -280,12 +280,14 @@
} }
if ($chatId == _chatId) { if ($chatId == _chatId) {
if ($settings.saveChatHistory ?? true) {
chat = await updateChatById(localStorage.token, _chatId, { chat = await updateChatById(localStorage.token, _chatId, {
messages: messages, messages: messages,
history: history history: history
}); });
await chats.set(await getChatList(localStorage.token)); await chats.set(await getChatList(localStorage.token));
} }
}
} else { } else {
if (res !== null) { if (res !== null) {
const error = await res.json(); const error = await res.json();
@ -444,12 +446,14 @@
} }
if ($chatId == _chatId) { if ($chatId == _chatId) {
if ($settings.saveChatHistory ?? true) {
chat = await updateChatById(localStorage.token, _chatId, { chat = await updateChatById(localStorage.token, _chatId, {
messages: messages, messages: messages,
history: history history: history
}); });
await chats.set(await getChatList(localStorage.token)); await chats.set(await getChatList(localStorage.token));
} }
}
} else { } else {
if (res !== null) { if (res !== null) {
const error = await res.json(); const error = await res.json();
@ -527,6 +531,7 @@
// Create new chat if only one message in messages // Create new chat if only one message in messages
if (messages.length == 1) { if (messages.length == 1) {
if ($settings.saveChatHistory ?? true) {
chat = await createNewChat(localStorage.token, { chat = await createNewChat(localStorage.token, {
id: $chatId, id: $chatId,
title: 'New Chat', title: 'New Chat',
@ -541,6 +546,9 @@
}); });
await chats.set(await getChatList(localStorage.token)); await chats.set(await getChatList(localStorage.token));
await chatId.set(chat.id); await chatId.set(chat.id);
} else {
await chatId.set('local');
}
await tick(); await tick();
} }
@ -592,8 +600,10 @@
title = _title; title = _title;
} }
if ($settings.saveChatHistory ?? true) {
chat = await updateChatById(localStorage.token, _chatId, { title: _title }); chat = await updateChatById(localStorage.token, _chatId, { title: _title });
await chats.set(await getChatList(localStorage.token)); await chats.set(await getChatList(localStorage.token));
}
}; };
</script> </script>

View file

@ -609,6 +609,12 @@
chat = await updateChatById(localStorage.token, _chatId, { title: _title }); chat = await updateChatById(localStorage.token, _chatId, { title: _title });
await chats.set(await getChatList(localStorage.token)); await chats.set(await getChatList(localStorage.token));
}; };
onMount(async () => {
if (!($settings.saveChatHistory ?? true)) {
await goto('/');
}
});
</script> </script>
<svelte:window <svelte:window