external.tsx 4.27 KB
import { FC, useState } from 'react'
import { InputMessageType } from '../../../chat/types'
import { useChatContext } from '@/app/components/base/chat/chat-with-history/context/chat-context'
import type { LinkProps } from './index'
import { Dialog, DialogPanel, DialogTitle } from '@headlessui/react'
import Copy from '@/app/components/base/chat/chat/answer/components/operation/copy'
import CopyBtn from '@/app/components/base/copy-btn'
const ExternalLink: FC<LinkProps> = ({ node, children }) => {
  const [isOpen, setIsOpen] = useState(false)
  const [isLoading, setIsLoading] = useState(true)
  const [iframeError, setIframeError] = useState(false)
  const handleClick = (e: React.MouseEvent) => {
    e.preventDefault()
    setIsOpen(true)
    setIsLoading(true)
    setIframeError(false)
  }

  return (
    <>
      <a
        href={node.properties.href}
        onClick={handleClick}
        className="cursor-pointer underline !decoration-primary-700 decoration-dashed">
        {children || 'Open'}
      </a>

      <Dialog open={isOpen} onClose={() => setIsOpen(false)} className="relative z-50">
        <div className="fixed inset-0 bg-black/30" aria-hidden="true" />

        <div className="fixed inset-0 flex items-center justify-center h-full">
          <DialogPanel className="w-full  h-full  backdrop-blur-sm rounded-lg  flex flex-col">
            <div className="flex bg-white h-8 border-b border-gray-200">
              {/* 左侧:自适应宽度区域 */}
              <div className="flex-1 flex items-center overflow-hidden">
                <span className="text-sm text-gray-500 truncate">{node.properties.href}</span>
                <CopyBtn value={node.properties.href} className="ml-2" />
              </div>

              {/* 右侧:按钮组固定宽度 */}
              <div className="flex items-center gap-3">
                <button
                  onClick={() => window.open(node.properties.href, '_blank')}
                  className="px-2 py-1 text-xs bg-primary-500 text-white rounded hover:bg-primary-600">
                  新窗口打开
                </button>
                <button
                  onClick={() => setIsOpen(false)}
                  className="p-1 rounded-full bg-gray-400 hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-primary-500">
                  <span className="sr-only">关闭</span>
                  <svg
                    xmlns="http://www.w3.org/2000/svg"
                    viewBox="0 0 20 20"
                    fill="currentColor"
                    className="w-5 h-5 text-blue-500-600 hover:text-white">
                    <path d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z" />
                  </svg>
                </button>
              </div>
            </div>
            <div className="flex-1 relative flex items-center justify-center p-4 pt-2">
              {isLoading && (
                <div className="absolute inset-0 flex items-center justify-center">
                  <div className="animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-primary-500" />
                </div>
              )}

              {iframeError ? (
                <div className="absolute inset-0 flex items-center justify-center flex-col gap-2">
                  <p className="text-red-400">该页面不支持内嵌显示</p>
                  <button
                    onClick={() => window.open(node.properties.href, '_blank')}
                    className="px-4 py-2 bg-primary-500 text-white rounded hover:bg-primary-600">
                    在浏览器中打开
                  </button>
                </div>
              ) : (
                <iframe
                  src={node.properties.href}
                  className="w-full  max-w-[800px] h-full  bg-white border-0 mx-auto"
                  allow="fullscreen"
                  loading="lazy"
                  onLoad={() => setIsLoading(false)}
                  onError={() => {
                    setIsLoading(false)
                    setIframeError(true)
                  }}
                />
              )}
            </div>
          </DialogPanel>
        </div>
      </Dialog>
    </>
  )
}
export default ExternalLink