Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve routers for collections and users page #377

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions client/src/components/customTabList/RouteTabList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { Box, makeStyles, Tab, Tabs, Theme } from '@material-ui/core';
import { FC, useState } from 'react';
import { useNavigate, useLocation } from 'react-router-dom';
import { ITabListProps, ITabPanel } from './Types';

const useStyles = makeStyles((theme: Theme) => ({
wrapper: {
display: 'flex',
flexDirection: 'column',
flexBasis: 0,
flexGrow: 1,
'& .MuiTab-wrapper': {
textTransform: 'capitalize',
fontWeight: 'bold',
color: '#323232',
},
},
tab: {
height: theme.spacing(0.5),
backgroundColor: theme.palette.primary.main,
},
tabContainer: {
borderBottom: '1px solid #e9e9ed',
},
tabContent: {
minWidth: 0,
marginRight: theme.spacing(3),
},
tabPanel: {
flexBasis: 0,
flexGrow: 1,
marginTop: theme.spacing(2),
overflow: 'hidden',
},
}));

const TabPanel = (props: ITabPanel) => {
const { children, value, index, className = '', ...other } = props;

return (
<div
role="tabpanel"
hidden={value !== index}
className={className}
id={`tabpanel-${index}`}
aria-labelledby={`tabpanel-${index}`}
{...other}
>
{value === index && <Box height="100%">{children}</Box>}
</div>
);
};

const a11yProps = (index: number) => {
return {
id: `tab-${index}`,
'aria-controls': `tabpanel-${index}`,
};
};

const RouteTabList: FC<ITabListProps> = props => {
const { tabs, activeIndex = 0, wrapperClass = '' } = props;
const classes = useStyles();
const [value, setValue] = useState<number>(activeIndex);
const navigate = useNavigate();
const location = useLocation();

const handleChange = (event: any, newValue: any) => {
setValue(newValue);
const newPath =
location.pathname.split('/').slice(0, -1).join('/') +
'/' +
tabs[newValue].path;

navigate(`${newPath}`);
};

return (
<div className={`${classes.wrapper} ${wrapperClass}`}>
<Tabs
classes={{
indicator: classes.tab,
flexContainer: classes.tabContainer,
}}
// if not provide this property, Material will add single span element by default
TabIndicatorProps={{ children: <div className="tab-indicator" /> }}
value={value}
onChange={handleChange}
aria-label="tabs"
>
{tabs.map((tab, index) => (
<Tab
classes={{ root: classes.tabContent }}
textColor="primary"
key={tab.label}
label={tab.label}
{...a11yProps(index)}
></Tab>
))}
</Tabs>

{tabs.map((tab, index) => (
<TabPanel
key={tab.label}
value={value}
index={index}
className={classes.tabPanel}
>
{tab.component}
</TabPanel>
))}
</div>
);
};

export default RouteTabList;
3 changes: 2 additions & 1 deletion client/src/components/customTabList/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { ReactElement } from 'react';
export interface ITab {
label: string;
component: ReactElement;
path?: string;
}

export interface ITabListProps {
tabs: ITab[];
activeIndex?: number;
handleTabChange?: (index: number) => void;
handleTabChange?:(index:number) => void;
wrapperClass?: string;
}

Expand Down
48 changes: 19 additions & 29 deletions client/src/pages/collections/Collection.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { useMemo, useContext } from 'react';
import { useNavigate, useLocation, useParams } from 'react-router-dom';
import { useContext } from 'react';
import { useParams } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { makeStyles, Theme } from '@material-ui/core';
import { authContext } from '@/context';
import { useNavigationHook } from '@/hooks';
import { ALL_ROUTER_TYPES } from '@/router/Types';
import CustomTabList from '@/components/customTabList/CustomTabList';
import RouteTabList from '@/components/customTabList/RouteTabList';
import { ITab } from '@/components/customTabList/Types';
import Partitions from '../partitions/Partitions';
import { parseLocationSearch } from '@/utils';
import Schema from '../schema/Schema';
import Query from '../query/Query';
import Preview from '../preview/Preview';
import Segments from '../segments/Segments';
import { TAB_ENUM } from './Types';

