Databases.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { useContext } from 'react';
  2. import { useParams } from 'react-router-dom';
  3. import { useTranslation } from 'react-i18next';
  4. import { makeStyles, Theme } from '@material-ui/core';
  5. import { useNavigationHook } from '@/hooks';
  6. import { ALL_ROUTER_TYPES } from '@/router/Types';
  7. import RouteTabList from '@/components/customTabList/RouteTabList';
  8. import DatabaseTree from '@/pages/databases/tree';
  9. import { ITab } from '@/components/customTabList/Types';
  10. import Partitions from './collections/partitions/Partitions';
  11. import Overview from './collections/overview/Overview';
  12. import Data from './collections/data/Data';
  13. import Segments from './collections/segments/Segments';
  14. import { dataContext, authContext } from '@/context';
  15. import Collections from './collections/Collections';
  16. import StatusIcon, { LoadingType } from '@/components/status/StatusIcon';
  17. import RefreshButton from './RefreshButton';
  18. const useStyles = makeStyles((theme: Theme) => ({
  19. wrapper: {
  20. flexDirection: 'row',
  21. },
  22. tree: {
  23. boxShadow: 'none',
  24. flexBasis: theme.spacing(28),
  25. width: theme.spacing(28),
  26. flexGrow: 0,
  27. flexShrink: 0,
  28. height: 'calc(100vh - 96px)',
  29. overflow: 'auto',
  30. boxSizing: 'border-box',
  31. padding: theme.spacing(0, 2, 0, 0),
  32. },
  33. tab: {
  34. flexGrow: 1,
  35. flexShrink: 1,
  36. overflowX: 'auto',
  37. padding: theme.spacing(0, 2),
  38. },
  39. }));
  40. const Databases = () => {
  41. // context
  42. const { isManaged } = useContext(authContext);
  43. const { database, collections, loading, fetchCollection } =
  44. useContext(dataContext);
  45. // get current collection from url
  46. const params = useParams();
  47. const {
  48. databaseName = '',
  49. collectionName = '',
  50. collectionPage = '',
  51. } = params;
  52. // refresh collection
  53. const refreshCollection = async () => {
  54. await fetchCollection(collectionName);
  55. };
  56. // get style
  57. const classes = useStyles();
  58. // update navigation
  59. useNavigationHook(ALL_ROUTER_TYPES.DATABASES, {
  60. collectionName,
  61. databaseName,
  62. extra: <RefreshButton onClick={refreshCollection} />,
  63. });
  64. // i18n
  65. const { t: collectionTrans } = useTranslation('collection');
  66. const dbTab: ITab[] = [
  67. {
  68. label: collectionTrans('collections'),
  69. component: <Collections />,
  70. path: `collections`,
  71. },
  72. ];
  73. const actionDbTab = dbTab.findIndex(t => t.path === databaseName);
  74. // collection tabs
  75. const collectionTabs: ITab[] = [
  76. {
  77. label: collectionTrans('schemaTab'),
  78. component: <Overview />,
  79. path: `info`,
  80. },
  81. {
  82. label: collectionTrans('dataTab'),
  83. component: <Data />,
  84. path: `data`,
  85. },
  86. {
  87. label: collectionTrans('partitionTab'),
  88. component: <Partitions />,
  89. path: `partitions`,
  90. },
  91. ];
  92. if (!isManaged) {
  93. collectionTabs.push({
  94. label: collectionTrans('segmentsTab'),
  95. component: <Segments />,
  96. path: `segments`,
  97. });
  98. }
  99. // get active collection tab
  100. const activeColTab = collectionTabs.findIndex(t => t.path === collectionPage);
  101. // render
  102. return (
  103. <section className={`page-wrapper ${classes.wrapper}`}>
  104. <section className={classes.tree}>
  105. {loading ? (
  106. <StatusIcon type={LoadingType.CREATING} />
  107. ) : (
  108. <DatabaseTree
  109. key="collections"
  110. collections={collections}
  111. database={database}
  112. params={params}
  113. />
  114. )}
  115. </section>
  116. {!collectionName && (
  117. <RouteTabList
  118. tabs={dbTab}
  119. wrapperClass={classes.tab}
  120. activeIndex={actionDbTab !== -1 ? actionDbTab : 0}
  121. />
  122. )}
  123. {collectionName && (
  124. <RouteTabList
  125. tabs={collectionTabs}
  126. wrapperClass={classes.tab}
  127. activeIndex={activeColTab !== -1 ? activeColTab : 0}
  128. />
  129. )}
  130. </section>
  131. );
  132. };
  133. export default Databases;