Spaces:
Running
Running
axeronis commited on
Commit ·
44051f4
1
Parent(s): fc9d557
fix: eliminate toggle jump by clearing stale conversation state and adding leadID match guard
Browse files
frontend/app/chats/page.tsx
CHANGED
|
@@ -45,6 +45,8 @@ function ChatsContent() {
|
|
| 45 |
// Fetch Conversation
|
| 46 |
const fetchConversation = async (leadId: string) => {
|
| 47 |
setMessagesLoading(true)
|
|
|
|
|
|
|
| 48 |
try {
|
| 49 |
const data = await leadsApi.getConversation(leadId)
|
| 50 |
setMessages(data.messages || [])
|
|
|
|
| 45 |
// Fetch Conversation
|
| 46 |
const fetchConversation = async (leadId: string) => {
|
| 47 |
setMessagesLoading(true)
|
| 48 |
+
setConversation(null) // Clear immediately to prevent stale data jump
|
| 49 |
+
setMessages([])
|
| 50 |
try {
|
| 51 |
const data = await leadsApi.getConversation(leadId)
|
| 52 |
setMessages(data.messages || [])
|
frontend/components/ChatWindow.tsx
CHANGED
|
@@ -107,25 +107,28 @@ export function ChatWindow({
|
|
| 107 |
|
| 108 |
const messagesEndRef = useRef<HTMLDivElement>(null)
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
// Sync local state with props
|
| 111 |
useEffect(() => {
|
| 112 |
// Determine the next state based on available data
|
| 113 |
-
// We only update if we have a concrete boolean value to avoid flickering/jumping
|
| 114 |
let nextValue: boolean | undefined = undefined
|
| 115 |
|
| 116 |
-
|
|
|
|
| 117 |
nextValue = !conversation.human_takeover
|
| 118 |
-
}
|
|
|
|
|
|
|
| 119 |
nextValue = !lead.human_takeover
|
| 120 |
}
|
| 121 |
|
| 122 |
if (nextValue !== undefined && nextValue !== localAIActive) {
|
| 123 |
setLocalAIActive(nextValue)
|
| 124 |
}
|
| 125 |
-
}, [conversation?.human_takeover, lead?.human_takeover])
|
| 126 |
-
|
| 127 |
-
// Computed values
|
| 128 |
-
const isAIActive = localAIActive
|
| 129 |
const allMessages = [...messages, ...optimisticMessages, ...realtimeMessages]
|
| 130 |
.filter((msg, index, self) => index === self.findIndex((t) => t.id === msg.id))
|
| 131 |
.sort((a, b) => new Date(a.created_at).getTime() - new Date(b.created_at).getTime())
|
|
|
|
| 107 |
|
| 108 |
const messagesEndRef = useRef<HTMLDivElement>(null)
|
| 109 |
|
| 110 |
+
// Computed values
|
| 111 |
+
const isConversationStale = conversation && conversation.lead_id !== leadId
|
| 112 |
+
const isAIActive = localAIActive
|
| 113 |
+
|
| 114 |
// Sync local state with props
|
| 115 |
useEffect(() => {
|
| 116 |
// Determine the next state based on available data
|
|
|
|
| 117 |
let nextValue: boolean | undefined = undefined
|
| 118 |
|
| 119 |
+
// 1. If we have a fresh conversation (matching current lead), it's the ultimate truth
|
| 120 |
+
if (conversation && !isConversationStale && typeof conversation.human_takeover === 'boolean') {
|
| 121 |
nextValue = !conversation.human_takeover
|
| 122 |
+
}
|
| 123 |
+
// 2. If no fresh conversation yet, use lead data (which we now pass from the list)
|
| 124 |
+
else if (lead && typeof lead.human_takeover === 'boolean') {
|
| 125 |
nextValue = !lead.human_takeover
|
| 126 |
}
|
| 127 |
|
| 128 |
if (nextValue !== undefined && nextValue !== localAIActive) {
|
| 129 |
setLocalAIActive(nextValue)
|
| 130 |
}
|
| 131 |
+
}, [conversation?.human_takeover, lead?.human_takeover, isConversationStale])
|
|
|
|
|
|
|
|
|
|
| 132 |
const allMessages = [...messages, ...optimisticMessages, ...realtimeMessages]
|
| 133 |
.filter((msg, index, self) => index === self.findIndex((t) => t.id === msg.id))
|
| 134 |
.sort((a, b) => new Date(a.created_at).getTime() - new Date(b.created_at).getTime())
|