index.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 {
  11. authContext,
  12. rootContext,
  13. prometheusContext,
  14. dataContext,
  15. } from '@/context';
  16. import Overview from '@/pages/overview/Overview';
  17. const useStyles = makeStyles((theme: Theme) =>
  18. createStyles({
  19. root: {
  20. minHeight: '100vh',
  21. backgroundColor: '#f5f5f5',
  22. },
  23. content: {
  24. display: 'flex',
  25. '& .active': {
  26. '& path': {
  27. fill: theme.palette.attuGrey.dark,
  28. },
  29. },
  30. '& .normal': {
  31. '& path': {
  32. fill: theme.palette.primary.main,
  33. },
  34. },
  35. },
  36. body: {
  37. flex: 1,
  38. display: 'flex',
  39. flexDirection: 'column',
  40. height: '100vh',
  41. overflowY: 'scroll',
  42. },
  43. })
  44. );
  45. function Index() {
  46. const navigate = useNavigate();
  47. const { isAuth, isManaged } = useContext(authContext);
  48. const { isPrometheusReady } = useContext(prometheusContext);
  49. const { database } = useContext(dataContext);
  50. const { versionInfo } = useContext(rootContext);
  51. const { t: navTrans } = useTranslation('nav');
  52. const classes = useStyles();
  53. const location = useLocation();
  54. const isIndex = location.pathname === '/';
  55. const defaultActive = useMemo(() => {
  56. if (location.pathname.includes('databases')) {
  57. return navTrans('database');
  58. }
  59. if (location.pathname.includes('db-admin')) {
  60. return navTrans('dbAdmin');
  61. }
  62. if (location.pathname.includes('search')) {
  63. return navTrans('search');
  64. }
  65. if (location.pathname.includes('system')) {
  66. return navTrans('system');
  67. }
  68. if (location.pathname.includes('users') || location.pathname.includes('roles')) {
  69. return navTrans('user');
  70. }
  71. return navTrans('overview');
  72. }, [location, navTrans]);
  73. const menuItems: NavMenuItem[] = [
  74. {
  75. icon: icons.navOverview,
  76. label: navTrans('overview'),
  77. onClick: () => navigate('/'),
  78. },
  79. {
  80. icon: icons.database,
  81. label: navTrans('database'),
  82. onClick: () => navigate(`/databases/${database}`),
  83. },
  84. {
  85. icon: icons.navSearch,
  86. label: navTrans('search'),
  87. onClick: () => navigate('/search'),
  88. iconActiveClass: 'normal',
  89. iconNormalClass: 'active',
  90. },
  91. ];
  92. if (!isManaged) {
  93. menuItems.push(
  94. {
  95. icon: icons.navPerson,
  96. label: navTrans('user'),
  97. onClick: () => navigate('/users'),
  98. },
  99. {
  100. icon: icons.navSystem,
  101. label: navTrans('system'),
  102. onClick: () =>
  103. isPrometheusReady ? navigate('/system_healthy') : navigate('/system'),
  104. iconActiveClass: 'normal',
  105. iconNormalClass: 'active',
  106. },
  107. {
  108. icon: icons.settings,
  109. label: navTrans('dbAdmin'),
  110. onClick: () => navigate('/db-admin'),
  111. }
  112. );
  113. }
  114. // check if is connected
  115. if (!isAuth) {
  116. return <Navigate to="/connect" />;
  117. }
  118. return (
  119. <>
  120. <div className={classes.root}>
  121. <GlobalEffect>
  122. <div className={classes.content}>
  123. <NavMenu
  124. width="172px"
  125. data={menuItems}
  126. defaultActive={defaultActive}
  127. // used for nested child menu
  128. defaultOpen={{ [navTrans('overview')]: true }}
  129. versionInfo={versionInfo}
  130. />
  131. <div className={classes.body}>
  132. <Header />
  133. {isIndex ? <Overview /> : <Outlet />}
  134. </div>
  135. </div>
  136. </GlobalEffect>
  137. </div>
  138. </>
  139. );
  140. }
  141. export default Index;