File size: 1,000 Bytes
e2eff86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import React, { useState, useEffect } from 'react';
import { ChatbotProvider } from '@site/src/contexts/ChatbotContext';
import FloatingChatIcon from '@site/src/components/Chatbot/FloatingChatIcon';
import ChatbotInterface from '@site/src/components/Chatbot/ChatbotInterface';

// Global state provider component
const Root = ({ children }) => {
  const [showChatbot, setShowChatbot] = useState(false);
  const [isClient, setIsClient] = useState(false);

  useEffect(() => {
    // Set isClient to true after component mounts on client
    setIsClient(true);
  }, []);

  const toggleChatbot = () => {
    setShowChatbot(!showChatbot);
  };

  return (
    <ChatbotProvider>
      {children}
      {isClient && (
        <>
          <FloatingChatIcon onClick={toggleChatbot} isOpen={showChatbot} />
          <ChatbotInterface
            isVisible={showChatbot}
            onClose={() => setShowChatbot(false)}
          />
        </>
      )}
    </ChatbotProvider>
  );
};

export default Root;