import React, { useEffect, useState } from "react"; import AppBar from "@mui/material/AppBar"; import Toolbar from "@mui/material/Toolbar"; import Typography from "@mui/material/Typography"; import Button from "@mui/material/Button"; import Box from "@mui/material/Box"; import Menu from "@mui/material/Menu"; import MenuItem from "@mui/material/MenuItem"; import IconButton from "@mui/material/IconButton"; import Drawer from "@mui/material/Drawer"; import List from "@mui/material/List"; import ListItem from "@mui/material/ListItem"; import ListItemButton from "@mui/material/ListItemButton"; import ListItemIcon from "@mui/material/ListItemIcon"; import ListItemText from "@mui/material/ListItemText"; import Divider from "@mui/material/Divider"; import Collapse from "@mui/material/Collapse"; import KeyboardArrowDownIcon from '@mui/icons-material/KeyboardArrowDown'; import MenuIcon from '@mui/icons-material/Menu'; import CloseIcon from '@mui/icons-material/Close'; import BuildIcon from '@mui/icons-material/Build'; import ViewListIcon from '@mui/icons-material/ViewList'; import ExpandLess from '@mui/icons-material/ExpandLess'; import ExpandMore from '@mui/icons-material/ExpandMore'; import PersonIcon from '@mui/icons-material/Person'; import LogoutIcon from '@mui/icons-material/Logout'; import LoginIcon from '@mui/icons-material/Login'; import PersonAddIcon from '@mui/icons-material/PersonAdd'; import { Dialog, DialogTitle, DialogContent, DialogContentText, DialogActions } from "@mui/material"; import MemoryIcon from '@mui/icons-material/Memory'; import DeveloperBoardIcon from '@mui/icons-material/DeveloperBoard'; import StorageIcon from '@mui/icons-material/Storage'; import SdStorageIcon from '@mui/icons-material/SdStorage'; import RouterIcon from '@mui/icons-material/Router'; import LanIcon from '@mui/icons-material/Lan'; import SpeakerIcon from '@mui/icons-material/Speaker'; import AlbumIcon from '@mui/icons-material/Album'; import SdCardIcon from '@mui/icons-material/SdCard'; import CableIcon from '@mui/icons-material/Cable'; import LogoUrl from '../assets/projectlogo.png'; import { onGetAuthState } from "../pages/+Layout.telefunc"; import ComponentDialog from "./ComponentDialog"; type AuthState = { isLoggedIn: boolean; username: string | null; isAdmin?: boolean }; const COMPONENT_CATEGORIES = [ { id: 'cpu', label: 'Processors', icon: }, { id: 'gpu', label: 'Graphics Cards', icon: }, { id: 'motherboard', label: 'Motherboards', icon: }, { id: 'memory', label: 'Memory (RAM)', icon: }, { id: 'storage', label: 'Storage', icon: }, { id: 'case', label: 'Cases', icon: }, { id: 'power_supply', label: 'Power Supplies', icon: }, { id: 'cooler', label: 'Cooling', icon: }, { id: 'network_adapter', label: 'Network Adapters (WiFi)', icon: }, { id: 'network_card', label: 'Network Cards (Ethernet)', icon: }, { id: 'sound_card', label: 'Sound Cards', icon: }, { id: 'optical_drive', label: 'Optical Drives', icon: }, { id: 'memory_card', label: 'Storage Cards', icon: }, { id: 'cables', label: 'Cables', icon: }, ]; export default function Navbar() { const [auth, setAuth] = useState(null); const [openLogoutDialog, setOpenLogoutDialog] = useState(false); const [mobileDrawerOpen, setMobileDrawerOpen] = useState(false); const [mobileComponentsOpen, setMobileComponentsOpen] = useState(false); const [anchorEl, setAnchorEl] = useState(null); const openMenu = Boolean(anchorEl); const [browserOpen, setBrowserOpen] = useState(false); const [selectedCategory, setSelectedCategory] = useState(null); const handleComponentsClick = (event: React.MouseEvent) => { setAnchorEl(event.currentTarget); }; const handleMenuClose = () => { setAnchorEl(null); }; const handleCategorySelect = (categoryId: string) => { setSelectedCategory(categoryId); setBrowserOpen(true); handleMenuClose(); setMobileDrawerOpen(false); setMobileComponentsOpen(false); }; const handleLogoutClick = (e: React.MouseEvent) => { e.preventDefault(); setOpenLogoutDialog(true); setMobileDrawerOpen(false); }; const confirmLogout = async () => { setAuth({ isLoggedIn: false, username: null, isAdmin: false }); setOpenLogoutDialog(false); const csrfRes = await fetch("/api/auth/csrf"); const { csrfToken } = await csrfRes.json(); await fetch("/api/auth/signout", { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" }, body: new URLSearchParams({ csrfToken: csrfToken, callbackUrl: "/" }), }); window.location.href = "/"; }; useEffect(() => { let active = true; onGetAuthState() .then((data) => active && setAuth({ isLoggedIn: data.isLoggedIn, username: data.username, isAdmin: data.isAdmin })) .catch(() => active && setAuth({ isLoggedIn: false, username: null, isAdmin: false })); return () => { active = false; }; }, []); const checkDashboardUrl = auth?.isAdmin ? '/dashboard/admin' : '/dashboard/user'; const onHoverNav = { color: 'inherit', '&:hover': { backgroundColor: '#ff8201', color: 'white', fontWeight: 'bold' } }; return ( <> window.location.href='/'} /> PC Forge {COMPONENT_CATEGORIES.map((cat) => ( handleCategorySelect(cat.id)}> {cat.icon} {cat.label} ))} {auth?.isLoggedIn ? ( <> ) : ( <> )} setMobileDrawerOpen(true)} sx={{ display: { xs: 'block', md: 'none' } }} > setMobileDrawerOpen(false)} sx={{ display: { xs: 'block', md: 'none' }, '& .MuiDrawer-paper': { width: 280 , height: '60%'} }} > Menu setMobileDrawerOpen(false)}> { window.location.href = '/forge'; }}> setMobileComponentsOpen(!mobileComponentsOpen)}> {mobileComponentsOpen ? : } {COMPONENT_CATEGORIES.map((cat) => ( handleCategorySelect(cat.id)} > {cat.icon} ))} { window.location.href = '/completed-builds'; }}> {auth?.isLoggedIn ? ( <> { window.location.href = checkDashboardUrl; }}> ) : ( <> { window.location.href = '/auth/login'; }}> { window.location.href = '/auth/register'; }}> )} setOpenLogoutDialog(false)}> Confirm Logout Are you sure you want to leave the Forge? setBrowserOpen(false)} /> ); }