|
| 1 | +import React, { useState, useEffect } from 'react' |
| 2 | +import Image from 'next/image' |
| 3 | +import { motion, AnimatePresence } from 'framer-motion' |
| 4 | +import { FaHome, FaTrophy, FaDiscord, FaSketch, FaCog, FaPlay, FaGift, FaBook } from 'react-icons/fa' |
| 5 | + |
| 6 | +interface HeaderProps { |
| 7 | + activeSection: string; |
| 8 | + setActiveSection: (section: string) => void; |
| 9 | + user: User | null; |
| 10 | + onLogin: () => void; |
| 11 | +} |
| 12 | + |
| 13 | +interface User { |
| 14 | + steamName: string; |
| 15 | + avatarUrl: string; |
| 16 | +} |
| 17 | + |
| 18 | +export default function Header({ activeSection, setActiveSection, user, onLogin }: HeaderProps) { |
| 19 | + const [isExpanded, setIsExpanded] = useState(false); |
| 20 | + const [isMobile, setIsMobile] = useState(false); |
| 21 | + |
| 22 | + useEffect(() => { |
| 23 | + const handleResize = () => { |
| 24 | + setIsMobile(window.innerWidth < 768); |
| 25 | + }; |
| 26 | + handleResize(); |
| 27 | + window.addEventListener('resize', handleResize); |
| 28 | + return () => window.removeEventListener('resize', handleResize); |
| 29 | + }, []); |
| 30 | + |
| 31 | + const navItems = [ |
| 32 | + { name: 'home', icon: FaPlay, label: 'Play' }, |
| 33 | + { name: 'best', icon: FaTrophy, label: 'Best' }, |
| 34 | + { name: 'skins', icon: () => <Image src="/skins2.svg" alt="Skins" width={22} height={24} />, label: 'Skins', isExternal: true, url: 'https://cs2surf.de' }, |
| 35 | + { name: 'inventory', icon: FaSketch, label: 'Inventory', isExternal: true, url: 'http://inv.cs2surf.pro:3000' }, |
| 36 | + { name: 'giveaway', icon: FaGift, label: 'Giveaway' }, |
| 37 | + ] |
| 38 | + |
| 39 | + const secondaryItems = [ |
| 40 | + { name: 'docs', icon: FaBook, label: 'Docs', isExternal: true, url: 'https://docs.cs2surf.pro' }, |
| 41 | + { name: 'discord', icon: FaDiscord, label: 'Join Discord', isExternal: true, url: 'https://discord.gg/n4xCDWrQRB' }, |
| 42 | + { name: 'settings', icon: FaCog, label: 'Settings' }, |
| 43 | + ] |
| 44 | + |
| 45 | + return ( |
| 46 | + <motion.nav |
| 47 | + className="fixed top-0 left-0 h-screen bg-gray-900 flex flex-col items-start py-4 shadow-lg z-50 overflow-hidden" |
| 48 | + initial={false} |
| 49 | + animate={{ |
| 50 | + width: isMobile |
| 51 | + ? isExpanded ? '100%' : '0px' |
| 52 | + : isExpanded ? 'var(--nav-width-expanded)' : 'var(--nav-width-collapsed)' |
| 53 | + }} |
| 54 | + transition={{ duration: 0.3, ease: 'easeInOut' }} |
| 55 | + onMouseEnter={() => !isMobile && setIsExpanded(true)} |
| 56 | + onMouseLeave={() => !isMobile && setIsExpanded(false)} |
| 57 | + style={{ |
| 58 | + '--nav-width-collapsed': '64px', |
| 59 | + '--nav-width-expanded': '220px', |
| 60 | + } as React.CSSProperties} |
| 61 | + > |
| 62 | + <div className="flex justify-between items-center w-full px-2 mb-6"> |
| 63 | + <div className="ml-1"> |
| 64 | + <Image src="/logo.png" alt="Logo" width={40} height={40} className="rounded-full" /> |
| 65 | + </div> |
| 66 | + {isMobile && ( |
| 67 | + <button |
| 68 | + onClick={() => setIsExpanded(!isExpanded)} |
| 69 | + className="p-2 text-white" |
| 70 | + > |
| 71 | + {isExpanded ? 'Close' : 'Menu'} |
| 72 | + </button> |
| 73 | + )} |
| 74 | + </div> |
| 75 | + <div className="flex flex-col space-y-1 w-full px-2 flex-grow overflow-y-auto"> |
| 76 | + {navItems.map((item) => ( |
| 77 | + <NavItem |
| 78 | + key={item.name} |
| 79 | + item={item} |
| 80 | + isActive={activeSection === item.name} |
| 81 | + isExpanded={isExpanded || isMobile} |
| 82 | + onClick={() => { |
| 83 | + handleItemClick(item, setActiveSection); |
| 84 | + isMobile && setIsExpanded(false); |
| 85 | + }} |
| 86 | + /> |
| 87 | + ))} |
| 88 | + </div> |
| 89 | + <div className="mt-auto mb-4 px-2 w-full space-y-1"> |
| 90 | + {secondaryItems.map((item) => ( |
| 91 | + <NavItem |
| 92 | + key={item.name} |
| 93 | + item={item} |
| 94 | + isActive={activeSection === item.name} |
| 95 | + isExpanded={isExpanded || isMobile} |
| 96 | + onClick={() => { |
| 97 | + handleItemClick(item, setActiveSection); |
| 98 | + isMobile && setIsExpanded(false); |
| 99 | + }} |
| 100 | + /> |
| 101 | + ))} |
| 102 | + </div> |
| 103 | + </motion.nav> |
| 104 | + ) |
| 105 | +} |
| 106 | + |
| 107 | +interface NavItemProps { |
| 108 | + item: { |
| 109 | + name: string; |
| 110 | + icon: React.ElementType; |
| 111 | + label: string; |
| 112 | + isExternal?: boolean; |
| 113 | + url?: string; |
| 114 | + }; |
| 115 | + isActive: boolean; |
| 116 | + isExpanded: boolean; |
| 117 | + onClick: () => void; |
| 118 | +} |
| 119 | + |
| 120 | +function NavItem({ item, isActive, isExpanded, onClick }: NavItemProps) { |
| 121 | + return ( |
| 122 | + <motion.button |
| 123 | + className={`p-2 rounded-lg transition-colors duration-200 ease-in-out flex items-center w-full ${ |
| 124 | + isActive ? 'bg-blue-600 text-white' : 'text-gray-400 hover:bg-gray-800 hover:text-white' |
| 125 | + }`} |
| 126 | + onClick={onClick} |
| 127 | + whileHover={{ scale: 1.03 }} |
| 128 | + whileTap={{ scale: 0.97 }} |
| 129 | + > |
| 130 | + <item.icon className="text-xl min-w-[20px]" /> |
| 131 | + <motion.div |
| 132 | + className="ml-3 overflow-hidden" |
| 133 | + initial={false} |
| 134 | + animate={{ width: isExpanded ? 'auto' : 0 }} |
| 135 | + transition={{ duration: 0.3, ease: 'easeInOut' }} |
| 136 | + > |
| 137 | + <motion.span |
| 138 | + className="text-sm font-medium whitespace-nowrap block" |
| 139 | + initial={false} |
| 140 | + animate={{ opacity: isExpanded ? 1 : 0 }} |
| 141 | + transition={{ duration: 0.2, ease: 'easeInOut' }} |
| 142 | + > |
| 143 | + {item.label} |
| 144 | + </motion.span> |
| 145 | + </motion.div> |
| 146 | + </motion.button> |
| 147 | + ) |
| 148 | +} |
| 149 | + |
| 150 | +function handleItemClick(item: NavItemProps['item'], setActiveSection: (section: string) => void) { |
| 151 | + if (item.isExternal && item.url) { |
| 152 | + window.open(item.url, '_blank') |
| 153 | + } else { |
| 154 | + setActiveSection(item.name) |
| 155 | + } |
| 156 | +} |
0 commit comments