|
| 1 | +import { useState, useEffect, MouseEvent } from 'react'; |
| 2 | +import { useRouter } from 'next/router' |
| 3 | +import { |
| 4 | + Box, |
| 5 | + AppBar, |
| 6 | + Toolbar, |
| 7 | + Menu, |
| 8 | + MenuItem, |
| 9 | + Divider, |
| 10 | + Typography, |
| 11 | + IconButton, |
| 12 | +} from '@mui/material'; |
| 13 | +import { makeStyles } from '@mui/styles' |
| 14 | +import AddIcon from '@mui/icons-material/Add'; |
| 15 | +import WidgetsIcon from '@mui/icons-material/Widgets'; |
| 16 | +import AccountCircleIcon from '@mui/icons-material/AccountCircle'; |
| 17 | +import { ref, onValue, DataSnapshot } from '@firebase/database'; |
| 18 | + |
| 19 | +import { auth, db } from '@/lib/firebase'; |
| 20 | +import { AddProfileDialog, AddWidgetDialog } from '@/components/admin/Dialog'; |
| 21 | + |
| 22 | +const useStyles = makeStyles((_) => ({ |
| 23 | + title: { |
| 24 | + flexGrow: 1, |
| 25 | + }, |
| 26 | +})); |
| 27 | + |
| 28 | +type NavbarProps = { |
| 29 | + profile?: string; |
| 30 | +} |
| 31 | + |
| 32 | +const Navbar = ({ profile }: NavbarProps) => { |
| 33 | + const classes = useStyles(); |
| 34 | + const router = useRouter(); |
| 35 | + |
| 36 | + const [userAnchorEl, setUserAnchorEl] = useState<HTMLElement | null>(null); |
| 37 | + const [profileAnchorEl, setProfileAnchorEl] = useState<HTMLElement | null>(null); |
| 38 | + const [addProfileDialogOpened, setAddProfileDialogOpened] = useState(false); |
| 39 | + const [addWidgetDialogOpened, setAddWidgetDialogOpened] = useState(false); |
| 40 | + const [profiles, setProfiles] = useState<string[]>([]); |
| 41 | + |
| 42 | + const userMenuId = 'user-menu'; |
| 43 | + const handleUserMenuOpen = (event: MouseEvent<HTMLElement>) => { |
| 44 | + setUserAnchorEl(event.currentTarget); |
| 45 | + }; |
| 46 | + const handleUserMenuClose = () => { |
| 47 | + setUserAnchorEl(null); |
| 48 | + }; |
| 49 | + |
| 50 | + const profileMenuId = 'profile-menu'; |
| 51 | + const handleProfileMenuOpen = (event: MouseEvent<HTMLElement>) => { |
| 52 | + setProfileAnchorEl(event.currentTarget); |
| 53 | + }; |
| 54 | + const handleProfileMenuClose = () => { |
| 55 | + setProfileAnchorEl(null); |
| 56 | + }; |
| 57 | + |
| 58 | + const isUserMenuOpen = Boolean(userAnchorEl); |
| 59 | + const isProfileMenuOpen = Boolean(profileAnchorEl); |
| 60 | + |
| 61 | + const signout = async () => { |
| 62 | + try { |
| 63 | + await auth.signOut(); |
| 64 | + } catch (err) { |
| 65 | + alert(err.message); |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + useEffect(() => { |
| 70 | + const profilesRef = ref(db, `/profiles`); |
| 71 | + onValue(profilesRef, (snap: DataSnapshot) => { |
| 72 | + if (snap?.val()) { |
| 73 | + setProfiles(Object.keys(snap.val())); |
| 74 | + } |
| 75 | + }); |
| 76 | + }, []); |
| 77 | + |
| 78 | + return ( |
| 79 | + <> |
| 80 | + <AppBar position="static"> |
| 81 | + <Toolbar> |
| 82 | + <Typography variant="h6" className={classes.title}> |
| 83 | + Admin |
| 84 | + </Typography> |
| 85 | + {profile && (<Typography variant="h6" className={classes.title}> |
| 86 | + Profile:{' '} |
| 87 | + {profile} |
| 88 | + </Typography>)} |
| 89 | + <Box sx={{ flexGrow: 1 }} /> |
| 90 | + <Box sx={{ display: { xs: 'none', md: 'flex' } }}> |
| 91 | + <IconButton |
| 92 | + size="large" |
| 93 | + color="inherit" |
| 94 | + edge="end" |
| 95 | + aria-controls={profileMenuId} |
| 96 | + aria-haspopup="true" |
| 97 | + aria-expanded={isProfileMenuOpen ? 'true' : undefined} |
| 98 | + onClick={handleProfileMenuOpen} |
| 99 | + > |
| 100 | + <WidgetsIcon /> |
| 101 | + </IconButton> |
| 102 | + <IconButton |
| 103 | + size="large" |
| 104 | + color="inherit" |
| 105 | + onClick={()=>{setAddWidgetDialogOpened(true);}} |
| 106 | + > |
| 107 | + <AddIcon /> |
| 108 | + </IconButton> |
| 109 | + <IconButton |
| 110 | + size="large" |
| 111 | + color="inherit" |
| 112 | + edge="end" |
| 113 | + aria-controls={userMenuId} |
| 114 | + aria-haspopup="true" |
| 115 | + aria-expanded={isUserMenuOpen ? 'true' : undefined} |
| 116 | + onClick={handleUserMenuOpen} |
| 117 | + > |
| 118 | + <AccountCircleIcon /> |
| 119 | + </IconButton> |
| 120 | + </Box> |
| 121 | + </Toolbar> |
| 122 | + </AppBar> |
| 123 | + <Menu |
| 124 | + id={profileMenuId} |
| 125 | + anchorEl={profileAnchorEl} |
| 126 | + open={isProfileMenuOpen} |
| 127 | + onClose={handleProfileMenuClose} |
| 128 | + > |
| 129 | + {profiles.map((profile) => ( |
| 130 | + <MenuItem key={profile} color="inherit" onClick={() => { router.push(`/admin/${profile}`); }}>{profile}</MenuItem> |
| 131 | + ))} |
| 132 | + <Divider /> |
| 133 | + <MenuItem color="inherit" onClick={() => { setAddProfileDialogOpened(true);}}>Add</MenuItem> |
| 134 | + </Menu> |
| 135 | + <Menu |
| 136 | + id={userMenuId} |
| 137 | + anchorEl={userAnchorEl} |
| 138 | + open={isUserMenuOpen} |
| 139 | + onClose={handleUserMenuClose} |
| 140 | + > |
| 141 | + <MenuItem color="inherit" onClick={signout}>Logout</MenuItem> |
| 142 | + </Menu> |
| 143 | + <AddProfileDialog |
| 144 | + open={addProfileDialogOpened} |
| 145 | + onClose={() => { |
| 146 | + setAddProfileDialogOpened(false); |
| 147 | + }} |
| 148 | + /> |
| 149 | + {profile && (<AddWidgetDialog |
| 150 | + profile={profile} |
| 151 | + open={addWidgetDialogOpened} |
| 152 | + onClose={() => { |
| 153 | + setAddWidgetDialogOpened(false); |
| 154 | + }} |
| 155 | + />)} |
| 156 | + </> |
| 157 | + ); |
| 158 | +}; |
| 159 | + |
| 160 | +export { Navbar }; |
| 161 | +export type { NavbarProps }; |
0 commit comments