2
0

Collection.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { useMemo, useContext } from 'react';
  2. import { useNavigate, useLocation, useParams } from 'react-router-dom';
  3. import { useTranslation } from 'react-i18next';
  4. import { makeStyles, Theme } from '@material-ui/core';
  5. import { authContext } from '@/context/Auth';
  6. import { useNavigationHook } from '@/hooks/Navigation';
  7. import { ALL_ROUTER_TYPES } from '@/router/Types';
  8. import CustomTabList from '@/components/customTabList/CustomTabList';
  9. import { ITab } from '@/components/customTabList/Types';
  10. import Partitions from '../partitions/Partitions';
  11. import { parseLocationSearch } from '@/utils/Format';
  12. import Schema from '../schema/Schema';
  13. import Query from '../query/Query';
  14. import Preview from '../preview/Preview';
  15. import { TAB_ENUM } from './Types';
  16. const useStyles = makeStyles((theme: Theme) => ({
  17. wrapper: {
  18. flexDirection: 'row',
  19. gap: theme.spacing(4),
  20. },
  21. card: {
  22. boxShadow: 'none',
  23. flexBasis: theme.spacing(28),
  24. width: theme.spacing(28),
  25. flexGrow: 0,
  26. flexShrink: 0,
  27. },
  28. tab: {
  29. flexGrow: 1,
  30. flexShrink: 1,
  31. overflowX: 'auto',
  32. },
  33. }));
  34. const Collection = () => {
  35. const classes = useStyles();
  36. const { isManaged } = useContext(authContext);
  37. const { collectionName = '' } = useParams<{
  38. collectionName: string;
  39. }>();
  40. useNavigationHook(ALL_ROUTER_TYPES.COLLECTION_DETAIL, { collectionName });
  41. const navigate = useNavigate();
  42. const location = useLocation();
  43. const { t: collectionTrans } = useTranslation('collection');
  44. const activeTabIndex = useMemo(() => {
  45. const { activeIndex } = location.search
  46. ? parseLocationSearch(location.search)
  47. : { activeIndex: TAB_ENUM.schema };
  48. return Number(activeIndex);
  49. }, [location]);
  50. const handleTabChange = (activeIndex: number) => {
  51. const path = location.pathname;
  52. navigate(`${path}?activeIndex=${activeIndex}`);
  53. };
  54. const tabs: ITab[] = [
  55. {
  56. label: collectionTrans('schemaTab'),
  57. component: <Schema collectionName={collectionName} />,
  58. },
  59. {
  60. label: collectionTrans('partitionTab'),
  61. component: <Partitions collectionName={collectionName} />,
  62. },
  63. {
  64. label: collectionTrans('previewTab'),
  65. component: <Preview collectionName={collectionName} />,
  66. },
  67. {
  68. label: collectionTrans('queryTab'),
  69. component: <Query collectionName={collectionName} />,
  70. },
  71. ];
  72. // exclude parititon on cloud
  73. if (isManaged) {
  74. tabs.splice(1, 1);
  75. }
  76. return (
  77. <section className={`page-wrapper ${classes.wrapper}`}>
  78. <CustomTabList
  79. tabs={tabs}
  80. wrapperClass={classes.tab}
  81. activeIndex={activeTabIndex}
  82. handleTabChange={handleTabChange}
  83. />
  84. </section>
  85. );
  86. };
  87. export default Collection;