diff --git a/apps/mai-sc/src/lib/chat/actions.tsx b/apps/mai-sc/src/lib/chat/actions.tsx index bc467a94d..f14575b4f 100644 --- a/apps/mai-sc/src/lib/chat/actions.tsx +++ b/apps/mai-sc/src/lib/chat/actions.tsx @@ -478,7 +478,7 @@ export interface Message { export interface AIState { chatId: string; messages: Message[]; - saved?: boolean; + saved: boolean; } export type UIState = { @@ -492,7 +492,7 @@ export const AI = createAI({ confirmPurchase, }, initialUIState: [], - initialAIState: { chatId: nanoid(), messages: [] }, + initialAIState: { chatId: nanoid(), messages: [], saved: false }, unstable_onGetUIState: async () => { 'use server'; diff --git a/apps/mai-sc/src/ui/chat.tsx b/apps/mai-sc/src/ui/chat.tsx index 15fccc71c..e80539fee 100644 --- a/apps/mai-sc/src/ui/chat.tsx +++ b/apps/mai-sc/src/ui/chat.tsx @@ -52,26 +52,12 @@ export function Chat({ id, className, session, missingKeys }: ChatProps) { console.log('🤖 Checking AI state for message length of 2'); // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access const messagesLength = aiState.messages?.length; - if (messagesLength === 2) { - const refreshAfterSave = async () => { - await new Promise((resolve) => { - const checkSaveStatus = () => { - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access - if (aiState.saved) { - resolve(true); - } else { - setTimeout(checkSaveStatus, 100); - } - }; - checkSaveStatus(); - }); - - router.refresh(); - console.log( - '🤖 Refreshed the router after the chat is saved with 2 messages', - ); - }; - void refreshAfterSave(); + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + if (messagesLength === 2 && aiState.saved) { + router.refresh(); + console.log( + '🤖 Refreshed the router after the chat is saved with 2 messages', + ); } // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access }, [aiState.messages, aiState.saved, router]);