11import { useEffect , useState } from "react" ;
2- import { useSelector } from "react-redux" ;
2+ import { useSelector , useDispatch } from "react-redux" ;
33import { Box , Slide , useTheme , IconButton } from "@mui/material" ;
44import { ChevronRight } from "@mui/icons-material" ;
55import IdpDetails from "../../rootApp/security/IdpDetails.ts" ;
6- const DifyChatbot = ( { onChatToggle } ) => {
7- // Check if this is first time after login
8- const [ isPanelOpen , setIsPanelOpen ] = useState ( ( ) => {
9- const hasSeenChatbot = localStorage . getItem ( "avni-chatbot-seen" ) ;
10- return ! hasSeenChatbot ; // Open only if user hasn't seen it before
11- } ) ;
6+ import { setChatOpen } from "../../rootApp/ducks" ;
7+ const DifyChatbot = ( ) => {
8+ const dispatch = useDispatch ( ) ;
129 const theme = useTheme ( ) ;
1310
1411 const [ aiConfig , setAiConfig ] = useState ( null ) ;
1512 const [ configError , setConfigError ] = useState ( false ) ;
1613
17- // Notify parent when panel state changes
18- useEffect ( ( ) => {
19- onChatToggle ?. ( isPanelOpen ) ;
20- } , [ isPanelOpen , onChatToggle ] ) ;
14+ // Get user and organization data from Redux state
15+ const userInfo = useSelector ( ( state ) => state . app ?. userInfo ) ;
16+ const organisation = useSelector ( ( state ) => state . app ?. organisation ) ;
17+ const authSession = useSelector ( ( state ) => state . app ?. authSession ) ;
18+ const isChatOpen = useSelector ( ( state ) => state . app ?. isChatOpen ) ;
2119
2220 // Fetch AI assistant configuration
2321 useEffect ( ( ) => {
@@ -39,11 +37,6 @@ const DifyChatbot = ({ onChatToggle }) => {
3937
4038 fetchAiConfig ( ) ;
4139 } , [ ] ) ;
42-
43- // Get user and organization data from Redux state
44- const userInfo = useSelector ( ( state ) => state . app ?. userInfo ) ;
45- const organisation = useSelector ( ( state ) => state . app ?. organisation ) ;
46- const authSession = useSelector ( ( state ) => state . app ?. authSession ) ;
4740 const authToken = localStorage . getItem ( IdpDetails . AuthTokenName ) ;
4841 const avni_mcp_server_url = aiConfig ?. mcp_server_url || "" ;
4942
@@ -148,7 +141,7 @@ const DifyChatbot = ({ onChatToggle }) => {
148141 z-index: 1201;
149142 box-shadow: ${ theme . shadows [ 4 ] } ;
150143 transition: all 0.3s ease;
151- display: ${ isPanelOpen ? "none" : "flex" } ;
144+ display: ${ isChatOpen ? "none" : "flex" } ;
152145 align-items: center;
153146 justify-content: center;
154147 ` ;
@@ -164,7 +157,7 @@ const DifyChatbot = ({ onChatToggle }) => {
164157 } ;
165158
166159 chatButton . onclick = ( ) => {
167- setIsPanelOpen ( true ) ;
160+ dispatch ( setChatOpen ( true ) ) ;
168161 } ;
169162
170163 document . body . appendChild ( chatButton ) ;
@@ -179,7 +172,7 @@ const DifyChatbot = ({ onChatToggle }) => {
179172 const style = document . getElementById ( "dify-chat-animations" ) ;
180173 if ( style ) style . remove ( ) ;
181174 } ;
182- } , [ isPanelOpen , theme , aiConfig , configError ] ) ;
175+ } , [ isChatOpen , theme , aiConfig , configError , dispatch ] ) ;
183176
184177 // Build chatbot URL with user context
185178 const buildChatUrl = ( ) => {
@@ -214,7 +207,7 @@ const DifyChatbot = ({ onChatToggle }) => {
214207 return (
215208 < >
216209 { /* Embedded Right Panel */ }
217- < Slide direction = "left" in = { isPanelOpen } mountOnEnter unmountOnExit >
210+ < Slide direction = "left" in = { isChatOpen } mountOnEnter unmountOnExit >
218211 < Box
219212 sx = { {
220213 position : "fixed" ,
@@ -233,7 +226,7 @@ const DifyChatbot = ({ onChatToggle }) => {
233226 { /* Close Button */ }
234227 < IconButton
235228 onClick = { ( ) => {
236- setIsPanelOpen ( false ) ;
229+ dispatch ( setChatOpen ( false ) ) ;
237230 // Mark chatbot as seen so it won't auto-open again
238231 localStorage . setItem ( "avni-chatbot-seen" , "true" ) ;
239232 } }
0 commit comments