Files
spacedrive/interface/app/$libraryId/Layout/Sidebar/JobManager/useGroupJobTimeText.tsx
ameer2468 0a4c1f41e7 Job Manager improvement & refactor (#783)
* d

* wip

* Nit: margin and padding tweak

* UI design tweaks

* Update Job.tsx

* Improve UI

* [WIP] - Refactor job manager

* remove invalidate explorer event on thumb generation. the event above performs atomic updates on the front end when new thumbnails are generated, now just need to make that work

* prettier formatting + removed unused imports

* UI tweaks

* progress bar width adjustment

* tweaks

* fix em

* fix thumbnail generation

* fix progress bar

* fix time

---------

Co-authored-by: Jamie Pine <ijamespine@me.com>
Co-authored-by: Brendan Allan <brendonovich@outlook.com>
Co-authored-by: Oscar Beaumont <oscar@otbeaumont.me>
2023-05-08 22:31:49 +00:00

61 lines
1.6 KiB
TypeScript

import dayjs from 'dayjs';
import duration from 'dayjs/plugin/duration';
import { useEffect, useMemo } from 'react';
import { JobReport } from '@sd/client';
import { useForceUpdate } from '~/util';
dayjs.extend(duration);
export function useTotalElapsedTimeText(jobs: JobReport[] = []) {
const forceUpdate = useForceUpdate();
const elapsedTimeText = useMemo(() => {
let total = 0;
let text: string | null = '';
const groupedJobs = jobs.reduce((acc: Record<string, JobReport[]>, job) => {
const parentId = String(job.parent_id);
if (!acc[parentId]) {
acc[parentId] = [];
}
acc[parentId]?.push(job);
return acc;
}, {});
Object.values(groupedJobs).forEach((group: JobReport[]) => {
let groupTotal = 0;
group.forEach((job) => {
const start = dayjs(job.started_at);
const end = job.completed_at ? dayjs(job.completed_at) : dayjs();
groupTotal += end.diff(start, 'minutes');
});
total += groupTotal;
const lastJob = group[group.length - 1];
if (lastJob?.status === 'Failed' || lastJob?.status === 'Canceled') {
text = 'Job failed or canceled';
} else {
text = lastJob?.completed_at
? `Took ${dayjs.duration(groupTotal, 'minutes').humanize()}`
: null;
}
});
return text;
}, [jobs]);
useEffect(() => {
const allJobsCompleted = jobs.every((job) => job.completed_at);
const isJobsQueued = jobs.some((job) => job.status === 'Queued');
if (!allJobsCompleted || isJobsQueued) {
const interval = setInterval(forceUpdate, 1000);
return () => clearInterval(interval);
}
}, [jobs, forceUpdate]);
return elapsedTimeText === 'Took NaN years' ? null : elapsedTimeText;
}