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

209 lines
6.6 KiB
TypeScript
Raw Normal View History

2024-06-24 22:38:57 +05:30
import * as React from 'react';
2024-12-10 21:45:09 +05:30
import { useEffect, useState } from "react";
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';
2024-12-10 21:45:09 +05:30
import { Accordion, AccordionSummary, AccordionDetails, Typography, Box, TextField } from '@mui/material';
import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
import SearchIcon from '@mui/icons-material/Search';
2024-06-24 22:38:57 +05:30
import { useGlobalInfoStore } from "../../context/globalInfo";
import { getStoredRuns } from "../../api/storage";
import { RunSettings } from "./RunSettings";
import { CollapsibleRow } from "./ColapsibleRow";
2024-12-10 21:45:09 +05:30
// Export columns before the component
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-10 21:45:09 +05:30
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;
}
2024-12-10 21:45:09 +05:30
export const RunsTable: React.FC<RunsTableProps> = ({
currentInterpretationLog,
abortRunHandler,
runId,
runningRecordingName
}) => {
const { t } = useTranslation();
// Update column labels using translation if needed
const translatedColumns = columns.map(column => ({
...column,
label: t(`runstable.${column.id}`, column.label)
}));
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('');
2024-06-24 22:38:57 +05:30
const { notify, rerenderRuns, setRerenderRuns } = useGlobalInfoStore();
const handleChangePage = (event: unknown, newPage: number) => {
setPage(newPage);
};
const handleChangeRowsPerPage = (event: React.ChangeEvent<HTMLInputElement>) => {
setRowsPerPage(+event.target.value);
setPage(0);
};
2024-11-20 14:16:26 +05:30
const handleSearchChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setSearchTerm(event.target.value);
setPage(0);
};
2024-06-24 22:38:57 +05:30
const fetchRuns = async () => {
const runs = await getStoredRuns();
if (runs) {
2024-12-10 21:45:09 +05:30
const parsedRows: Data[] = runs.map((run: any, index: number) => ({
id: index,
...run,
}));
2024-06-24 22:38:57 +05:30
setRows(parsedRows);
} else {
2024-12-21 13:39:52 +05:30
notify('error', t('runstable.notifications.no_runs'));
2024-06-24 22:38:57 +05:30
}
2024-09-09 08:00:48 +05:30
};
2024-06-24 22:38:57 +05:30
2024-09-09 08:00:48 +05:30
useEffect(() => {
2024-06-24 22:38:57 +05:30
if (rows.length === 0 || rerenderRuns) {
fetchRuns();
setRerenderRuns(false);
}
2024-12-10 21:45:09 +05:30
}, [rerenderRuns, rows.length, setRerenderRuns]);
2024-06-24 22:38:57 +05:30
const handleDelete = () => {
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();
2024-09-09 08:00:48 +05:30
};
2024-11-20 14:16:26 +05:30
// Filter rows based on search term
const filteredRows = rows.filter((row) =>
row.name.toLowerCase().includes(searchTerm.toLowerCase())
);
2024-11-20 14:28:53 +05:30
// Group filtered rows by robot meta id
2024-11-20 14:16:26 +05:30
const groupedRows = filteredRows.reduce((acc, row) => {
2024-11-20 14:28:53 +05:30
if (!acc[row.robotMetaId]) {
acc[row.robotMetaId] = [];
2024-09-09 08:00:48 +05:30
}
2024-11-20 14:28:53 +05:30
acc[row.robotMetaId].push(row);
2024-09-09 08:00:48 +05:30
return acc;
}, {} as Record<string, Data[]>);
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}>
<Typography variant="h6" gutterBottom>
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
value={searchTerm}
onChange={handleSearchChange}
InputProps={{
startAdornment: <SearchIcon sx={{ color: 'action.active', mr: 1 }} />
}}
sx={{ width: '250px' }}
/>
</Box>
2024-06-24 22:38:57 +05:30
<TableContainer component={Paper} sx={{ width: '100%', overflow: 'hidden' }}>
2024-11-20 14:28:53 +05:30
{Object.entries(groupedRows).map(([id, data]) => (
<Accordion key={id}>
2024-09-09 08:00:48 +05:30
<AccordionSummary expandIcon={<ExpandMoreIcon />}>
2024-11-20 14:28:53 +05:30
<Typography variant="h6">{data[data.length - 1].name}</Typography>
2024-09-09 08:00:48 +05:30
</AccordionSummary>
<AccordionDetails>
<Table stickyHeader aria-label="sticky table">
<TableHead>
<TableRow>
<TableCell />
2024-12-10 21:45:09 +05:30
{translatedColumns.map((column) => (
2024-09-09 08:00:48 +05:30
<TableCell
key={column.id}
align={column.align}
style={{ minWidth: column.minWidth }}
>
{column.label}
</TableCell>
))}
</TableRow>
</TableHead>
<TableBody>
2024-11-20 14:28:53 +05:30
{data
.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
.map((row) => (
<CollapsibleRow
row={row}
handleDelete={handleDelete}
key={`row-${row.id}`}
isOpen={runId === row.runId && runningRecordingName === row.name}
currentLog={currentInterpretationLog}
abortRunHandler={abortRunHandler}
runningRecordingName={runningRecordingName}
/>
))}
2024-09-09 08:00:48 +05:30
</TableBody>
</Table>
</AccordionDetails>
</Accordion>
))}
2024-06-24 22:38:57 +05:30
</TableContainer>
<TablePagination
rowsPerPageOptions={[10, 25, 50]}
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}
/>
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
};