From b8a197ed16834cc4f862ded5c07a4bf2c922b1bb Mon Sep 17 00:00:00 2001 From: Kirjava Date: Mon, 29 Jul 2024 16:36:21 +0100 Subject: [PATCH] fix: issue with generating a new url id every time (#18) --- packages/bolt/app/components/chat/Chat.client.tsx | 5 ++++- packages/bolt/app/lib/persistence/useChatHistory.ts | 8 ++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/bolt/app/components/chat/Chat.client.tsx b/packages/bolt/app/components/chat/Chat.client.tsx index 42ff709..e5400f9 100644 --- a/packages/bolt/app/components/chat/Chat.client.tsx +++ b/packages/bolt/app/components/chat/Chat.client.tsx @@ -63,7 +63,10 @@ export function ChatImpl({ initialMessages, storeMessageHistory }: ChatProps) { useEffect(() => { parseMessages(messages, isLoading); - storeMessageHistory(messages).catch((error) => toast.error(error.message)); + + if (messages.length > initialMessages.length) { + storeMessageHistory(messages).catch((error) => toast.error(error.message)); + } }, [messages, isLoading, parseMessages]); const scrollTextArea = () => { diff --git a/packages/bolt/app/lib/persistence/useChatHistory.ts b/packages/bolt/app/lib/persistence/useChatHistory.ts index cd6895f..526e1a6 100644 --- a/packages/bolt/app/lib/persistence/useChatHistory.ts +++ b/packages/bolt/app/lib/persistence/useChatHistory.ts @@ -14,12 +14,13 @@ export interface ChatHistory { const persistenceEnabled = !import.meta.env.VITE_DISABLE_PERSISTENCE; -const db = persistenceEnabled ? await openDatabase() : undefined; +export const db = persistenceEnabled ? await openDatabase() : undefined; export function useChatHistory() { const navigate = useNavigate(); - const { id: chatId } = useLoaderData<{ id?: string }>(); + const { id: mixedId } = useLoaderData<{ id?: string }>(); + const [chatId, setChatId] = useState(mixedId); const [initialMessages, setInitialMessages] = useState([]); const [ready, setReady] = useState(false); const [entryId, setEntryId] = useState(); @@ -42,6 +43,9 @@ export function useChatHistory() { .then((storedMessages) => { if (storedMessages && storedMessages.messages.length > 0) { setInitialMessages(storedMessages.messages); + setUrlId(storedMessages.urlId); + setDescription(storedMessages.description); + setChatId(storedMessages.id); } else { navigate(`/`, { replace: true }); }