index.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { useMemo, useContext } from 'react';
  2. import { Outlet, useNavigate, useLocation, Navigate } from 'react-router-dom';
  3. import { useTranslation } from 'react-i18next';
  4. import { makeStyles, Theme, createStyles } from '@material-ui/core';
  5. import GlobalEffect from '../components/layout/GlobalEffect';
  6. import Header from '../components/layout/Header';
  7. import NavMenu from '../components/menu/NavMenu';
  8. import { NavMenuItem } from '../components/menu/Types';
  9. import icons from '../components/icons/Icons';
  10. import { authContext } from '../context/Auth';
  11. import { rootContext } from '../context/Root';
  12. import Overview from '../pages/overview/Overview';
  13. const useStyles = makeStyles((theme: Theme) =>
  14. createStyles({
  15. root: {
  16. minHeight: '100vh',
  17. backgroundColor: '#f5f5f5',
  18. },
  19. content: {
  20. display: 'flex',
  21. '& .active': {
  22. '& path': {
  23. fill: theme.palette.attuGrey.dark,
  24. },
  25. },
  26. '& .normal': {
  27. '& path': {
  28. fill: theme.palette.primary.main,
  29. },
  30. },
  31. },
  32. body: {
  33. flex: 1,
  34. display: 'flex',
  35. flexDirection: 'column',
  36. height: '100vh',
  37. overflowY: 'scroll',
  38. },
  39. })
  40. );
  41. function Index() {
  42. const navigate = useNavigate();
  43. const { isAuth } = useContext(authContext);
  44. const { versionInfo } = useContext(rootContext);
  45. const { t: navTrans } = useTranslation('nav');
  46. const classes = useStyles();
  47. const location = useLocation();
  48. const isIndex = location.pathname === '/';
  49. const defaultActive = useMemo(() => {
  50. if (location.pathname.includes('collection')) {
  51. return navTrans('collection');
  52. }
  53. if (location.pathname.includes('search')) {
  54. return navTrans('search');
  55. }
  56. if (location.pathname.includes('system')) {
  57. return navTrans('system');
  58. }
  59. if (location.pathname.includes('users')) {
  60. return navTrans('user');
  61. }
  62. return navTrans('overview');
  63. }, [location, navTrans]);
  64. const menuItems: NavMenuItem[] = [
  65. {
  66. icon: icons.navOverview,
  67. label: navTrans('overview'),
  68. onClick: () => navigate('/'),
  69. },
  70. {
  71. icon: icons.navCollection,
  72. label: navTrans('collection'),
  73. onClick: () => navigate('/collections'),
  74. },
  75. {
  76. icon: icons.navSearch,
  77. label: navTrans('search'),
  78. onClick: () => navigate('/search'),
  79. iconActiveClass: 'normal',
  80. iconNormalClass: 'active',
  81. },
  82. {
  83. icon: icons.navSystem,
  84. label: navTrans('system'),
  85. onClick: () => navigate('/system'),
  86. iconActiveClass: 'normal',
  87. iconNormalClass: 'active',
  88. },
  89. {
  90. icon: icons.navPerson,
  91. label: navTrans('user'),
  92. onClick: () => navigate('/users'),
  93. },
  94. ];
  95. // check if is connected
  96. if (!isAuth) {
  97. return <Navigate to="/connect" />;
  98. }
  99. return (
  100. <>
  101. <div className={classes.root}>
  102. <GlobalEffect>
  103. <div className={classes.content}>
  104. <NavMenu
  105. width="200px"
  106. data={menuItems}
  107. defaultActive={defaultActive}
  108. // used for nested child menu
  109. defaultOpen={{ [navTrans('overview')]: true }}
  110. versionInfo={versionInfo}
  111. />
  112. <div className={classes.body}>
  113. <Header />
  114. {isIndex ? <Overview /> : <Outlet />}
  115. </div>
  116. </div>
  117. </GlobalEffect>
  118. </div>
  119. </>
  120. );
  121. }
  122. export default Index;