import Checkbox from "@material-ui/core/Checkbox"; import IconButton from "@material-ui/core/IconButton"; import Paper from "@material-ui/core/Paper"; 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 TableContainer from "@material-ui/core/TableContainer"; import TableFooter from "@material-ui/core/TableFooter"; import TableHead from "@material-ui/core/TableHead"; import TablePagination from "@material-ui/core/TablePagination"; import TableRow from "@material-ui/core/TableRow"; import Tooltip from "@material-ui/core/Tooltip"; import CancelIcon from "@material-ui/icons/Cancel"; import FileCopyOutlinedIcon from "@material-ui/icons/FileCopyOutlined"; import MoreHorizIcon from "@material-ui/icons/MoreHoriz"; import Alert from "@material-ui/lab/Alert"; import AlertTitle from "@material-ui/lab/AlertTitle"; import React, { useCallback, useState } from "react"; import { connect, ConnectedProps } from "react-redux"; import { useHistory } from "react-router-dom"; import { taskRowsPerPageChange } from "../actions/settingsActions"; import { batchCancelActiveTasksAsync, cancelActiveTaskAsync, cancelAllActiveTasksAsync, listActiveTasksAsync, } from "../actions/tasksActions"; import { usePolling } from "../hooks"; import { taskDetailsPath } from "../paths"; import { ActiveTaskExtended } from "../reducers/tasksReducer"; import { AppState } from "../store"; import { TableColumn } from "../types/table"; import { durationBefore, prettifyPayload, timeAgo, uuidPrefix } from "../utils"; import SyntaxHighlighter from "./SyntaxHighlighter"; import TableActions from "./TableActions"; import TablePaginationActions, { rowsPerPageOptions, } from "./TablePaginationActions"; 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.activeTasks.loading, error: state.tasks.activeTasks.error, tasks: state.tasks.activeTasks.data, batchActionPending: state.tasks.activeTasks.batchActionPending, allActionPending: state.tasks.activeTasks.allActionPending, pollInterval: state.settings.pollInterval, pageSize: state.settings.taskRowsPerPage, }; } const mapDispatchToProps = { listActiveTasksAsync, cancelActiveTaskAsync, batchCancelActiveTasksAsync, cancelAllActiveTasksAsync, taskRowsPerPageChange, }; const columns: TableColumn[] = [ { key: "id", label: "ID", align: "left" }, { key: "type", label: "Type", align: "left" }, { key: "payload", label: "Payload", align: "left" }, { key: "status", label: "Status", align: "left" }, { key: "start-time", label: "Started", align: "left" }, { key: "deadline", label: "Deadline", align: "left" }, { key: "actions", label: "Actions", align: "center" }, ]; const connector = connect(mapStateToProps, mapDispatchToProps); type ReduxProps = ConnectedProps; interface Props { queue: string; // name of the queue } function ActiveTasksTable(props: Props & ReduxProps) { const { pollInterval, listActiveTasksAsync, queue, pageSize } = props; const classes = useStyles(); const [page, setPage] = useState(0); const [selectedIds, setSelectedIds] = useState([]); const [activeTaskId, setActiveTaskId] = useState(""); const handlePageChange = ( event: React.MouseEvent | null, newPage: number ) => { setPage(newPage); }; const handleRowsPerPageChange = ( event: React.ChangeEvent ) => { props.taskRowsPerPageChange(parseInt(event.target.value, 10)); setPage(0); }; const handleSelectAllClick = (event: React.ChangeEvent) => { if (event.target.checked) { const newSelected = props.tasks.map((t) => t.id); setSelectedIds(newSelected); } else { setSelectedIds([]); } }; const handleCancelAllClick = () => { props.cancelAllActiveTasksAsync(queue); }; const handleBatchCancelClick = () => { props .batchCancelActiveTasksAsync(queue, selectedIds) .then(() => setSelectedIds([])); }; const fetchData = useCallback(() => { const pageOpts = { page: page + 1, size: pageSize }; listActiveTasksAsync(queue, pageOpts); }, [page, pageSize, queue, listActiveTasksAsync]); usePolling(fetchData, pollInterval); if (props.error.length > 0) { return ( Error {props.error} ); } if (props.tasks.length === 0) { return ( Info No active tasks at this time. ); } const rowCount = props.tasks.length; const numSelected = selectedIds.length; return (
{!window.READ_ONLY && ( 0} iconButtonActions={[ { tooltip: "Cancel", icon: , onClick: handleBatchCancelClick, disabled: props.batchActionPending, }, ]} menuItemActions={[ { label: "Cancel All", onClick: handleCancelAllClick, disabled: props.allActionPending, }, ]} /> )} {!window.READ_ONLY && ( 0 && numSelected < rowCount} checked={rowCount > 0 && numSelected === rowCount} onChange={handleSelectAllClick} inputProps={{ "aria-label": "select all tasks shown in the table", }} /> )} {columns .filter((col) => { // Filter out actions column in readonly mode. return !window.READ_ONLY || col.key !== "actions"; }) .map((col) => ( {col.label} ))} {/* TODO: loading and empty state */} {props.tasks.map((task) => ( { if (checked) { setSelectedIds(selectedIds.concat(task.id)); } else { setSelectedIds(selectedIds.filter((id) => id !== task.id)); } }} onCancelClick={() => { props.cancelActiveTaskAsync(queue, task.id); }} onActionCellEnter={() => setActiveTaskId(task.id)} onActionCellLeave={() => setActiveTaskId("")} showActions={activeTaskId === task.id} /> ))}
); } const useRowStyles = makeStyles((theme) => ({ root: { cursor: "pointer", "& #copy-button": { display: "none", }, "&:hover": { boxShadow: theme.shadows[2], }, "&:hover $copyButton": { display: "inline-block", }, "&:hover .MuiTableCell-root": { borderBottomColor: theme.palette.background.paper, }, }, idCell: { width: "200px", }, copyButton: { display: "none", }, IdGroup: { display: "flex", alignItems: "center", }, })); interface RowProps { task: ActiveTaskExtended; isSelected: boolean; onSelectChange: (checked: boolean) => void; onCancelClick: () => void; showActions: boolean; onActionCellEnter: () => void; onActionCellLeave: () => void; } function Row(props: RowProps) { const { task } = props; const classes = useRowStyles(); const history = useHistory(); return ( history.push(taskDetailsPath(task.queue, task.id))} > {!window.READ_ONLY && ( e.stopPropagation()}> ) => props.onSelectChange(event.target.checked) } checked={props.isSelected} /> )}
{uuidPrefix(task.id)} { e.stopPropagation(); navigator.clipboard.writeText(task.id); }} size="small" className={classes.copyButton} >
{task.type} {prettifyPayload(task.payload)} {task.canceling ? "Canceling" : task.is_orphaned ? "Orphaned" : "Running"} {task.is_orphaned ? "-" : task.start_time === "-" ? "just now" : timeAgo(task.start_time)} {task.deadline === "-" ? "-" : durationBefore(task.deadline)} {!window.READ_ONLY && ( e.stopPropagation()} > {props.showActions ? ( ) : ( )} )}
); } export default connector(ActiveTasksTable);