@@ -147,6 +147,8 @@ export default function FinanceAnalysis() {
147147
148148 const [ selectedCharts , setSelectedCharts ] = React . useState < BarChartDef [ ] > ( [ ] ) ;
149149 const [ isChartsExpanded , setIsChartsExpanded ] = React . useState ( false ) ;
150+ // Map message IDs to their associated charts
151+ const [ messageCharts , setMessageCharts ] = React . useState < Map < string | number , BarChartDef [ ] > > ( new Map ( ) ) ;
150152
151153 // Predefined suggestions related to financial data
152154 const financialSuggestions : ChatSuggestion [ ] = [
@@ -178,36 +180,48 @@ export default function FinanceAnalysis() {
178180 suggestions : financialSuggestions ,
179181 } ) ;
180182
181- // Watch for changes in the latest response to update charts
183+ // Associate charts with the latest bot message when response arrives
182184 React . useEffect ( ( ) => {
183185 if (
184186 chatBot . latestResponse ?. json ?. charts &&
185- Array . isArray ( chatBot . latestResponse . json . charts )
187+ Array . isArray ( chatBot . latestResponse . json . charts ) &&
188+ chatBot . latestResponse . messageId
186189 ) {
187190 const validCharts : BarChartDef [ ] = chatBot . latestResponse . json . charts
188191 . filter ( isBarChartDef )
189192 . slice ( 0 , 3 ) ;
190- setSelectedCharts ( validCharts ) ;
191- } else {
192- setSelectedCharts ( [ ] ) ;
193+
194+ if ( validCharts . length > 0 ) {
195+ setMessageCharts ( prev => {
196+ const newMap = new Map ( prev ) ;
197+ newMap . set ( chatBot . latestResponse ! . messageId ! , validCharts ) ;
198+ return newMap ;
199+ } ) ;
200+ setSelectedCharts ( validCharts ) ;
201+ }
193202 }
194203 } , [ chatBot . latestResponse , isBarChartDef ] ) ;
195204
196205 // Custom message template that includes thumbnail when charts are available
197206 const customMessageTemplate = React . useCallback ( ( props : ChatMessageTemplateProps ) => {
198207 const isBot = props . item . author . id !== chatBot . user . id ;
199- const isLatestBotMessage = isBot && props . item . id === chatBot . messages [ chatBot . messages . length - 1 ] ?. id ;
200- const hasCharts = selectedCharts . length > 0 ;
208+ const chartsForThisMessage = messageCharts . get ( props . item . id ) ;
201209
202210 return (
203211 < div >
204212 < ChatMessage { ...props } />
205- { isLatestBotMessage && hasCharts && (
206- < ChartThumbnail onClick = { ( ) => setIsChartsExpanded ( true ) } />
213+ { isBot && chartsForThisMessage && chartsForThisMessage . length > 0 && (
214+ < ChartThumbnail onClick = { ( ) => {
215+ const charts = messageCharts . get ( props . item . id ) ;
216+ if ( charts ) {
217+ setSelectedCharts ( charts ) ;
218+ setIsChartsExpanded ( true ) ;
219+ }
220+ } } />
207221 ) }
208222 </ div >
209223 ) ;
210- } , [ chatBot . user . id , chatBot . messages , selectedCharts ] ) ;
224+ } , [ chatBot . user . id , messageCharts ] ) ;
211225
212226 // Memoized callback for sending messages
213227 const handleSendMessage = React . useCallback ( ( text : string ) => {
0 commit comments