@@ -14,6 +14,7 @@ import {getChats} from "api/bot/getChats";
14
14
import { useAlertSnackbar } from "@postgres.ai/shared/components/AlertSnackbar/useAlertSnackbar" ;
15
15
import { localStorage } from "../../helpers/localStorage" ;
16
16
import { makeChatPublic } from "../../api/bot/makeChatPublic" ;
17
+ import { usePrev } from "../../hooks/usePrev" ;
17
18
18
19
19
20
const WS_URL = process . env . REACT_APP_WS_URL || '' ;
@@ -41,26 +42,34 @@ type UseAiBotReturnType = {
41
42
wsReadyState : ReadyState ;
42
43
changeChatVisibility : ( threadId : string , isPublic : boolean ) => void ;
43
44
isChangeVisibilityLoading : boolean ;
44
- unsubscribe : ( threadId : string ) => void
45
+ unsubscribe : ( threadId : string ) => void ;
46
+ chatsList : UseBotChatsListHook [ 'chatsList' ] ;
47
+ chatsListLoading : UseBotChatsListHook [ 'loading' ] ;
48
+ getChatsList : UseBotChatsListHook [ 'getChatsList' ]
45
49
}
46
50
47
51
type UseAiBotArgs = {
48
52
threadId ?: string ;
49
- prevThreadId ?: string ;
50
- onChatLoadingError ?: ( ) => void ;
53
+ orgId ?: number
51
54
}
52
55
53
56
export const useAiBot = ( args : UseAiBotArgs ) : UseAiBotReturnType => {
54
- const { threadId, onChatLoadingError } = args ;
57
+ const { threadId, orgId } = args ;
55
58
const { showMessage, closeSnackbar } = useAlertSnackbar ( ) ;
56
59
let location = useLocation < { skipReloading ?: boolean } > ( ) ;
57
60
61
+ const {
62
+ chatsList,
63
+ loading : chatsListLoading ,
64
+ getChatsList,
65
+ } = useBotChatsList ( orgId ) ;
66
+
58
67
const [ messages , setMessages ] = useState < BotMessage [ ] | null > ( null ) ;
59
68
const [ isLoading , setLoading ] = useState < boolean > ( false ) ;
60
69
const [ error , setError ] = useState < ErrorType | null > ( null ) ;
61
70
const [ wsLoading , setWsLoading ] = useState < boolean > ( false ) ;
62
71
const [ isChangeVisibilityLoading , setIsChangeVisibilityLoading ] = useState < boolean > ( false )
63
-
72
+
64
73
const token = localStorage . getAuthToken ( )
65
74
66
75
const onWebSocketError = ( error : WebSocketEventMap [ 'error' ] ) => {
@@ -92,6 +101,9 @@ export const useAiBot = (args: UseAiBotArgs): UseAiBotReturnType => {
92
101
} ) ;
93
102
}
94
103
}
104
+ } else if ( threadId !== messageData . thread_id ) {
105
+ const threadInList = chatsList ?. find ( ( item ) => item . thread_id === messageData . thread_id )
106
+ if ( ! threadInList ) getChatsList ( )
95
107
}
96
108
} else {
97
109
showMessage ( 'An error occurred. Please try again' )
@@ -137,7 +149,6 @@ export const useAiBot = (args: UseAiBotArgs): UseAiBotReturnType => {
137
149
if ( response && response . length > 0 ) {
138
150
setMessages ( response ) ;
139
151
} else {
140
- if ( onChatLoadingError ) onChatLoadingError ( ) ;
141
152
setError ( {
142
153
code : 404 ,
143
154
message : 'Specified chat not found or you have no access.' ,
@@ -168,7 +179,6 @@ export const useAiBot = (args: UseAiBotArgs): UseAiBotReturnType => {
168
179
} else if ( threadId ) {
169
180
subscribe ( threadId )
170
181
}
171
-
172
182
return ( ) => {
173
183
isCancelled = true ;
174
184
} ;
@@ -294,15 +304,18 @@ export const useAiBot = (args: UseAiBotArgs): UseAiBotReturnType => {
294
304
sendMessage,
295
305
clearChat,
296
306
messages,
297
- unsubscribe
307
+ unsubscribe,
308
+ chatsList,
309
+ chatsListLoading,
310
+ getChatsList
298
311
}
299
312
}
300
313
301
314
type UseBotChatsListHook = {
302
315
chatsList : BotMessage [ ] | null ;
303
316
error : Response | null ;
304
317
loading : boolean ;
305
- getChatsList : ( ) => void
318
+ getChatsList : ( ) => void ;
306
319
} ;
307
320
308
321
export const useBotChatsList = ( orgId ?: number ) : UseBotChatsListHook => {
0 commit comments