Collections.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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/Grid';
  6. import CustomToolBar from '../../components/grid/ToolBar';
  7. import { CollectionCreateParam, CollectionView, DataTypeEnum } 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 } 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. import { useDialogHook } from '../../hooks/Dialog';
  23. import Highlighter from 'react-highlight-words';
  24. import { parseLocationSearch } from '../../utils/Format';
  25. const useStyles = makeStyles((theme: Theme) => ({
  26. emptyWrapper: {
  27. marginTop: theme.spacing(2),
  28. },
  29. icon: {
  30. fontSize: '20px',
  31. marginLeft: theme.spacing(0.5),
  32. },
  33. dialogContent: {
  34. lineHeight: '24px',
  35. fontSize: '16px',
  36. },
  37. link: {
  38. color: theme.palette.common.black,
  39. },
  40. highlight: {
  41. color: theme.palette.primary.main,
  42. backgroundColor: 'transparent',
  43. },
  44. }));
  45. let timer: NodeJS.Timeout | null = null;
  46. // get init search value from url
  47. const { search = '' } = parseLocationSearch(window.location.search);
  48. const Collections = () => {
  49. useNavigationHook(ALL_ROUTER_TYPES.COLLECTIONS);
  50. const { handleAction } = useDialogHook({ type: 'collection' });
  51. const [collections, setCollections] = useState<CollectionView[]>([]);
  52. const [searchedCollections, setSearchedCollections] = useState<
  53. CollectionView[]
  54. >([]);
  55. const {
  56. pageSize,
  57. handlePageSize,
  58. currentPage,
  59. handleCurrentPage,
  60. total,
  61. data: collectionList,
  62. } = usePaginationHook(searchedCollections);
  63. const [loading, setLoading] = useState<boolean>(true);
  64. const [selectedCollections, setSelectedCollections] = useState<
  65. CollectionView[]
  66. >([]);
  67. const { setDialog, handleCloseDialog, openSnackBar } =
  68. useContext(rootContext);
  69. const { t: collectionTrans } = useTranslation('collection');
  70. const { t: btnTrans } = useTranslation('btn');
  71. const { t: dialogTrans } = useTranslation('dialog');
  72. const { t: successTrans } = useTranslation('success');
  73. const classes = useStyles();
  74. const LoadIcon = icons.load;
  75. const ReleaseIcon = icons.release;
  76. const InfoIcon = icons.info;
  77. const fetchData = useCallback(async () => {
  78. try {
  79. const res = await CollectionHttp.getCollections();
  80. const statusRes = await CollectionHttp.getCollectionsIndexState();
  81. setLoading(false);
  82. const collections = res.map(v => {
  83. const indexStatus = statusRes.find(item => item._name === v._name);
  84. Object.assign(v, {
  85. nameElement: (
  86. <Link to={`/collections/${v._name}`} className={classes.link}>
  87. <Highlighter
  88. textToHighlight={v._name}
  89. searchWords={[search]}
  90. highlightClassName={classes.highlight}
  91. />
  92. </Link>
  93. ),
  94. statusElement: <Status status={v._status} />,
  95. indexCreatingElement: (
  96. <StatusIcon
  97. type={indexStatus?._indexState || ChildrenStatusType.FINISH}
  98. />
  99. ),
  100. });
  101. return v;
  102. });
  103. // filter collection if url contains search param
  104. const filteredCollections = collections.filter(collection =>
  105. collection._name.includes(search)
  106. );
  107. setCollections(collections);
  108. setSearchedCollections(filteredCollections);
  109. } catch (err) {
  110. setLoading(false);
  111. }
  112. }, [classes.link, classes.highlight]);
  113. useEffect(() => {
  114. fetchData();
  115. }, [fetchData]);
  116. const handleCreateCollection = async (param: CollectionCreateParam) => {
  117. const data: CollectionCreateParam = JSON.parse(JSON.stringify(param));
  118. const vectorType = [DataTypeEnum.BinaryVector, DataTypeEnum.FloatVector];
  119. data.fields = data.fields.map(v =>
  120. vectorType.includes(v.data_type)
  121. ? {
  122. ...v,
  123. type_params: [{ key: 'dim', value: v.dimension }],
  124. }
  125. : v
  126. );
  127. await CollectionHttp.createCollection(data);
  128. handleCloseDialog();
  129. openSnackBar(
  130. successTrans('create', { name: collectionTrans('collection') })
  131. );
  132. fetchData();
  133. };
  134. const handleRelease = async (data: CollectionView) => {
  135. const res = await CollectionHttp.releaseCollection(data._name);
  136. openSnackBar(
  137. successTrans('release', { name: collectionTrans('collection') })
  138. );
  139. fetchData();
  140. return res;
  141. };
  142. const handleLoad = async (data: CollectionView) => {
  143. const res = await CollectionHttp.loadCollection(data._name);
  144. openSnackBar(successTrans('load', { name: collectionTrans('collection') }));
  145. fetchData();
  146. return res;
  147. };
  148. const handleDelete = async () => {
  149. for (const item of selectedCollections) {
  150. await CollectionHttp.deleteCollection(item._name);
  151. }
  152. openSnackBar(
  153. successTrans('delete', { name: collectionTrans('collection') })
  154. );
  155. fetchData();
  156. handleCloseDialog();
  157. setSelectedCollections([]);
  158. };
  159. const handleSearch = (value: string) => {
  160. if (timer) {
  161. clearTimeout(timer);
  162. }
  163. // add loading manually
  164. setLoading(true);
  165. timer = setTimeout(() => {
  166. const searchWords = [value];
  167. const list = value
  168. ? collections.filter(c => c._name.includes(value))
  169. : collections;
  170. const highlightList = list.map(c => {
  171. Object.assign(c, {
  172. nameElement: (
  173. <Link to={`/collections/${c._name}`} className={classes.link}>
  174. <Highlighter
  175. textToHighlight={c._name}
  176. searchWords={searchWords}
  177. highlightClassName={classes.highlight}
  178. />
  179. </Link>
  180. ),
  181. });
  182. return c;
  183. });
  184. setLoading(false);
  185. setSearchedCollections(highlightList);
  186. }, 300);
  187. };
  188. const toolbarConfigs: ToolBarConfig[] = [
  189. {
  190. label: collectionTrans('create'),
  191. onClick: () => {
  192. setDialog({
  193. open: true,
  194. type: 'custom',
  195. params: {
  196. component: (
  197. <CreateCollection handleCreate={handleCreateCollection} />
  198. ),
  199. },
  200. });
  201. },
  202. icon: 'add',
  203. },
  204. {
  205. type: 'iconBtn',
  206. onClick: () => {
  207. setDialog({
  208. open: true,
  209. type: 'custom',
  210. params: {
  211. component: (
  212. <DeleteTemplate
  213. label={btnTrans('delete')}
  214. title={dialogTrans('deleteTitle', {
  215. type: collectionTrans('collection'),
  216. })}
  217. text={collectionTrans('deleteWarning')}
  218. handleDelete={handleDelete}
  219. />
  220. ),
  221. },
  222. });
  223. },
  224. label: collectionTrans('delete'),
  225. icon: 'delete',
  226. disabled: data => data.length === 0,
  227. },
  228. {
  229. label: 'Search',
  230. icon: 'search',
  231. searchText: search,
  232. onSearch: (value: string) => {
  233. handleSearch(value);
  234. },
  235. },
  236. ];
  237. const colDefinitions: ColDefinitionsType[] = [
  238. {
  239. id: '_id',
  240. align: 'left',
  241. disablePadding: true,
  242. label: collectionTrans('id'),
  243. },
  244. {
  245. id: 'nameElement',
  246. align: 'left',
  247. disablePadding: true,
  248. sortBy: '_name',
  249. label: collectionTrans('name'),
  250. },
  251. {
  252. id: 'statusElement',
  253. align: 'left',
  254. disablePadding: false,
  255. sortBy: '_status',
  256. label: collectionTrans('status'),
  257. },
  258. {
  259. id: '_rowCount',
  260. align: 'left',
  261. disablePadding: false,
  262. label: (
  263. <span className="flex-center">
  264. {collectionTrans('rowCount')}
  265. <CustomToolTip title={collectionTrans('tooltip')}>
  266. <InfoIcon classes={{ root: classes.icon }} />
  267. </CustomToolTip>
  268. </span>
  269. ),
  270. },
  271. {
  272. id: '_desc',
  273. align: 'left',
  274. disablePadding: false,
  275. label: collectionTrans('desc'),
  276. },
  277. {
  278. id: 'indexCreatingElement',
  279. align: 'left',
  280. disablePadding: false,
  281. label: '',
  282. },
  283. {
  284. id: 'action',
  285. align: 'center',
  286. disablePadding: false,
  287. label: '',
  288. showActionCell: true,
  289. isHoverAction: true,
  290. actionBarConfigs: [
  291. {
  292. onClick: (e: React.MouseEvent, row: CollectionView) => {
  293. const cb =
  294. row._status === StatusEnum.unloaded ? handleLoad : handleRelease;
  295. handleAction(row, cb);
  296. },
  297. icon: 'load',
  298. label: 'load',
  299. showIconMethod: 'renderFn',
  300. getLabel: (row: CollectionView) =>
  301. row._status === StatusEnum.loaded ? 'release' : 'load',
  302. renderIconFn: (row: CollectionView) =>
  303. row._status === StatusEnum.loaded ? <ReleaseIcon /> : <LoadIcon />,
  304. },
  305. ],
  306. },
  307. ];
  308. const handleSelectChange = (value: any) => {
  309. setSelectedCollections(value);
  310. };
  311. const handlePageChange = (e: any, page: number) => {
  312. handleCurrentPage(page);
  313. setSelectedCollections([]);
  314. };
  315. const CollectionIcon = icons.navCollection;
  316. return (
  317. <section className="page-wrapper">
  318. {collections.length > 0 || loading ? (
  319. <MilvusGrid
  320. toolbarConfigs={toolbarConfigs}
  321. colDefinitions={colDefinitions}
  322. rows={collectionList}
  323. rowCount={total}
  324. primaryKey="_name"
  325. openCheckBox={true}
  326. showHoverStyle={true}
  327. selected={selectedCollections}
  328. setSelected={handleSelectChange}
  329. page={currentPage}
  330. onChangePage={handlePageChange}
  331. rowsPerPage={pageSize}
  332. setRowsPerPage={handlePageSize}
  333. isLoading={loading}
  334. />
  335. ) : (
  336. <>
  337. <CustomToolBar toolbarConfigs={toolbarConfigs} />
  338. <EmptyCard
  339. wrapperClass={`page-empty-card ${classes.emptyWrapper}`}
  340. icon={<CollectionIcon />}
  341. text={collectionTrans('noData')}
  342. />
  343. </>
  344. )}
  345. </section>
  346. );
  347. };
  348. export default Collections;