Collections.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. import { useContext, useEffect, useState } from 'react';
  2. import { useNavigationHook } from '../../hooks/Navigation';
  3. import { ALL_ROUTER_TYPES } from '../../router/Types';
  4. import MilvusGrid from '../../components/grid';
  5. import CustomToolBar from '../../components/grid/ToolBar';
  6. import { CollectionCreateParam, CollectionView } from './Types';
  7. import { ColDefinitionsType, ToolBarConfig } from '../../components/grid/Types';
  8. import { usePaginationHook } from '../../hooks/Pagination';
  9. import icons from '../../components/icons/Icons';
  10. import EmptyCard from '../../components/cards/EmptyCard';
  11. import Status from '../../components/status/Status';
  12. import { useTranslation } from 'react-i18next';
  13. import { StatusEnum } from '../../components/status/Types';
  14. import { makeStyles, Theme, Link, Typography } from '@material-ui/core';
  15. import StatusIcon from '../../components/status/StatusIcon';
  16. import CustomToolTip from '../../components/customToolTip/CustomToolTip';
  17. import { rootContext } from '../../context/Root';
  18. import CreateCollection from './Create';
  19. import DeleteTemplate from '../../components/customDialog/DeleteDialogTemplate';
  20. import { useHistory } from 'react-router-dom';
  21. const useStyles = makeStyles((theme: Theme) => ({
  22. emptyWrapper: {
  23. marginTop: theme.spacing(2),
  24. },
  25. icon: {
  26. fontSize: '20px',
  27. marginLeft: theme.spacing(0.5),
  28. },
  29. dialogContent: {
  30. lineHeight: '24px',
  31. fontSize: '16px',
  32. },
  33. }));
  34. const Collections = () => {
  35. useNavigationHook(ALL_ROUTER_TYPES.COLLECTIONS);
  36. const {
  37. pageSize,
  38. currentPage,
  39. handleCurrentPage,
  40. // offset,
  41. total,
  42. // setTotal
  43. } = usePaginationHook();
  44. const [collections, setCollections] = useState<CollectionView[]>([]);
  45. // const [loading, setLoading] = useState<boolean>(false);
  46. const [selectedCollections, setSelectedCollections] = useState<
  47. CollectionView[]
  48. >([]);
  49. const history = useHistory();
  50. const { setDialog, handleCloseDialog } = useContext(rootContext);
  51. const { t } = useTranslation('collection');
  52. const { t: btnTrans } = useTranslation('btn');
  53. const { t: dialogTrans } = useTranslation('dialog');
  54. const classes = useStyles();
  55. const loading = false;
  56. const LoadIcon = icons.load;
  57. const ReleaseIcon = icons.release;
  58. const InfoIcon = icons.info;
  59. useEffect(() => {
  60. const mockCollections: CollectionView[] = [
  61. {
  62. name: 'collection_1',
  63. nameElement: (
  64. <Link
  65. component="button"
  66. onClick={() => history.push(`/collection/collection_1`)}
  67. underline="always"
  68. color="textPrimary"
  69. >
  70. collection_1
  71. </Link>
  72. ),
  73. id: 'c1',
  74. status: StatusEnum.unloaded,
  75. statusElement: <Status status={StatusEnum.unloaded} />,
  76. rowCount: '200,000',
  77. desc: 'description',
  78. indexCreatingElement: <StatusIcon type="creating" />,
  79. },
  80. {
  81. name: 'collection_2',
  82. nameElement: (
  83. <Link
  84. component="button"
  85. onClick={() => history.push(`/collection/collection_2`)}
  86. underline="always"
  87. color="textPrimary"
  88. >
  89. collection_2
  90. </Link>
  91. ),
  92. id: 'c2',
  93. status: StatusEnum.loaded,
  94. statusElement: <Status status={StatusEnum.loaded} />,
  95. rowCount: '300,000',
  96. desc: 'description 2',
  97. indexCreatingElement: <StatusIcon type="finish" />,
  98. },
  99. ];
  100. setCollections(mockCollections);
  101. }, [history]);
  102. const handleCreateCollection = (param: CollectionCreateParam) => {
  103. handleCloseDialog();
  104. };
  105. const handleRelease = async (data: CollectionView) => {};
  106. const handleLoad = async (data: CollectionView) => {};
  107. const handleDelete = async () => {
  108. console.log('selected', selectedCollections);
  109. };
  110. const handleAction = (data: CollectionView) => {
  111. const actionType: 'release' | 'load' =
  112. data.status === StatusEnum.loaded ? 'release' : 'load';
  113. const actionsMap = {
  114. release: {
  115. title: t('releaseTitle'),
  116. component: (
  117. <Typography className={classes.dialogContent}>
  118. {t('releaseContent')}
  119. </Typography>
  120. ),
  121. confirmLabel: t('releaseConfirmLabel'),
  122. confirm: () => handleRelease(data),
  123. },
  124. load: {
  125. title: t('loadTitle'),
  126. component: (
  127. <Typography className={classes.dialogContent}>
  128. {t('loadContent')}
  129. </Typography>
  130. ),
  131. confirmLabel: t('loadConfirmLabel'),
  132. confirm: () => handleLoad(data),
  133. },
  134. };
  135. const { title, component, confirmLabel, confirm } = actionsMap[actionType];
  136. setDialog({
  137. open: true,
  138. type: 'notice',
  139. params: {
  140. title,
  141. component,
  142. confirmLabel,
  143. confirm,
  144. },
  145. });
  146. };
  147. const toolbarConfigs: ToolBarConfig[] = [
  148. {
  149. label: t('create'),
  150. onClick: () => {
  151. setDialog({
  152. open: true,
  153. type: 'custom',
  154. params: {
  155. component: (
  156. <CreateCollection handleCreate={handleCreateCollection} />
  157. ),
  158. },
  159. });
  160. },
  161. icon: 'add',
  162. },
  163. {
  164. type: 'iconBtn',
  165. onClick: () => {
  166. setDialog({
  167. open: true,
  168. type: 'custom',
  169. params: {
  170. component: (
  171. <DeleteTemplate
  172. label={btnTrans('delete')}
  173. title={dialogTrans('deleteTitle', { type: t('collection') })}
  174. text={t('deleteWarning')}
  175. handleDelete={handleDelete}
  176. />
  177. ),
  178. },
  179. });
  180. },
  181. label: t('delete'),
  182. icon: 'delete',
  183. },
  184. ];
  185. const colDefinitions: ColDefinitionsType[] = [
  186. {
  187. id: 'id',
  188. align: 'left',
  189. disablePadding: true,
  190. label: t('id'),
  191. },
  192. {
  193. id: 'nameElement',
  194. align: 'left',
  195. disablePadding: true,
  196. label: t('name'),
  197. },
  198. {
  199. id: 'statusElement',
  200. align: 'left',
  201. disablePadding: false,
  202. label: t('status'),
  203. },
  204. {
  205. id: 'rowCount',
  206. align: 'left',
  207. disablePadding: false,
  208. label: (
  209. <span className="flex-center">
  210. {t('rowCount')}
  211. <CustomToolTip title={t('tooltip')}>
  212. <InfoIcon classes={{ root: classes.icon }} />
  213. </CustomToolTip>
  214. </span>
  215. ),
  216. },
  217. {
  218. id: 'desc',
  219. align: 'left',
  220. disablePadding: false,
  221. label: t('desc'),
  222. },
  223. {
  224. id: 'indexCreatingElement',
  225. align: 'left',
  226. disablePadding: false,
  227. label: '',
  228. },
  229. {
  230. id: 'action',
  231. align: 'center',
  232. disablePadding: false,
  233. label: '',
  234. showActionCell: true,
  235. isHoverAction: true,
  236. actionBarConfigs: [
  237. {
  238. onClick: (e: React.MouseEvent, row: CollectionView) => {
  239. handleAction(row);
  240. },
  241. icon: 'load',
  242. label: 'load',
  243. showIconMethod: 'renderFn',
  244. getLabel: (row: CollectionView) =>
  245. row.status === StatusEnum.loaded ? 'release' : 'load',
  246. renderIconFn: (row: CollectionView) =>
  247. row.status === StatusEnum.loaded ? <ReleaseIcon /> : <LoadIcon />,
  248. },
  249. ],
  250. },
  251. ];
  252. const handleSelectChange = (value: any) => {
  253. setSelectedCollections(value);
  254. };
  255. const handlePageChange = (e: any, page: number) => {
  256. handleCurrentPage(page);
  257. setSelectedCollections([]);
  258. };
  259. const CollectionIcon = icons.navCollection;
  260. return (
  261. <section className="page-wrapper">
  262. {collections.length > 0 || loading ? (
  263. <MilvusGrid
  264. toolbarConfigs={toolbarConfigs}
  265. colDefinitions={colDefinitions}
  266. rows={collections}
  267. rowCount={total}
  268. primaryKey="id"
  269. openCheckBox={true}
  270. showHoverStyle={true}
  271. selected={selectedCollections}
  272. setSelected={handleSelectChange}
  273. page={currentPage}
  274. onChangePage={handlePageChange}
  275. rowsPerPage={pageSize}
  276. // isLoading={loading}
  277. />
  278. ) : (
  279. <>
  280. <CustomToolBar toolbarConfigs={toolbarConfigs} />
  281. <EmptyCard
  282. wrapperClass={`page-empty-card ${classes.emptyWrapper}`}
  283. icon={<CollectionIcon />}
  284. text={t('noData')}
  285. />
  286. </>
  287. )}
  288. </section>
  289. );
  290. };
  291. export default Collections;