import { useEffect, useRef, useState, useSyncExternalStore } from "react"
import { NavLink, useNavigate } from "react-router-dom"
import { logout } from "@/api/auth"
import Wrap from "@/components/wrap"
import { cn } from "@/lib/utils"
function ProfileOrLogin() {
const [authed, setAuthed] = useState(
() => !!localStorage.getItem("auth_token"),
)
const navigate = useNavigate()
useEffect(() => {
const onFocus = () => setAuthed(!!localStorage.getItem("auth_token"))
window.addEventListener("focus", onFocus)
return () => window.removeEventListener("focus", onFocus)
}, [])
const handleLogout = async () => {
await logout()
}
if (authed) {
return (
)
}
return (
登录
注册
)
}
export default function Header() {
const scroll = useSyncExternalStore(
callback => {
window.addEventListener("scroll", callback)
return () => {
window.removeEventListener("scroll", callback)
}
},
() => window.scrollY > 48,
() => false,
)
// ======================
// 菜单状态
// ======================
const [openMenu, setOpenMenu] = useState(false)
const handleEnter = () => setOpenMenu(true)
const handleLeave = () => setOpenMenu(false)
return (
)
}
const NavItem = ({
to,
children,
}: {
to: string
children: React.ReactNode
}) => (
{children}
)
const MenuItem = ({
text,
children,
isOpen,
onMouseEnter,
onMouseLeave,
}: {
text: string
children: React.ReactNode
isOpen: boolean
onMouseEnter: () => void
onMouseLeave: () => void
}) => {
const timeoutRef = useRef(null)
const handleMouseEnter = () => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current)
timeoutRef.current = null
}
onMouseEnter()
}
const handleMouseLeave = () => {
timeoutRef.current = setTimeout(() => {
onMouseLeave()
}, 150)
}
return (
)
}
const ProductDropdown = ({ onNavigate }: { onNavigate: () => void }) => {
const navigate = useNavigate()
const handleClick = (path: string) => {
navigate(path)
onNavigate()
}
return (
handleClick("/product?tab=short")}
/>
window.open("https://lanhuip.com", "_blank")}
/>
handleClick("/product/http")}
/>
handleClick("/product/routeros")}
/>
window.open("http://vps.juip.com", "_blank")}
/>
热门推荐:
|
)
}
const ProductCard = ({
title,
desc,
tag,
onClick,
}: {
title: string
desc: string
tag?: string
onClick: () => void
}) => (
)