Skip to content

Commit 1d5a1f7

Browse files
committed
Add conversation management to the Chat component using session storage
1 parent 9e8040d commit 1d5a1f7

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

frontend/src/components/Header/Chat.tsx

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
} from "react-icons/fa";
1919
import { sendAssistantMessage } from "../../api/apiClient";
2020

21-
interface ChatLogItem {
21+
export interface ChatLogItem {
2222
is_user: boolean;
2323
content: string;
2424
timestamp: string; // EX: 2025-01-16T16:21:14.981090Z
@@ -46,6 +46,28 @@ const Chat: React.FC<ChatDropDownProps> = ({ showChat, setShowChat }) => {
4646
const [isLoading, setIsLoading] = useState(false);
4747
const [error, setError] = useState<Error | null>(null);
4848

49+
// Session storage functions for conversation management
50+
const saveConversationToStorage = (messages: ChatLogItem[], responseId?: string) => {
51+
const conversationData = {
52+
messages,
53+
responseId,
54+
timestamp: new Date().toISOString(),
55+
};
56+
sessionStorage.setItem('currentConversation', JSON.stringify(conversationData));
57+
};
58+
59+
const loadConversationFromStorage = () => {
60+
const stored = sessionStorage.getItem('currentConversation');
61+
if (stored) {
62+
try {
63+
return JSON.parse(stored);
64+
} catch (error) {
65+
console.error('Error parsing stored conversation:', error);
66+
}
67+
}
68+
return null;
69+
};
70+
4971
const suggestionPrompts = [
5072
"What are the side effects of Latuda?",
5173
"Why is cariprazine better than valproate for a pregnant patient?",
@@ -58,6 +80,24 @@ const Chat: React.FC<ChatDropDownProps> = ({ showChat, setShowChat }) => {
5880

5981
const [bottom, setBottom] = useState(false);
6082

83+
// Load conversation from sessionStorage on component mount
84+
useEffect(() => {
85+
const storedConversation = loadConversationFromStorage();
86+
if (storedConversation) {
87+
setCurrentMessages(storedConversation.messages || []);
88+
setCurrentResponseId(storedConversation.responseId);
89+
}
90+
}, []);
91+
92+
// Save conversation to sessionStorage when component unmounts
93+
useEffect(() => {
94+
return () => {
95+
if (currentMessages.length > 0) {
96+
saveConversationToStorage(currentMessages, currentResponseId);
97+
}
98+
};
99+
}, [currentMessages, currentResponseId]);
100+
61101
const handleScroll = (event: React.UIEvent<HTMLElement>) => {
62102
const target = event.target as HTMLElement;
63103
const bottom =
@@ -120,6 +160,9 @@ const Chat: React.FC<ChatDropDownProps> = ({ showChat, setShowChat }) => {
120160
// Add user message to current conversation
121161
const updatedMessages = [...currentMessages, newMessage];
122162
setCurrentMessages(updatedMessages);
163+
164+
// Save user message immediately to prevent loss
165+
saveConversationToStorage(updatedMessages, currentResponseId);
123166

124167
// Call assistant API with previous response ID for continuity
125168
const data = await sendAssistantMessage(
@@ -135,8 +178,12 @@ const Chat: React.FC<ChatDropDownProps> = ({ showChat, setShowChat }) => {
135178
};
136179

137180
// Update messages and store new response ID for next message
138-
setCurrentMessages((prev) => [...prev, assistantMessage]);
181+
const finalMessages = [...updatedMessages, assistantMessage];
182+
setCurrentMessages(finalMessages);
139183
setCurrentResponseId(data.final_response_id);
184+
185+
// Save conversation to sessionStorage
186+
saveConversationToStorage(finalMessages, data.final_response_id);
140187
} catch (error) {
141188
console.error("Error handling message:", error);
142189
let errorMessage = "Error submitting message";
@@ -207,6 +254,7 @@ const Chat: React.FC<ChatDropDownProps> = ({ showChat, setShowChat }) => {
207254
onClick={() => {
208255
setCurrentMessages([]);
209256
setCurrentResponseId(undefined);
257+
sessionStorage.removeItem('currentConversation');
210258
}}
211259
className="flex items-center justify-center"
212260
title="New Conversation"

frontend/src/services/actions/auth.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ export const login =
169169
};
170170

171171
export const logout = () => async (dispatch: AppDispatch) => {
172+
// Clear chat conversation data on logout for security
173+
sessionStorage.removeItem('currentConversation');
174+
172175
dispatch({
173176
type: LOGOUT,
174177
});

0 commit comments

Comments
 (0)