Files
parcer/src/components/run/RunsTable.tsx

269 lines
8.2 KiB
TypeScript
Raw Normal View History

2024-06-24 22:38:57 +05:30
import * as React from 'react';
2025-01-23 19:50:09 +05:30
import { useCallback, useEffect, useMemo, useState } from "react";
2024-12-10 21:45:09 +05:30
import { useTranslation } from 'react-i18next';
2024-06-24 22:38:57 +05:30
import Paper from '@mui/material/Paper';
import Table from '@mui/material/Table';
import TableBody from '@mui/material/TableBody';
import TableCell from '@mui/material/TableCell';
import TableContainer from '@mui/material/TableContainer';
import TableHead from '@mui/material/TableHead';
import TablePagination from '@mui/material/TablePagination';
import TableRow from '@mui/material/TableRow';
2025-01-17 22:51:01 +05:30
import { Accordion, AccordionSummary, AccordionDetails, Typography, Box, TextField, CircularProgress } from '@mui/material';
2024-12-10 21:45:09 +05:30
import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
import SearchIcon from '@mui/icons-material/Search';
import { useNavigate } from 'react-router-dom';
2024-06-24 22:38:57 +05:30
import { useGlobalInfoStore } from "../../context/globalInfo";
import { getStoredRuns } from "../../api/storage";
import { RunSettings } from "./RunSettings";
2025-01-09 20:03:34 +05:30
import { CollapsibleRow } from "./ColapsibleRow";
2024-12-10 21:45:09 +05:30
export const columns: readonly Column[] = [
{ id: 'runStatus', label: 'Status', minWidth: 80 },
{ id: 'name', label: 'Name', minWidth: 80 },
{ id: 'startedAt', label: 'Started At', minWidth: 80 },
{ id: 'finishedAt', label: 'Finished At', minWidth: 80 },
{ id: 'settings', label: 'Settings', minWidth: 80 },
{ id: 'delete', label: 'Delete', minWidth: 80 },
];
2024-06-24 22:38:57 +05:30
interface Column {
2024-10-28 20:47:09 +05:30
id: 'runStatus' | 'name' | 'startedAt' | 'finishedAt' | 'delete' | 'settings';
2024-06-24 22:38:57 +05:30
label: string;
minWidth?: number;
align?: 'right';
format?: (value: string) => string;
}
2024-12-21 17:21:52 +05:30
export interface Data {
2024-06-24 22:38:57 +05:30
id: number;
status: string;
name: string;
startedAt: string;
finishedAt: string;
2024-10-25 02:08:15 +05:30
runByUserId?: string;
runByScheduleId?: string;
runByAPI?: boolean;
2024-06-24 22:38:57 +05:30
log: string;
runId: string;
2024-11-20 14:28:53 +05:30
robotId: string;
robotMetaId: string;
2024-06-24 22:38:57 +05:30
interpreterSettings: RunSettings;
serializableOutput: any;
binaryOutput: any;
}
interface RunsTableProps {
currentInterpretationLog: string;
abortRunHandler: () => void;
runId: string;
runningRecordingName: string;
}
2025-01-09 19:18:19 +05:30
export const RunsTable: React.FC<RunsTableProps> = ({
currentInterpretationLog,
abortRunHandler,
runId,
runningRecordingName
2024-12-10 21:45:09 +05:30
}) => {
const { t } = useTranslation();
const navigate = useNavigate();
2024-12-10 21:45:09 +05:30
2025-01-23 19:50:09 +05:30
const translatedColumns = useMemo(() =>
columns.map(column => ({
...column,
label: t(`runstable.${column.id}`, column.label)
})),
[t]
);
2024-12-10 21:45:09 +05:30
2024-06-24 22:38:57 +05:30
const [page, setPage] = useState(0);
const [rowsPerPage, setRowsPerPage] = useState(10);
const [rows, setRows] = useState<Data[]>([]);
2024-11-20 14:16:26 +05:30
const [searchTerm, setSearchTerm] = useState('');
2025-01-23 19:50:09 +05:30
const [isLoading, setIsLoading] = useState(true);
2024-06-24 22:38:57 +05:30
const { notify, rerenderRuns, setRerenderRuns } = useGlobalInfoStore();
2025-01-23 19:50:09 +05:30
const handleAccordionChange = useCallback((robotMetaId: string, isExpanded: boolean) => {
navigate(isExpanded ? `/runs/${robotMetaId}` : '/runs');
}, [navigate]);
2025-01-23 19:50:09 +05:30
const handleChangePage = useCallback((event: unknown, newPage: number) => {
2024-06-24 22:38:57 +05:30
setPage(newPage);
2025-01-23 19:50:09 +05:30
}, []);
2024-06-24 22:38:57 +05:30
2025-01-23 19:50:09 +05:30
const handleChangeRowsPerPage = useCallback((event: React.ChangeEvent<HTMLInputElement>) => {
2024-06-24 22:38:57 +05:30
setRowsPerPage(+event.target.value);
setPage(0);
2025-01-23 19:50:09 +05:30
}, []);
2024-06-24 22:38:57 +05:30
2025-01-23 19:50:09 +05:30
const debouncedSearch = useCallback((fn: Function, delay: number) => {
let timeoutId: NodeJS.Timeout;
return (...args: any[]) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => fn(...args), delay);
};
}, []);
const handleSearchChange = useCallback((event: React.ChangeEvent<HTMLInputElement>) => {
const debouncedSetSearch = debouncedSearch((value: string) => {
setSearchTerm(value);
setPage(0);
}, 300);
debouncedSetSearch(event.target.value);
}, [debouncedSearch]);
const fetchRuns = useCallback(async () => {
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);
2024-06-24 22:38:57 +05:30
}
2025-01-23 19:50:09 +05:30
}, [notify, t]);
2024-06-24 22:38:57 +05:30
2024-09-09 08:00:48 +05:30
useEffect(() => {
2025-01-23 19:50:09 +05:30
let mounted = true;
2024-06-24 22:38:57 +05:30
if (rows.length === 0 || rerenderRuns) {
2025-01-23 19:50:09 +05:30
fetchRuns().then(() => {
if (mounted) {
setRerenderRuns(false);
}
});
2024-06-24 22:38:57 +05:30
}
2025-01-23 19:50:09 +05:30
return () => {
mounted = false;
};
}, [rerenderRuns, rows.length, setRerenderRuns, fetchRuns]);
const handleDelete = useCallback(() => {
2024-06-24 22:38:57 +05:30
setRows([]);
2024-12-21 13:39:52 +05:30
notify('success', t('runstable.notifications.delete_success'));
2024-06-24 22:38:57 +05:30
fetchRuns();
2025-01-23 19:50:09 +05:30
}, [notify, t, fetchRuns]);
2024-09-09 08:00:48 +05:30
2024-11-20 14:16:26 +05:30
// Filter rows based on search term
2025-01-23 19:50:09 +05:30
const filteredRows = useMemo(() =>
rows.filter((row) =>
row.name.toLowerCase().includes(searchTerm.toLowerCase())
),
[rows, searchTerm]
2024-11-20 14:16:26 +05:30
);
2024-11-20 14:28:53 +05:30
// Group filtered rows by robot meta id
2025-01-23 19:50:09 +05:30
const groupedRows = useMemo(() =>
filteredRows.reduce((acc, row) => {
if (!acc[row.robotMetaId]) {
acc[row.robotMetaId] = [];
}
acc[row.robotMetaId].push(row);
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>
);
}
2024-06-24 22:38:57 +05:30
return (
<React.Fragment>
2024-11-20 14:16:26 +05:30
<Box display="flex" justifyContent="space-between" alignItems="center" mb={2}>
2025-01-23 19:50:09 +05:30
<Typography variant="h6" component="h2">
2024-12-10 21:45:09 +05:30
{t('runstable.runs', 'Runs')}
2024-11-20 14:16:26 +05:30
</Typography>
<TextField
size="small"
2024-12-10 21:45:09 +05:30
placeholder={t('runstable.search', 'Search runs...')}
2024-11-20 14:16:26 +05:30
onChange={handleSearchChange}
InputProps={{
startAdornment: <SearchIcon sx={{ color: 'action.active', mr: 1 }} />
}}
sx={{ width: '250px' }}
/>
</Box>
2025-01-23 19:50:09 +05:30
<TableContainer component={Paper} sx={{ width: '100%', overflow: 'hidden' }}>
{Object.entries(groupedRows).map(([id, data]) => (
<Accordion
key={id}
onChange={(event, isExpanded) => handleAccordionChange(id, isExpanded)}
TransitionProps={{ unmountOnExit: true }} // Optimize accordion rendering
>
<AccordionSummary expandIcon={<ExpandMoreIcon />}>
<Typography variant="h6">{data[data.length - 1].name}</Typography>
</AccordionSummary>
<AccordionDetails>
<Table stickyHeader aria-label="sticky table">
<TableHead>
<TableRow>
<TableCell />
{translatedColumns.map((column) => (
<TableCell
key={column.id}
align={column.align}
style={{ minWidth: column.minWidth }}
>
{column.label}
</TableCell>
))}
</TableRow>
</TableHead>
<TableBody>
{renderTableRows(data)}
</TableBody>
</Table>
</AccordionDetails>
</Accordion>
))}
</TableContainer>
2024-06-24 22:38:57 +05:30
<TablePagination
component="div"
2024-11-20 14:16:26 +05:30
count={filteredRows.length}
2024-06-24 22:38:57 +05:30
rowsPerPage={rowsPerPage}
page={page}
onPageChange={handleChangePage}
onRowsPerPageChange={handleChangeRowsPerPage}
2025-01-23 19:50:09 +05:30
rowsPerPageOptions={[10, 25, 50]}
2024-06-24 22:38:57 +05:30
/>
2024-09-09 08:00:48 +05:30
</React.Fragment>
2024-06-24 22:38:57 +05:30
);
2024-12-10 21:45:09 +05:30
};