import { useState } from 'react' import { useTranslation } from 'react-i18next' import { AlertTriangle, X } from 'lucide-react' import { Button } from '@/components/ui/button' /** * HTTP 警告横幅组件 * 当用户通过 HTTP 访问时显示安全警告 */ export function HttpWarningBanner() { const { t } = useTranslation() // 直接计算初始状态,避免 effect 中调用 setState const isHttp = window.location.protocol === 'http:' const hostname = window.location.hostname.toLowerCase() const isLocalhost = hostname === 'localhost' || hostname === '127.0.0.1' || hostname === '::1' const dismissed = sessionStorage.getItem('http-warning-dismissed') === 'true' // 本地访问(localhost/127.0.0.1)不显示警告 const [isVisible, setIsVisible] = useState(isHttp && !isLocalhost && !dismissed) const [isDismissed, setIsDismissed] = useState(false) const handleDismiss = () => { setIsDismissed(true) setIsVisible(false) sessionStorage.setItem('http-warning-dismissed', 'true') } if (!isVisible || isDismissed) { return null } return (
{t('httpWarning.title')} {t('httpWarning.message')}
{t('httpWarning.description')}