import React, { useCallback, useState } from "react"; import { connect, ConnectedProps } from "react-redux"; import { makeStyles } from "@material-ui/core/styles"; import Table from "@material-ui/core/Table"; import TableBody from "@material-ui/core/TableBody"; import TableCell from "@material-ui/core/TableCell"; import Checkbox from "@material-ui/core/Checkbox"; import TableContainer from "@material-ui/core/TableContainer"; import TableHead from "@material-ui/core/TableHead"; import TableRow from "@material-ui/core/TableRow"; import Tooltip from "@material-ui/core/Tooltip"; import Paper from "@material-ui/core/Paper"; import IconButton from "@material-ui/core/IconButton"; import PlayArrowIcon from "@material-ui/icons/PlayArrow"; import DeleteIcon from "@material-ui/icons/Delete"; import MoreHorizIcon from "@material-ui/icons/MoreHoriz"; import TableFooter from "@material-ui/core/TableFooter"; import TablePagination from "@material-ui/core/TablePagination"; import Alert from "@material-ui/lab/Alert"; import AlertTitle from "@material-ui/lab/AlertTitle"; import SyntaxHighlighter from "./SyntaxHighlighter"; import { AppState } from "../store"; import { batchDeleteArchivedTasksAsync, batchRunArchivedTasksAsync, deleteArchivedTaskAsync, deleteAllArchivedTasksAsync, listArchivedTasksAsync, runArchivedTaskAsync, runAllArchivedTasksAsync, } from "../actions/tasksActions"; import TablePaginationActions, { defaultPageSize, rowsPerPageOptions, } from "./TablePaginationActions"; import TableActions from "./TableActions"; import { timeAgo, uuidPrefix } from "../utils"; import { usePolling } from "../hooks"; import { ArchivedTaskExtended } from "../reducers/tasksReducer"; import { TableColumn } from "../types/table"; const useStyles = makeStyles((theme) => ({ table: { minWidth: 650, }, stickyHeaderCell: { background: theme.palette.background.paper, }, alert: { borderTopLeftRadius: 0, borderTopRightRadius: 0, }, pagination: { border: "none", }, })); function mapStateToProps(state: AppState) { return { loading: state.tasks.archivedTasks.loading, error: state.tasks.archivedTasks.error, tasks: state.tasks.archivedTasks.data, batchActionPending: state.tasks.archivedTasks.batchActionPending, allActionPending: state.tasks.archivedTasks.allActionPending, pollInterval: state.settings.pollInterval, }; } const mapDispatchToProps = { listArchivedTasksAsync, runArchivedTaskAsync, runAllArchivedTasksAsync, deleteArchivedTaskAsync, deleteAllArchivedTasksAsync, batchRunArchivedTasksAsync, batchDeleteArchivedTasksAsync, }; const connector = connect(mapStateToProps, mapDispatchToProps); type ReduxProps = ConnectedProps; interface Props { queue: string; // name of the queue. totalTaskCount: number; // totoal number of archived tasks. } function ArchivedTasksTable(props: Props & ReduxProps) { const { pollInterval, listArchivedTasksAsync, queue } = props; const classes = useStyles(); const [page, setPage] = useState(0); const [pageSize, setPageSize] = useState(defaultPageSize); const [selectedKeys, setSelectedKeys] = useState([]); const [activeTaskId, setActiveTaskId] = useState(""); const handleChangePage = ( event: React.MouseEvent | null, newPage: number ) => { setPage(newPage); }; const handleChangeRowsPerPage = ( event: React.ChangeEvent ) => { setPageSize(parseInt(event.target.value, 10)); setPage(0); }; const handleSelectAllClick = (event: React.ChangeEvent) => { if (event.target.checked) { const newSelected = props.tasks.map((t) => t.key); setSelectedKeys(newSelected); } else { setSelectedKeys([]); } }; const handleRunAllClick = () => { props.runAllArchivedTasksAsync(queue); }; const handleDeleteAllClick = () => { props.deleteAllArchivedTasksAsync(queue); }; const handleBatchRunClick = () => { props .batchRunArchivedTasksAsync(queue, selectedKeys) .then(() => setSelectedKeys([])); }; const handleBatchDeleteClick = () => { props .batchDeleteArchivedTasksAsync(queue, selectedKeys) .then(() => setSelectedKeys([])); }; const fetchData = useCallback(() => { const pageOpts = { page: page + 1, size: pageSize }; listArchivedTasksAsync(queue, pageOpts); }, [page, pageSize, queue, listArchivedTasksAsync]); usePolling(fetchData, pollInterval); if (props.error.length > 0) { return ( Error {props.error} ); } if (props.tasks.length === 0) { return ( Info No archived tasks at this time. ); } const columns: TableColumn[] = [ { key: "id", label: "ID", align: "left" }, { key: "type", label: "Type", align: "left" }, { key: "payload", label: "Payload", align: "left" }, { key: "last_failed", label: "Last Failed", align: "left" }, { key: "last_error", label: "Last Error", align: "left" }, { key: "actions", label: "Actions", align: "center" }, ]; const rowCount = props.tasks.length; const numSelected = selectedKeys.length; return (
0} iconButtonActions={[ { tooltip: "Delete", icon: , onClick: handleBatchDeleteClick, disabled: props.batchActionPending, }, { tooltip: "Run", icon: , onClick: handleBatchRunClick, disabled: props.batchActionPending, }, ]} menuItemActions={[ { label: "Delete All", onClick: handleDeleteAllClick, disabled: props.allActionPending, }, { label: "Run All", onClick: handleRunAllClick, disabled: props.allActionPending, }, ]} /> 0 && numSelected < rowCount} checked={rowCount > 0 && numSelected === rowCount} onChange={handleSelectAllClick} inputProps={{ "aria-label": "select all tasks shown in the table", }} /> {columns.map((col) => ( {col.label} ))} {props.tasks.map((task) => ( { if (checked) { setSelectedKeys(selectedKeys.concat(task.key)); } else { setSelectedKeys( selectedKeys.filter((key) => key !== task.key) ); } }} onRunClick={() => { props.runArchivedTaskAsync(queue, task.key); }} onDeleteClick={() => { props.deleteArchivedTaskAsync(queue, task.key); }} allActionPending={props.allActionPending} onActionCellEnter={() => setActiveTaskId(task.id)} onActionCellLeave={() => setActiveTaskId("")} showActions={activeTaskId === task.id} /> ))}
); } const useRowStyles = makeStyles(theme => ({ actionCell: { width: "96px", }, actionButton: { marginLeft: 3, marginRight: 3, } })); interface RowProps { task: ArchivedTaskExtended; isSelected: boolean; onSelectChange: (checked: boolean) => void; onRunClick: () => void; onDeleteClick: () => void; allActionPending: boolean; showActions: boolean; onActionCellEnter: () => void; onActionCellLeave: () => void; } function Row(props: RowProps) { const { task } = props; const classes = useRowStyles(); return ( ) => props.onSelectChange(event.target.checked) } checked={props.isSelected} /> {uuidPrefix(task.id)} {task.type} {JSON.stringify(task.payload)} {timeAgo(task.last_failed_at)} {task.error_message} {props.showActions ? ( ) : ( )} ); } export default connector(ArchivedTasksTable);