2022-04-18 10:26:36 +02:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
|
import IconButton from '@mui/material/IconButton';
|
|
|
|
|
import RefreshIcon from '@mui/icons-material/Refresh';
|
|
|
|
|
import CircularProgress from '@mui/material/CircularProgress';
|
|
|
|
|
import { Box } from '@mui/system';
|
|
|
|
|
import Typography from '@mui/material/Typography';
|
|
|
|
|
import client from 'util/client';
|
2023-02-05 16:15:20 +01:00
|
|
|
import makeToast from 'components/util/Toast';
|
2022-04-18 10:26:36 +02:00
|
|
|
|
|
|
|
|
interface IProgressProps {
|
|
|
|
|
progress: number
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Progress({ progress }: IProgressProps) {
|
|
|
|
|
return (
|
|
|
|
|
<Box sx={{ display: 'grid', placeItems: 'center', position: 'relative' }}>
|
|
|
|
|
<CircularProgress variant="determinate" value={progress} />
|
|
|
|
|
<Box sx={{ position: 'absolute' }}>
|
|
|
|
|
<Typography fontSize="0.8rem">
|
|
|
|
|
{`${Math.round(progress)}%`}
|
|
|
|
|
</Typography>
|
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
|
|
|
|
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const baseWebsocketUrl = JSON.parse(window.localStorage.getItem('serverBaseURL')!).replace('http', 'ws');
|
|
|
|
|
|
2023-01-06 17:35:26 +01:00
|
|
|
interface IUpdateCheckerProps {
|
|
|
|
|
handleFinishedUpdate: (time: number) => void
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function UpdateChecker({ handleFinishedUpdate }: IUpdateCheckerProps) {
|
2022-04-18 10:26:36 +02:00
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
|
const [progress, setProgress] = useState(0);
|
|
|
|
|
|
|
|
|
|
const onClick = async () => {
|
|
|
|
|
try {
|
|
|
|
|
setLoading(true);
|
|
|
|
|
setProgress(0);
|
|
|
|
|
await client.post('/api/v1/update/fetch');
|
|
|
|
|
} catch (e) {
|
|
|
|
|
makeToast('Checking for updates failed!', 'error');
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const wsc = new WebSocket(`${baseWebsocketUrl}/api/v1/update`);
|
2023-01-06 17:35:26 +01:00
|
|
|
|
|
|
|
|
// "loading" can't be used since it will be outdated once the state gets changed
|
|
|
|
|
// it could be used by adding it as a dependency of "useEffect" but then the socket would
|
|
|
|
|
// get closed and connected again every time it changes
|
|
|
|
|
let updateStarted = false;
|
|
|
|
|
|
2022-04-18 10:26:36 +02:00
|
|
|
wsc.onmessage = (e) => {
|
2023-01-06 17:35:26 +01:00
|
|
|
const { running, statusMap } = JSON.parse(e.data) as IUpdateStatus;
|
|
|
|
|
const { COMPLETE = [], RUNNING = [], PENDING = [] } = statusMap;
|
2022-04-18 10:26:36 +02:00
|
|
|
|
2023-01-01 17:58:23 +01:00
|
|
|
const currentProgress = 100 * (
|
|
|
|
|
COMPLETE.length / (COMPLETE.length + RUNNING.length + PENDING.length)
|
|
|
|
|
);
|
2022-04-18 10:26:36 +02:00
|
|
|
|
2023-01-06 17:35:26 +01:00
|
|
|
const isUpdateFinished = currentProgress === 100;
|
|
|
|
|
const ignoreFaultyMessage = !updateStarted && !running && isUpdateFinished;
|
|
|
|
|
|
|
|
|
|
// for some reason the server sends 100% completed manga updates when connecting to the
|
|
|
|
|
// socket while no update is running
|
|
|
|
|
if (ignoreFaultyMessage) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateStarted = running;
|
|
|
|
|
setLoading(running);
|
2022-04-18 10:26:36 +02:00
|
|
|
setProgress(Number.isNaN(currentProgress) ? 0 : currentProgress);
|
2023-01-06 17:35:26 +01:00
|
|
|
|
|
|
|
|
if (isUpdateFinished) {
|
|
|
|
|
handleFinishedUpdate(Date.now());
|
|
|
|
|
}
|
2022-04-18 10:26:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return () => wsc.close();
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<IconButton onClick={onClick} disabled={loading}>
|
|
|
|
|
{loading ? <Progress progress={progress} /> : <RefreshIcon />}
|
|
|
|
|
</IconButton>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default UpdateChecker;
|