'use client'; import { useRouter } from 'next/navigation'; import { useEffect, useState } from 'react'; import { useAdminStore } from '@/store/adminStore'; import { tokenStorage } from '@/lib/api'; import { Loader2 } from 'lucide-react'; interface AdminHeaderProps { title: string; description?: string; actions?: React.ReactNode; } export function AdminHeader({ title, description, actions }: AdminHeaderProps) { const router = useRouter(); const token = useAdminStore((s) => s.token); const [checking, setChecking] = useState(true); useEffect(() => { // 客户端兜底:如果 store 中没 token 但 localStorage 有,刷新一下 const lsToken = tokenStorage.get(); if (!token && lsToken) { useAdminStore.setState({ token: lsToken, admin: useAdminStore.getState().admin, }); } setChecking(false); }, [token]); useEffect(() => { if (!checking && !token) { router.replace('/admin/login'); } }, [checking, token, router]); if (checking || !token) { return (
正在校验登录状态…
); } return (

{title}

{description && (

{description}

)}
{actions &&
{actions}
}
); }