chore: update model list polling
This commit is contained in:
@@ -2,7 +2,7 @@ import { DownOutlined, RightOutlined } from '@ant-design/icons';
|
||||
import { Button, Checkbox, Col, Empty, Row, Spin } from 'antd';
|
||||
import classNames from 'classnames';
|
||||
import _ from 'lodash';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import React, { useEffect, useRef, useState } from 'react';
|
||||
import RowContext from '../row-context';
|
||||
import { RowContextProps, SealTableProps } from '../types';
|
||||
|
||||
@@ -17,6 +17,7 @@ const TableRow: React.FC<
|
||||
rowSelection,
|
||||
rowKey,
|
||||
columns,
|
||||
pollingChildren,
|
||||
onExpand,
|
||||
renderChildren,
|
||||
loadChildren
|
||||
@@ -26,6 +27,7 @@ const TableRow: React.FC<
|
||||
const [checked, setChecked] = useState(false);
|
||||
const [childrenData, setChildrenData] = useState<any[]>([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const pollTimer = useRef<any>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (rowSelection) {
|
||||
@@ -38,12 +40,26 @@ const TableRow: React.FC<
|
||||
}
|
||||
}, [rowSelection]);
|
||||
|
||||
const handleRowExpand = async () => {
|
||||
setExpanded(!expanded);
|
||||
onExpand?.(!expanded, record);
|
||||
if (expanded) {
|
||||
return;
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
if (pollTimer.current) {
|
||||
clearInterval(pollTimer.current);
|
||||
}
|
||||
};
|
||||
}, []);
|
||||
|
||||
const handlePolling = async () => {
|
||||
if (pollingChildren) {
|
||||
try {
|
||||
const data = await loadChildren?.(record);
|
||||
setChildrenData(data || []);
|
||||
} catch (error) {
|
||||
setChildrenData([]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const handleLoadChildren = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
const data = await loadChildren?.(record);
|
||||
@@ -55,6 +71,28 @@ const TableRow: React.FC<
|
||||
}
|
||||
};
|
||||
|
||||
const handleRowExpand = async () => {
|
||||
setExpanded(!expanded);
|
||||
onExpand?.(!expanded, record);
|
||||
|
||||
if (pollTimer.current) {
|
||||
clearInterval(pollTimer.current);
|
||||
}
|
||||
|
||||
if (expanded) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (pollingChildren) {
|
||||
await handleLoadChildren();
|
||||
pollTimer.current = setInterval(() => {
|
||||
handlePolling();
|
||||
}, 1000);
|
||||
} else {
|
||||
handleLoadChildren();
|
||||
}
|
||||
};
|
||||
|
||||
const handleSelectChange = (e: any) => {
|
||||
if (e.target.checked) {
|
||||
// update selectedRowKeys
|
||||
|
||||
@@ -14,6 +14,7 @@ const SealTable: React.FC<SealTableProps> = (props) => {
|
||||
onExpand,
|
||||
loading,
|
||||
expandable,
|
||||
pollingChildren,
|
||||
rowSelection,
|
||||
renderChildren,
|
||||
loadChildren
|
||||
@@ -129,6 +130,7 @@ const SealTable: React.FC<SealTableProps> = (props) => {
|
||||
rowSelection={rowSelection}
|
||||
expandable={expandable}
|
||||
rowKey={rowKey}
|
||||
pollingChildren={pollingChildren}
|
||||
renderChildren={renderChildren}
|
||||
loadChildren={loadChildren}
|
||||
onExpand={onExpand}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
.row-children {
|
||||
position: relative;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
height: 54px;
|
||||
@@ -6,6 +7,7 @@
|
||||
border-radius: var(--ant-table-header-border-radius);
|
||||
background-color: var(--color-fill-1);
|
||||
transition: all 0.2s ease;
|
||||
|
||||
&:hover {
|
||||
background-color: var(--ant-table-row-hover-bg);
|
||||
transition: all 0.2s ease;
|
||||
|
||||
@@ -29,6 +29,7 @@ export interface SealTableProps {
|
||||
empty?: React.ReactNode;
|
||||
expandable?: React.ReactNode;
|
||||
dataSource: any[];
|
||||
pollingChildren?: boolean;
|
||||
loading?: boolean;
|
||||
onExpand?: (expanded: boolean, record: any) => void;
|
||||
renderChildren?: (data: any) => React.ReactNode;
|
||||
@@ -39,5 +40,6 @@ export interface SealTableProps {
|
||||
export interface RowContextProps {
|
||||
record: Record<string, any>;
|
||||
columns: React.ReactNode[];
|
||||
pollingChildren?: boolean;
|
||||
rowIndex: number;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user