mirror of
https://github.com/spacedriveapp/spacedrive.git
synced 2026-04-23 16:07:15 -04:00
* Fixing some warnings * Optimizing job workers * Removing the delay from getting job reports * remove some commented out stuff * Trying to optimize job report route * fix thread 'tokio-runtime-worker' panicked at 'attempt to subtract with overflow' * fix progress bar * Now pause works from the UI * Fix * Now the worker set job report to paused * show errors for job pause/resume * bunch 'o ui fixes * order location in the sidebar * fix some text * fix clear all jobs * fix clear jobs & job group ui * show queued jobs * object validator job text * make cancel button work * better executable logo * Now cancel button works instantly * disable resume button * remove disabled props from pause/resume buttons * remove large comments * show paused progress --------- Co-authored-by: James Pine <ijamespine@me.com> Co-authored-by: Jamie Pine <32987599+jamiepine@users.noreply.github.com>
62 lines
1.6 KiB
TypeScript
62 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);
|
|
|
|
// TODO: refactor this, its a mess.
|
|
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 = null;
|
|
} 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;
|
|
}
|