Spaces:
Running
Running
axeronis commited on
Commit ·
00a9ef7
1
Parent(s): 0efd976
fix: eliminate AI toggle ping-pong by adding a 3-second lockout guard for background updates
Browse files
frontend/components/ChatWindow.tsx
CHANGED
|
@@ -90,6 +90,7 @@ export function ChatWindow({
|
|
| 90 |
const [realtimeMessages, setRealtimeMessages] = useState<Message[]>([])
|
| 91 |
const [isMobileDrawerOpen, setIsMobileDrawerOpen] = useState(false)
|
| 92 |
const [isTogglingAI, setIsTogglingAI] = useState(false)
|
|
|
|
| 93 |
const [localAIActive, setLocalAIActive] = useState<boolean>(() => {
|
| 94 |
// Initial state priority: Lead has the latest mode status from the list
|
| 95 |
if (lead && typeof lead.human_takeover === 'boolean') {
|
|
@@ -113,9 +114,14 @@ export function ChatWindow({
|
|
| 113 |
|
| 114 |
// Sync local state with props
|
| 115 |
useEffect(() => {
|
| 116 |
-
// GUARD: If we are currently toggling, ignore incoming prop updates
|
| 117 |
if (isTogglingAI) return
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
// Determine the next state based on available data
|
| 120 |
let nextValue: boolean | undefined = undefined
|
| 121 |
|
|
@@ -226,7 +232,8 @@ export function ChatWindow({
|
|
| 226 |
const prevValue = localAIActive
|
| 227 |
const newValue = !prevValue
|
| 228 |
|
| 229 |
-
// 1. Optimistic Update
|
|
|
|
| 230 |
setLocalAIActive(newValue)
|
| 231 |
setIsTogglingAI(true)
|
| 232 |
|
|
|
|
| 90 |
const [realtimeMessages, setRealtimeMessages] = useState<Message[]>([])
|
| 91 |
const [isMobileDrawerOpen, setIsMobileDrawerOpen] = useState(false)
|
| 92 |
const [isTogglingAI, setIsTogglingAI] = useState(false)
|
| 93 |
+
const lastInteractionTime = useRef<number>(0)
|
| 94 |
const [localAIActive, setLocalAIActive] = useState<boolean>(() => {
|
| 95 |
// Initial state priority: Lead has the latest mode status from the list
|
| 96 |
if (lead && typeof lead.human_takeover === 'boolean') {
|
|
|
|
| 114 |
|
| 115 |
// Sync local state with props
|
| 116 |
useEffect(() => {
|
| 117 |
+
// GUARD 1: If we are currently toggling, ignore incoming prop updates
|
| 118 |
if (isTogglingAI) return
|
| 119 |
|
| 120 |
+
// GUARD 2: Temporal lockout. If the user interacted in the last 3s,
|
| 121 |
+
// ignore server updates to allow DB/Cache propagation to settle.
|
| 122 |
+
// This solves the ON -> OFF -> ON -> OFF "ping-pong" effect.
|
| 123 |
+
if (Date.now() - lastInteractionTime.current < 3000) return
|
| 124 |
+
|
| 125 |
// Determine the next state based on available data
|
| 126 |
let nextValue: boolean | undefined = undefined
|
| 127 |
|
|
|
|
| 232 |
const prevValue = localAIActive
|
| 233 |
const newValue = !prevValue
|
| 234 |
|
| 235 |
+
// 1. Optimistic Update & Lockout
|
| 236 |
+
lastInteractionTime.current = Date.now()
|
| 237 |
setLocalAIActive(newValue)
|
| 238 |
setIsTogglingAI(true)
|
| 239 |
|