Files
parcer/src/components/robot/RecordingsTable.tsx

591 lines
17 KiB
TypeScript
Raw Normal View History

2024-06-24 22:37:49 +05:30
import * as React from 'react';
2024-12-10 20:40:59 +05:30
import { useTranslation } from 'react-i18next';
2024-06-24 22:37:49 +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-23 19:12:31 +05:30
import { memo, useCallback, useEffect, useMemo } from "react";
2024-07-31 22:46:38 +05:30
import { WorkflowFile } from "maxun-core";
2024-11-20 14:16:26 +05:30
import SearchIcon from '@mui/icons-material/Search';
2025-01-22 19:05:41 +05:30
import {
IconButton,
Button,
Box,
Typography,
TextField,
MenuItem,
Menu,
ListItemIcon,
ListItemText,
CircularProgress,
RadioGroup,
FormControlLabel,
2025-01-22 19:07:20 +05:30
Radio,
Checkbox,
2025-01-22 19:05:41 +05:30
} from "@mui/material";
import {
Schedule,
DeleteForever,
Edit,
PlayCircle,
Settings,
Power,
ContentCopy,
MoreHoriz
} from "@mui/icons-material";
2024-06-24 22:37:49 +05:30
import { useGlobalInfoStore } from "../../context/globalInfo";
import { checkRunsForRecording, deleteRecordingFromStorage, getStoredRecordings } from "../../api/storage";
2024-10-21 01:10:16 +05:30
import { Add } from "@mui/icons-material";
import { useNavigate } from 'react-router-dom';
2024-10-21 00:59:46 +05:30
import { stopRecording } from "../../api/recording";
2025-01-09 19:49:20 +05:30
import { GenericModal } from '../ui/GenericModal';
2024-10-21 01:04:20 +05:30
2024-12-10 20:40:59 +05:30
2024-10-10 23:05:15 +05:30
/** TODO:
* 1. allow editing existing robot after persisting browser steps
*/
2024-06-24 22:37:49 +05:30
interface Column {
2024-11-19 02:41:02 +05:30
id: 'interpret' | 'name' | 'options' | 'schedule' | 'integrate' | 'settings';
2024-06-24 22:37:49 +05:30
label: string;
minWidth?: number;
align?: 'right';
format?: (value: string) => string;
}
interface Data {
2024-10-09 22:30:13 +05:30
id: string;
2024-06-24 22:37:49 +05:30
name: string;
2024-10-08 20:51:12 +05:30
createdAt: string;
2024-10-08 20:52:25 +05:30
updatedAt: string;
2024-06-24 22:37:49 +05:30
content: WorkflowFile;
params: string[];
}
interface RecordingsTableProps {
2024-10-09 22:30:13 +05:30
handleEditRecording: (id: string, fileName: string) => void;
2024-10-10 03:37:12 +05:30
handleRunRecording: (id: string, fileName: string, params: string[]) => void;
handleScheduleRecording: (id: string, fileName: string, params: string[]) => void;
handleIntegrateRecording: (id: string, fileName: string, params: string[]) => void;
2024-10-24 01:39:28 +05:30
handleSettingsRecording: (id: string, fileName: string, params: string[]) => void;
2024-11-19 02:41:02 +05:30
handleEditRobot: (id: string, name: string, params: string[]) => void;
handleDuplicateRobot: (id: string, name: string, params: string[]) => void;
2024-06-24 22:37:49 +05:30
}
2025-01-23 19:12:31 +05:30
// Virtualized row component for efficient rendering
const TableRowMemoized = memo(({ row, columns, handlers }: any) => {
return (
<TableRow hover role="checkbox" tabIndex={-1}>
{columns.map((column: Column) => {
const value: any = row[column.id];
if (value !== undefined) {
return (
<MemoizedTableCell key={column.id} align={column.align}>
{value}
</MemoizedTableCell>
);
} else {
switch (column.id) {
case 'interpret':
return (
<MemoizedTableCell key={column.id} align={column.align}>
<MemoizedInterpretButton handleInterpret={() => handlers.handleRunRecording(row.id, row.name, row.params || [])} />
</MemoizedTableCell>
);
case 'schedule':
return (
<MemoizedTableCell key={column.id} align={column.align}>
<MemoizedScheduleButton handleSchedule={() => handlers.handleScheduleRecording(row.id, row.name, row.params || [])} />
</MemoizedTableCell>
);
case 'integrate':
return (
<MemoizedTableCell key={column.id} align={column.align}>
<MemoizedIntegrateButton handleIntegrate={() => handlers.handleIntegrateRecording(row.id, row.name, row.params || [])} />
</MemoizedTableCell>
);
case 'options':
return (
<MemoizedTableCell key={column.id} align={column.align}>
<MemoizedOptionsButton
handleEdit={() => handlers.handleEditRobot(row.id, row.name, row.params || [])}
handleDuplicate={() => handlers.handleDuplicateRobot(row.id, row.name, row.params || [])}
handleDelete={() => handlers.handleDelete(row.id)}
/>
</MemoizedTableCell>
);
case 'settings':
return (
<MemoizedTableCell key={column.id} align={column.align}>
<MemoizedSettingsButton handleSettings={() => handlers.handleSettingsRecording(row.id, row.name, row.params || [])} />
</MemoizedTableCell>
);
default:
return null;
}
}
})}
</TableRow>
);
});
2025-01-23 20:49:30 +05:30
export const RecordingsTable = ({
handleEditRecording,
handleRunRecording,
handleScheduleRecording,
handleIntegrateRecording,
handleSettingsRecording,
handleEditRobot,
handleDuplicateRobot }: RecordingsTableProps) => {
2025-01-09 19:17:23 +05:30
const { t } = useTranslation();
2024-06-24 22:37:49 +05:30
const [page, setPage] = React.useState(0);
const [rowsPerPage, setRowsPerPage] = React.useState(10);
const [rows, setRows] = React.useState<Data[]>([]);
2024-10-21 01:00:33 +05:30
const [isModalOpen, setModalOpen] = React.useState(false);
2024-11-20 14:16:26 +05:30
const [searchTerm, setSearchTerm] = React.useState('');
2025-01-23 19:12:31 +05:30
const [isLoading, setIsLoading] = React.useState(true);
2024-06-24 22:37:49 +05:30
2025-01-23 19:12:31 +05:30
const columns = useMemo(() => [
2024-12-10 20:40:59 +05:30
{ id: 'interpret', label: t('recordingtable.run'), minWidth: 80 },
{ id: 'name', label: t('recordingtable.name'), minWidth: 80 },
2025-01-23 19:12:31 +05:30
{ id: 'schedule', label: t('recordingtable.schedule'), minWidth: 80 },
{ id: 'integrate', label: t('recordingtable.integrate'), minWidth: 80 },
{ id: 'settings', label: t('recordingtable.settings'), minWidth: 80 },
{ id: 'options', label: t('recordingtable.options'), minWidth: 80 },
], [t]);
2024-12-10 20:40:59 +05:30
2025-01-23 20:50:16 +05:30
const {
notify,
setRecordings,
browserId,
setBrowserId,
setInitialUrl,
recordingUrl,
setRecordingUrl,
isLogin,
setIsLogin,
2025-02-01 11:34:29 +05:30
rerenderRobots,
setRerenderRobots,
2025-01-23 20:50:16 +05:30
recordingName,
setRecordingName,
recordingId,
setRecordingId } = useGlobalInfoStore();
2024-10-21 00:59:46 +05:30
const navigate = useNavigate();
2024-06-24 22:37:49 +05:30
2025-01-23 19:12:31 +05:30
const handleChangePage = useCallback((event: unknown, newPage: number) => {
2024-06-24 22:37:49 +05:30
setPage(newPage);
2025-01-23 19:12:31 +05:30
}, []);
2024-06-24 22:37:49 +05:30
const handleChangeRowsPerPage = (event: React.ChangeEvent<HTMLInputElement>) => {
setRowsPerPage(+event.target.value);
setPage(0);
};
2025-01-23 19:12:31 +05:30
const handleSearchChange = useCallback((event: React.ChangeEvent<HTMLInputElement>) => {
2024-11-20 14:16:26 +05:30
setSearchTerm(event.target.value);
setPage(0);
2025-01-23 19:12:31 +05:30
}, []);
2024-11-20 14:16:26 +05:30
const parseDateString = (dateStr: string): Date => {
try {
if (dateStr.includes('PM') || dateStr.includes('AM')) {
return new Date(dateStr);
}
return new Date(dateStr.replace(/(\d+)\/(\d+)\//, '$2/$1/'))
} catch {
return new Date(0);
}
};
2025-01-23 19:12:31 +05:30
const fetchRecordings = useCallback(async () => {
setIsLoading(true);
try {
const recordings = await getStoredRecordings();
if (recordings) {
const parsedRows = recordings
.map((recording: any, index: number) => {
if (recording?.recording_meta) {
const parsedDate = parseDateString(recording.recording_meta.createdAt);
return {
id: index,
...recording.recording_meta,
content: recording.recording,
parsedDate
};
}
return null;
})
.filter(Boolean)
.sort((a, b) => b.parsedDate.getTime() - a.parsedDate.getTime());
2025-01-23 19:12:31 +05:30
setRecordings(parsedRows.map((recording) => recording.name));
setRows(parsedRows);
}
} catch (error) {
console.error('Error fetching recordings:', error);
notify('error', t('recordingtable.notifications.fetch_error'));
} finally {
setIsLoading(false);
2024-06-24 22:37:49 +05:30
}
2025-01-23 19:12:31 +05:30
}, [setRecordings, notify, t]);
2024-06-24 22:37:49 +05:30
2025-01-23 19:12:31 +05:30
const handleNewRecording = useCallback(async () => {
2024-10-21 00:59:46 +05:30
if (browserId) {
setBrowserId(null);
await stopRecording(browserId);
}
setModalOpen(true);
2025-01-23 19:12:31 +05:30
}, [browserId]);
2024-10-21 00:59:46 +05:30
2025-01-23 19:12:31 +05:30
const handleStartRecording = useCallback(() => {
2024-10-21 01:07:11 +05:30
setBrowserId('new-recording');
setRecordingName('');
setRecordingId('');
navigate('/recording');
2025-01-23 19:12:31 +05:30
}, [navigate]);
2024-10-21 01:07:11 +05:30
2024-10-21 01:04:20 +05:30
const startRecording = () => {
setModalOpen(false);
2024-10-21 01:07:11 +05:30
handleStartRecording();
2024-10-21 01:04:20 +05:30
};
const setBrowserRecordingUrl = (event: React.ChangeEvent<HTMLInputElement>) => {
setInitialUrl(event.target.value);
setRecordingUrl(event.target.value);
}
2024-09-10 03:09:34 +05:30
useEffect(() => {
2025-02-01 11:34:29 +05:30
if (rows.length === 0) {
2024-06-24 22:37:49 +05:30
fetchRecordings();
}
2025-02-01 11:34:29 +05:30
}, [fetchRecordings]);
useEffect(() => {
if (rerenderRobots) {
fetchRecordings().then(() => {
setRerenderRobots(false);
});
}
}, [rerenderRobots, fetchRecordings, setRerenderRobots]);
2025-01-23 19:12:31 +05:30
function useDebounce<T>(value: T, delay: number): T {
const [debouncedValue, setDebouncedValue] = React.useState<T>(value);
useEffect(() => {
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);
return () => {
clearTimeout(handler);
};
}, [value, delay]);
return debouncedValue;
}
2024-06-24 22:37:49 +05:30
2025-01-23 19:12:31 +05:30
const debouncedSearchTerm = useDebounce(searchTerm, 300);
2024-11-20 14:16:26 +05:30
// Filter rows based on search term
2025-01-23 19:12:31 +05:30
const filteredRows = useMemo(() => {
2025-01-23 19:21:53 +05:30
const searchLower = debouncedSearchTerm.toLowerCase();
return debouncedSearchTerm
2025-01-23 19:12:31 +05:30
? rows.filter(row => row.name.toLowerCase().includes(searchLower))
: rows;
}, [rows, debouncedSearchTerm]);
const visibleRows = useMemo(() => {
const start = page * rowsPerPage;
return filteredRows.slice(start, start + rowsPerPage);
}, [filteredRows, page, rowsPerPage]);
const handlers = useMemo(() => ({
handleRunRecording,
handleScheduleRecording,
handleIntegrateRecording,
handleSettingsRecording,
handleEditRobot,
handleDuplicateRobot,
handleDelete: async (id: string) => {
const hasRuns = await checkRunsForRecording(id);
if (hasRuns) {
notify('warning', t('recordingtable.notifications.delete_warning'));
return;
}
const success = await deleteRecordingFromStorage(id);
if (success) {
setRows([]);
notify('success', t('recordingtable.notifications.delete_success'));
fetchRecordings();
}
}
}), [handleRunRecording, handleScheduleRecording, handleIntegrateRecording, handleSettingsRecording, handleEditRobot, handleDuplicateRobot, notify, t]);
2024-11-22 21:56:02 +05:30
2024-06-24 22:37:49 +05:30
return (
<React.Fragment>
2024-10-21 00:55:23 +05:30
<Box display="flex" justifyContent="space-between" alignItems="center">
<Typography variant="h6" gutterBottom>
2024-12-10 20:40:59 +05:30
{t('recordingtable.heading')}
2024-10-21 00:55:23 +05:30
</Typography>
2024-11-20 14:16:26 +05:30
<Box display="flex" alignItems="center" gap={2}>
<TextField
size="small"
2024-12-10 20:40:59 +05:30
placeholder={t('recordingtable.search')}
2024-11-20 14:16:26 +05:30
value={searchTerm}
onChange={handleSearchChange}
InputProps={{
startAdornment: <SearchIcon sx={{ color: 'action.active', mr: 1 }} />
}}
sx={{ width: '250px' }}
/>
<IconButton
aria-label="new"
size={"small"}
onClick={handleNewRecording}
sx={{
width: '140px',
borderRadius: '5px',
padding: '8px',
background: '#ff00c3',
color: 'white',
marginRight: '10px',
fontFamily: '"Roboto","Helvetica","Arial",sans-serif',
fontWeight: '500',
fontSize: '0.875rem',
lineHeight: '1.75',
letterSpacing: '0.02857em',
'&:hover': { color: 'white', backgroundColor: '#ff00c3' }
}}
>
2024-12-10 20:40:59 +05:30
<Add sx={{ marginRight: '5px' }} /> {t('recordingtable.new')}
2024-11-20 14:16:26 +05:30
</IconButton>
</Box>
2024-10-21 00:55:23 +05:30
</Box>
2025-01-23 19:12:31 +05:30
{isLoading ? (
2025-01-17 22:54:34 +05:30
<Box display="flex" justifyContent="center" alignItems="center" height="50%">
<CircularProgress />
</Box>
) : (
<TableContainer component={Paper} sx={{ width: '100%', overflow: 'hidden', marginTop: '15px' }}>
<Table stickyHeader aria-label="sticky table">
<TableHead>
<TableRow>
{columns.map((column) => (
2025-01-23 19:12:31 +05:30
<MemoizedTableCell
2025-01-17 22:54:34 +05:30
key={column.id}
2025-01-23 19:12:31 +05:30
// align={column.align}
2025-01-17 22:54:34 +05:30
style={{ minWidth: column.minWidth }}
>
{column.label}
2025-01-23 19:12:31 +05:30
</MemoizedTableCell>
2025-01-17 22:54:34 +05:30
))}
</TableRow>
</TableHead>
<TableBody>
2025-01-23 19:12:31 +05:30
{visibleRows.map((row) => (
<TableRowMemoized
key={row.id}
row={row}
columns={columns}
handlers={handlers}
/>
))}
2025-01-17 22:54:34 +05:30
</TableBody>
</Table>
</TableContainer>
)}
2025-01-23 19:12:31 +05:30
2024-06-24 22:37:49 +05:30
<TablePagination
2025-01-23 19:12:31 +05:30
rowsPerPageOptions={[10, 25, 50, 100]}
2024-06-24 22:37:49 +05:30
component="div"
2024-11-20 14:16:26 +05:30
count={filteredRows.length}
2024-06-24 22:37:49 +05:30
rowsPerPage={rowsPerPage}
page={page}
onPageChange={handleChangePage}
onRowsPerPageChange={handleChangeRowsPerPage}
/>
2024-10-25 00:11:22 +05:30
<GenericModal isOpen={isModalOpen} onClose={() => setModalOpen(false)} modalStyle={modalStyle}>
2025-01-22 19:11:38 +05:30
<div style={{ padding: '10px' }}>
2024-12-10 20:40:59 +05:30
<Typography variant="h6" gutterBottom>{t('recordingtable.modal.title')}</Typography>
2024-10-21 01:07:53 +05:30
<TextField
2024-12-10 20:40:59 +05:30
label={t('recordingtable.modal.label')}
2024-10-21 01:07:53 +05:30
variant="outlined"
fullWidth
value={recordingUrl}
onChange={setBrowserRecordingUrl}
2025-01-22 19:14:12 +05:30
style={{ marginBottom: '10px', marginTop: '20px' }}
2024-10-21 01:07:53 +05:30
/>
2025-01-22 19:09:03 +05:30
<FormControlLabel
control={
<Checkbox
checked={isLogin}
onChange={(e) => setIsLogin(e.target.checked)}
color="primary"
/>
}
label={t('recordingtable.modal.login_title')}
2025-01-22 19:14:12 +05:30
style={{ marginBottom: '10px' }}
2025-01-22 19:13:00 +05:30
/>
2025-01-22 19:12:39 +05:30
<br />
2024-10-21 01:07:53 +05:30
<Button
variant="contained"
color="primary"
onClick={startRecording}
disabled={!recordingUrl}
>
2024-12-10 20:40:59 +05:30
{t('recordingtable.modal.button')}
2024-10-21 01:07:53 +05:30
</Button>
</div>
</GenericModal>
2024-06-24 22:37:49 +05:30
</React.Fragment>
);
}
interface InterpretButtonProps {
handleInterpret: () => void;
}
2024-09-10 03:09:34 +05:30
const InterpretButton = ({ handleInterpret }: InterpretButtonProps) => {
2024-06-24 22:37:49 +05:30
return (
2024-09-10 03:09:34 +05:30
<IconButton aria-label="add" size="small" onClick={() => {
2024-06-24 22:37:49 +05:30
handleInterpret();
}}
2024-10-19 03:45:34 +05:30
>
2024-09-10 03:09:34 +05:30
<PlayCircle />
2024-06-24 22:37:49 +05:30
</IconButton>
)
}
2024-09-10 03:17:11 +05:30
interface ScheduleButtonProps {
handleSchedule: () => void;
}
const ScheduleButton = ({ handleSchedule }: ScheduleButtonProps) => {
return (
2024-09-10 06:41:22 +05:30
<IconButton aria-label="add" size="small" onClick={() => {
handleSchedule();
}}
2024-10-19 03:45:34 +05:30
>
2024-09-10 06:43:04 +05:30
<Schedule />
2024-09-10 03:17:11 +05:30
</IconButton>
2024-09-10 06:41:22 +05:30
)
2024-09-15 01:30:48 +05:30
}
interface IntegrateButtonProps {
handleIntegrate: () => void;
}
const IntegrateButton = ({ handleIntegrate }: IntegrateButtonProps) => {
return (
<IconButton aria-label="add" size="small" onClick={() => {
handleIntegrate();
}}
2024-10-19 03:45:34 +05:30
>
2024-10-28 06:24:07 +05:30
<Power />
2024-09-15 01:30:48 +05:30
</IconButton>
)
}
2024-10-24 01:39:28 +05:30
interface SettingsButtonProps {
handleSettings: () => void;
}
const SettingsButton = ({ handleSettings }: SettingsButtonProps) => {
return (
<IconButton aria-label="add" size="small" onClick={() => {
handleSettings();
}}
>
<Settings />
</IconButton>
)
2024-10-25 00:11:22 +05:30
}
2024-11-19 02:41:02 +05:30
interface OptionsButtonProps {
handleEdit: () => void;
handleDelete: () => void;
handleDuplicate: () => void;
}
const OptionsButton = ({ handleEdit, handleDelete, handleDuplicate }: OptionsButtonProps) => {
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);
const handleClick = (event: React.MouseEvent<HTMLElement>) => {
setAnchorEl(event.currentTarget);
};
const handleClose = () => {
setAnchorEl(null);
};
2025-01-09 19:17:23 +05:30
const { t } = useTranslation();
2024-12-10 20:40:59 +05:30
2024-11-19 02:41:02 +05:30
return (
<>
<IconButton
aria-label="options"
size="small"
onClick={handleClick}
>
2024-11-22 21:55:52 +05:30
<MoreHoriz />
2024-11-19 02:41:02 +05:30
</IconButton>
<Menu
anchorEl={anchorEl}
open={Boolean(anchorEl)}
onClose={handleClose}
>
<MenuItem onClick={() => { handleEdit(); handleClose(); }}>
<ListItemIcon>
<Edit fontSize="small" />
</ListItemIcon>
2024-12-10 20:40:59 +05:30
<ListItemText>{t('recordingtable.edit')}</ListItemText>
2024-11-19 02:41:02 +05:30
</MenuItem>
2024-11-19 02:41:02 +05:30
<MenuItem onClick={() => { handleDelete(); handleClose(); }}>
<ListItemIcon>
<DeleteForever fontSize="small" />
</ListItemIcon>
2024-12-10 20:40:59 +05:30
<ListItemText>{t('recordingtable.delete')}</ListItemText>
2024-11-19 02:41:02 +05:30
</MenuItem>
2024-11-19 02:41:02 +05:30
<MenuItem onClick={() => { handleDuplicate(); handleClose(); }}>
<ListItemIcon>
<ContentCopy fontSize="small" />
</ListItemIcon>
2024-12-10 20:40:59 +05:30
<ListItemText>{t('recordingtable.duplicate')}</ListItemText>
2024-11-19 02:41:02 +05:30
</MenuItem>
</Menu>
</>
);
};
2025-01-23 19:12:31 +05:30
const MemoizedTableCell = memo(TableCell);
// Memoized action buttons
const MemoizedInterpretButton = memo(InterpretButton);
const MemoizedScheduleButton = memo(ScheduleButton);
const MemoizedIntegrateButton = memo(IntegrateButton);
const MemoizedSettingsButton = memo(SettingsButton);
const MemoizedOptionsButton = memo(OptionsButton);
2024-10-25 00:11:22 +05:30
const modalStyle = {
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)',
width: '30%',
backgroundColor: 'background.paper',
p: 4,
height: 'fit-content',
display: 'block',
padding: '20px',
};