Skip to content

Commit 7ac6db8

Browse files
authored
fix: chat UX and message handling (#206)
* fix: redirect first message * feat: update createNewChat optimistic * fix: reset message when navigation to /
1 parent f006ef8 commit 7ac6db8

File tree

4 files changed

+43
-17
lines changed

4 files changed

+43
-17
lines changed

app/components/chat/chat.tsx

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export function Chat() {
9999
)
100100

101101
const hasSentFirstMessageRef = useRef(false)
102+
const prevChatIdRef = useRef<string | null>(chatId)
102103
const isAuthenticated = useMemo(() => !!user?.id, [user?.id])
103104

104105
const { draftValue, clearDraft } = useChatDraft(chatId)
@@ -137,6 +138,16 @@ export function Chat() {
137138
onError: handleError,
138139
})
139140

141+
// Reset messages when navigating from a chat to home (not on every render)
142+
if (
143+
prevChatIdRef.current !== null &&
144+
chatId === null &&
145+
messages.length > 0
146+
) {
147+
setMessages([])
148+
}
149+
prevChatIdRef.current = chatId
150+
140151
const { checkLimitsAndNotify, ensureChatExists } = useChatUtils({
141152
isAuthenticated,
142153
chatId,
@@ -199,9 +210,6 @@ export function Chat() {
199210
return
200211
}
201212

202-
// refresh the chat history (in sidebar+command/drawer)
203-
bumpChat(currentChatId)
204-
205213
if (input.length > MESSAGE_MAX_LENGTH) {
206214
toast({
207215
title: `The message you submitted was too long, please submit something shorter. (Max ${MESSAGE_MAX_LENGTH} characters)`,
@@ -243,6 +251,12 @@ export function Chat() {
243251
cacheAndAddMessage(optimisticMessage)
244252
clearDraft()
245253
hasSentFirstMessageRef.current = true
254+
255+
// Bump existing chats to top (non-blocking, after submit)
256+
// If messages.length === 0, this is a new chat that was just created
257+
if (messages.length > 0) {
258+
bumpChat(currentChatId)
259+
}
246260
} catch (submitError) {
247261
setMessages((prev) => prev.filter((msg) => msg.id !== optimisticId))
248262
cleanupOptimisticAttachments(optimisticMessage.experimental_attachments)
@@ -270,6 +284,8 @@ export function Chat() {
270284
handleSubmit,
271285
cacheAndAddMessage,
272286
clearDraft,
287+
messages.length,
288+
bumpChat,
273289
])
274290

275291
const handleSuggestion = useCallback(
@@ -422,8 +438,17 @@ export function Chat() {
422438
]
423439
)
424440

425-
// Check for redirect condition - removed hydrated dependency
426-
if (chatId && !isChatsLoading && !currentChat) {
441+
// Handle redirect for invalid chatId - only redirect if we're certain the chat doesn't exist
442+
// and we're not in a transient state during chat creation
443+
if (
444+
chatId &&
445+
!isChatsLoading &&
446+
!currentChat &&
447+
!isSubmitting &&
448+
status === "ready" &&
449+
messages.length === 0 &&
450+
!hasSentFirstMessageRef.current // Don't redirect if we've already sent a message in this session
451+
) {
427452
return redirect("/")
428453
}
429454

app/components/layout/button-new-chat.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
TooltipContent,
77
TooltipTrigger,
88
} from "@/components/ui/tooltip"
9-
import { NotePencil } from "@phosphor-icons/react/dist/ssr"
9+
import { NotePencilIcon } from "@phosphor-icons/react/dist/ssr"
1010
import Link from "next/link"
1111
import { usePathname, useRouter } from "next/navigation"
1212

@@ -29,7 +29,7 @@ export function ButtonNewChat() {
2929
prefetch
3030
aria-label="New Chat"
3131
>
32-
<NotePencil size={24} />
32+
<NotePencilIcon size={24} />
3333
</Link>
3434
</TooltipTrigger>
3535
<TooltipContent>New Chat ⌘⇧U</TooltipContent>

app/components/layout/sidebar/app-sidebar.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ export function AppSidebar() {
3030
const params = useParams<{ chatId: string }>()
3131
const currentChatId = params.chatId
3232

33-
const groupedChats = useMemo(() => groupChatsByDate(chats, ""), [chats])
33+
const groupedChats = useMemo(() => {
34+
const result = groupChatsByDate(chats, "")
35+
return result
36+
}, [chats])
3437
const hasChats = chats.length > 0
3538
const router = useRouter()
3639

lib/chat-store/chats/provider.tsx

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export function ChatsProvider({
145145
public: true,
146146
updated_at: new Date().toISOString(),
147147
}
148-
setChats((prev) => [...prev, optimisticChat])
148+
setChats((prev) => [optimisticChat, ...prev])
149149

150150
try {
151151
const newChat = await createNewChatFromDb(
@@ -155,14 +155,12 @@ export function ChatsProvider({
155155
isAuthenticated,
156156
agentId
157157
)
158-
setChats((prev) =>
159-
prev
160-
.map((c) => (c.id === optimisticId ? newChat : c))
161-
.sort(
162-
(a, b) =>
163-
+new Date(b.created_at || "") - +new Date(a.created_at || "")
164-
)
165-
)
158+
159+
setChats((prev) => [
160+
newChat,
161+
...prev.filter((c) => c.id !== optimisticId),
162+
])
163+
166164
return newChat
167165
} catch {
168166
setChats(prev)

0 commit comments

Comments
 (0)