123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- import { useCallback, useContext, useEffect, useState } from 'react';
- import { Link } from 'react-router-dom';
- import { useNavigationHook } from '../../hooks/Navigation';
- import { ALL_ROUTER_TYPES } from '../../router/Types';
- import MilvusGrid from '../../components/grid/Grid';
- import CustomToolBar from '../../components/grid/ToolBar';
- import { CollectionCreateParam, CollectionView, DataTypeEnum } from './Types';
- import { ColDefinitionsType, ToolBarConfig } from '../../components/grid/Types';
- import { usePaginationHook } from '../../hooks/Pagination';
- import icons from '../../components/icons/Icons';
- import EmptyCard from '../../components/cards/EmptyCard';
- import Status from '../../components/status/Status';
- import { useTranslation } from 'react-i18next';
- import { ChildrenStatusType, StatusEnum } from '../../components/status/Types';
- import { makeStyles, Theme } from '@material-ui/core';
- import StatusIcon from '../../components/status/StatusIcon';
- import CustomToolTip from '../../components/customToolTip/CustomToolTip';
- import { rootContext } from '../../context/Root';
- import CreateCollection from './Create';
- import DeleteTemplate from '../../components/customDialog/DeleteDialogTemplate';
- import { CollectionHttp } from '../../http/Collection';
- import { useDialogHook } from '../../hooks/Dialog';
- import Highlighter from 'react-highlight-words';
- import { parseLocationSearch } from '../../utils/Format';
- const useStyles = makeStyles((theme: Theme) => ({
- emptyWrapper: {
- marginTop: theme.spacing(2),
- },
- icon: {
- fontSize: '20px',
- marginLeft: theme.spacing(0.5),
- },
- dialogContent: {
- lineHeight: '24px',
- fontSize: '16px',
- },
- link: {
- color: theme.palette.common.black,
- },
- highlight: {
- color: theme.palette.primary.main,
- backgroundColor: 'transparent',
- },
- }));
- let timer: NodeJS.Timeout | null = null;
- // get init search value from url
- const { search = '' } = parseLocationSearch(window.location.search);
- const Collections = () => {
- useNavigationHook(ALL_ROUTER_TYPES.COLLECTIONS);
- const { handleAction } = useDialogHook({ type: 'collection' });
- const [collections, setCollections] = useState<CollectionView[]>([]);
- const [searchedCollections, setSearchedCollections] = useState<
- CollectionView[]
- >([]);
- const {
- pageSize,
- handlePageSize,
- currentPage,
- handleCurrentPage,
- total,
- data: collectionList,
- } = usePaginationHook(searchedCollections);
- const [loading, setLoading] = useState<boolean>(true);
- const [selectedCollections, setSelectedCollections] = useState<
- CollectionView[]
- >([]);
- const { setDialog, handleCloseDialog, openSnackBar } =
- useContext(rootContext);
- const { t: collectionTrans } = useTranslation('collection');
- const { t: btnTrans } = useTranslation('btn');
- const { t: dialogTrans } = useTranslation('dialog');
- const { t: successTrans } = useTranslation('success');
- const classes = useStyles();
- const LoadIcon = icons.load;
- const ReleaseIcon = icons.release;
- const InfoIcon = icons.info;
- const fetchData = useCallback(async () => {
- try {
- const res = await CollectionHttp.getCollections();
- const statusRes = await CollectionHttp.getCollectionsIndexState();
- setLoading(false);
- const collections = res.map(v => {
- const indexStatus = statusRes.find(item => item._name === v._name);
- Object.assign(v, {
- nameElement: (
- <Link to={`/collections/${v._name}`} className={classes.link}>
- <Highlighter
- textToHighlight={v._name}
- searchWords={[search]}
- highlightClassName={classes.highlight}
- />
- </Link>
- ),
- statusElement: <Status status={v._status} />,
- indexCreatingElement: (
- <StatusIcon
- type={indexStatus?._indexState || ChildrenStatusType.FINISH}
- />
- ),
- });
- return v;
- });
- // filter collection if url contains search param
- const filteredCollections = collections.filter(collection =>
- collection._name.includes(search)
- );
- setCollections(collections);
- setSearchedCollections(filteredCollections);
- } catch (err) {
- setLoading(false);
- }
- }, [classes.link, classes.highlight]);
- useEffect(() => {
- fetchData();
- }, [fetchData]);
- const handleCreateCollection = async (param: CollectionCreateParam) => {
- const data: CollectionCreateParam = JSON.parse(JSON.stringify(param));
- const vectorType = [DataTypeEnum.BinaryVector, DataTypeEnum.FloatVector];
- data.fields = data.fields.map(v =>
- vectorType.includes(v.data_type)
- ? {
- ...v,
- type_params: [{ key: 'dim', value: v.dimension }],
- }
- : v
- );
- await CollectionHttp.createCollection(data);
- handleCloseDialog();
- openSnackBar(
- successTrans('create', { name: collectionTrans('collection') })
- );
- fetchData();
- };
- const handleRelease = async (data: CollectionView) => {
- const res = await CollectionHttp.releaseCollection(data._name);
- openSnackBar(
- successTrans('release', { name: collectionTrans('collection') })
- );
- fetchData();
- return res;
- };
- const handleLoad = async (data: CollectionView) => {
- const res = await CollectionHttp.loadCollection(data._name);
- openSnackBar(successTrans('load', { name: collectionTrans('collection') }));
- fetchData();
- return res;
- };
- const handleDelete = async () => {
- for (const item of selectedCollections) {
- await CollectionHttp.deleteCollection(item._name);
- }
- openSnackBar(
- successTrans('delete', { name: collectionTrans('collection') })
- );
- fetchData();
- handleCloseDialog();
- setSelectedCollections([]);
- };
- const handleSearch = (value: string) => {
- if (timer) {
- clearTimeout(timer);
- }
- // add loading manually
- setLoading(true);
- timer = setTimeout(() => {
- const searchWords = [value];
- const list = value
- ? collections.filter(c => c._name.includes(value))
- : collections;
- const highlightList = list.map(c => {
- Object.assign(c, {
- nameElement: (
- <Link to={`/collections/${c._name}`} className={classes.link}>
- <Highlighter
- textToHighlight={c._name}
- searchWords={searchWords}
- highlightClassName={classes.highlight}
- />
- </Link>
- ),
- });
- return c;
- });
- setLoading(false);
- setSearchedCollections(highlightList);
- }, 300);
- };
- const toolbarConfigs: ToolBarConfig[] = [
- {
- label: collectionTrans('create'),
- onClick: () => {
- setDialog({
- open: true,
- type: 'custom',
- params: {
- component: (
- <CreateCollection handleCreate={handleCreateCollection} />
- ),
- },
- });
- },
- icon: 'add',
- },
- {
- type: 'iconBtn',
- onClick: () => {
- setDialog({
- open: true,
- type: 'custom',
- params: {
- component: (
- <DeleteTemplate
- label={btnTrans('delete')}
- title={dialogTrans('deleteTitle', {
- type: collectionTrans('collection'),
- })}
- text={collectionTrans('deleteWarning')}
- handleDelete={handleDelete}
- />
- ),
- },
- });
- },
- label: collectionTrans('delete'),
- icon: 'delete',
- disabled: data => data.length === 0,
- },
- {
- label: 'Search',
- icon: 'search',
- searchText: search,
- onSearch: (value: string) => {
- handleSearch(value);
- },
- },
- ];
- const colDefinitions: ColDefinitionsType[] = [
- {
- id: '_id',
- align: 'left',
- disablePadding: true,
- label: collectionTrans('id'),
- },
- {
- id: 'nameElement',
- align: 'left',
- disablePadding: true,
- sortBy: '_name',
- label: collectionTrans('name'),
- },
- {
- id: 'statusElement',
- align: 'left',
- disablePadding: false,
- sortBy: '_status',
- label: collectionTrans('status'),
- },
- {
- id: '_rowCount',
- align: 'left',
- disablePadding: false,
- label: (
- <span className="flex-center">
- {collectionTrans('rowCount')}
- <CustomToolTip title={collectionTrans('tooltip')}>
- <InfoIcon classes={{ root: classes.icon }} />
- </CustomToolTip>
- </span>
- ),
- },
- {
- id: '_desc',
- align: 'left',
- disablePadding: false,
- label: collectionTrans('desc'),
- },
- {
- id: 'indexCreatingElement',
- align: 'left',
- disablePadding: false,
- label: '',
- },
- {
- id: 'action',
- align: 'center',
- disablePadding: false,
- label: '',
- showActionCell: true,
- isHoverAction: true,
- actionBarConfigs: [
- {
- onClick: (e: React.MouseEvent, row: CollectionView) => {
- const cb =
- row._status === StatusEnum.unloaded ? handleLoad : handleRelease;
- handleAction(row, cb);
- },
- icon: 'load',
- label: 'load',
- showIconMethod: 'renderFn',
- getLabel: (row: CollectionView) =>
- row._status === StatusEnum.loaded ? 'release' : 'load',
- renderIconFn: (row: CollectionView) =>
- row._status === StatusEnum.loaded ? <ReleaseIcon /> : <LoadIcon />,
- },
- ],
- },
- ];
- const handleSelectChange = (value: any) => {
- setSelectedCollections(value);
- };
- const handlePageChange = (e: any, page: number) => {
- handleCurrentPage(page);
- setSelectedCollections([]);
- };
- const CollectionIcon = icons.navCollection;
- return (
- <section className="page-wrapper">
- {collections.length > 0 || loading ? (
- <MilvusGrid
- toolbarConfigs={toolbarConfigs}
- colDefinitions={colDefinitions}
- rows={collectionList}
- rowCount={total}
- primaryKey="_name"
- openCheckBox={true}
- showHoverStyle={true}
- selected={selectedCollections}
- setSelected={handleSelectChange}
- page={currentPage}
- onChangePage={handlePageChange}
- rowsPerPage={pageSize}
- setRowsPerPage={handlePageSize}
- isLoading={loading}
- />
- ) : (
- <>
- <CustomToolBar toolbarConfigs={toolbarConfigs} />
- <EmptyCard
- wrapperClass={`page-empty-card ${classes.emptyWrapper}`}
- icon={<CollectionIcon />}
- text={collectionTrans('noData')}
- />
- </>
- )}
- </section>
- );
- };
- export default Collections;
|