import { useGlobal } from '@/lib/global' import { useEffect, useState } from 'react' /** * @see https://css-loaders.com/ * @returns 加载动画 */ export default function LoadingCover() { const { onLoading, setOnLoading } = useGlobal() const [isVisible, setIsVisible] = useState(false) // 初始状态设置为false,避免服务端渲染与客户端渲染不一致 useEffect(() => { // 确保在客户端渲染时才设置可见性 if (onLoading) { setIsVisible(true) } else { const timeout = setTimeout(() => setIsVisible(false), 1800) // 等待淡出动画结束 return () => clearTimeout(timeout) } }, [onLoading]) const handleClick = () => { setOnLoading(false) // 强行关闭 LoadingCover } if (typeof window === 'undefined') { return null // 避免在服务端渲染时渲染出这个组件 } return isVisible ? (