123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- import { makeStyles, Theme } from '@material-ui/core';
- import { useContext, useEffect, useState } from 'react';
- import { useSearchParams, useParams } from 'react-router-dom';
- import Highlighter from 'react-highlight-words';
- import AttuGrid from '@/components/grid/Grid';
- import { ColDefinitionsType, ToolBarConfig } from '@/components/grid/Types';
- import { useTranslation } from 'react-i18next';
- import { usePaginationHook, useInsertDialogHook } from '@/hooks';
- import icons from '@/components/icons/Icons';
- import CustomToolTip from '@/components/customToolTip/CustomToolTip';
- import { rootContext } from '@/context';
- import { Collection, Partition, FieldHttp, DataService } from '@/http';
- import InsertContainer from '../dialogs/insert/Dialog';
- import { InsertDataParam } from '../collections/Types';
- import CreatePartitionDialog from '../dialogs/CreatePartitionDialog';
- import DropPartitionDialog from '../dialogs/DropPartitionDialog';
- import { PartitionView } from './Types';
- const useStyles = makeStyles((theme: Theme) => ({
- wrapper: {
- height: `calc(100vh - 160px)`,
- },
- icon: {
- fontSize: '20px',
- marginLeft: theme.spacing(0.5),
- },
- highlight: {
- color: theme.palette.primary.main,
- backgroundColor: 'transparent',
- },
- }));
- let timer: NodeJS.Timeout | null = null;
- const Partitions = () => {
- const { collectionName = '' } = useParams<{ collectionName: string }>();
- const classes = useStyles();
- const { t } = useTranslation('partition');
- const { t: successTrans } = useTranslation('success');
- const { t: btnTrans } = useTranslation('btn');
- const [searchParams] = useSearchParams();
- const [search, setSearch] = useState<string>(
- (searchParams.get('search') as string) || ''
- );
- const InfoIcon = icons.info;
- const { handleInsertDialog } = useInsertDialogHook();
- const [selectedPartitions, setSelectedPartitions] = useState<PartitionView[]>(
- []
- );
- const [partitions, setPartitions] = useState<PartitionView[]>([]);
- const [searchedPartitions, setSearchedPartitions] = useState<PartitionView[]>(
- []
- );
- const {
- pageSize,
- handlePageSize,
- currentPage,
- handleCurrentPage,
- total,
- data: partitionList,
- order,
- orderBy,
- handleGridSort,
- } = usePaginationHook(searchedPartitions);
- const [loading, setLoading] = useState<boolean>(true);
- const { setDialog, openSnackBar } = useContext(rootContext);
- const fetchPartitions = async (collectionName: string) => {
- try {
- const res = await Partition.getPartitions(collectionName);
- setLoading(false);
- setPartitions(res);
- } catch (err) {
- setLoading(false);
- }
- };
- const fetchCollectionDetail = async (name: string) => {
- const res = await Collection.getCollectionInfo(name);
- return res;
- };
- useEffect(() => {
- fetchPartitions(collectionName);
- }, [collectionName]);
- useEffect(() => {
- if (timer) {
- clearTimeout(timer);
- }
- // add loading manually
- setLoading(true);
- timer = setTimeout(() => {
- const searchWords = [search];
- const list = search
- ? partitions.filter(p => p.partitionName.includes(search))
- : partitions;
- const highlightList = list.map(c => {
- Object.assign(c, {
- _nameElement: (
- <Highlighter
- textToHighlight={c.partitionName}
- searchWords={searchWords}
- highlightClassName={classes.highlight}
- />
- ),
- });
- return c;
- });
- setLoading(false);
- setSearchedPartitions(highlightList);
- }, 300);
- }, [search, partitions]);
- const onDelete = () => {
- openSnackBar(successTrans('delete', { name: t('partition') }));
- fetchPartitions(collectionName);
- };
- const handleSearch = (value: string) => {
- setSearch(value);
- };
- const handleInsert = async (
- collectionName: string,
- partitionName: string,
- fieldData: any[]
- ): Promise<{ result: boolean; msg: string }> => {
- const param: InsertDataParam = {
- partition_name: partitionName,
- fields_data: fieldData,
- };
- try {
- await DataService.insertData(collectionName, param);
- await DataService.flush(collectionName);
- // update partitions
- fetchPartitions(collectionName);
- return { result: true, msg: '' };
- } catch (err: any) {
- const {
- response: {
- data: { message },
- },
- } = err;
- return { result: false, msg: message || '' };
- }
- };
- const toolbarConfigs: ToolBarConfig[] = [
- {
- label: t('create'),
- onClick: () => {
- setDialog({
- open: true,
- type: 'custom',
- params: {
- component: (
- <CreatePartitionDialog
- collectionName={collectionName}
- onCreate={onCreate}
- />
- ),
- },
- });
- },
- icon: 'add',
- },
- {
- type: 'button',
- btnVariant: 'text',
- btnColor: 'secondary',
- label: btnTrans('insert'),
- onClick: async () => {
- const collection = await fetchCollectionDetail(collectionName);
- const schema = collection.schema.fields.map(f => new FieldHttp(f));
- handleInsertDialog(
- <InsertContainer
- schema={schema}
- defaultSelectedCollection={collectionName}
- defaultSelectedPartition={
- selectedPartitions.length === 1
- ? selectedPartitions[0].partitionName
- : ''
- }
- partitions={partitions}
- handleInsert={handleInsert}
- />
- );
- },
- /**
- * insert validation:
- * 1. At least 1 available partition
- * 2. selected partition quantity shouldn't over 1
- */
- disabled: () => partitions.length === 0 || selectedPartitions.length > 1,
- },
- {
- icon: 'delete',
- type: 'button',
- btnVariant: 'text',
- btnColor: 'secondary',
- onClick: () => {
- setDialog({
- open: true,
- type: 'custom',
- params: {
- component: (
- <DropPartitionDialog
- partitions={selectedPartitions}
- collectionName={collectionName}
- onDelete={onDelete}
- />
- ),
- },
- });
- },
- label: btnTrans('drop'),
- // can't delete default partition
- disabled: () =>
- selectedPartitions.length === 0 ||
- selectedPartitions.some(p => p.name === '_default'),
- tooltip: selectedPartitions.some(p => p.name === '_default')
- ? t('deletePartitionError')
- : '',
- },
- {
- label: 'Search',
- icon: 'search',
- searchText: search,
- onSearch: (value: string) => {
- handleSearch(value);
- },
- },
- ];
- const colDefinitions: ColDefinitionsType[] = [
- {
- id: '_nameElement',
- align: 'left',
- disablePadding: false,
- label: t('name'),
- },
- {
- id: 'createdAt',
- align: 'left',
- disablePadding: false,
- label: t('createdTime'),
- },
- // {
- // id: '_statusElement',
- // align: 'left',
- // disablePadding: false,
- // label: t('status'),
- // },
- {
- id: 'entityCount',
- align: 'left',
- disablePadding: false,
- label: (
- <span className="flex-center">
- {t('rowCount')}
- <CustomToolTip title={t('tooltip')}>
- <InfoIcon classes={{ root: classes.icon }} />
- </CustomToolTip>
- </span>
- ),
- },
- // {
- // id: 'action',
- // align: 'center',
- // disablePadding: false,
- // label: '',
- // showActionCell: true,
- // isHoverAction: true,
- // actionBarConfigs: [
- // {
- // onClick: (e: React.MouseEvent, row: PartitionView) => {
- // const cb =
- // row._status === StatusEnum.unloaded ? handleLoad : handleRelease;
- // handleAction(row, cb);
- // },
- // icon: 'load',
- // label: 'load',
- // showIconMethod: 'renderFn',
- // getLabel: (row: PartitionView) =>
- // row._status === StatusEnum.loaded ? 'release' : 'load',
- // renderIconFn: (row: PartitionView) =>
- // row._status === StatusEnum.loaded ? <ReleaseIcon /> : <LoadIcon />,
- // },
- // ],
- // },
- ];
- const handleSelectChange = (value: PartitionView[]) => {
- setSelectedPartitions(value);
- };
- const handlePageChange = (e: any, page: number) => {
- handleCurrentPage(page);
- setSelectedPartitions([]);
- };
- const onCreate = () => {
- openSnackBar(successTrans('create', { name: t('partition') }));
- // refresh partitions
- fetchPartitions(collectionName);
- setSelectedPartitions([]);
- };
- return (
- <section className={classes.wrapper}>
- <AttuGrid
- toolbarConfigs={toolbarConfigs}
- colDefinitions={colDefinitions}
- rows={partitionList}
- rowCount={total}
- primaryKey="id"
- selected={selectedPartitions}
- setSelected={handleSelectChange}
- page={currentPage}
- onPageChange={handlePageChange}
- rowsPerPage={pageSize}
- setRowsPerPage={handlePageSize}
- isLoading={loading}
- order={order}
- orderBy={orderBy}
- handleSort={handleGridSort}
- />
- </section>
- );
- };
- export default Partitions;
|