From 1d5375e7598759c6e97d46d4c0a32d7ec81434e3 Mon Sep 17 00:00:00 2001 From: Ben Lopata <42045469+bLopata@users.noreply.github.com> Date: Mon, 16 Dec 2024 13:55:11 -0600 Subject: [PATCH] Adds back in default conversation selector/create logic. (#188) * Adds back in default conversation selector/create logic. * Fix conversation loading on mount. --- www/app/Chat.tsx | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/www/app/Chat.tsx b/www/app/Chat.tsx index f4aa79a..65aac00 100644 --- a/www/app/Chat.tsx +++ b/www/app/Chat.tsx @@ -153,7 +153,8 @@ export default function Chat({ }; const conversationsFetcher = async () => { - return getConversations(); + const result = await getConversations(); + return result; }; const conversationsKey = useMemo(() => userId, [userId]); @@ -164,10 +165,32 @@ 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, - revalidateOnMount: false, + revalidateOnMount: true } );