@@ -11,6 +11,8 @@ import { ChatProps, connectionState, Messages, UserCredentials } from '../../typ
1111import { getIsLoading } from '../../utils/Utils' ;
1212import ThemeWrapper from '../../context/ThemeWrapper' ;
1313import { SpotlightProvider } from '@neo4j-ndl/react' ;
14+ import { envConnectionAPI } from '../../services/ConnectAPI' ;
15+ import { showErrorToast } from '../../utils/Toasts' ;
1416
1517const ChatContent : React . FC < ChatProps > = ( { chatMessages } ) => {
1618 const { clearHistoryData, messages, setMessages, setClearHistoryData, setIsDeleteChatLoading, isDeleteChatLoading } =
@@ -23,10 +25,47 @@ const ChatContent: React.FC<ChatProps> = ({ chatMessages }) => {
2325 vectorIndexMisMatch : false ,
2426 chunksExistsWithDifferentDimension : false ,
2527 } ) ;
26- /**
27- * Initializes connection settings based on URL parameters.
28- */
29- const initialiseConnection = useCallback ( ( ) => {
28+ const resolveConnectionFromBackend = useCallback ( async ( ) : Promise < boolean > => {
29+ try {
30+ const backendApiResponse = await envConnectionAPI ( ) ;
31+ const connectionData = backendApiResponse . data ;
32+ if ( connectionData . data && connectionData . status === 'Success' ) {
33+ const credentials : UserCredentials = {
34+ uri : connectionData . data . uri ,
35+ connection : 'backendApi' ,
36+ email : '' ,
37+ } ;
38+ setUserCredentials ( credentials ) ;
39+ setConnectionStatus ( Boolean ( connectionData . data . graph_connection ) ) ;
40+ setShowDisconnectButton ( true ) ;
41+ if ( chatMessages . length ) {
42+ setMessages ( chatMessages ) ;
43+ }
44+ return true ;
45+ }
46+ if ( ! connectionData . data && connectionData . status === 'Success' ) {
47+ const storedCredentials = localStorage . getItem ( 'neo4j.connection' ) ;
48+ if ( storedCredentials ) {
49+ const parsed = JSON . parse ( storedCredentials ) as UserCredentials ;
50+ parsed . password = atob ( parsed . password as string ) ;
51+ setUserCredentials ( parsed ) ;
52+ setConnectionStatus ( Boolean ( parsed . connection === 'connectAPI' ) ) ;
53+ setShowDisconnectButton ( true ) ;
54+ if ( chatMessages . length ) {
55+ setMessages ( chatMessages ) ;
56+ }
57+ return true ;
58+ }
59+ }
60+ } catch ( _err ) {
61+ const message =
62+ _err instanceof Error ? _err . message : 'Unable to reach the backend. Please check your connection.' ;
63+ showErrorToast ( message ) ;
64+ }
65+ return false ;
66+ } , [ chatMessages , setUserCredentials , setConnectionStatus , setShowDisconnectButton , setMessages ] ) ;
67+
68+ const initialiseConnection = useCallback ( async ( ) => {
3069 const urlParams = new URLSearchParams ( window . location . search ) ;
3170 const uri = urlParams . get ( 'uri' ) ;
3271 const user = urlParams . get ( 'user' ) ;
@@ -40,9 +79,14 @@ const ChatContent: React.FC<ChatProps> = ({ chatMessages }) => {
4079 if ( connectionStatus ) {
4180 setShowBackButton ( ) ;
4281 setConnectionStatus ( connectionStatus ) ;
43- setMessages ( chatMessages ) ;
82+ if ( chatMessages . length ) {
83+ setMessages ( chatMessages ) ;
84+ }
4485 } else {
45- setOpenConnection ( ( prev ) => ( { ...prev , openPopUp : true } ) ) ;
86+ const resolved = await resolveConnectionFromBackend ( ) ;
87+ if ( ! resolved ) {
88+ setOpenConnection ( ( prev ) => ( { ...prev , openPopUp : true } ) ) ;
89+ }
4690 }
4791 } else {
4892 const credentialsForAPI : UserCredentials = {
@@ -56,11 +100,13 @@ const ChatContent: React.FC<ChatProps> = ({ chatMessages }) => {
56100 setShowBackButton ( ) ;
57101 setUserCredentials ( credentialsForAPI ) ;
58102 setConnectionStatus ( true ) ;
59- setMessages ( chatMessages ) ;
60- // Remove query params from URL
103+ setShowDisconnectButton ( true ) ;
104+ if ( chatMessages . length ) {
105+ setMessages ( chatMessages ) ;
106+ }
61107 window . history . replaceState ( { } , document . title , window . location . pathname ) ;
62108 }
63- } , [ chatMessages , setUserCredentials , setConnectionStatus , setMessages ] ) ;
109+ } , [ chatMessages , setUserCredentials , setConnectionStatus , setMessages , resolveConnectionFromBackend ] ) ;
64110
65111 useEffect ( ( ) => {
66112 initialiseConnection ( ) ;
@@ -92,7 +138,7 @@ const ChatContent: React.FC<ChatProps> = ({ chatMessages }) => {
92138 }
93139 } catch ( error ) {
94140 setIsDeleteChatLoading ( false ) ;
95- console . error ( 'Error clearing chat history:' , error ) ;
141+ showErrorToast ( error instanceof Error ? error . message : 'Error clearing chat history' ) ;
96142 setClearHistoryData ( false ) ;
97143 }
98144 } ;
0 commit comments