bolt.new/app/lib/persistence/useChatHistory.ts

110 lines
2.9 KiB
TypeScript
Raw Normal View History

import { useLoaderData, useNavigate } from '@remix-run/react';
import { useState, useEffect } from 'react';
import { atom } from 'nanostores';
2024-07-25 13:03:38 +00:00
import type { Message } from 'ai';
import { toast } from 'react-toastify';
import { workbenchStore } from '~/lib/stores/workbench';
import { getMessages, getNextId, getUrlId, openDatabase, setMessages } from './db';
2024-07-25 13:03:38 +00:00
export interface ChatHistoryItem {
2024-07-25 13:03:38 +00:00
id: string;
urlId?: string;
description?: string;
2024-07-25 13:03:38 +00:00
messages: Message[];
2024-07-31 21:21:40 +00:00
timestamp: string;
2024-07-25 13:03:38 +00:00
}
const persistenceEnabled = !import.meta.env.VITE_DISABLE_PERSISTENCE;
export const db = persistenceEnabled ? await openDatabase() : undefined;
2024-07-25 13:03:38 +00:00
export const chatId = atom<string | undefined>(undefined);
export const description = atom<string | undefined>(undefined);
2024-07-25 13:03:38 +00:00
export function useChatHistory() {
const navigate = useNavigate();
const { id: mixedId } = useLoaderData<{ id?: string }>();
2024-07-25 13:03:38 +00:00
const [initialMessages, setInitialMessages] = useState<Message[]>([]);
const [ready, setReady] = useState<boolean>(false);
const [urlId, setUrlId] = useState<string | undefined>();
2024-07-25 13:03:38 +00:00
useEffect(() => {
if (!db) {
setReady(true);
if (persistenceEnabled) {
toast.error(`Chat persistence is unavailable`);
}
return;
}
if (mixedId) {
getMessages(db, mixedId)
2024-07-25 13:03:38 +00:00
.then((storedMessages) => {
if (storedMessages && storedMessages.messages.length > 0) {
setInitialMessages(storedMessages.messages);
setUrlId(storedMessages.urlId);
description.set(storedMessages.description);
chatId.set(storedMessages.id);
2024-07-25 13:03:38 +00:00
} else {
navigate(`/`, { replace: true });
}
setReady(true);
})
.catch((error) => {
toast.error(error.message);
});
}
}, []);
return {
ready: !mixedId || ready,
2024-07-25 13:03:38 +00:00
initialMessages,
storeMessageHistory: async (messages: Message[]) => {
if (!db || messages.length === 0) {
return;
}
const { firstArtifact } = workbenchStore;
if (!urlId && firstArtifact?.id) {
const urlId = await getUrlId(db, firstArtifact.id);
navigateChat(urlId);
setUrlId(urlId);
}
if (!description.get() && firstArtifact?.title) {
description.set(firstArtifact?.title);
}
if (initialMessages.length === 0 && !chatId.get()) {
const nextId = await getNextId(db);
2024-07-25 13:03:38 +00:00
chatId.set(nextId);
2024-07-25 13:03:38 +00:00
if (!urlId) {
navigateChat(nextId);
2024-07-25 13:03:38 +00:00
}
}
await setMessages(db, chatId.get() as string, messages, urlId, description.get());
2024-07-25 13:03:38 +00:00
},
};
}
function navigateChat(nextId: string) {
/**
* FIXME: Using the intended navigate function causes a rerender for <Chat /> that breaks the app.
*
* `navigate(`/chat/${nextId}`, { replace: true });`
*/
const url = new URL(window.location.href);
url.pathname = `/chat/${nextId}`;
window.history.replaceState({}, '', url);
}