index.jsx 4.64 KB
Newer Older
wuhao's avatar
wuhao committed
1
import { useLocation, useModel ,history} from "@umijs/max";
wuhao's avatar
wuhao committed
2
import PropTypes from "prop-types";
wuhao's avatar
wuhao committed
3
import { useEffect } from "react";
wuhao's avatar
wuhao committed
4 5 6

// @mui
import ChevronLeftIcon from "@mui/icons-material/ChevronLeft";
wuhao's avatar
wuhao committed
7
import { Avatar, Box, Button, Drawer, Stack, Typography } from "@mui/material";
wuhao's avatar
wuhao committed
8 9 10 11 12 13
// mock
// hooks
import useResponsive from "@/hooks/useResponsive";
// components
import Logo from "@/components/logo";
import NavSection from "@/components/nav-section";
wuhao's avatar
wuhao committed
14
import { Scrollbars } from "react-custom-scrollbars";
wuhao's avatar
wuhao committed
15 16 17 18 19 20 21 22 23 24 25
import navConfig from "./config";

Nav.propTypes = {
  openNav: PropTypes.bool,
  onCloseNav: PropTypes.func,
};

export default function Nav({ openNav, onCloseNav }) {
  const { pathname } = useLocation();

  const isDesktop = useResponsive("up", "lg");
wuhao's avatar
wuhao committed
26 27 28 29 30

  const {
    initialState: { nav, currentUser },
    setInitialState,
  } = useModel("@@initialState");
wuhao's avatar
wuhao committed
31 32 33

  const setnav = (fn) => {
    const res = fn(nav);
wuhao's avatar
wuhao committed
34 35 36 37 38
    setInitialState((s) => ({
      ...s,
      nav: res,
    }));
  };
wuhao's avatar
wuhao committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

  useEffect(() => {
    if (openNav) {
      onCloseNav();
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [pathname]);

  const ifs = nav === 88;

  const renderContent = (
    <Box
      sx={{
        height: 1,
        display: "flex",
        flexDirection: "column",
        overflow: "hidden",
      }}
    >
      <Box
        sx={{
          px: 2,
          py: 1.8,
          display: "flex",
          justifyContent: "center",
          alignItems: "center",
        }}
      >
        <Logo />
        {!ifs && (
          <Typography
            component="b"
            sx={{ fontSize: 16, paddingLeft: "10px", fontWeight: "bold" }}
            noWrap
          >
            精密测量虚拟仿真实训平台
          </Typography>
        )}
      </Box>

wuhao's avatar
wuhao committed
79
      <Box sx={{ px: 1, pb: 1.5, mt: 7 }}>
wuhao's avatar
wuhao committed
80 81 82 83 84
        <Stack
          alignItems="center"
          spacing={3}
          sx={{ pt: ifs ? 0 : 5, borderRadius: 2, position: "relative" }}
        >
wuhao's avatar
wuhao committed
85 86
          <Avatar
            src={currentUser?.picUrl ?? DEFAULT_HEAD_IMG}
wuhao's avatar
wuhao committed
87
            sx={{
wuhao's avatar
wuhao committed
88 89
              width: ifs ? "2.5vw" : "12vw",
              height: ifs ? "2.5vw" : "12vw",
wuhao's avatar
wuhao committed
90
              maxWidth: 100,
wuhao's avatar
wuhao committed
91
              maxHeight: 100,
wuhao's avatar
wuhao committed
92
              position: "absolute",
wuhao's avatar
wuhao committed
93 94
              top: -46,
              borderRadius: 600,
wuhao's avatar
wuhao committed
95
              cursor: "pointer",
wuhao's avatar
wuhao committed
96 97 98 99
              boxShadow: "0 0 6px #999",
            }}
            onClick={()=>{
              history.push("/work/usercenter")
wuhao's avatar
wuhao committed
100 101 102 103
            }}
          />
          {!ifs && (
            <Box sx={{ textAlign: "center" }}>
wuhao's avatar
wuhao committed
104 105
              <Typography gutterBottom variant="h6" mt={1.2}>
                {currentUser?.name}
wuhao's avatar
wuhao committed
106 107
              </Typography>
              <Typography variant="body2" sx={{ color: "text.secondary" }}>
wuhao's avatar
wuhao committed
108
                角色:{currentUser?.typeName}
wuhao's avatar
wuhao committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
              </Typography>
            </Box>
          )}
        </Stack>
      </Box>

      <Box component={"div"} sx={{ flex: 1, overflow: "hidden" }}>
        <Scrollbars
          thumbMinSize={10}
          autoHide
          style={{
            width: "100%",
            height: "100%",
          }}
          hideTracksWhenNotNeeded
        >
          <NavSection collspan={ifs} data={navConfig} />
        </Scrollbars>
      </Box>
      <Box
        component={"div"}
        sx={{ display: "flex", justifyContent: "center", alignItems: "center" }}
        width="100%"
      >
        <Button
          fullWidth
          size="large"
          sx={{ margin: 1, backgroundColor: "rgba(0,0,0,0.04)", color: "#666" }}
          onClick={() => {
            setnav((s) => {
              if (s === 280) {
                return 88;
              } else {
                return 280;
              }
            });
          }}
        >
          <ChevronLeftIcon
            style={{
              transform: ifs ? "rotate(180deg)" : "rotate(0deg)",
            }}
          />
          <span>{ifs ? "" : "收起"}</span>
        </Button>
      </Box>
    </Box>
  );

  return (
    <Box
      component="nav"
      sx={{
        flexShrink: { lg: 0 },
        width: { lg: nav },
      }}
    >
      {isDesktop ? (
        <Drawer
          open
          variant="permanent"
          PaperProps={{
            sx: {
              width: nav,
wuhao's avatar
wuhao committed
173

wuhao's avatar
wuhao committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
              bgcolor: "background.default",
            },
          }}
        >
          {renderContent}
        </Drawer>
      ) : (
        <Drawer
          open={openNav}
          onClose={onCloseNav}
          ModalProps={{
            keepMounted: true,
          }}
          PaperProps={{
            sx: { width: nav, transition: "all 0.4s" },
          }}
        >
          {renderContent}
        </Drawer>
      )}
    </Box>
  );
}