import React, { useCallback, useState } from "react"; import clsx from "clsx"; 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 Alert from "@material-ui/lab/Alert"; import AlertTitle from "@material-ui/lab/AlertTitle"; 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 Box from "@material-ui/core/Box"; import Checkbox from "@material-ui/core/Checkbox"; import Collapse from "@material-ui/core/Collapse"; import IconButton from "@material-ui/core/IconButton"; import KeyboardArrowUpIcon from "@material-ui/icons/KeyboardArrowUp"; import KeyboardArrowDownIcon from "@material-ui/icons/KeyboardArrowDown"; import Typography from "@material-ui/core/Typography"; import TableFooter from "@material-ui/core/TableFooter"; import TablePagination from "@material-ui/core/TablePagination"; import DeleteIcon from "@material-ui/icons/Delete"; import ArchiveIcon from "@material-ui/icons/Archive"; import MoreHorizIcon from "@material-ui/icons/MoreHoriz"; import SyntaxHighlighter from "./SyntaxHighlighter"; import TablePaginationActions, { defaultPageSize, rowsPerPageOptions, } from "./TablePaginationActions"; import TableActions from "./TableActions"; import { listPendingTasksAsync, deletePendingTaskAsync, batchDeletePendingTasksAsync, deleteAllPendingTasksAsync, archivePendingTaskAsync, batchArchivePendingTasksAsync, archiveAllPendingTasksAsync, } from "../actions/tasksActions"; import { AppState } from "../store"; import { usePolling } from "../hooks"; import { uuidPrefix } from "../utils"; import { TableColumn } from "../types/table"; import { PendingTaskExtended } from "../reducers/tasksReducer"; const useStyles = makeStyles((theme) => ({ table: { minWidth: 650, }, stickyHeaderCell: { background: theme.palette.background.paper, }, })); function mapStateToProps(state: AppState) { return { loading: state.tasks.pendingTasks.loading, error: state.tasks.pendingTasks.error, tasks: state.tasks.pendingTasks.data, batchActionPending: state.tasks.pendingTasks.batchActionPending, allActionPending: state.tasks.pendingTasks.allActionPending, pollInterval: state.settings.pollInterval, }; } const mapDispatchToProps = { listPendingTasksAsync, deletePendingTaskAsync, batchDeletePendingTasksAsync, deleteAllPendingTasksAsync, archivePendingTaskAsync, batchArchivePendingTasksAsync, archiveAllPendingTasksAsync, }; const connector = connect(mapStateToProps, mapDispatchToProps); type ReduxProps = ConnectedProps; interface Props { queue: string; totalTaskCount: number; // total number of pending tasks } function PendingTasksTable(props: Props & ReduxProps) { const { pollInterval, listPendingTasksAsync, 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 handleDeleteAllClick = () => { props.deleteAllPendingTasksAsync(queue); }; const handleArchiveAllClick = () => { props.archiveAllPendingTasksAsync(queue); }; const handleBatchDeleteClick = () => { props .batchDeletePendingTasksAsync(queue, selectedKeys) .then(() => setSelectedKeys([])); }; const handleBatchArchiveClick = () => { props .batchArchivePendingTasksAsync(queue, selectedKeys) .then(() => setSelectedKeys([])); }; const fetchData = useCallback(() => { const pageOpts = { page: page + 1, size: pageSize }; listPendingTasksAsync(queue, pageOpts); }, [page, pageSize, queue, listPendingTasksAsync]); usePolling(fetchData, pollInterval); if (props.error.length > 0) { return ( Error {props.error} ); } if (props.tasks.length === 0) { return ( Info No pending tasks at this time. ); } const columns: TableColumn[] = [ { key: "icon", label: "", align: "left" }, { key: "id", label: "ID", align: "left" }, { key: "type", label: "Type", 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: "Archive", icon: , onClick: handleBatchArchiveClick, disabled: props.batchActionPending, }, ]} menuItemActions={[ { label: "Delete All", onClick: handleDeleteAllClick, disabled: props.allActionPending, }, { label: "Archive All", onClick: handleArchiveAllClick, 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) ); } }} allActionPending={props.allActionPending} onDeleteClick={() => props.deletePendingTaskAsync(queue, task.key) } onArchiveClick={() => { props.archivePendingTaskAsync(queue, task.key); }} onActionCellEnter={() => setActiveTaskId(task.id)} onActionCellLeave={() => setActiveTaskId("")} showActions={activeTaskId === task.id} /> ))}
); } const useRowStyles = makeStyles({ root: { "& > *": { borderBottom: "unset", }, }, actionCell: { width: "96px", }, activeActionCell: { display: "flex", justifyContent: "space-between", }, }); interface RowProps { task: PendingTaskExtended; isSelected: boolean; onSelectChange: (checked: boolean) => void; onDeleteClick: () => void; onArchiveClick: () => void; allActionPending: boolean; showActions: boolean; onActionCellEnter: () => void; onActionCellLeave: () => void; } function Row(props: RowProps) { const { task } = props; const [open, setOpen] = React.useState(false); const classes = useRowStyles(); return ( ) => props.onSelectChange(event.target.checked) } checked={props.isSelected} /> setOpen(!open)} > {open ? : } {uuidPrefix(task.id)} {task.type} {props.showActions ? ( ) : ( )} Payload {JSON.stringify(task.payload, null, 2)} ); } export default connector(PendingTasksTable);