Skip to content

Commit cb4a073

Browse files
committed
chore: fix chat charts persistance
1 parent c2e2c7c commit cb4a073

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

client/src/hooks/useChatBot.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface StreamingResponse {
1717
json?: Record<string, unknown>; // For chart data or other structured responses
1818
error?: string;
1919
incomplete?: boolean;
20+
messageId?: string | number; // ID of the message associated with this response
2021
}
2122

2223
export interface UseChatBotReturn {
@@ -174,25 +175,27 @@ export const useChatBot = (config: ChatBotConfig): UseChatBotReturn => {
174175
setMessages(prev => prev.filter(msg => msg.id !== typingMessageId));
175176

176177
// Then add complete response after a tiny delay
178+
const botMessageId = Date.now() + 2;
177179
setTimeout(() => {
178180
const botMessage: Message = {
179-
id: Date.now() + 2,
181+
id: botMessageId,
180182
author: bot,
181183
timestamp: new Date(),
182184
text: currentAnswer
183185
};
184186

185187
setMessages(prev => [...prev, botMessage]);
186188
}, 10);
189+
190+
// Set the final response with the message ID for any additional processing
191+
if (finalResponse) {
192+
finalResponse.messageId = botMessageId;
193+
setLatestResponse(finalResponse);
194+
}
187195
} else {
188196
// If no response, just remove typing indicator
189197
setMessages(prev => prev.filter(msg => msg.id !== typingMessageId));
190198
}
191-
192-
// Set the final response for any additional processing
193-
if (finalResponse) {
194-
setLatestResponse(finalResponse);
195-
}
196199
} catch (error) {
197200
const errorMessage = error instanceof Error ? error.message : 'Network error';
198201

client/src/pages/FinanceAnalysis.tsx

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)