hongjli
2025-04-10 c1c456128bec733c46ad03a09a5bae4c79e02367
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"use client";
 
import { useEffect, useRef } from 'react';
 
interface ChatDialogProps {
  isOpen: boolean;
  onClose: () => void;
  chatbotId: string;
}
 
const ChatDialog = ({ isOpen, onClose, chatbotId }: ChatDialogProps) => {
  const dialogRef = useRef<HTMLDivElement>(null);
  const iframeContainerRef = useRef<HTMLDivElement>(null);
 
  useEffect(() => {
    // 每次 chatbotId 改变时更新 iframe
    if (iframeContainerRef.current) {
      // 清除现有的 iframe
      iframeContainerRef.current.innerHTML = '';
      
      // 创建新的 iframe
      const iframe = document.createElement('iframe');
      iframe.src = `http://121.43.139.99/chatbot/${chatbotId}`;
      iframe.style.width = '100%';
      iframe.style.height = '100%';
      iframe.style.minHeight = '500px';
      iframe.style.border = 'none';
      iframe.allow = "microphone";
      iframeContainerRef.current.appendChild(iframe);
    }
  }, [chatbotId]);
 
  useEffect(() => {
    const handleClickOutside = (event: MouseEvent) => {
      if (dialogRef.current && !dialogRef.current.contains(event.target as Node)) {
        onClose();
      }
    };
 
    if (isOpen) {
      document.addEventListener('mousedown', handleClickOutside);
    }
 
    return () => {
      document.removeEventListener('mousedown', handleClickOutside);
    };
  }, [isOpen, onClose]);
 
  // 使用 CSS 来控制显示/隐藏,而不是移除 DOM
  return (
    <div className={`fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 transition-opacity duration-300 ${isOpen ? 'opacity-100 visible' : 'opacity-0 invisible'}`}>
      <div ref={dialogRef} className="bg-white rounded-lg w-[90%] h-[90%] max-w-6xl relative">
        <button
          onClick={onClose}
          className="absolute top-4 right-4 text-gray-500 hover:text-gray-700"
        >
          <svg className="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
          </svg>
        </button>
        <div ref={iframeContainerRef} className="w-full h-full p-4" />
      </div>
    </div>
  );
};
 
export default ChatDialog;