fix: watch api block
This commit is contained in:
@@ -6,6 +6,7 @@ import classNames from 'classnames';
|
|||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import React, { useEffect, useRef, useState } from 'react';
|
import React, { useEffect, useRef, useState } from 'react';
|
||||||
import RowContext from '../row-context';
|
import RowContext from '../row-context';
|
||||||
|
import TableContext from '../table-context';
|
||||||
import { RowContextProps, SealTableProps } from '../types';
|
import { RowContextProps, SealTableProps } from '../types';
|
||||||
|
|
||||||
const TableRow: React.FC<
|
const TableRow: React.FC<
|
||||||
@@ -19,6 +20,7 @@ const TableRow: React.FC<
|
|||||||
rowSelection,
|
rowSelection,
|
||||||
expandedRowKeys,
|
expandedRowKeys,
|
||||||
rowKey,
|
rowKey,
|
||||||
|
childParentKey,
|
||||||
columns,
|
columns,
|
||||||
pollingChildren,
|
pollingChildren,
|
||||||
watchChildren,
|
watchChildren,
|
||||||
@@ -27,6 +29,9 @@ const TableRow: React.FC<
|
|||||||
loadChildren,
|
loadChildren,
|
||||||
loadChildrenAPI
|
loadChildrenAPI
|
||||||
} = props;
|
} = props;
|
||||||
|
const tableContext: any = React.useContext<{
|
||||||
|
allChildren?: any[];
|
||||||
|
}>(TableContext);
|
||||||
const { setChunkRequest } = useSetChunkRequest();
|
const { setChunkRequest } = useSetChunkRequest();
|
||||||
const [expanded, setExpanded] = useState(false);
|
const [expanded, setExpanded] = useState(false);
|
||||||
const [checked, setChecked] = useState(false);
|
const [checked, setChecked] = useState(false);
|
||||||
@@ -44,6 +49,7 @@ const TableRow: React.FC<
|
|||||||
setDataList: setChildrenData
|
setDataList: setChildrenData
|
||||||
// callback: (list) => renderChildren?.(list)
|
// callback: (list) => renderChildren?.(list)
|
||||||
});
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (rowSelection) {
|
if (rowSelection) {
|
||||||
const { selectedRowKeys } = rowSelection;
|
const { selectedRowKeys } = rowSelection;
|
||||||
@@ -55,13 +61,13 @@ const TableRow: React.FC<
|
|||||||
}
|
}
|
||||||
}, [rowSelection]);
|
}, [rowSelection]);
|
||||||
|
|
||||||
// useEffect(() => {
|
useEffect(() => {
|
||||||
// if (expandedRowKeys?.includes(record[rowKey])) {
|
if (expandedRowKeys?.includes(record[rowKey])) {
|
||||||
// setExpanded(true);
|
setExpanded(true);
|
||||||
// } else {
|
} else {
|
||||||
// setExpanded(false);
|
setExpanded(false);
|
||||||
// }
|
}
|
||||||
// }, [expandedRowKeys]);
|
}, [expandedRowKeys]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
return () => {
|
return () => {
|
||||||
@@ -92,22 +98,31 @@ const TableRow: React.FC<
|
|||||||
try {
|
try {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
const data = await loadChildren?.(record);
|
const data = await loadChildren?.(record);
|
||||||
|
|
||||||
setChildrenData(data || []);
|
setChildrenData(data || []);
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setChildrenData([]);
|
setChildrenData([]);
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
} finally {
|
} finally {
|
||||||
childrenDataRef.current = childrenData;
|
|
||||||
setFirstLoad(false);
|
setFirstLoad(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const filterUpdateChildrenHandler = () => {
|
||||||
|
_.each(tableContext.allChildren || [], (data: any) => {
|
||||||
|
if (_.get(data, ['data', [childParentKey]]) === _.get(record, [rowKey])) {
|
||||||
|
updateChunkedList(data);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const updateChildrenHandler = (list: any) => {
|
const updateChildrenHandler = (list: any) => {
|
||||||
_.each(list, (data: any) => {
|
_.each(list, (data: any) => {
|
||||||
updateChunkedList(data, childrenDataRef.current);
|
updateChunkedList(data, childrenDataRef.current);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const createChunkRequest = () => {
|
const createChunkRequest = () => {
|
||||||
chunkRequestRef.current?.current?.cancel?.();
|
chunkRequestRef.current?.current?.cancel?.();
|
||||||
if (!watchChildren) {
|
if (!watchChildren) {
|
||||||
@@ -167,13 +182,19 @@ const TableRow: React.FC<
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!firstLoad && expanded) {
|
if (!firstLoad && expanded) {
|
||||||
createChunkRequest();
|
console.log(
|
||||||
|
'update children=====',
|
||||||
|
record.name,
|
||||||
|
tableContext.allChildren
|
||||||
|
);
|
||||||
|
cacheDataListRef.current = childrenData;
|
||||||
|
filterUpdateChildrenHandler();
|
||||||
}
|
}
|
||||||
return () => {
|
return () => {
|
||||||
chunkRequestRef.current?.current?.cancel?.();
|
chunkRequestRef.current?.current?.cancel?.();
|
||||||
cacheDataListRef.current = [];
|
cacheDataListRef.current = [];
|
||||||
};
|
};
|
||||||
}, [firstLoad, expanded]);
|
}, [firstLoad, expanded, tableContext.allChildren]);
|
||||||
|
|
||||||
const renderRowPrefix = () => {
|
const renderRowPrefix = () => {
|
||||||
if (expandable && rowSelection) {
|
if (expandable && rowSelection) {
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ const SealTable: React.FC<SealTableProps> = (props) => {
|
|||||||
const {
|
const {
|
||||||
children,
|
children,
|
||||||
rowKey,
|
rowKey,
|
||||||
|
childParentKey,
|
||||||
onExpand,
|
onExpand,
|
||||||
expandedRowKeys,
|
expandedRowKeys,
|
||||||
loading,
|
loading,
|
||||||
@@ -137,6 +138,7 @@ const SealTable: React.FC<SealTableProps> = (props) => {
|
|||||||
rowSelection={rowSelection}
|
rowSelection={rowSelection}
|
||||||
expandable={expandable}
|
expandable={expandable}
|
||||||
rowKey={rowKey}
|
rowKey={rowKey}
|
||||||
|
childParentKey={childParentKey}
|
||||||
pollingChildren={pollingChildren}
|
pollingChildren={pollingChildren}
|
||||||
watchChildren={watchChildren}
|
watchChildren={watchChildren}
|
||||||
renderChildren={renderChildren}
|
renderChildren={renderChildren}
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
const TableContext = React.createContext({});
|
||||||
|
|
||||||
|
export default TableContext;
|
||||||
@@ -24,6 +24,7 @@ export interface RowSelectionProps {
|
|||||||
onChange: (selectedRowKeys: React.Key[]) => void;
|
onChange: (selectedRowKeys: React.Key[]) => void;
|
||||||
}
|
}
|
||||||
export interface SealTableProps {
|
export interface SealTableProps {
|
||||||
|
childParentKey?: string;
|
||||||
expandedRowKeys?: React.Key[];
|
expandedRowKeys?: React.Key[];
|
||||||
rowSelection?: RowSelectionProps;
|
rowSelection?: RowSelectionProps;
|
||||||
children: React.ReactNode[];
|
children: React.ReactNode[];
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ export function useUpdateChunkedList(options: {
|
|||||||
const cacheDataListRef = useRef<any[]>(options.dataList || []);
|
const cacheDataListRef = useRef<any[]>(options.dataList || []);
|
||||||
const updateChunkedList = (
|
const updateChunkedList = (
|
||||||
data: ChunkedCollection,
|
data: ChunkedCollection,
|
||||||
dataList: { id: string | number }[]
|
dataList?: { id: string | number }[]
|
||||||
) => {
|
) => {
|
||||||
console.log('updateChunkedList=====', {
|
console.log('updateChunkedList=====', {
|
||||||
ids: data?.ids,
|
ids: data?.ids,
|
||||||
@@ -54,7 +54,7 @@ export function useUpdateChunkedList(options: {
|
|||||||
const updateItem = _.cloneDeep(item);
|
const updateItem = _.cloneDeep(item);
|
||||||
newDataList.push(updateItem);
|
newDataList.push(updateItem);
|
||||||
}
|
}
|
||||||
console.log('create=========', updateIndex, dataList, collections);
|
console.log('create=========', updateIndex, collections);
|
||||||
});
|
});
|
||||||
cacheDataListRef.current = [...newDataList, ...cacheDataListRef.current];
|
cacheDataListRef.current = [...newDataList, ...cacheDataListRef.current];
|
||||||
options.setDataList?.([...cacheDataListRef.current]);
|
options.setDataList?.([...cacheDataListRef.current]);
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import { PageContainer } from '@ant-design/pro-components';
|
|||||||
import { Access, useAccess, useIntl, useNavigate } from '@umijs/max';
|
import { Access, useAccess, useIntl, useNavigate } from '@umijs/max';
|
||||||
import { Button, Input, Modal, Space, message } from 'antd';
|
import { Button, Input, Modal, Space, message } from 'antd';
|
||||||
import dayjs from 'dayjs';
|
import dayjs from 'dayjs';
|
||||||
|
import _ from 'lodash';
|
||||||
import { memo, useCallback, useState } from 'react';
|
import { memo, useCallback, useState } from 'react';
|
||||||
import {
|
import {
|
||||||
MODELS_API,
|
MODELS_API,
|
||||||
@@ -101,6 +102,15 @@ const Models: React.FC<ModelsProps> = ({
|
|||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
|
const setActionList = (record: ListItem) => {
|
||||||
|
return _.filter(ActionList, (action: any) => {
|
||||||
|
if (action.key === 'chat') {
|
||||||
|
return record.ready_replicas > 0;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const handleTableChange = (pagination: any, filters: any, sorter: any) => {
|
const handleTableChange = (pagination: any, filters: any, sorter: any) => {
|
||||||
console.log('handleTableChange=======', pagination, filters, sorter);
|
console.log('handleTableChange=======', pagination, filters, sorter);
|
||||||
setSortOrder(sorter.order);
|
setSortOrder(sorter.order);
|
||||||
@@ -315,6 +325,7 @@ const Models: React.FC<ModelsProps> = ({
|
|||||||
onExpand={handleExpandChange}
|
onExpand={handleExpandChange}
|
||||||
loading={loading}
|
loading={loading}
|
||||||
rowKey="id"
|
rowKey="id"
|
||||||
|
childParentKey="model_id"
|
||||||
expandable={true}
|
expandable={true}
|
||||||
onChange={handleTableChange}
|
onChange={handleTableChange}
|
||||||
pollingChildren={false}
|
pollingChildren={false}
|
||||||
@@ -388,7 +399,7 @@ const Models: React.FC<ModelsProps> = ({
|
|||||||
render={(text, record) => {
|
render={(text, record) => {
|
||||||
return !record.transition ? (
|
return !record.transition ? (
|
||||||
<DropdownButtons
|
<DropdownButtons
|
||||||
items={ActionList}
|
items={setActionList(record)}
|
||||||
onSelect={(val) => handleSelect(val, record)}
|
onSelect={(val) => handleSelect(val, record)}
|
||||||
></DropdownButtons>
|
></DropdownButtons>
|
||||||
) : null;
|
) : null;
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
|
import TableContext from '@/components/seal-table/table-context';
|
||||||
import useSetChunkRequest, {
|
import useSetChunkRequest, {
|
||||||
createAxiosToken
|
createAxiosToken
|
||||||
} from '@/hooks/use-chunk-request';
|
} from '@/hooks/use-chunk-request';
|
||||||
import useUpdateChunkedList from '@/hooks/use-update-chunk-list';
|
import useUpdateChunkedList from '@/hooks/use-update-chunk-list';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import { memo, useCallback, useEffect, useRef, useState } from 'react';
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||||
import { MODELS_API, queryModelsList } from './apis';
|
import { MODELS_API, MODEL_INSTANCE_API, queryModelsList } from './apis';
|
||||||
import TableList from './components/table-list';
|
import TableList from './components/table-list';
|
||||||
import { ListItem } from './config/types';
|
import { ListItem } from './config/types';
|
||||||
|
|
||||||
@@ -13,12 +14,15 @@ const Models: React.FC = () => {
|
|||||||
console.log('model list====1');
|
console.log('model list====1');
|
||||||
|
|
||||||
const { setChunkRequest } = useSetChunkRequest();
|
const { setChunkRequest } = useSetChunkRequest();
|
||||||
|
const { setChunkRequest: setModelInstanceChunkRequest } =
|
||||||
|
useSetChunkRequest();
|
||||||
const [total, setTotal] = useState(100);
|
const [total, setTotal] = useState(100);
|
||||||
|
const [modelInstances, setModelInstances] = useState<any[]>([]);
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [dataSource, setDataSource] = useState<ListItem[]>([]);
|
const [dataSource, setDataSource] = useState<ListItem[]>([]);
|
||||||
const [firstLoad, setFirstLoad] = useState(true);
|
const [firstLoad, setFirstLoad] = useState(true);
|
||||||
const chunkRequedtRef = useRef<any>();
|
const chunkRequedtRef = useRef<any>();
|
||||||
|
const chunkInstanceRequedtRef = useRef<any>();
|
||||||
const dataSourceRef = useRef<any>();
|
const dataSourceRef = useRef<any>();
|
||||||
let axiosToken = createAxiosToken();
|
let axiosToken = createAxiosToken();
|
||||||
const [queryParams, setQueryParams] = useState({
|
const [queryParams, setQueryParams] = useState({
|
||||||
@@ -34,6 +38,13 @@ const Models: React.FC = () => {
|
|||||||
dataList: dataSource,
|
dataList: dataSource,
|
||||||
setDataList: setDataSource
|
setDataList: setDataSource
|
||||||
});
|
});
|
||||||
|
const {
|
||||||
|
updateChunkedList: updateInstanceChunkList,
|
||||||
|
cacheDataListRef: cacheInstanceDataListRef
|
||||||
|
} = useUpdateChunkedList({
|
||||||
|
dataList: modelInstances,
|
||||||
|
setDataList: setModelInstances
|
||||||
|
});
|
||||||
|
|
||||||
const fetchData = useCallback(async () => {
|
const fetchData = useCallback(async () => {
|
||||||
axiosToken?.cancel?.();
|
axiosToken?.cancel?.();
|
||||||
@@ -47,9 +58,6 @@ const Models: React.FC = () => {
|
|||||||
cancelToken: axiosToken.token
|
cancelToken: axiosToken.token
|
||||||
});
|
});
|
||||||
console.log('res=======', res);
|
console.log('res=======', res);
|
||||||
// if (!firstLoad) {
|
|
||||||
// setDataSource(res.items);
|
|
||||||
// }
|
|
||||||
setDataSource(res.items);
|
setDataSource(res.items);
|
||||||
setTotal(res.pagination.total);
|
setTotal(res.pagination.total);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -84,11 +92,20 @@ const Models: React.FC = () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const updateHandler = (list: any) => {
|
const updateHandler = (list: any) => {
|
||||||
|
console.log('updateHandler=====', list);
|
||||||
_.each(list, (data: any) => {
|
_.each(list, (data: any) => {
|
||||||
updateChunkedList(data, dataSourceRef.current);
|
updateChunkedList(data, dataSourceRef.current);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const updateInstanceHandler = (list: any) => {
|
||||||
|
console.log('updateInstanceHandler=====', list);
|
||||||
|
setModelInstances(list);
|
||||||
|
// _.each(list, (data: any) => {
|
||||||
|
// updateInstanceChunkList(data);
|
||||||
|
// });
|
||||||
|
};
|
||||||
|
|
||||||
const createModelsChunkRequest = () => {
|
const createModelsChunkRequest = () => {
|
||||||
chunkRequedtRef.current?.current?.cancel?.();
|
chunkRequedtRef.current?.current?.cancel?.();
|
||||||
try {
|
try {
|
||||||
@@ -103,6 +120,18 @@ const Models: React.FC = () => {
|
|||||||
// ignore
|
// ignore
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
const createModelsInstanceChunkRequest = () => {
|
||||||
|
chunkInstanceRequedtRef.current?.current?.cancel?.();
|
||||||
|
try {
|
||||||
|
chunkInstanceRequedtRef.current = setModelInstanceChunkRequest({
|
||||||
|
url: `${MODEL_INSTANCE_API}`,
|
||||||
|
params: {},
|
||||||
|
handler: updateInstanceHandler
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const handleSearch = useCallback((e: any) => {
|
const handleSearch = useCallback((e: any) => {
|
||||||
fetchData();
|
fetchData();
|
||||||
@@ -121,11 +150,13 @@ const Models: React.FC = () => {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!firstLoad) {
|
if (!firstLoad) {
|
||||||
createModelsChunkRequest();
|
createModelsChunkRequest();
|
||||||
|
createModelsInstanceChunkRequest();
|
||||||
}
|
}
|
||||||
createModelsChunkRequest();
|
|
||||||
return () => {
|
return () => {
|
||||||
chunkRequedtRef.current?.current?.cancel?.();
|
chunkRequedtRef.current?.current?.cancel?.();
|
||||||
cacheDataListRef.current = [];
|
cacheDataListRef.current = [];
|
||||||
|
chunkInstanceRequedtRef.current?.current?.cancel?.();
|
||||||
|
cacheInstanceDataListRef.current = [];
|
||||||
};
|
};
|
||||||
}, [firstLoad]);
|
}, [firstLoad]);
|
||||||
|
|
||||||
@@ -137,19 +168,25 @@ const Models: React.FC = () => {
|
|||||||
}, [queryParams]);
|
}, [queryParams]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<TableList
|
<TableContext.Provider
|
||||||
dataSource={dataSource}
|
value={{
|
||||||
handleNameChange={handleNameChange}
|
allChildren: modelInstances
|
||||||
handleSearch={handleSearch}
|
}}
|
||||||
handleShowSizeChange={handleShowSizeChange}
|
>
|
||||||
handlePageChange={handlePageChange}
|
<TableList
|
||||||
createModelsChunkRequest={createModelsChunkRequest}
|
dataSource={dataSource}
|
||||||
queryParams={queryParams}
|
handleNameChange={handleNameChange}
|
||||||
loading={loading}
|
handleSearch={handleSearch}
|
||||||
total={total}
|
handleShowSizeChange={handleShowSizeChange}
|
||||||
fetchData={fetchData}
|
handlePageChange={handlePageChange}
|
||||||
></TableList>
|
createModelsChunkRequest={createModelsChunkRequest}
|
||||||
|
queryParams={queryParams}
|
||||||
|
loading={loading}
|
||||||
|
total={total}
|
||||||
|
fetchData={fetchData}
|
||||||
|
></TableList>
|
||||||
|
</TableContext.Provider>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default memo(Models);
|
export default Models;
|
||||||
|
|||||||
Reference in New Issue
Block a user