@@ -10,7 +10,9 @@ import {
1010 IStackTokens ,
1111 mergeStyleSets ,
1212 IModalStyles ,
13- PrimaryButton
13+ PrimaryButton ,
14+ Spinner ,
15+ SpinnerSize
1416} from '@fluentui/react'
1517import { SquareRegular , ShieldLockRegular , ErrorCircleRegular } from '@fluentui/react-icons'
1618
@@ -46,6 +48,7 @@ import { AuthNotConfigure } from './Components/AuthNotConfigure'
4648import { ChatMessageContainer } from './Components/ChatMessageContainer' ;
4749import { parseErrorMessage , cleanJSON } from '../../helpers/helpers' ;
4850
51+
4952const enum messageStatus {
5053 NotRunning = 'Not Running' ,
5154 Processing = 'Processing' ,
@@ -119,6 +122,11 @@ const Chat = ({ type = ChatType.Browse }: Props) => {
119122 const [ isModalOpen , setIsModalOpen ] = useState ( false )
120123 const [ modalUrl , setModalUrl ] = useState ( '' ) ;
121124 const [ finalMessages , setFinalMessages ] = useState < ChatMessage [ ] > ( [ ] )
125+ if ( ! appStateContext || ! appStateContext . state ) {
126+ console . error ( "AppStateContext is undefined. Ensure AppProvider wraps this component." ) ;
127+ return null ; // Prevents execution if context is missing
128+ }
129+ const { currentChat } = appStateContext ?. state ;
122130
123131 const errorDialogContentProps = {
124132 type : DialogType . close ,
@@ -422,6 +430,9 @@ const Chat = ({ type = ChatType.Browse }: Props) => {
422430 let conversation
423431 if ( conversationId ) {
424432 conversation = appStateContext ?. state ?. chatHistory ?. find ( conv => conv . id === conversationId )
433+ if ( ! conversation ) {
434+ conversation = appStateContext ?. state ?. currentChat
435+ }
425436 if ( ! conversation ) {
426437 console . error ( 'Conversation not found.' )
427438 setIsLoading ( false )
@@ -460,6 +471,9 @@ const Chat = ({ type = ChatType.Browse }: Props) => {
460471 let resultConversation
461472 if ( conversationId ) {
462473 resultConversation = appStateContext ?. state ?. chatHistory ?. find ( conv => conv . id === conversationId )
474+ if ( ! resultConversation ) {
475+ resultConversation = appStateContext ?. state ?. currentChat
476+ }
463477 if ( ! resultConversation ) {
464478 console . error ( 'Conversation not found.' )
465479 setIsLoading ( false )
@@ -530,6 +544,9 @@ const Chat = ({ type = ChatType.Browse }: Props) => {
530544 let resultConversation
531545 if ( conversationId ) {
532546 resultConversation = appStateContext ?. state ?. chatHistory ?. find ( conv => conv . id === conversationId )
547+ if ( ! resultConversation ) {
548+ resultConversation = appStateContext ?. state ?. currentChat
549+ }
533550 if ( ! resultConversation ) {
534551 console . error ( 'Conversation not found.' )
535552 setIsLoading ( false )
@@ -585,6 +602,9 @@ const Chat = ({ type = ChatType.Browse }: Props) => {
585602 let resultConversation
586603 if ( conversationId ) {
587604 resultConversation = appStateContext ?. state ?. chatHistory ?. find ( conv => conv . id === conversationId )
605+ if ( ! resultConversation ) {
606+ resultConversation = appStateContext ?. state ?. currentChat
607+ }
588608 if ( ! resultConversation ) {
589609 console . error ( 'Conversation not found.' )
590610 setIsLoading ( false )
@@ -817,19 +837,23 @@ const Chat = ({ type = ChatType.Browse }: Props) => {
817837 ) : (
818838 < Stack horizontal className = { styles . chatRoot } >
819839 < div className = { styles . chatContainer } >
820- { ! messages || messages . length < 1 ? (
840+ { appStateContext . state . isLoading ? (
841+ < Stack horizontalAlign = "center" verticalAlign = "center" className = { styles . chatLoadingState } style = { { padding : 250 , paddingBottom : 150 } } >
842+ < Spinner label = "Loading your chat..." size = { SpinnerSize . large } />
843+ </ Stack >
844+ ) : currentChat ?. messages && currentChat . messages . length > 0 ? (
845+ < ChatMessageContainer
846+ messages = { currentChat . messages }
847+ isLoading = { isLoading }
848+ type = { type }
849+ onShowCitation = { onShowCitation }
850+ showLoadingMessage = { showLoadingMessage }
851+ ref = { chatMessageStreamEnd }
852+ />
853+ ) : (
821854 < Stack className = { styles . chatEmptyState } >
822855 < h1 className = { styles . chatEmptyStateTitle } > { ui ?. chat_title } </ h1 >
823856 </ Stack >
824- ) : (
825- < ChatMessageContainer
826- messages = { messages }
827- isLoading = { isLoading }
828- type = { type }
829- onShowCitation = { onShowCitation }
830- showLoadingMessage = { showLoadingMessage }
831- ref = { chatMessageStreamEnd }
832- />
833857 ) }
834858
835859 < Stack horizontal className = { styles . chatInput } >
0 commit comments