Collections.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. import { useCallback, useContext, useMemo, useState } from 'react';
  2. import { Link, useSearchParams } from 'react-router-dom';
  3. import { makeStyles, Theme, Chip, Tooltip } from '@material-ui/core';
  4. import { useTranslation } from 'react-i18next';
  5. import Highlighter from 'react-highlight-words';
  6. import { rootContext, authContext, dataContext } from '@/context';
  7. import { CollectionService } from '@/http';
  8. import { useNavigationHook, usePaginationHook } from '@/hooks';
  9. import { ALL_ROUTER_TYPES } from '@/router/Types';
  10. import AttuGrid from '@/components/grid/Grid';
  11. import CustomToolBar from '@/components/grid/ToolBar';
  12. import { ColDefinitionsType, ToolBarConfig } from '@/components/grid/Types';
  13. import icons from '@/components/icons/Icons';
  14. import EmptyCard from '@/components/cards/EmptyCard';
  15. import StatusAction from '@/pages/collections/StatusAction';
  16. import CustomToolTip from '@/components/customToolTip/CustomToolTip';
  17. import CreateCollectionDialog from '../dialogs/CreateCollectionDialog';
  18. import LoadCollectionDialog from '../dialogs/LoadCollectionDialog';
  19. import ReleaseCollectionDialog from '../dialogs/ReleaseCollectionDialog';
  20. import DropCollectionDialog from '../dialogs/DropCollectionDialog';
  21. import RenameCollectionDialog from '../dialogs/RenameCollectionDialog';
  22. import DuplicateCollectionDialog from '../dialogs/DuplicateCollectionDailog';
  23. import InsertDialog from '../dialogs/insert/Dialog';
  24. import ImportSampleDialog from '../dialogs/ImportSampleDialog';
  25. import { getLabelDisplayedRows } from '../search/Utils';
  26. import { LOADING_STATE } from '@/consts';
  27. import { formatNumber } from '@/utils';
  28. import Aliases from './Aliases';
  29. import { CollectionObject } from '@server/types';
  30. const useStyles = makeStyles((theme: Theme) => ({
  31. root: {
  32. display: 'flex',
  33. flexDirection: 'column',
  34. height: `100%`,
  35. },
  36. emptyWrapper: {
  37. marginTop: theme.spacing(2),
  38. },
  39. icon: {
  40. fontSize: '14px',
  41. marginLeft: theme.spacing(0.5),
  42. },
  43. dialogContent: {
  44. lineHeight: '24px',
  45. fontSize: '16px',
  46. },
  47. link: {
  48. color: theme.palette.common.black,
  49. display: 'inline-block',
  50. wordBreak: 'break-all',
  51. whiteSpace: 'nowrap',
  52. width: '150px',
  53. overflow: 'hidden',
  54. textOverflow: 'ellipsis',
  55. height: '20px',
  56. },
  57. highlight: {
  58. color: theme.palette.primary.main,
  59. backgroundColor: 'transparent',
  60. },
  61. chip: {
  62. color: theme.palette.text.primary,
  63. marginRight: theme.spacing(0.5),
  64. background: `rgba(0, 0, 0, 0.04)`,
  65. },
  66. }));
  67. const Collections = () => {
  68. useNavigationHook(ALL_ROUTER_TYPES.COLLECTIONS);
  69. const { isManaged } = useContext(authContext);
  70. const { collections, database, loading, fetchCollections, fetchCollection } =
  71. useContext(dataContext);
  72. const [searchParams] = useSearchParams();
  73. const [search, setSearch] = useState<string>(
  74. (searchParams.get('search') as string) || ''
  75. );
  76. const [selectedCollections, setSelectedCollections] = useState<
  77. CollectionObject[]
  78. >([]);
  79. const { setDialog, openSnackBar } = useContext(rootContext);
  80. const { t: collectionTrans } = useTranslation('collection');
  81. const { t: btnTrans } = useTranslation('btn');
  82. const { t: successTrans } = useTranslation('success');
  83. const { t: commonTrans } = useTranslation();
  84. const gridTrans = commonTrans('grid');
  85. const classes = useStyles();
  86. const QuestionIcon = icons.question;
  87. const SourceIcon = icons.source;
  88. const consistencyTooltipsMap: Record<string, string> = {
  89. Strong: collectionTrans('consistencyStrongTooltip'),
  90. Bounded: collectionTrans('consistencyBoundedTooltip'),
  91. Session: collectionTrans('consistencySessionTooltip'),
  92. Eventually: collectionTrans('consistencyEventuallyTooltip'),
  93. };
  94. const clearIndexCache = useCallback(async () => {
  95. await CollectionService.flush();
  96. }, []);
  97. const formatCollections = useMemo(() => {
  98. const filteredCollections = search
  99. ? collections.filter(collection =>
  100. collection.collection_name.includes(search)
  101. )
  102. : collections;
  103. return filteredCollections;
  104. }, [search, collections]);
  105. const {
  106. pageSize,
  107. handlePageSize,
  108. currentPage,
  109. handleCurrentPage,
  110. total,
  111. data: collectionList,
  112. handleGridSort,
  113. order,
  114. orderBy,
  115. } = usePaginationHook(formatCollections);
  116. const toolbarConfigs: ToolBarConfig[] = [
  117. {
  118. label: collectionTrans('create'),
  119. onClick: () => {
  120. setDialog({
  121. open: true,
  122. type: 'custom',
  123. params: {
  124. component: (
  125. <CreateCollectionDialog
  126. onCreate={async () => {
  127. openSnackBar(
  128. successTrans('create', {
  129. name: collectionTrans('collection'),
  130. })
  131. );
  132. }}
  133. />
  134. ),
  135. },
  136. });
  137. },
  138. icon: 'add',
  139. },
  140. {
  141. type: 'button',
  142. btnVariant: 'text',
  143. btnColor: 'secondary',
  144. label: btnTrans('load'),
  145. onClick: () => {
  146. setDialog({
  147. open: true,
  148. type: 'custom',
  149. params: {
  150. component: (
  151. <LoadCollectionDialog
  152. collection={selectedCollections[0].collection_name}
  153. onLoad={async () => {
  154. openSnackBar(
  155. successTrans('load', {
  156. name: collectionTrans('collection'),
  157. })
  158. );
  159. setSelectedCollections([]);
  160. }}
  161. />
  162. ),
  163. },
  164. });
  165. },
  166. icon: 'load',
  167. disabled: data => {
  168. return (
  169. data.length !== 1 ||
  170. data[0].status !== LOADING_STATE.UNLOADED ||
  171. !data[0].schema.hasVectorIndex
  172. );
  173. },
  174. tooltip: btnTrans('loadColTooltip'),
  175. },
  176. {
  177. type: 'button',
  178. btnVariant: 'text',
  179. btnColor: 'secondary',
  180. label: btnTrans('release'),
  181. onClick: () => {
  182. setDialog({
  183. open: true,
  184. type: 'custom',
  185. params: {
  186. component: (
  187. <ReleaseCollectionDialog
  188. collection={selectedCollections[0].collection_name}
  189. onRelease={async () => {
  190. openSnackBar(
  191. successTrans('release', {
  192. name: collectionTrans('collection'),
  193. })
  194. );
  195. setSelectedCollections([]);
  196. }}
  197. />
  198. ),
  199. },
  200. });
  201. },
  202. icon: 'release',
  203. tooltip: btnTrans('releaseColTooltip'),
  204. disabled: data => {
  205. return data.length !== 1 || data[0].status !== LOADING_STATE.LOADED;
  206. },
  207. },
  208. {
  209. icon: 'uploadFile',
  210. type: 'button',
  211. btnVariant: 'text',
  212. btnColor: 'secondary',
  213. label: btnTrans('importFile'),
  214. tooltip: btnTrans('importFileTooltip'),
  215. onClick: () => {
  216. setDialog({
  217. open: true,
  218. type: 'custom',
  219. params: {
  220. component: (
  221. <InsertDialog
  222. collections={formatCollections}
  223. defaultSelectedCollection={
  224. selectedCollections.length === 1
  225. ? selectedCollections[0].collection_name
  226. : ''
  227. }
  228. // user can't select partition on collection page, so default value is ''
  229. defaultSelectedPartition={''}
  230. onInsert={async (collectionName: string) => {
  231. setTimeout(async () => {
  232. await fetchCollection(collectionName);
  233. });
  234. setSelectedCollections([]);
  235. }}
  236. />
  237. ),
  238. },
  239. });
  240. },
  241. /**
  242. * insert validation:
  243. * 1. At least 1 available collection
  244. * 2. selected collections quantity shouldn't over 1
  245. */
  246. disabled: () =>
  247. collectionList.length === 0 || selectedCollections.length > 1,
  248. },
  249. {
  250. icon: 'edit',
  251. type: 'button',
  252. btnColor: 'secondary',
  253. btnVariant: 'text',
  254. onClick: () => {
  255. setDialog({
  256. open: true,
  257. type: 'custom',
  258. params: {
  259. component: (
  260. <RenameCollectionDialog
  261. cb={async (collectionName: string) => {
  262. openSnackBar(
  263. successTrans('rename', {
  264. name: collectionTrans('collection'),
  265. })
  266. );
  267. setSelectedCollections([]);
  268. }}
  269. collectionName={selectedCollections[0].collection_name}
  270. />
  271. ),
  272. },
  273. });
  274. },
  275. label: btnTrans('rename'),
  276. tooltip: btnTrans('renameTooltip'),
  277. disabled: data => data.length !== 1,
  278. },
  279. {
  280. icon: 'copy',
  281. type: 'button',
  282. btnVariant: 'text',
  283. onClick: () => {
  284. setDialog({
  285. open: true,
  286. type: 'custom',
  287. params: {
  288. component: (
  289. <DuplicateCollectionDialog
  290. cb={async () => {
  291. openSnackBar(
  292. successTrans('duplicate', {
  293. name: collectionTrans('collection'),
  294. })
  295. );
  296. setSelectedCollections([]);
  297. }}
  298. collectionName={selectedCollections[0].collection_name}
  299. collections={collections}
  300. />
  301. ),
  302. },
  303. });
  304. },
  305. label: btnTrans('duplicate'),
  306. tooltip: btnTrans('duplicateTooltip'),
  307. disabled: data => data.length !== 1,
  308. },
  309. {
  310. icon: 'delete',
  311. type: 'button',
  312. btnVariant: 'text',
  313. onClick: () => {
  314. setDialog({
  315. open: true,
  316. type: 'custom',
  317. params: {
  318. component: (
  319. <DropCollectionDialog
  320. onDelete={async () => {
  321. openSnackBar(
  322. successTrans('delete', {
  323. name: collectionTrans('collection'),
  324. })
  325. );
  326. setSelectedCollections([]);
  327. }}
  328. collections={selectedCollections}
  329. />
  330. ),
  331. },
  332. });
  333. },
  334. label: btnTrans('drop'),
  335. tooltip: btnTrans('deleteColTooltip'),
  336. disabledTooltip: btnTrans('deleteDisableTooltip'),
  337. disabled: data => data.length < 1,
  338. },
  339. {
  340. icon: 'refresh',
  341. type: 'button',
  342. btnVariant: 'text',
  343. onClick: () => {
  344. clearIndexCache();
  345. fetchCollections();
  346. },
  347. disabled: () => {
  348. return loading;
  349. },
  350. label: btnTrans('refresh'),
  351. },
  352. {
  353. label: 'Search',
  354. icon: 'search',
  355. searchText: search,
  356. onSearch: (value: string) => {
  357. setSearch(value);
  358. },
  359. },
  360. ];
  361. const colDefinitions: ColDefinitionsType[] = [
  362. {
  363. id: 'collection_name',
  364. align: 'left',
  365. disablePadding: true,
  366. sortBy: 'collection_name',
  367. formatter({ collection_name }) {
  368. return (
  369. <Link
  370. to={`/databases/${database}/${collection_name}/data`}
  371. className={classes.link}
  372. title={collection_name}
  373. >
  374. <Highlighter
  375. textToHighlight={collection_name}
  376. searchWords={[search]}
  377. highlightClassName={classes.highlight}
  378. />
  379. </Link>
  380. );
  381. },
  382. label: collectionTrans('name'),
  383. },
  384. {
  385. id: 'status',
  386. align: 'left',
  387. disablePadding: false,
  388. sortBy: 'loadedPercentage',
  389. label: collectionTrans('status'),
  390. formatter(v) {
  391. return (
  392. <StatusAction
  393. status={v.status}
  394. percentage={v.loadedPercentage}
  395. field={v.schema}
  396. collectionName={v.collection_name}
  397. action={() => {
  398. setDialog({
  399. open: true,
  400. type: 'custom',
  401. params: {
  402. component:
  403. v.status === LOADING_STATE.UNLOADED ? (
  404. <LoadCollectionDialog
  405. collectionName={v.collection_name}
  406. onLoad={async () => {
  407. openSnackBar(
  408. successTrans('load', {
  409. name: collectionTrans('collection'),
  410. })
  411. );
  412. }}
  413. />
  414. ) : (
  415. <ReleaseCollectionDialog
  416. collectionName={v.collection_name}
  417. onRelease={async () => {
  418. openSnackBar(
  419. successTrans('release', {
  420. name: collectionTrans('collection'),
  421. })
  422. );
  423. }}
  424. />
  425. ),
  426. },
  427. });
  428. }}
  429. />
  430. );
  431. },
  432. },
  433. {
  434. id: 'collection_name',
  435. align: 'left',
  436. disablePadding: true,
  437. notSort: true,
  438. label: collectionTrans('features'),
  439. formatter(v) {
  440. return (
  441. <>
  442. {v.autoID ? (
  443. <Tooltip
  444. title={collectionTrans('autoIDTooltip')}
  445. placement="top"
  446. arrow
  447. >
  448. <Chip
  449. className={classes.chip}
  450. label={collectionTrans('autoID')}
  451. size="small"
  452. />
  453. </Tooltip>
  454. ) : null}
  455. {v.schema && v.schema.enable_dynamic_field ? (
  456. <Tooltip
  457. title={collectionTrans('dynamicSchemaTooltip')}
  458. placement="top"
  459. arrow
  460. >
  461. <Chip
  462. className={classes.chip}
  463. label={collectionTrans('dynamicSchema')}
  464. size="small"
  465. />
  466. </Tooltip>
  467. ) : null}
  468. <Tooltip
  469. title={consistencyTooltipsMap[v.consistency_level] || ''}
  470. placement="top"
  471. arrow
  472. >
  473. <Chip
  474. className={classes.chip}
  475. label={v.consistency_level}
  476. size="small"
  477. />
  478. </Tooltip>
  479. </>
  480. );
  481. },
  482. },
  483. {
  484. id: 'rowCount',
  485. align: 'left',
  486. disablePadding: false,
  487. sortBy: 'rowCount',
  488. label: (
  489. <span className="flex-center with-max-content">
  490. {collectionTrans('rowCount')}
  491. <CustomToolTip title={collectionTrans('entityCountInfo')}>
  492. <QuestionIcon classes={{ root: classes.icon }} />
  493. </CustomToolTip>
  494. </span>
  495. ),
  496. formatter(v) {
  497. return formatNumber(Number(v.rowCount));
  498. },
  499. },
  500. {
  501. id: 'description',
  502. align: 'left',
  503. disablePadding: false,
  504. label: (
  505. <span className="flex-center with-max-content">
  506. {collectionTrans('description')}
  507. </span>
  508. ),
  509. formatter(v) {
  510. return v.description || '--';
  511. },
  512. },
  513. {
  514. id: 'createdTime',
  515. align: 'left',
  516. disablePadding: false,
  517. label: collectionTrans('createdTime'),
  518. formatter(data) {
  519. return new Date(data.createdTime).toLocaleString();
  520. },
  521. },
  522. {
  523. id: 'import',
  524. align: 'center',
  525. disablePadding: false,
  526. label: '',
  527. showActionCell: true,
  528. isHoverAction: true,
  529. actionBarConfigs: [
  530. {
  531. onClick: (e: React.MouseEvent, row: CollectionObject) => {
  532. setDialog({
  533. open: true,
  534. type: 'custom',
  535. params: {
  536. component: (
  537. <ImportSampleDialog
  538. collection={row.collection_name}
  539. cb={async (collectionName: string) => {
  540. setTimeout(async () => {
  541. await fetchCollection(collectionName);
  542. });
  543. }}
  544. />
  545. ),
  546. },
  547. });
  548. },
  549. icon: 'source',
  550. label: 'Import',
  551. showIconMethod: 'renderFn',
  552. getLabel: () => 'Import sample data',
  553. renderIconFn: () => <SourceIcon />,
  554. },
  555. ],
  556. },
  557. ];
  558. if (!isManaged) {
  559. colDefinitions.splice(4, 0, {
  560. id: 'aliases',
  561. align: 'left',
  562. disablePadding: false,
  563. label: (
  564. <span className="flex-center with-max-content">
  565. {collectionTrans('alias')}
  566. <CustomToolTip title={collectionTrans('aliasInfo')}>
  567. <QuestionIcon classes={{ root: classes.icon }} />
  568. </CustomToolTip>
  569. </span>
  570. ),
  571. formatter(v) {
  572. return (
  573. <Aliases aliases={v.aliases} collectionName={v.collection_name} />
  574. );
  575. },
  576. });
  577. }
  578. const handleSelectChange = (value: any) => {
  579. setSelectedCollections(value);
  580. };
  581. const handlePageChange = (e: any, page: number) => {
  582. handleCurrentPage(page);
  583. setSelectedCollections([]);
  584. };
  585. const CollectionIcon = icons.navCollection;
  586. return (
  587. <section className={classes.root}>
  588. {collections.length > 0 || loading ? (
  589. <AttuGrid
  590. toolbarConfigs={toolbarConfigs}
  591. colDefinitions={colDefinitions}
  592. rows={collectionList}
  593. rowCount={total}
  594. primaryKey="id"
  595. selected={selectedCollections}
  596. setSelected={handleSelectChange}
  597. page={currentPage}
  598. onPageChange={handlePageChange}
  599. rowsPerPage={pageSize}
  600. tableHeaderHeight={49}
  601. rowHeight={49}
  602. setRowsPerPage={handlePageSize}
  603. isLoading={loading}
  604. handleSort={handleGridSort}
  605. order={order}
  606. orderBy={orderBy}
  607. hideOnDisable={true}
  608. labelDisplayedRows={getLabelDisplayedRows(gridTrans.collections)}
  609. />
  610. ) : (
  611. <>
  612. <CustomToolBar toolbarConfigs={toolbarConfigs} />
  613. <EmptyCard
  614. wrapperClass={`page-empty-card ${classes.emptyWrapper}`}
  615. icon={<CollectionIcon />}
  616. text={collectionTrans('noData')}
  617. />
  618. </>
  619. )}
  620. </section>
  621. );
  622. };
  623. export default Collections;