import moment from 'moment'; import React, { useCallback, useEffect, useMemo, useState } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import SpinnerIconButton from 'Components/Link/SpinnerIconButton'; import TableRowCell from 'Components/Table/Cells/TableRowCell'; import TableRow from 'Components/Table/TableRow'; import { icons } from 'Helpers/Props'; import { executeCommand } from 'Store/Actions/commandActions'; import createCommandSelector from 'Store/Selectors/createCommandSelector'; import createUISettingsSelector from 'Store/Selectors/createUISettingsSelector'; import { isCommandExecuting } from 'Utilities/Command'; import formatDate from 'Utilities/Date/formatDate'; import formatDateTime from 'Utilities/Date/formatDateTime'; import formatTimeSpan from 'Utilities/Date/formatTimeSpan'; import styles from './ScheduledTaskRow.css'; interface ScheduledTaskRowProps { id: number; taskName: string; name: string; interval: number; lastExecution: string; lastStartTime: string; lastDuration: string; nextExecution: string; } function ScheduledTaskRow({ taskName, name, interval, lastExecution, lastStartTime, lastDuration, nextExecution, }: ScheduledTaskRowProps) { const dispatch = useDispatch(); const { showRelativeDates, longDateFormat, shortDateFormat, timeFormat } = useSelector(createUISettingsSelector()); const command = useSelector(createCommandSelector(taskName)); const [time, setTime] = useState(Date.now()); const isQueued = !!(command && command.status === 'queued'); const isExecuting = isCommandExecuting(command); const isDisabled = interval === 0; const executeNow = !isDisabled && moment().isAfter(nextExecution); const hasNextExecutionTime = !isDisabled && !executeNow; const hasLastStartTime = moment(lastStartTime).isAfter('2010-01-01'); const duration = useMemo(() => { return moment .duration(interval, 'minutes') .humanize() .replace(/an?(?=\s)/, '1'); }, [interval]); const { lastExecutionTime, nextExecutionTime } = useMemo(() => { const isDisabled = interval === 0; if (showRelativeDates && time) { return { lastExecutionTime: moment(lastExecution).fromNow(), nextExecutionTime: isDisabled ? '-' : moment(nextExecution).fromNow(), }; } return { lastExecutionTime: formatDate(lastExecution, shortDateFormat), nextExecutionTime: isDisabled ? '-' : formatDate(nextExecution, shortDateFormat), }; }, [ time, interval, lastExecution, nextExecution, showRelativeDates, shortDateFormat, ]); const handleExecutePress = useCallback(() => { dispatch(executeCommand({ name: taskName })); }, [taskName, dispatch]); useEffect(() => { const interval = setInterval(() => setTime(Date.now()), 1000); return () => { clearInterval(interval); }; }, [setTime]); return ( {name} {isDisabled ? 'disabled' : duration} {lastExecutionTime} {hasLastStartTime ? ( {formatTimeSpan(lastDuration)} ) : ( - )} {isDisabled ? ( - ) : null} {executeNow && isQueued ? ( queued ) : null} {executeNow && !isQueued ? ( now ) : null} {hasNextExecutionTime ? ( {nextExecutionTime} ) : null} ); } export default ScheduledTaskRow;