Header.jsx 2.81 KB
import React from 'react';
import { Link, useNavigate } from 'react-router-dom';
import { useAuth } from '../../hooks/useAuth';  // 修正路径
import { useCart } from '../../hooks/useCart';  // 修正路径


const Header = () => {
  const { user, logout, isAuthenticated } = useAuth();
  const { getCartItemsCount } = useCart();
  const navigate = useNavigate();

  const handleLogout = () => {
    logout();
    navigate('/');
  };

  return (
    <header style={headerStyle}>
      <div style={containerStyle}>
        <Link to="/" style={logoStyle}>
          <h1 style={logoTitleStyle}>普罗米修甄选</h1>
        </Link>
        
        <nav style={navStyle}>
          <Link to="/" style={linkStyle}>首页</Link>
          <Link to="/products" style={linkStyle}>产品</Link>
          
          {isAuthenticated ? (
            <>
              <Link to="/cart" style={linkStyle}>
                购物车 ({getCartItemsCount()})
              </Link>
              <Link to="/profile" style={linkStyle}>个人中心</Link>
              <Link to="/orders" style={linkStyle}>我的订单</Link>
              <button onClick={handleLogout} style={logoutButtonStyle}>
                退出
              </button>
            </>
          ) : (
            <>
              <Link to="/login" style={linkStyle}>登录</Link>
              <Link to="/register" style={linkStyle}>注册</Link>
            </>
          )}
        </nav>
      </div>
    </header>
  );
};

const headerStyle = {
  backgroundColor: '#ff5722', // 橙色红色
  padding: '0.5rem 0',
  color: 'white',
  boxShadow: '0 2px 10px rgba(255, 87, 34, 0.3)',
  position: 'sticky',
  top: 0,
  zIndex: 1000,
  borderBottom: '2px solid #ff8a65',
};

const containerStyle = {
  maxWidth: '1200px',
  margin: '0 auto',
  padding: '0 1rem',
  display: 'flex',
  justifyContent: 'space-between',
  alignItems: 'center',
};

const logoStyle = {
  color: 'white',
  textDecoration: 'none',
};

const logoTitleStyle = {
  fontSize: '1.25rem',
  fontWeight: 'bold',
  margin: 0,
};

const navStyle = {
  display: 'flex',
  alignItems: 'center',
  gap: '1rem',
};

const linkStyle = {
  color: 'white',
  textDecoration: 'none',
  padding: '0.4rem 0.8rem',
  borderRadius: '4px',
  transition: 'all 0.3s ease',
  fontSize: '0.9rem',
};

const logoutButtonStyle = {
  backgroundColor: 'transparent',
  color: 'white',
  border: '1px solid white',
  padding: '0.4rem 0.8rem',
  borderRadius: '4px',
  cursor: 'pointer',
  transition: 'all 0.3s ease',
  fontSize: '0.9rem',
};

// 添加悬停效果
Object.assign(linkStyle, {
  ':hover': {
    backgroundColor: 'rgba(255, 255, 255, 0.2)',
    transform: 'translateY(-1px)',
  }
});

Object.assign(logoutButtonStyle, {
  ':hover': {
    backgroundColor: 'rgba(255, 255, 255, 0.2)',
    transform: 'translateY(-1px)',
  }
});

export default Header;