Collections.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 {
  41. pageSize,
  42. currentPage,
  43. handleCurrentPage,
  44. // offset,
  45. total,
  46. // setTotal
  47. } = usePaginationHook();
  48. const [collections, setCollections] = useState<CollectionView[]>([]);
  49. // const [loading, setLoading] = useState<boolean>(false);
  50. const [selectedCollections, setSelectedCollections] = useState<
  51. CollectionView[]
  52. >([]);
  53. const { setDialog, handleCloseDialog } = useContext(rootContext);
  54. const { t } = useTranslation('collection');
  55. const { t: btnTrans } = useTranslation('btn');
  56. const { t: dialogTrans } = useTranslation('dialog');
  57. const classes = useStyles();
  58. const loading = false;
  59. const LoadIcon = icons.load;
  60. const ReleaseIcon = icons.release;
  61. const InfoIcon = icons.info;
  62. const fetchData = useCallback(async () => {
  63. const res = await CollectionHttp.getCollections();
  64. const statusRes = await CollectionHttp.getCollectionsIndexState();
  65. setCollections(
  66. res.map(v => {
  67. const indexStatus = statusRes.find(item => item._name === v._name);
  68. Object.assign(v, {
  69. nameElement: (
  70. <Link to={`/collections/${v._name}`} className={classes.link}>
  71. {v._name}
  72. </Link>
  73. ),
  74. statusElement: <Status status={v._status} />,
  75. indexCreatingElement: (
  76. <StatusIcon
  77. type={indexStatus?._indexState || ChildrenStatusType.FINISH}
  78. />
  79. ),
  80. });
  81. return v;
  82. })
  83. );
  84. }, [classes.link]);
  85. useEffect(() => {
  86. fetchData();
  87. }, [fetchData]);
  88. const handleCreateCollection = (param: CollectionCreateParam) => {
  89. handleCloseDialog();
  90. };
  91. const handleRelease = async (data: CollectionView) => {};
  92. const handleLoad = async (data: CollectionView) => {};
  93. const handleDelete = async () => {
  94. console.log('selected', selectedCollections);
  95. };
  96. const handleAction = (data: CollectionView) => {
  97. const actionType: 'release' | 'load' =
  98. data._status === StatusEnum.loaded ? 'release' : 'load';
  99. const actionsMap = {
  100. release: {
  101. title: t('releaseTitle'),
  102. component: (
  103. <Typography className={classes.dialogContent}>
  104. {t('releaseContent')}
  105. </Typography>
  106. ),
  107. confirmLabel: t('releaseConfirmLabel'),
  108. confirm: () => handleRelease(data),
  109. },
  110. load: {
  111. title: t('loadTitle'),
  112. component: (
  113. <Typography className={classes.dialogContent}>
  114. {t('loadContent')}
  115. </Typography>
  116. ),
  117. confirmLabel: t('loadConfirmLabel'),
  118. confirm: () => handleLoad(data),
  119. },
  120. };
  121. const { title, component, confirmLabel, confirm } = actionsMap[actionType];
  122. setDialog({
  123. open: true,
  124. type: 'notice',
  125. params: {
  126. title,
  127. component,
  128. confirmLabel,
  129. confirm,
  130. },
  131. });
  132. };
  133. const toolbarConfigs: ToolBarConfig[] = [
  134. {
  135. label: t('create'),
  136. onClick: () => {
  137. setDialog({
  138. open: true,
  139. type: 'custom',
  140. params: {
  141. component: (
  142. <CreateCollection handleCreate={handleCreateCollection} />
  143. ),
  144. },
  145. });
  146. },
  147. icon: 'add',
  148. },
  149. {
  150. type: 'iconBtn',
  151. onClick: () => {
  152. setDialog({
  153. open: true,
  154. type: 'custom',
  155. params: {
  156. component: (
  157. <DeleteTemplate
  158. label={btnTrans('delete')}
  159. title={dialogTrans('deleteTitle', { type: t('collection') })}
  160. text={t('deleteWarning')}
  161. handleDelete={handleDelete}
  162. />
  163. ),
  164. },
  165. });
  166. },
  167. label: t('delete'),
  168. icon: 'delete',
  169. },
  170. ];
  171. const colDefinitions: ColDefinitionsType[] = [
  172. {
  173. id: '_id',
  174. align: 'left',
  175. disablePadding: true,
  176. label: t('id'),
  177. },
  178. {
  179. id: 'nameElement',
  180. align: 'left',
  181. disablePadding: true,
  182. label: t('name'),
  183. },
  184. {
  185. id: 'statusElement',
  186. align: 'left',
  187. disablePadding: false,
  188. label: t('status'),
  189. },
  190. {
  191. id: '_rowCount',
  192. align: 'left',
  193. disablePadding: false,
  194. label: (
  195. <span className="flex-center">
  196. {t('rowCount')}
  197. <CustomToolTip title={t('tooltip')}>
  198. <InfoIcon classes={{ root: classes.icon }} />
  199. </CustomToolTip>
  200. </span>
  201. ),
  202. },
  203. {
  204. id: '_desc',
  205. align: 'left',
  206. disablePadding: false,
  207. label: t('desc'),
  208. },
  209. {
  210. id: 'indexCreatingElement',
  211. align: 'left',
  212. disablePadding: false,
  213. label: '',
  214. },
  215. {
  216. id: 'action',
  217. align: 'center',
  218. disablePadding: false,
  219. label: '',
  220. showActionCell: true,
  221. isHoverAction: true,
  222. actionBarConfigs: [
  223. {
  224. onClick: (e: React.MouseEvent, row: CollectionView) => {
  225. handleAction(row);
  226. },
  227. icon: 'load',
  228. label: 'load',
  229. showIconMethod: 'renderFn',
  230. getLabel: (row: CollectionView) =>
  231. row._status === StatusEnum.loaded ? 'release' : 'load',
  232. renderIconFn: (row: CollectionView) =>
  233. row._status === StatusEnum.loaded ? <ReleaseIcon /> : <LoadIcon />,
  234. },
  235. ],
  236. },
  237. ];
  238. const handleSelectChange = (value: any) => {
  239. setSelectedCollections(value);
  240. };
  241. const handlePageChange = (e: any, page: number) => {
  242. handleCurrentPage(page);
  243. setSelectedCollections([]);
  244. };
  245. const CollectionIcon = icons.navCollection;
  246. return (
  247. <section className="page-wrapper">
  248. {collections.length > 0 || loading ? (
  249. <MilvusGrid
  250. toolbarConfigs={toolbarConfigs}
  251. colDefinitions={colDefinitions}
  252. rows={collections}
  253. rowCount={total}
  254. primaryKey="id"
  255. openCheckBox={true}
  256. showHoverStyle={true}
  257. selected={selectedCollections}
  258. setSelected={handleSelectChange}
  259. page={currentPage}
  260. onChangePage={handlePageChange}
  261. rowsPerPage={pageSize}
  262. // isLoading={loading}
  263. />
  264. ) : (
  265. <>
  266. <CustomToolBar toolbarConfigs={toolbarConfigs} />
  267. <EmptyCard
  268. wrapperClass={`page-empty-card ${classes.emptyWrapper}`}
  269. icon={<CollectionIcon />}
  270. text={t('noData')}
  271. />
  272. </>
  273. )}
  274. </section>
  275. );
  276. };
  277. export default Collections;