Spaces:
Running

axeronis commited on
Commit
5e6a8ea
·
1 Parent(s): 6f659d7

fix: finalize AI toggle stability by using explicit boolean checks and guard clauses

Browse files
Files changed (1) hide show
  1. frontend/components/ChatWindow.tsx +22 -6
frontend/components/ChatWindow.tsx CHANGED
@@ -90,7 +90,16 @@ 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>(lead ? !lead.human_takeover : !conversation?.human_takeover)
 
 
 
 
 
 
 
 
 
94
  const [isBookingModalOpen, setIsBookingModalOpen] = useState(false)
95
  const [isTraining, setIsTraining] = useState(false)
96
  const [isTrainConfirmOpen, setIsTrainConfirmOpen] = useState(false)
@@ -100,11 +109,18 @@ export function ChatWindow({
100
 
101
  // Sync local state with props
102
  useEffect(() => {
103
- // Priority: Lead has the latest mode from the list, Conversation has it from the details
104
- // Only update if we definitely have a change
105
- const nextState = conversation ? !conversation.human_takeover : (lead ? !lead.human_takeover : true)
106
- if (nextState !== localAIActive) {
107
- setLocalAIActive(nextState)
 
 
 
 
 
 
 
108
  }
109
  }, [conversation?.human_takeover, lead?.human_takeover])
110
 
 
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') {
96
+ return !lead.human_takeover
97
+ }
98
+ if (conversation && typeof conversation.human_takeover === 'boolean') {
99
+ return !conversation.human_takeover
100
+ }
101
+ return true // Default to ON if no data yet
102
+ })
103
  const [isBookingModalOpen, setIsBookingModalOpen] = useState(false)
104
  const [isTraining, setIsTraining] = useState(false)
105
  const [isTrainConfirmOpen, setIsTrainConfirmOpen] = useState(false)
 
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
+ if (conversation && typeof conversation.human_takeover === 'boolean') {
117
+ nextValue = !conversation.human_takeover
118
+ } else if (lead && typeof lead.human_takeover === 'boolean') {
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