From 29a0cb83489febb30637a797b51bd2ca7274f20b Mon Sep 17 00:00:00 2001 From: Ben Lopata <42045469+bLopata@users.noreply.github.com> Date: Mon, 16 Dec 2024 13:02:17 -0600 Subject: [PATCH] Adds back in default conversation selector/create logic. --- www/app/Chat.tsx | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/www/app/Chat.tsx b/www/app/Chat.tsx index e32d618..f1ed4a3 100644 --- a/www/app/Chat.tsx +++ b/www/app/Chat.tsx @@ -158,6 +158,28 @@ const { data: conversations, mutate: mutateConversations } = useSWR( { fallbackData: initialConversations, provider: localStorageProvider, + onSuccess: async (conversations) => { + if (conversations.length) { + // If there are existing conversations: + // 1. Set the current conversation to the first one if none is selected + // 2. Or if the selected conversation doesn't exist in the list + if ( + !conversationId || + !conversations.find((c) => c.conversationId === conversationId) + ) { + setConversationId(conversations[0].conversationId); + } + setCanSend(true); + } else { + // If no conversations exist: + // 1. Create a new conversation + // 2. Set it as the current conversation + // 3. Refresh the conversations list + const newConvo = await createConversation(); + setConversationId(newConvo?.conversationId); + await mutateConversations(); + } + }, revalidateOnFocus: false, dedupingInterval: 60000, revalidateIfStale: false,