Databases.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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/CollectionData';
  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. import CopyButton from '@/components/advancedSearch/CopyButton';
  19. import { CollectionObject } from '@server/types';
  20. const useStyles = makeStyles((theme: Theme) => ({
  21. wrapper: {
  22. flexDirection: 'row',
  23. },
  24. tree: {
  25. boxShadow: 'none',
  26. flexBasis: theme.spacing(28),
  27. width: theme.spacing(28),
  28. flexGrow: 0,
  29. flexShrink: 0,
  30. height: 'calc(100vh - 96px)',
  31. overflow: 'auto',
  32. boxSizing: 'border-box',
  33. padding: theme.spacing(0, 2, 0, 0),
  34. },
  35. tab: {
  36. flexGrow: 1,
  37. flexShrink: 1,
  38. overflowX: 'auto',
  39. padding: theme.spacing(0, 2),
  40. },
  41. headerIcon: {
  42. marginLeft: theme.spacing(0.5),
  43. '& svg': {
  44. fontSize: 15,
  45. color: theme.palette.primary.main,
  46. },
  47. },
  48. }));
  49. // Databases page(tree and tabs)
  50. const Databases = () => {
  51. // context
  52. const { database, collections, loading, fetchCollection } =
  53. useContext(dataContext);
  54. // get current collection from url
  55. const params = useParams();
  56. const {
  57. databaseName = '',
  58. collectionName = '',
  59. collectionPage = '',
  60. } = params;
  61. // get style
  62. const classes = useStyles();
  63. // update navigation
  64. useNavigationHook(ALL_ROUTER_TYPES.DATABASES, {
  65. collectionName,
  66. databaseName,
  67. extra: (
  68. <>
  69. <CopyButton
  70. label=""
  71. value={collectionName}
  72. className={classes.headerIcon}
  73. />
  74. <RefreshButton
  75. className={classes.headerIcon}
  76. onClick={async () => {
  77. await fetchCollection(collectionName);
  78. }}
  79. />
  80. </>
  81. ),
  82. });
  83. // render
  84. return (
  85. <section className={`page-wrapper ${classes.wrapper}`}>
  86. <section className={classes.tree}>
  87. {loading ? (
  88. <StatusIcon type={LoadingType.CREATING} />
  89. ) : (
  90. <DatabaseTree
  91. key="collections"
  92. collections={collections}
  93. database={database}
  94. params={params}
  95. />
  96. )}
  97. </section>
  98. {!collectionName && (
  99. <DatabasesTab databaseName={databaseName} tabClass={classes.tab} />
  100. )}
  101. {collectionName && (
  102. <CollectionTabs
  103. collectionPage={collectionPage}
  104. collectionName={collectionName}
  105. tabClass={classes.tab}
  106. collections={collections}
  107. />
  108. )}
  109. </section>
  110. );
  111. };
  112. // Database tab pages
  113. const DatabasesTab = (props: {
  114. databaseName: string;
  115. tabClass: string; // tab class
  116. }) => {
  117. const { databaseName, tabClass } = props;
  118. const { t: collectionTrans } = useTranslation('collection');
  119. const dbTab: ITab[] = [
  120. {
  121. label: collectionTrans('collections'),
  122. component: <Collections />,
  123. path: `collections`,
  124. },
  125. ];
  126. const actionDbTab = dbTab.findIndex(t => t.path === databaseName);
  127. return (
  128. <RouteTabList
  129. tabs={dbTab}
  130. wrapperClass={tabClass}
  131. activeIndex={actionDbTab !== -1 ? actionDbTab : 0}
  132. />
  133. );
  134. };
  135. // Collection tab pages
  136. const CollectionTabs = (props: {
  137. collectionPage: string; // current collection page
  138. collectionName: string; // current collection name
  139. tabClass: string; // tab class
  140. collections: CollectionObject[]; // collections
  141. }) => {
  142. // props
  143. const { collectionPage, collectionName, tabClass, collections } = props;
  144. // context
  145. const { isManaged } = useContext(authContext);
  146. // i18n
  147. const { t: collectionTrans } = useTranslation('collection');
  148. // collection tabs
  149. const collectionTabs: ITab[] = [
  150. {
  151. label: collectionTrans('overviewTab'),
  152. component: <Overview />,
  153. path: `overview`,
  154. },
  155. {
  156. label: collectionTrans('dataTab'),
  157. component: (
  158. <Data collections={collections} collectionName={collectionName} />
  159. ),
  160. path: `data`,
  161. },
  162. {
  163. label: collectionTrans('partitionTab'),
  164. component: <Partitions />,
  165. path: `partitions`,
  166. },
  167. ];
  168. // get active collection tab
  169. const activeColTab = collectionTabs.findIndex(t => t.path === collectionPage);
  170. if (!isManaged) {
  171. collectionTabs.push({
  172. label: collectionTrans('segmentsTab'),
  173. component: <Segments />,
  174. path: `segments`,
  175. });
  176. }
  177. return (
  178. <RouteTabList
  179. tabs={collectionTabs}
  180. wrapperClass={tabClass}
  181. activeIndex={activeColTab !== -1 ? activeColTab : 0}
  182. />
  183. );
  184. };
  185. export default Databases;