akmhatey-ai commited on
Commit
207064d
·
1 Parent(s): c0a7e8e

fix: clear chat messages on document switch

Browse files
frontend/src/components/chat/ChatPanel.tsx CHANGED
@@ -45,14 +45,26 @@ export default function ChatPanel({ activeDoc, onCitationClick }: Props) {
45
 
46
  // Load history on doc change
47
  useEffect(() => {
48
- if (!activeDoc || activeDoc.id === prevDocId.current) return;
49
- prevDocId.current = activeDoc.id;
 
 
 
 
 
 
 
 
 
 
50
 
51
  api
52
  .get<{ messages: Array<{ id: string; role: string; content: string; sources?: SourceChunk[] }> }>(
53
- `/api/v1/chat/history/${activeDoc.id}`
54
  )
55
  .then((data) => {
 
 
56
  setMessages(
57
  data.messages.map((m) => ({
58
  id: m.id,
@@ -62,7 +74,14 @@ export default function ChatPanel({ activeDoc, onCitationClick }: Props) {
62
  }))
63
  );
64
  })
65
- .catch(() => setMessages([]));
 
 
 
 
 
 
 
66
  }, [activeDoc]);
67
 
68
  const handleSend = async () => {
 
45
 
46
  // Load history on doc change
47
  useEffect(() => {
48
+ if (!activeDoc) {
49
+ prevDocId.current = null;
50
+ setMessages([]);
51
+ return;
52
+ }
53
+
54
+ if (activeDoc.id === prevDocId.current) return;
55
+
56
+ const documentId = activeDoc.id;
57
+ prevDocId.current = documentId;
58
+ setMessages([]);
59
+ let cancelled = false;
60
 
61
  api
62
  .get<{ messages: Array<{ id: string; role: string; content: string; sources?: SourceChunk[] }> }>(
63
+ `/api/v1/chat/history/${documentId}`
64
  )
65
  .then((data) => {
66
+ if (cancelled || prevDocId.current !== documentId) return;
67
+
68
  setMessages(
69
  data.messages.map((m) => ({
70
  id: m.id,
 
74
  }))
75
  );
76
  })
77
+ .catch(() => {
78
+ if (cancelled || prevDocId.current !== documentId) return;
79
+ setMessages([]);
80
+ });
81
+
82
+ return () => {
83
+ cancelled = true;
84
+ };
85
  }, [activeDoc]);
86
 
87
  const handleSend = async () => {