Skip to content

Commit

Permalink
chore: fiddle with states #283
Browse files Browse the repository at this point in the history
  • Loading branch information
srizvi committed Apr 28, 2024
1 parent fb3d08e commit 470a10c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
4 changes: 2 additions & 2 deletions apps/mai-sc/src/lib/chat/actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ export interface Message {
export interface AIState {
chatId: string;
messages: Message[];
saved?: boolean;
saved: boolean;
}

export type UIState = {
Expand All @@ -492,7 +492,7 @@ export const AI = createAI<AIState, UIState>({
confirmPurchase,
},
initialUIState: [],
initialAIState: { chatId: nanoid(), messages: [] },
initialAIState: { chatId: nanoid(), messages: [], saved: false },
unstable_onGetUIState: async () => {
'use server';

Expand Down
26 changes: 6 additions & 20 deletions apps/mai-sc/src/ui/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down

0 comments on commit 470a10c

Please sign in to comment.