Tranga-Website/Website/modules/QueuePopUp.tsx

120 lines
5.4 KiB
TypeScript
Raw Normal View History

import React, {useEffect, useState} from 'react';
import IJob from "./interfaces/IJob";
import '../styles/queuePopUp.css';
import '../styles/popup.css';
import Job from "./Job";
import IManga, {QueueItem} from "./interfaces/IManga";
import Manga from "./Manga";
export default function QueuePopUp({connectedToBackend, children, apiUri} : {connectedToBackend: boolean, children: JSX.Element[], apiUri: string}) {
const [StandbyJobs, setStandbyJobs] = React.useState<IJob[]>([]);
const [StandbyJobsManga, setStandbyJobsManga] = React.useState<IManga[]>([]);
const [RunningJobs, setRunningJobs] = React.useState<IJob[]>([]);
const [RunningJobsManga, setRunningJobsManga] = React.useState<IManga[]>([]);
const [showQueuePopup, setShowQueuePopup] = useState<boolean>(false);
const [queueListInterval, setQueueListInterval] = React.useState<number>();
useEffect(() => {
if(!showQueuePopup)
return;
UpdateMonitoringJobsList();
}, [showQueuePopup]);
useEffect(() => {
if(connectedToBackend){
UpdateMonitoringJobsList();
setQueueListInterval(setInterval(() => {
UpdateMonitoringJobsList();
}, 2000));
}else{
clearInterval(queueListInterval);
setQueueListInterval(undefined);
}
}, [connectedToBackend]);
function UpdateMonitoringJobsList(){
2024-10-20 20:12:27 +02:00
Job.GetStandbyJobs(apiUri)
.then((jobs:string[]) => {
2024-10-20 02:28:26 +02:00
if(jobs.length > 0)
2024-10-20 20:12:27 +02:00
return Job.GetJobs(apiUri, jobs);
2024-10-20 02:28:26 +02:00
return [];
})
.then((jobs:IJob[]) => {
2024-10-20 20:16:32 +02:00
//console.debug("Removing Metadata Jobs");
//console.log(StandbyJobs)
2024-10-20 02:40:17 +02:00
setStandbyJobs(jobs.filter(job => job.jobType <= 1));
2024-10-20 20:16:32 +02:00
//console.log(StandbyJobs)
2024-10-20 02:28:26 +02:00
});
2024-10-20 20:12:27 +02:00
Job.GetRunningJobs(apiUri)
.then((jobs:string[]) => {
2024-10-20 02:28:26 +02:00
if(jobs.length > 0)
2024-10-20 20:12:27 +02:00
return Job.GetJobs(apiUri, jobs);
2024-10-20 02:28:26 +02:00
return [];
})
.then((jobs:IJob[]) =>{
2024-10-20 20:16:32 +02:00
//console.debug("Removing Metadata Jobs");
2024-10-20 02:40:17 +02:00
setRunningJobs(jobs.filter(job => job.jobType <= 1));
2024-10-20 02:28:26 +02:00
});
}
useEffect(() => {
if(StandbyJobs.length < 1)
return;
2024-10-20 02:28:26 +02:00
const mangaIds = StandbyJobs.filter(job => job.jobType<=2).map((job) => job.mangaInternalId != undefined ? job.mangaInternalId : job.chapter != undefined ? job.chapter.parentManga.internalId : "");
2024-10-20 20:16:32 +02:00
//console.debug(`Waiting mangaIds: ${mangaIds.join(",")}`);
2024-10-20 20:12:27 +02:00
Manga.GetMangaByIds(apiUri, mangaIds)
.then(setStandbyJobsManga);
}, [StandbyJobs]);
useEffect(() => {
if(RunningJobs.length < 1)
return;
2024-10-20 20:16:32 +02:00
//console.log(RunningJobs);
2024-10-20 02:28:26 +02:00
const mangaIds = RunningJobs.filter(job => job.jobType<=2).map((job) => job.mangaInternalId != undefined ? job.mangaInternalId : job.chapter != undefined ? job.chapter.parentManga.internalId : "");
2024-10-20 20:16:32 +02:00
//console.debug(`Running mangaIds: ${mangaIds.join(",")}`);
2024-10-20 20:12:27 +02:00
Manga.GetMangaByIds(apiUri, mangaIds)
.then(setRunningJobsManga);
}, [RunningJobs]);
return (<>
<div onClick={() => setShowQueuePopup(true)}>
{children}
</div>
{showQueuePopup
? <div className="popup" id="QueuePopUp">
<div className="popupHeader">
<h1>Queue Status</h1>
<img alt="Close Search" className="close" src="../media/close-x.svg"
onClick={() => setShowQueuePopup(false)}/>
</div>
<div id="QueuePopUpBody" className="popupBody">
<div id="RunningJobQueue">
<h1>Running</h1>
<div className="JobQueue">
{RunningJobs.map((job: IJob) => {
const manga = RunningJobsManga.find(manga => manga.internalId == job.mangaInternalId || manga.internalId == job.chapter?.parentManga.internalId);
if (manga === undefined || manga === null)
return <div key={"QueueJob-" + job.id}>Error. Could not find matching manga for {job.id}</div>
return QueueItem(apiUri, manga, job, UpdateMonitoringJobsList);
})}
</div>
</div>
<div id="WaitingJobQueue">
<h1>Standby</h1>
<div className="JobQueue">
{StandbyJobs.map((job: IJob) => {
const manga = StandbyJobsManga.find(manga => manga.internalId == job.mangaInternalId || manga.internalId == job.chapter?.parentManga.internalId);
if (manga === undefined || manga === null)
return <div key={"QueueJob-" + job.id}>Error. Could not find matching manga for {job.id}</div>
return QueueItem(apiUri, manga, job, UpdateMonitoringJobsList);
})}
</div>
</div>
</div>
</div>
: <></>
}
</>
);
}