From 2b534d4c43f7f14efc07c3e9a97a3ce1cb326b4a Mon Sep 17 00:00:00 2001 From: Mannu Date: Fri, 29 May 2026 22:17:10 +0530 Subject: [PATCH] Fix AI chat: input blocked by bottom nav + first-chat crash - Add pb-16 to /ai root so the input clears the fixed BottomNav (only page using h-screen instead of min-h-screen+pb-24). - Normalize new sessions with messages:[] in createNewSession so render no longer hits undefined.length and crashes on first chat. - Make render defensive (messages?.length ?? 0). Co-Authored-By: Claude Opus 4.8 --- src/app/(app)/ai/page.tsx | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/app/(app)/ai/page.tsx b/src/app/(app)/ai/page.tsx index e639655..d19370b 100644 --- a/src/app/(app)/ai/page.tsx +++ b/src/app/(app)/ai/page.tsx @@ -51,8 +51,12 @@ export default function AIChatPage() { }); const data = await res.json(); if (data.session) { - setSessions([data.session, ...sessions]); - setCurrentSessionId(data.session.id); + // POST /api/chat returns a session without a `messages` field — normalize it + // to an empty array so the render path (currentSession.messages.length) never + // hits `undefined.length`, which crashed the page on the first new chat. + const newSession = { ...data.session, messages: [] }; + setSessions([newSession, ...sessions]); + setCurrentSessionId(newSession.id); setSidebarOpen(false); } } catch (err) { @@ -150,7 +154,7 @@ export default function AIChatPage() { }; return ( -
+
{/* Sidebar overlay on mobile */} {sidebarOpen && ( @@ -227,12 +231,12 @@ export default function AIChatPage() {

Tap ☰ to see past chats, or just type below to start

- ) : currentSession.messages.length === 0 ? ( + ) : (currentSession.messages?.length ?? 0) === 0 ? (

Type a question below to get started

) : ( - currentSession.messages.map((msg, i) => ( + (currentSession.messages ?? []).map((msg, i) => (