import React, { useState } from 'react'; import { formatDistanceStrict } from 'date-fns'; import Highlight from 'react-highlight'; import { Field, FIELDS, Status } from './constants'; import { PlusIcon } from './PlusIcon'; import { PlayIcon } from './PlayIcon'; import { CheckIcon } from './CheckIcon'; import { Timestamp } from './Timestamp'; import { AppJob } from 'types'; type FieldProps = { job: AppJob; retryJob: () => Promise; delayedJob: () => Promise; }; const fieldComponents: Record> = { id: ({ job }) => #{job.id}, timestamps: ({ job }) => (
{job.processedOn && (
)} {job.finishedOn && (
)}
), name: ({ job }) => <>{job.name === '__default__' ? '--' : job.name}, progress: ({ job }) => { switch (typeof job.progress) { case 'object': return ( {JSON.stringify(job.progress, null, 2)} ); case 'number': if (job.progress > 100) { return
{job.progress}
; } return (
{job.progress} % 
); default: return <>--; } }, attempts: ({ job }) => <>{job.attempts}, delay: ({ job }) => ( <> {formatDistanceStrict( Number(job.timestamp || 0) + Number(job.delay || 0), Date.now() )} ), failedReason: ({ job }) => { return ( <> {job.failedReason || 'NA'} {job.stacktrace} ); }, data: ({ job }) => { // eslint-disable-next-line const [showData, toggleData] = useState(false); return ( <> {showData && JSON.stringify(job.data, null, 2)} ); }, opts: ({ job }) => ( {JSON.stringify(job.opts, null, 2)} ), retry: ({ retryJob }) => , promote: ({ delayedJob }) => , }; export const Job = ({ job, status, queueName, retryJob, promoteJob, }: { job: AppJob; status: Status; queueName: string; retryJob: (job: AppJob) => () => Promise; promoteJob: (job: AppJob) => () => Promise; }) => { return ( {FIELDS[status].map(field => { const Field = fieldComponents[field]; return ( ); })} ); };