@@ -14,6 +14,7 @@ import {getChats} from "api/bot/getChats";
1414import { useAlertSnackbar } from "@postgres.ai/shared/components/AlertSnackbar/useAlertSnackbar" ;
1515import { localStorage } from "../../helpers/localStorage" ;
1616import { makeChatPublic } from "../../api/bot/makeChatPublic" ;
17+ import { usePrev } from "../../hooks/usePrev" ;
1718
1819
1920const WS_URL = process . env . REACT_APP_WS_URL || '' ;
@@ -41,26 +42,34 @@ type UseAiBotReturnType = {
4142 wsReadyState : ReadyState ;
4243 changeChatVisibility : ( threadId : string , isPublic : boolean ) => void ;
4344 isChangeVisibilityLoading : boolean ;
44- unsubscribe : ( threadId : string ) => void
45+ unsubscribe : ( threadId : string ) => void ;
46+ chatsList : UseBotChatsListHook [ 'chatsList' ] ;
47+ chatsListLoading : UseBotChatsListHook [ 'loading' ] ;
48+ getChatsList : UseBotChatsListHook [ 'getChatsList' ]
4549}
4650
4751type UseAiBotArgs = {
4852 threadId ?: string ;
49- prevThreadId ?: string ;
50- onChatLoadingError ?: ( ) => void ;
53+ orgId ?: number
5154}
5255
5356export const useAiBot = ( args : UseAiBotArgs ) : UseAiBotReturnType => {
54- const { threadId, onChatLoadingError } = args ;
57+ const { threadId, orgId } = args ;
5558 const { showMessage, closeSnackbar } = useAlertSnackbar ( ) ;
5659 let location = useLocation < { skipReloading ?: boolean } > ( ) ;
5760
61+ const {
62+ chatsList,
63+ loading : chatsListLoading ,
64+ getChatsList,
65+ } = useBotChatsList ( orgId ) ;
66+
5867 const [ messages , setMessages ] = useState < BotMessage [ ] | null > ( null ) ;
5968 const [ isLoading , setLoading ] = useState < boolean > ( false ) ;
6069 const [ error , setError ] = useState < ErrorType | null > ( null ) ;
6170 const [ wsLoading , setWsLoading ] = useState < boolean > ( false ) ;
6271 const [ isChangeVisibilityLoading , setIsChangeVisibilityLoading ] = useState < boolean > ( false )
63-
72+
6473 const token = localStorage . getAuthToken ( )
6574
6675 const onWebSocketError = ( error : WebSocketEventMap [ 'error' ] ) => {
@@ -92,6 +101,9 @@ export const useAiBot = (args: UseAiBotArgs): UseAiBotReturnType => {
92101 } ) ;
93102 }
94103 }
104+ } else if ( threadId !== messageData . thread_id ) {
105+ const threadInList = chatsList ?. find ( ( item ) => item . thread_id === messageData . thread_id )
106+ if ( ! threadInList ) getChatsList ( )
95107 }
96108 } else {
97109 showMessage ( 'An error occurred. Please try again' )
@@ -137,7 +149,6 @@ export const useAiBot = (args: UseAiBotArgs): UseAiBotReturnType => {
137149 if ( response && response . length > 0 ) {
138150 setMessages ( response ) ;
139151 } else {
140- if ( onChatLoadingError ) onChatLoadingError ( ) ;
141152 setError ( {
142153 code : 404 ,
143154 message : 'Specified chat not found or you have no access.' ,
@@ -168,7 +179,6 @@ export const useAiBot = (args: UseAiBotArgs): UseAiBotReturnType => {
168179 } else if ( threadId ) {
169180 subscribe ( threadId )
170181 }
171-
172182 return ( ) => {
173183 isCancelled = true ;
174184 } ;
@@ -294,15 +304,18 @@ export const useAiBot = (args: UseAiBotArgs): UseAiBotReturnType => {
294304 sendMessage,
295305 clearChat,
296306 messages,
297- unsubscribe
307+ unsubscribe,
308+ chatsList,
309+ chatsListLoading,
310+ getChatsList
298311 }
299312}
300313
301314type UseBotChatsListHook = {
302315 chatsList : BotMessage [ ] | null ;
303316 error : Response | null ;
304317 loading : boolean ;
305- getChatsList : ( ) => void
318+ getChatsList : ( ) => void ;
306319} ;
307320
308321export const useBotChatsList = ( orgId ?: number ) : UseBotChatsListHook => {
0 commit comments