Collections.tsx 8.7 KB

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