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"; import { WorkflowFile } from "maxun-core"; import SearchIcon from '@mui/icons-material/Search'; import { IconButton, Button, Box, Typography, TextField, MenuItem, Menu, ListItemIcon, ListItemText } from "@mui/material"; import { Schedule, DeleteForever, Edit, PlayCircle, Settings, Power, ContentCopy, MoreHoriz } from "@mui/icons-material"; import LinkIcon from '@mui/icons-material/Link'; import { useGlobalInfoStore } from "../../context/globalInfo"; import { checkRunsForRecording, deleteRecordingFromStorage, getStoredRecordings } from "../../api/storage"; import { Add } from "@mui/icons-material"; import { useNavigate } from 'react-router-dom'; import { stopRecording } from "../../api/recording"; import { GenericModal } from '../atoms/GenericModal'; import axios from 'axios'; import { apiUrl } from '../../apiConfig'; import { Menu as MenuIcon } from '@mui/icons-material'; /** TODO: * 1. allow editing existing robot after persisting browser steps * 2. show robot settings: id, url, etc. */ interface Column { id: 'interpret' | 'name' | 'options' | 'schedule' | 'integrate' | 'settings'; 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 }, { id: 'schedule', label: 'Schedule', minWidth: 80, }, { id: 'integrate', label: 'Integrate', minWidth: 80, }, { id: 'settings', label: 'Settings', minWidth: 80, }, { id: 'options', label: 'Options', minWidth: 80, }, ]; interface Data { id: string; name: string; createdAt: string; updatedAt: string; content: WorkflowFile; params: string[]; } interface RecordingsTableProps { handleEditRecording: (id: string, fileName: string) => void; handleRunRecording: (id: string, fileName: string, params: string[]) => void; handleScheduleRecording: (id: string, fileName: string, params: string[]) => void; handleIntegrateRecording: (id: string, fileName: string, params: string[]) => void; handleSettingsRecording: (id: string, fileName: string, params: string[]) => void; handleEditRobot: (id: string, name: string, params: string[]) => void; handleDuplicateRobot: (id: string, name: string, params: string[]) => void; } export const RecordingsTable = ({ handleEditRecording, handleRunRecording, handleScheduleRecording, handleIntegrateRecording, handleSettingsRecording, handleEditRobot, handleDuplicateRobot }: RecordingsTableProps) => { const [page, setPage] = React.useState(0); const [rowsPerPage, setRowsPerPage] = React.useState(10); const [rows, setRows] = React.useState([]); const [isModalOpen, setModalOpen] = React.useState(false); const [searchTerm, setSearchTerm] = React.useState(''); const { notify, setRecordings, browserId, setBrowserId, recordingUrl, setRecordingUrl, recordingName, setRecordingName, recordingId, setRecordingId } = useGlobalInfoStore(); const navigate = useNavigate(); const handleChangePage = (event: unknown, newPage: number) => { setPage(newPage); }; const handleChangeRowsPerPage = (event: React.ChangeEvent) => { setRowsPerPage(+event.target.value); setPage(0); }; const handleSearchChange = (event: React.ChangeEvent) => { setSearchTerm(event.target.value); setPage(0); }; const fetchRecordings = async () => { const recordings = await getStoredRecordings(); if (recordings) { const parsedRows: Data[] = []; recordings.map((recording: any, index: number) => { if (recording && recording.recording_meta) { parsedRows.push({ id: index, ...recording.recording_meta, content: recording.recording }); } }); setRecordings(parsedRows.map((recording) => recording.name)); setRows(parsedRows); } else { console.log('No recordings found.'); } } const handleNewRecording = async () => { if (browserId) { setBrowserId(null); await stopRecording(browserId); } setModalOpen(true); }; const handleStartRecording = () => { setBrowserId('new-recording'); setRecordingName(''); setRecordingId(''); navigate('/recording'); } const startRecording = () => { setModalOpen(false); handleStartRecording(); }; useEffect(() => { if (rows.length === 0) { fetchRecordings(); } }, []); // Filter rows based on search term const filteredRows = rows.filter((row) => row.name.toLowerCase().includes(searchTerm.toLowerCase()) ); return ( My Robots }} sx={{ width: '250px' }} /> Create Robot {columns.map((column) => ( {column.label} ))} {filteredRows.length !== 0 ? filteredRows .slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage) .map((row) => { return ( {columns.map((column) => { // @ts-ignore const value: any = row[column.id]; if (value !== undefined) { return ( {value} ); } else { switch (column.id) { case 'interpret': return ( handleRunRecording(row.id, row.name, row.params || [])} /> ); case 'schedule': return ( handleScheduleRecording(row.id, row.name, row.params || [])} /> ); case 'integrate': return ( handleIntegrateRecording(row.id, row.name, row.params || [])} /> ); case 'options': return ( handleEditRobot(row.id, row.name, row.params || [])} handleDelete={() => { checkRunsForRecording(row.id).then((result: boolean) => { if (result) { notify('warning', 'Cannot delete recording as it has active runs'); } }) deleteRecordingFromStorage(row.id).then((result: boolean) => { if (result) { setRows([]); notify('success', 'Recording deleted successfully'); fetchRecordings(); } }) }} handleDuplicate={() => { handleDuplicateRobot(row.id, row.name, row.params || []); }} /> ); case 'settings': return ( handleSettingsRecording(row.id, row.name, row.params || [])} /> ); default: return null; } } })} ); }) : null}
setModalOpen(false)} modalStyle={modalStyle}>
Enter URL To Extract Data setRecordingUrl(e.target.value)} style={{ marginBottom: '20px', marginTop: '20px' }} />
); } interface InterpretButtonProps { handleInterpret: () => void; } const InterpretButton = ({ handleInterpret }: InterpretButtonProps) => { return ( { handleInterpret(); }} > ) } interface ScheduleButtonProps { handleSchedule: () => void; } const ScheduleButton = ({ handleSchedule }: ScheduleButtonProps) => { return ( { handleSchedule(); }} > ) } interface IntegrateButtonProps { handleIntegrate: () => void; } const IntegrateButton = ({ handleIntegrate }: IntegrateButtonProps) => { return ( { handleIntegrate(); }} > ) } interface SettingsButtonProps { handleSettings: () => void; } const SettingsButton = ({ handleSettings }: SettingsButtonProps) => { return ( { handleSettings(); }} > ) } interface OptionsButtonProps { handleEdit: () => void; handleDelete: () => void; handleDuplicate: () => void; } const OptionsButton = ({ handleEdit, handleDelete, handleDuplicate }: OptionsButtonProps) => { const [anchorEl, setAnchorEl] = React.useState(null); const handleClick = (event: React.MouseEvent) => { setAnchorEl(event.currentTarget); }; const handleClose = () => { setAnchorEl(null); }; return ( <> { handleEdit(); handleClose(); }}> Edit { handleDelete(); handleClose(); }}> Delete { handleDuplicate(); handleClose(); }}> Duplicate ); }; const modalStyle = { top: '50%', left: '50%', transform: 'translate(-50%, -50%)', width: '30%', backgroundColor: 'background.paper', p: 4, height: 'fit-content', display: 'block', padding: '20px', };