11import type { StreamingUpdate } from '@databuddy/shared' ;
22import { useAtom } from 'jotai' ;
33import { useCallback , useEffect , useRef , useState } from 'react' ;
4- import { trpc } from '@/lib/trpc' ;
54import {
65 inputValueAtom ,
76 isLoadingAtom ,
@@ -121,6 +120,15 @@ export function useChat() {
121120 } ;
122121 } , [ ] ) ;
123122
123+ function updateAiMessage ( message : Message ) {
124+ setMessages ( ( prev ) => {
125+ //TODO: find a way to update the message with the correct id
126+ const newMessages = [ ...prev ] ;
127+ newMessages [ newMessages . length - 1 ] = message ;
128+ return newMessages ;
129+ } ) ;
130+ }
131+
124132 const sendMessage = useCallback (
125133 async ( content ?: string ) => {
126134 const messageContent = content || inputValue . trim ( ) ;
@@ -155,6 +163,8 @@ export function useChat() {
155163 thinkingSteps : [ ] ,
156164 } ;
157165
166+ setMessages ( ( prev ) => [ ...prev , assistantMessage ] ) ;
167+
158168 try {
159169 // Stream the AI response using the new single endpoint
160170 const response = await fetch (
@@ -304,6 +314,7 @@ export function useChat() {
304314 break ;
305315 }
306316 }
317+ updateAiMessage ( assistantMessage ) ;
307318 } catch ( _parseError ) {
308319 console . warn ( 'Failed to parse SSE data:' , line ) ;
309320 }
@@ -323,8 +334,6 @@ export function useChat() {
323334 } finally {
324335 setIsLoading ( false ) ;
325336 }
326-
327- setMessages ( ( prev ) => [ ...prev , assistantMessage ] ) ;
328337 } ,
329338 [
330339 inputValue ,
0 commit comments