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

270 lines
8.9 KiB
TypeScript
Raw Normal View History

2024-06-24 22:37:49 +05:30
import * as React from 'react';
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';
import { useEffect } from "react";
2024-07-31 22:46:38 +05:30
import { WorkflowFile } from "maxun-core";
2024-06-24 22:37:49 +05:30
import { IconButton } from "@mui/material";
2024-09-10 06:43:04 +05:30
import { Schedule, DeleteForever, Edit, PlayCircle } from "@mui/icons-material";
2024-09-15 01:41:01 +05:30
import LinkIcon from '@mui/icons-material/Link';
2024-06-24 22:37:49 +05:30
import { useGlobalInfoStore } from "../../context/globalInfo";
import { deleteRecordingFromStorage, getStoredRecordings } from "../../api/storage";
interface Column {
2024-10-08 20:52:25 +05:30
id: 'interpret' | 'name' | 'createdAt' | 'edit' | 'updatedAt' | 'delete' | 'schedule' | 'integrate';
2024-06-24 22:37:49 +05:30
label: string;
minWidth?: number;
align?: 'right';
format?: (value: string) => string;
}
const columns: readonly Column[] = [
{ id: 'interpret', label: 'Run', minWidth: 80 },
{ id: 'name', label: 'Name', minWidth: 80 },
{
2024-10-08 20:51:12 +05:30
id: 'createdAt',
2024-06-24 22:37:49 +05:30
label: 'Created at',
minWidth: 80,
//format: (value: string) => value.toLocaleString('en-US'),
},
{
id: 'edit',
label: 'Edit',
minWidth: 80,
},
2024-09-10 03:15:56 +05:30
{
2024-09-10 03:18:04 +05:30
id: 'schedule',
2024-09-10 03:15:56 +05:30
label: 'Schedule',
minWidth: 80,
},
2024-09-15 01:24:18 +05:30
{
id: 'integrate',
label: 'Integrate',
minWidth: 80,
},
2024-06-24 22:37:49 +05:30
{
2024-10-08 20:52:25 +05:30
id: 'updatedAt',
2024-06-24 22:37:49 +05:30
label: 'Updated at',
minWidth: 80,
//format: (value: string) => value.toLocaleString('en-US'),
},
{
id: 'delete',
label: 'Delete',
minWidth: 80,
},
];
interface Data {
id: number;
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-09-10 03:09:34 +05:30
handleEditRecording: (fileName: string) => void;
handleRunRecording: (fileName: string, params: string[]) => void;
2024-09-10 06:41:22 +05:30
handleScheduleRecording: (fileName: string, params: string[]) => void;
2024-09-15 01:30:48 +05:30
handleIntegrateRecording: (fileName: string, params: string[]) => void;
2024-06-24 22:37:49 +05:30
}
2024-09-15 01:30:48 +05:30
export const RecordingsTable = ({ handleEditRecording, handleRunRecording, handleScheduleRecording, handleIntegrateRecording }: RecordingsTableProps) => {
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[]>([]);
const { notify, setRecordings } = useGlobalInfoStore();
const handleChangePage = (event: unknown, newPage: number) => {
setPage(newPage);
};
const handleChangeRowsPerPage = (event: React.ChangeEvent<HTMLInputElement>) => {
setRowsPerPage(+event.target.value);
setPage(0);
};
const fetchRecordings = async () => {
const recordings = await getStoredRecordings();
if (recordings) {
const parsedRows: Data[] = [];
recordings.map((recording, index) => {
const parsedRecording = JSON.parse(recording);
if (parsedRecording.recording_meta) {
parsedRows.push({
id: index,
...parsedRecording.recording_meta,
content: parsedRecording.recording
});
}
});
setRecordings(parsedRows.map((recording) => recording.name));
setRows(parsedRows);
} else {
console.log('No recordings found.');
}
}
2024-09-10 03:09:34 +05:30
useEffect(() => {
2024-06-24 22:37:49 +05:30
if (rows.length === 0) {
fetchRecordings();
}
}, []);
return (
<React.Fragment>
<TableContainer component={Paper} sx={{ width: '100%', overflow: 'hidden' }}>
<Table stickyHeader aria-label="sticky table">
<TableHead>
<TableRow>
{columns.map((column) => (
<TableCell
key={column.id}
align={column.align}
style={{ minWidth: column.minWidth }}
>
{column.label}
</TableCell>
))}
</TableRow>
</TableHead>
<TableBody>
{rows.length !== 0 ? rows
.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
.map((row) => {
return (
<TableRow hover role="checkbox" tabIndex={-1} key={row.id}>
{columns.map((column) => {
// @ts-ignore
2024-09-10 03:09:34 +05:30
const value: any = row[column.id];
2024-06-24 22:37:49 +05:30
if (value !== undefined) {
return (
<TableCell key={column.id} align={column.align}>
{value}
</TableCell>
);
} else {
switch (column.id) {
case 'interpret':
return (
<TableCell key={column.id} align={column.align}>
2024-09-10 03:09:34 +05:30
<InterpretButton handleInterpret={() => handleRunRecording(row.name, row.params || [])} />
2024-06-24 22:37:49 +05:30
</TableCell>
);
case 'edit':
return (
<TableCell key={column.id} align={column.align}>
2024-09-10 03:09:34 +05:30
<IconButton aria-label="add" size="small" onClick={() => {
2024-06-24 22:37:49 +05:30
handleEditRecording(row.name);
2024-09-10 03:09:34 +05:30
}} sx={{ '&:hover': { color: '#1976d2', backgroundColor: 'transparent' } }}>
<Edit />
2024-06-24 22:37:49 +05:30
</IconButton>
</TableCell>
);
2024-09-10 03:18:04 +05:30
case 'schedule':
return (
<TableCell key={column.id} align={column.align}>
2024-09-10 06:43:29 +05:30
<ScheduleButton handleSchedule={() => handleScheduleRecording(row.name, row.params || [])} />
2024-09-10 03:18:04 +05:30
</TableCell>
);
2024-09-15 01:31:13 +05:30
case 'integrate':
2024-09-15 01:30:48 +05:30
return (
<TableCell key={column.id} align={column.align}>
<IntegrateButton handleIntegrate={() => handleIntegrateRecording(row.name, row.params || [])} />
</TableCell>
);
2024-06-24 22:37:49 +05:30
case 'delete':
return (
<TableCell key={column.id} align={column.align}>
2024-09-10 03:09:34 +05:30
<IconButton aria-label="add" size="small" onClick={() => {
2024-06-24 22:37:49 +05:30
deleteRecordingFromStorage(row.name).then((result: boolean) => {
if (result) {
setRows([]);
notify('success', 'Recording deleted successfully');
fetchRecordings();
}
})
2024-09-10 03:09:34 +05:30
}} sx={{ '&:hover': { color: '#1976d2', backgroundColor: 'transparent' } }}>
<DeleteForever />
2024-06-24 22:37:49 +05:30
</IconButton>
</TableCell>
);
default:
2024-09-10 03:09:34 +05:30
return null;
2024-06-24 22:37:49 +05:30
}
}
})}
</TableRow>
);
})
2024-09-10 03:09:34 +05:30
: null}
2024-06-24 22:37:49 +05:30
</TableBody>
</Table>
</TableContainer>
<TablePagination
rowsPerPageOptions={[10, 25, 50]}
component="div"
count={rows ? rows.length : 0}
rowsPerPage={rowsPerPage}
page={page}
onPageChange={handleChangePage}
onRowsPerPageChange={handleChangeRowsPerPage}
/>
</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-09-10 03:09:34 +05:30
sx={{ '&:hover': { color: '#1976d2', backgroundColor: 'transparent' } }}>
<PlayCircle />
2024-06-24 22:37:49 +05:30
</IconButton>
)
}
2024-09-10 03:17:11 +05:30
2024-09-10 06:41:22 +05:30
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-09-10 03:17:11 +05:30
sx={{ '&:hover': { color: '#1976d2', backgroundColor: 'transparent' } }}>
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();
}}
sx={{ '&:hover': { color: '#1976d2', backgroundColor: 'transparent' } }}>
2024-09-15 01:41:01 +05:30
<LinkIcon />
2024-09-15 01:30:48 +05:30
</IconButton>
)
}