Tranga-Website/Website/modules/MonitorJobsList.tsx

94 lines
4.3 KiB
TypeScript
Raw Normal View History

import React, {EventHandler, MouseEventHandler, ReactElement, useEffect, useState} from 'react';
2024-10-18 19:46:17 +02:00
import {Job} from './Job';
import '../styles/monitorMangaList.css';
import IJob from "./interfaces/IJob";
import IManga, {CoverCard} from "./interfaces/IManga";
2024-10-18 19:46:17 +02:00
import {Manga} from './Manga';
import '../styles/MangaCoverCard.css'
import Icon from '@mdi/react';
import { mdiTrashCanOutline, mdiPlayBoxOutline } from '@mdi/js';
2024-10-18 19:46:17 +02:00
2024-10-20 20:12:27 +02:00
export default function MonitorJobsList({onStartSearch, onJobsChanged, connectedToBackend, apiUri} : {onStartSearch() : void, onJobsChanged: EventHandler<any>, connectedToBackend: boolean, apiUri: string}) {
const [MonitoringJobs, setMonitoringJobs] = useState<IJob[]>([]);
const [AllManga, setAllManga] = useState<IManga[]>([]);
const [joblistUpdateInterval, setJoblistUpdateInterval] = React.useState<number>();
2024-10-18 19:46:17 +02:00
useEffect(() => {
2024-10-20 20:16:32 +02:00
//console.debug("Updating display list.");
2024-10-18 19:46:17 +02:00
//Remove all Manga that are not associated with a Job
setAllManga(AllManga.filter(manga => MonitoringJobs.find(job => job.mangaInternalId == manga.internalId)));
//Fetch Manga that are missing (from Jobs)
if(MonitoringJobs === null)
return;
MonitoringJobs.forEach(job => {
if(AllManga.find(manga => manga.internalId == job.mangaInternalId))
return;
2024-10-20 20:12:27 +02:00
Manga.GetMangaById(apiUri, job.mangaInternalId != undefined ? job.mangaInternalId : job.chapter != undefined ? job.chapter.parentManga.internalId : "").
then((manga: IManga) => setAllManga([...AllManga, manga]));
2024-10-18 19:46:17 +02:00
});
}, [MonitoringJobs]);
useEffect(() => {
if(connectedToBackend){
UpdateMonitoringJobsList();
setJoblistUpdateInterval(setInterval(() => {
UpdateMonitoringJobsList();
}, 1000));
}else{
clearInterval(joblistUpdateInterval);
setJoblistUpdateInterval(undefined);
}
}, [connectedToBackend]);
2024-10-18 19:46:17 +02:00
function UpdateMonitoringJobsList(){
2024-10-20 20:16:32 +02:00
//console.debug("Updating MonitoringJobsList");
2024-10-20 20:12:27 +02:00
Job.GetMonitoringJobs(apiUri)
.then((jobs) => {
if(jobs.length > 0)
2024-10-20 20:12:27 +02:00
return Job.GetJobs(apiUri, jobs)
return [];
})
.then((jobs) => setMonitoringJobs(jobs));
2024-10-18 19:46:17 +02:00
}
2024-10-19 16:28:49 +02:00
function StartSearchMangaEntry() : ReactElement {
return (<div key="monitorMangaEntry.StartSearch" className="monitorMangaEntry" onClick={onStartSearch}>
<div className="Manga" key="StartSearch.Manga">
<img src="../media/blahaj.png" alt="Blahaj"></img>
2024-10-19 16:28:49 +02:00
<div>
<p style={{textAlign: "center", width: "100%"}} className="Manga-name">Add new Manga</p>
<p style={{fontSize: "42pt", textAlign: "center"}}>+</p>
</div>
</div>
</div>);
}
const DeleteJob : MouseEventHandler = (e) => {
const jobId = e.currentTarget.id.slice(e.currentTarget.id.indexOf("-")+1);
2024-10-20 20:16:32 +02:00
//console.info(`Pressed ${e.currentTarget.id} => ${jobId}`);
2024-10-20 20:12:27 +02:00
Job.DeleteJob(apiUri, jobId).then(() => onJobsChanged(jobId));
}
const StartJob : MouseEventHandler = (e) => {
const jobId = e.currentTarget.id.slice(e.currentTarget.id.indexOf("-")+1);
2024-10-20 20:16:32 +02:00
//console.info(`Pressed ${e.currentTarget.id} => ${jobId}`);
2024-10-20 20:12:27 +02:00
Job.StartJob(apiUri, jobId);
}
2024-10-18 19:46:17 +02:00
return (
<div id="MonitorMangaList">
2024-10-19 16:28:49 +02:00
{StartSearchMangaEntry()}
2024-10-18 19:46:17 +02:00
{AllManga.map((manga: IManga) => {
const job = MonitoringJobs.find(job => job.mangaInternalId == manga.internalId);
2024-10-19 16:28:49 +02:00
if (job === undefined || job == null)
2024-10-18 19:46:17 +02:00
return <div>Error. Could not find matching job for {manga.internalId}</div>
return <div key={"monitorMangaEntry-" + manga.internalId} className="monitorMangaEntry">
{CoverCard(manga)}
<div className="MangaActionButtons">
<div id={"Delete-"+job.id} className="DeleteJobButton" onClick={DeleteJob}><Icon path={mdiTrashCanOutline} size={1.5} /></div>
<div id={"Start-"+job.id} className="StartJobNowButton" onClick={StartJob}><Icon path={mdiPlayBoxOutline} size={1.5} /></div>
</div>
2024-10-18 19:46:17 +02:00
</div>;
})}
</div>)
}