feat: memoize runs table
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { useEffect, useState } from "react";
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import Paper from '@mui/material/Paper';
|
import Paper from '@mui/material/Paper';
|
||||||
import Table from '@mui/material/Table';
|
import Table from '@mui/material/Table';
|
||||||
@@ -69,90 +69,149 @@ export const RunsTable: React.FC<RunsTableProps> = ({
|
|||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
const translatedColumns = columns.map(column => ({
|
const translatedColumns = useMemo(() =>
|
||||||
...column,
|
columns.map(column => ({
|
||||||
label: t(`runstable.${column.id}`, column.label)
|
...column,
|
||||||
}));
|
label: t(`runstable.${column.id}`, column.label)
|
||||||
|
})),
|
||||||
|
[t]
|
||||||
|
);
|
||||||
|
|
||||||
const [page, setPage] = useState(0);
|
const [page, setPage] = useState(0);
|
||||||
const [rowsPerPage, setRowsPerPage] = useState(10);
|
const [rowsPerPage, setRowsPerPage] = useState(10);
|
||||||
const [rows, setRows] = useState<Data[]>([]);
|
const [rows, setRows] = useState<Data[]>([]);
|
||||||
const [searchTerm, setSearchTerm] = useState('');
|
const [searchTerm, setSearchTerm] = useState('');
|
||||||
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
|
|
||||||
const { notify, rerenderRuns, setRerenderRuns } = useGlobalInfoStore();
|
const { notify, rerenderRuns, setRerenderRuns } = useGlobalInfoStore();
|
||||||
|
|
||||||
const handleAccordionChange = (robotMetaId: string, isExpanded: boolean) => {
|
const handleAccordionChange = useCallback((robotMetaId: string, isExpanded: boolean) => {
|
||||||
if (isExpanded) {
|
navigate(isExpanded ? `/runs/${robotMetaId}` : '/runs');
|
||||||
navigate(`/runs/${robotMetaId}`);
|
}, [navigate]);
|
||||||
} else {
|
|
||||||
navigate(`/runs`);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleChangePage = (event: unknown, newPage: number) => {
|
const handleChangePage = useCallback((event: unknown, newPage: number) => {
|
||||||
setPage(newPage);
|
setPage(newPage);
|
||||||
};
|
}, []);
|
||||||
|
|
||||||
const handleChangeRowsPerPage = (event: React.ChangeEvent<HTMLInputElement>) => {
|
const handleChangeRowsPerPage = useCallback((event: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
setRowsPerPage(+event.target.value);
|
setRowsPerPage(+event.target.value);
|
||||||
setPage(0);
|
setPage(0);
|
||||||
};
|
}, []);
|
||||||
|
|
||||||
const handleSearchChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
const debouncedSearch = useCallback((fn: Function, delay: number) => {
|
||||||
setSearchTerm(event.target.value);
|
let timeoutId: NodeJS.Timeout;
|
||||||
setPage(0);
|
return (...args: any[]) => {
|
||||||
};
|
clearTimeout(timeoutId);
|
||||||
|
timeoutId = setTimeout(() => fn(...args), delay);
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
const fetchRuns = async () => {
|
const handleSearchChange = useCallback((event: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
const runs = await getStoredRuns();
|
const debouncedSetSearch = debouncedSearch((value: string) => {
|
||||||
if (runs) {
|
setSearchTerm(value);
|
||||||
const parsedRows: Data[] = runs.map((run: any, index: number) => ({
|
setPage(0);
|
||||||
id: index,
|
}, 300);
|
||||||
...run,
|
debouncedSetSearch(event.target.value);
|
||||||
}));
|
}, [debouncedSearch]);
|
||||||
setRows(parsedRows);
|
|
||||||
} else {
|
const fetchRuns = useCallback(async () => {
|
||||||
notify('error', t('runstable.notifications.no_runs'));
|
try {
|
||||||
|
setIsLoading(true);
|
||||||
|
const runs = await getStoredRuns();
|
||||||
|
if (runs) {
|
||||||
|
const parsedRows: Data[] = runs.map((run: any, index: number) => ({
|
||||||
|
id: index,
|
||||||
|
...run,
|
||||||
|
}));
|
||||||
|
setRows(parsedRows);
|
||||||
|
} else {
|
||||||
|
notify('error', t('runstable.notifications.no_runs'));
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
notify('error', t('runstable.notifications.fetch_error'));
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
};
|
}, [notify, t]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (rows.length === 0 || rerenderRuns) {
|
let mounted = true;
|
||||||
fetchRuns();
|
|
||||||
setRerenderRuns(false);
|
|
||||||
}
|
|
||||||
}, [rerenderRuns, rows.length, setRerenderRuns]);
|
|
||||||
|
|
||||||
const handleDelete = () => {
|
if (rows.length === 0 || rerenderRuns) {
|
||||||
|
fetchRuns().then(() => {
|
||||||
|
if (mounted) {
|
||||||
|
setRerenderRuns(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
mounted = false;
|
||||||
|
};
|
||||||
|
}, [rerenderRuns, rows.length, setRerenderRuns, fetchRuns]);
|
||||||
|
|
||||||
|
const handleDelete = useCallback(() => {
|
||||||
setRows([]);
|
setRows([]);
|
||||||
notify('success', t('runstable.notifications.delete_success'));
|
notify('success', t('runstable.notifications.delete_success'));
|
||||||
fetchRuns();
|
fetchRuns();
|
||||||
};
|
}, [notify, t, fetchRuns]);
|
||||||
|
|
||||||
// Filter rows based on search term
|
// Filter rows based on search term
|
||||||
const filteredRows = rows.filter((row) =>
|
const filteredRows = useMemo(() =>
|
||||||
row.name.toLowerCase().includes(searchTerm.toLowerCase())
|
rows.filter((row) =>
|
||||||
|
row.name.toLowerCase().includes(searchTerm.toLowerCase())
|
||||||
|
),
|
||||||
|
[rows, searchTerm]
|
||||||
);
|
);
|
||||||
|
|
||||||
// Group filtered rows by robot meta id
|
// Group filtered rows by robot meta id
|
||||||
const groupedRows = filteredRows.reduce((acc, row) => {
|
const groupedRows = useMemo(() =>
|
||||||
if (!acc[row.robotMetaId]) {
|
filteredRows.reduce((acc, row) => {
|
||||||
acc[row.robotMetaId] = [];
|
if (!acc[row.robotMetaId]) {
|
||||||
}
|
acc[row.robotMetaId] = [];
|
||||||
acc[row.robotMetaId].push(row);
|
}
|
||||||
return acc;
|
acc[row.robotMetaId].push(row);
|
||||||
}, {} as Record<string, Data[]>);
|
return acc;
|
||||||
|
}, {} as Record<string, Data[]>),
|
||||||
|
[filteredRows]
|
||||||
|
);
|
||||||
|
|
||||||
|
const renderTableRows = useCallback((data: Data[]) => {
|
||||||
|
const start = page * rowsPerPage;
|
||||||
|
const end = start + rowsPerPage;
|
||||||
|
|
||||||
|
return data
|
||||||
|
.slice(start, end)
|
||||||
|
.map((row) => (
|
||||||
|
<CollapsibleRow
|
||||||
|
key={`row-${row.id}`}
|
||||||
|
row={row}
|
||||||
|
handleDelete={handleDelete}
|
||||||
|
isOpen={runId === row.runId && runningRecordingName === row.name}
|
||||||
|
currentLog={currentInterpretationLog}
|
||||||
|
abortRunHandler={abortRunHandler}
|
||||||
|
runningRecordingName={runningRecordingName}
|
||||||
|
/>
|
||||||
|
));
|
||||||
|
}, [page, rowsPerPage, runId, runningRecordingName, currentInterpretationLog, abortRunHandler, handleDelete]);
|
||||||
|
|
||||||
|
if (isLoading) {
|
||||||
|
return (
|
||||||
|
<Box display="flex" justifyContent="center" alignItems="center" height="50vh">
|
||||||
|
<CircularProgress />
|
||||||
|
</Box>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<React.Fragment>
|
<React.Fragment>
|
||||||
<Box display="flex" justifyContent="space-between" alignItems="center" mb={2}>
|
<Box display="flex" justifyContent="space-between" alignItems="center" mb={2}>
|
||||||
<Typography variant="h6" gutterBottom>
|
<Typography variant="h6" component="h2">
|
||||||
{t('runstable.runs', 'Runs')}
|
{t('runstable.runs', 'Runs')}
|
||||||
</Typography>
|
</Typography>
|
||||||
<TextField
|
<TextField
|
||||||
size="small"
|
size="small"
|
||||||
placeholder={t('runstable.search', 'Search runs...')}
|
placeholder={t('runstable.search', 'Search runs...')}
|
||||||
value={searchTerm}
|
|
||||||
onChange={handleSearchChange}
|
onChange={handleSearchChange}
|
||||||
InputProps={{
|
InputProps={{
|
||||||
startAdornment: <SearchIcon sx={{ color: 'action.active', mr: 1 }} />
|
startAdornment: <SearchIcon sx={{ color: 'action.active', mr: 1 }} />
|
||||||
@@ -160,62 +219,50 @@ export const RunsTable: React.FC<RunsTableProps> = ({
|
|||||||
sx={{ width: '250px' }}
|
sx={{ width: '250px' }}
|
||||||
/>
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
{rows.length === 0 ? (
|
|
||||||
<Box display="flex" justifyContent="center" alignItems="center" height="50%">
|
<TableContainer component={Paper} sx={{ width: '100%', overflow: 'hidden' }}>
|
||||||
<CircularProgress />
|
{Object.entries(groupedRows).map(([id, data]) => (
|
||||||
</Box>
|
<Accordion
|
||||||
) : (
|
key={id}
|
||||||
<TableContainer component={Paper} sx={{ width: '100%', overflow: 'hidden' }}>
|
onChange={(event, isExpanded) => handleAccordionChange(id, isExpanded)}
|
||||||
{Object.entries(groupedRows).map(([id, data]) => (
|
TransitionProps={{ unmountOnExit: true }} // Optimize accordion rendering
|
||||||
<Accordion key={id} onChange={(event, isExpanded) => handleAccordionChange(id, isExpanded)}>
|
>
|
||||||
<AccordionSummary expandIcon={<ExpandMoreIcon />}>
|
<AccordionSummary expandIcon={<ExpandMoreIcon />}>
|
||||||
<Typography variant="h6">{data[data.length - 1].name}</Typography>
|
<Typography variant="h6">{data[data.length - 1].name}</Typography>
|
||||||
</AccordionSummary>
|
</AccordionSummary>
|
||||||
<AccordionDetails>
|
<AccordionDetails>
|
||||||
<Table stickyHeader aria-label="sticky table">
|
<Table stickyHeader aria-label="sticky table">
|
||||||
<TableHead>
|
<TableHead>
|
||||||
<TableRow>
|
<TableRow>
|
||||||
<TableCell />
|
<TableCell />
|
||||||
{translatedColumns.map((column) => (
|
{translatedColumns.map((column) => (
|
||||||
<TableCell
|
<TableCell
|
||||||
key={column.id}
|
key={column.id}
|
||||||
align={column.align}
|
align={column.align}
|
||||||
style={{ minWidth: column.minWidth }}
|
style={{ minWidth: column.minWidth }}
|
||||||
>
|
>
|
||||||
{column.label}
|
{column.label}
|
||||||
</TableCell>
|
</TableCell>
|
||||||
))}
|
))}
|
||||||
</TableRow>
|
</TableRow>
|
||||||
</TableHead>
|
</TableHead>
|
||||||
<TableBody>
|
<TableBody>
|
||||||
{data
|
{renderTableRows(data)}
|
||||||
.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
|
</TableBody>
|
||||||
.map((row) => (
|
</Table>
|
||||||
<CollapsibleRow
|
</AccordionDetails>
|
||||||
row={row}
|
</Accordion>
|
||||||
handleDelete={handleDelete}
|
))}
|
||||||
key={`row-${row.id}`}
|
</TableContainer>
|
||||||
isOpen={runId === row.runId && runningRecordingName === row.name}
|
|
||||||
currentLog={currentInterpretationLog}
|
|
||||||
abortRunHandler={abortRunHandler}
|
|
||||||
runningRecordingName={runningRecordingName}
|
|
||||||
/>
|
|
||||||
))}
|
|
||||||
</TableBody>
|
|
||||||
</Table>
|
|
||||||
</AccordionDetails>
|
|
||||||
</Accordion>
|
|
||||||
))}
|
|
||||||
</TableContainer>
|
|
||||||
)}
|
|
||||||
<TablePagination
|
<TablePagination
|
||||||
rowsPerPageOptions={[10, 25, 50]}
|
|
||||||
component="div"
|
component="div"
|
||||||
count={filteredRows.length}
|
count={filteredRows.length}
|
||||||
rowsPerPage={rowsPerPage}
|
rowsPerPage={rowsPerPage}
|
||||||
page={page}
|
page={page}
|
||||||
onPageChange={handleChangePage}
|
onPageChange={handleChangePage}
|
||||||
onRowsPerPageChange={handleChangeRowsPerPage}
|
onRowsPerPageChange={handleChangeRowsPerPage}
|
||||||
|
rowsPerPageOptions={[10, 25, 50]}
|
||||||
/>
|
/>
|
||||||
</React.Fragment>
|
</React.Fragment>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user