|
| 1 | +import { makeStyles } from "@material-ui/core/styles" |
| 2 | +import VpnKeyOutlined from "@material-ui/icons/VpnKeyOutlined" |
| 3 | +import { User } from "api/typesGenerated" |
| 4 | +import { Stack } from "components/Stack/Stack" |
| 5 | +import { UserAvatar } from "components/UserAvatar/UserAvatar" |
| 6 | +import { FC, ElementType, PropsWithChildren, ReactNode } from "react" |
| 7 | +import { NavLink } from "react-router-dom" |
| 8 | +import { combineClasses } from "util/combineClasses" |
| 9 | +import AccountIcon from "@material-ui/icons/Person" |
| 10 | +import SecurityIcon from "@material-ui/icons/LockOutlined" |
| 11 | + |
| 12 | +const SidebarNavItem: FC< |
| 13 | + PropsWithChildren<{ href: string; icon: ReactNode }> |
| 14 | +> = ({ children, href, icon }) => { |
| 15 | + const styles = useStyles() |
| 16 | + return ( |
| 17 | + <NavLink |
| 18 | + to={href} |
| 19 | + className={({ isActive }) => |
| 20 | + combineClasses([ |
| 21 | + styles.sidebarNavItem, |
| 22 | + isActive ? styles.sidebarNavItemActive : undefined, |
| 23 | + ]) |
| 24 | + } |
| 25 | + > |
| 26 | + <Stack alignItems="center" spacing={1.5} direction="row"> |
| 27 | + {icon} |
| 28 | + {children} |
| 29 | + </Stack> |
| 30 | + </NavLink> |
| 31 | + ) |
| 32 | +} |
| 33 | + |
| 34 | +const SidebarNavItemIcon: React.FC<{ icon: ElementType }> = ({ |
| 35 | + icon: Icon, |
| 36 | +}) => { |
| 37 | + const styles = useStyles() |
| 38 | + return <Icon className={styles.sidebarNavItemIcon} /> |
| 39 | +} |
| 40 | + |
| 41 | +export const Sidebar: React.FC<{ user: User }> = ({ user }) => { |
| 42 | + const styles = useStyles() |
| 43 | + |
| 44 | + return ( |
| 45 | + <nav className={styles.sidebar}> |
| 46 | + <Stack direction="row" alignItems="center" className={styles.userInfo}> |
| 47 | + <UserAvatar username={user.username} avatarURL={user.avatar_url} /> |
| 48 | + <Stack spacing={0} className={styles.userData}> |
| 49 | + <span className={styles.username}>{user.username}</span> |
| 50 | + <span className={styles.email}>{user.email}</span> |
| 51 | + </Stack> |
| 52 | + </Stack> |
| 53 | + |
| 54 | + <SidebarNavItem |
| 55 | + href="../account" |
| 56 | + icon={<SidebarNavItemIcon icon={AccountIcon} />} |
| 57 | + > |
| 58 | + Account |
| 59 | + </SidebarNavItem> |
| 60 | + <SidebarNavItem |
| 61 | + href="../security" |
| 62 | + icon={<SidebarNavItemIcon icon={SecurityIcon} />} |
| 63 | + > |
| 64 | + Security |
| 65 | + </SidebarNavItem> |
| 66 | + <SidebarNavItem |
| 67 | + href="../ssh-keys" |
| 68 | + icon={<SidebarNavItemIcon icon={VpnKeyOutlined} />} |
| 69 | + > |
| 70 | + SSH Keys |
| 71 | + </SidebarNavItem> |
| 72 | + </nav> |
| 73 | + ) |
| 74 | +} |
| 75 | + |
| 76 | +const useStyles = makeStyles((theme) => ({ |
| 77 | + sidebar: { |
| 78 | + width: 245, |
| 79 | + flexShrink: 0, |
| 80 | + }, |
| 81 | + sidebarNavItem: { |
| 82 | + color: "inherit", |
| 83 | + display: "block", |
| 84 | + fontSize: 14, |
| 85 | + textDecoration: "none", |
| 86 | + padding: theme.spacing(1.5, 1.5, 1.5, 2), |
| 87 | + borderRadius: theme.shape.borderRadius / 2, |
| 88 | + transition: "background-color 0.15s ease-in-out", |
| 89 | + marginBottom: 1, |
| 90 | + position: "relative", |
| 91 | + |
| 92 | + "&:hover": { |
| 93 | + backgroundColor: theme.palette.action.hover, |
| 94 | + }, |
| 95 | + }, |
| 96 | + sidebarNavItemActive: { |
| 97 | + backgroundColor: theme.palette.action.hover, |
| 98 | + |
| 99 | + "&:before": { |
| 100 | + content: '""', |
| 101 | + display: "block", |
| 102 | + width: 3, |
| 103 | + height: "100%", |
| 104 | + position: "absolute", |
| 105 | + left: 0, |
| 106 | + top: 0, |
| 107 | + backgroundColor: theme.palette.secondary.dark, |
| 108 | + borderTopLeftRadius: theme.shape.borderRadius, |
| 109 | + borderBottomLeftRadius: theme.shape.borderRadius, |
| 110 | + }, |
| 111 | + }, |
| 112 | + sidebarNavItemIcon: { |
| 113 | + width: theme.spacing(2), |
| 114 | + height: theme.spacing(2), |
| 115 | + }, |
| 116 | + userInfo: { |
| 117 | + marginBottom: theme.spacing(2), |
| 118 | + }, |
| 119 | + userData: { |
| 120 | + overflow: "hidden", |
| 121 | + }, |
| 122 | + username: { |
| 123 | + fontWeight: 600, |
| 124 | + overflow: "hidden", |
| 125 | + textOverflow: "ellipsis", |
| 126 | + }, |
| 127 | + email: { |
| 128 | + color: theme.palette.text.secondary, |
| 129 | + fontSize: 12, |
| 130 | + overflow: "hidden", |
| 131 | + textOverflow: "ellipsis", |
| 132 | + }, |
| 133 | +})) |
0 commit comments