const useStyles = makeStyles((theme: Theme) => ({
wrapper: {
Expand All @@ -38,49 +36,40 @@ const Collection = () => {
const classes = useStyles();
const { isManaged } = useContext(authContext);

const { collectionName = '' } = useParams<{
const { collectionName = '', tab = '' } = useParams<{
collectionName: string;
tab: string;
}>();

useNavigationHook(ALL_ROUTER_TYPES.COLLECTION_DETAIL, { collectionName });

const navigate = useNavigate();
const location = useLocation();

const { t: collectionTrans } = useTranslation('collection');

const activeTabIndex = useMemo(() => {
const { activeIndex } = location.search
? parseLocationSearch(location.search)
: { activeIndex: TAB_ENUM.schema };
return Number(activeIndex);
}, [location]);

const handleTabChange = (activeIndex: number) => {
const path = location.pathname;
navigate(`${path}?activeIndex=${activeIndex}`);
};

const tabs: ITab[] = [
{
label: collectionTrans('schemaTab'),
component: <Schema collectionName={collectionName} />,
component: <Schema />,
path: `schema`,
},
{
label: collectionTrans('partitionTab'),
component: <Partitions collectionName={collectionName} />,
component: <Partitions />,
path: `partitions`,
},
{
label: collectionTrans('previewTab'),
component: <Preview collectionName={collectionName} />,
component: <Preview />,
path: `preview`,
},
{
label: collectionTrans('queryTab'),
component: <Query collectionName={collectionName} />,
component: <Query />,
path: `query`,
},
{
label: collectionTrans('segmentsTab'),
component: <Segments collectionName={collectionName} />,
component: <Segments />,
path: `segments`,
},
];

Expand All @@ -89,13 +78,14 @@ const Collection = () => {
tabs.splice(1, 1);
}

const activeTab = tabs.findIndex(t => t.path === tab);

return (
<section className={`page-wrapper ${classes.wrapper}`}>
<CustomTabList
<RouteTabList
tabs={tabs}
wrapperClass={classes.tab}
activeIndex={activeTabIndex}
handleTabChange={handleTabChange}
activeIndex={activeTab !== -1 ? activeTab : 0}
/>
</section>
);
Expand Down
2 changes: 1 addition & 1 deletion client/src/pages/collections/Collections.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ const Collections = () => {
Object.assign(v, {
nameElement: (
<Link
to={`/collections/${v.collectionName}`}
to={`/collections/${v.collectionName}/schema`}
className={classes.link}
title={v.collectionName}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ const CollectionCard: FC<CollectionCardProps> = ({
<div>
<Status status={status} percentage={loadedPercentage} />
</div>
<Link className="link" to={`/collections/${collectionName}`}>
<Link className="link" to={`/collections/${collectionName}/schema`}>
{collectionName}
<RightArrowIcon classes={{ root: classes.icon }} />
</Link>
Expand Down
15 changes: 6 additions & 9 deletions client/src/pages/partitions/Partitions.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { makeStyles, Theme } from '@material-ui/core';
import { FC, useContext, useEffect, useState } from 'react';
import { useSearchParams } from 'react-router-dom';
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';
Expand Down Expand Up @@ -31,11 +31,9 @@ const useStyles = makeStyles((theme: Theme) => ({
}));

let timer: NodeJS.Timeout | null = null;
// get init search value from url
// const { search = '' } = parseLocationSearch(window.location.search);
const Partitions: FC<{
collectionName: string;
}> = ({ collectionName }) => {

const Partitions = () => {
const { collectionName = '' } = useParams<{ collectionName: string }>();
const classes = useStyles();
const { t } = useTranslation('partition');
const { t: successTrans } = useTranslation('success');
Expand Down Expand Up @@ -67,8 +65,7 @@ const Partitions: FC<{
handleGridSort,
} = usePaginationHook(searchedPartitions);
const [loading, setLoading] = useState<boolean>(true);
const { setDialog, handleCloseDialog, openSnackBar } =
useContext(rootContext);
const { setDialog, openSnackBar } = useContext(rootContext);

const fetchPartitions = async (collectionName: string) => {
try {
Expand Down
10 changes: 5 additions & 5 deletions client/src/pages/preview/Preview.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FC, useEffect, useState } from 'react';
import { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import AttuGrid from '@/components/grid/Grid';
import { useParams } from 'react-router-dom';
import { Collection, MilvusIndex } from '@/http';
import { usePaginationHook, useSearchResult } from '@/hooks';
import { generateVector } from '@/utils';
Expand All @@ -15,9 +16,8 @@ import { ToolBarConfig } from '@/components/grid/Types';
import CustomToolBar from '@/components/grid/ToolBar';
import { getQueryStyles } from '../query/Styles';

const Preview: FC<{
collectionName: string;
}> = ({ collectionName }) => {
const Preview = () => {
const { collectionName } = useParams<{ collectionName: string }>();
const [fields, setFields] = useState<any[]>([]);
const [tableLoading, setTableLoading] = useState<any>();
const [queryResult, setQueryResult] = useState<any>();
Expand Down Expand Up @@ -140,7 +140,7 @@ const Preview: FC<{
type: 'button',
btnVariant: 'text',
onClick: () => {
loadData(collectionName);
loadData(collectionName!);
},
label: btnTrans('refresh'),
},
Expand Down
12 changes: 6 additions & 6 deletions client/src/pages/query/Query.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FC, useEffect, useState, useRef, useContext } from 'react';
import { useEffect, useState, useRef, useContext } from 'react';
import { TextField } from '@material-ui/core';
import { useTranslation } from 'react-i18next';
import { useParams } from 'react-router-dom';
import { rootContext } from '@/context';
import { Collection, DataService } from '@/http';
import { usePaginationHook, useSearchResult } from '@/hooks';
Expand All @@ -22,9 +23,8 @@ import {
} from '@/consts';
import CustomSelector from '@/components/customSelector/CustomSelector';

const Query: FC<{
collectionName: string;
}> = ({ collectionName }) => {
const Query = () => {
const { collectionName } = useParams<{ collectionName: string }>();
const [fields, setFields] = useState<any[]>([]);
const [expression, setExpression] = useState('');
const [tableLoading, setTableLoading] = useState<any>();
Expand Down Expand Up @@ -122,7 +122,7 @@ const Query: FC<{
return;
}
try {
const res = await Collection.queryData(collectionName, {
const res = await Collection.queryData(collectionName!, {
expr: expr,
output_fields: fields.map(i => i.name),
offset: 0,
Expand All @@ -145,7 +145,7 @@ const Query: FC<{
};

const handleDelete = async () => {
await DataService.deleteEntities(collectionName, {
await DataService.deleteEntities(collectionName!, {
expr: `${primaryKey.value} in [${selectedData
.map(v =>
primaryKey.type === DataTypeStringEnum.VarChar
Expand Down
10 changes: 5 additions & 5 deletions client/src/pages/schema/Schema.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { makeStyles, Theme, Typography, Chip } from '@material-ui/core';
import { FC, useCallback, useEffect, useState } from 'react';
import { useCallback, useEffect, useState } from 'react';
import { useParams } from 'react-router-dom';
import AttuGrid from '@/components/grid/Grid';
import { ColDefinitionsType } from '@/components/grid/Types';
import { useTranslation } from 'react-i18next';
Expand Down Expand Up @@ -58,9 +59,8 @@ const useStyles = makeStyles((theme: Theme) => ({
},
}));

const Schema: FC<{
collectionName: string;
}> = ({ collectionName }) => {
const Schema = () => {
const { collectionName = '' } = useParams<{ collectionName: string }>();
const classes = useStyles();
const { t: collectionTrans } = useTranslation('collection');
const { t: indexTrans } = useTranslation('index');
Expand Down Expand Up @@ -187,7 +187,7 @@ const Schema: FC<{
id: 'indexName',
align: 'left',
disablePadding: true,
label: indexTrans('indexName')
label: indexTrans('indexName'),
},
{
id: '_indexTypeElement',
Expand Down
Loading
Loading