2023-05-18 13:17:41 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) Contributors to the Suwayomi project
|
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-11-25 15:35:36 +01:00
|
|
|
import { useEffect, useMemo } from 'react';
|
2022-04-18 10:26:36 +02:00
|
|
|
import IconButton from '@mui/material/IconButton';
|
|
|
|
|
import RefreshIcon from '@mui/icons-material/Refresh';
|
2023-03-23 13:59:32 +01:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2024-04-14 15:50:12 +02:00
|
|
|
import Tooltip from '@mui/material/Tooltip';
|
2023-10-28 00:32:02 +02:00
|
|
|
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
|
|
|
|
import { makeToast } from '@/components/util/Toast';
|
2023-10-04 22:34:32 +02:00
|
|
|
import { UpdaterSubscription } from '@/lib/graphql/generated/graphql.ts';
|
2023-10-29 01:49:27 +02:00
|
|
|
import { Progress } from '@/components/util/Progress';
|
2023-12-31 01:42:54 +01:00
|
|
|
import { defaultPromiseErrorHandler } from '@/util/defaultPromiseErrorHandler.ts';
|
2022-04-18 10:26:36 +02:00
|
|
|
|
2023-10-04 22:34:32 +02:00
|
|
|
const calcProgress = (status: UpdaterSubscription['updateStatusChanged'] | undefined) => {
|
|
|
|
|
if (!status) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const finishedUpdates = status.failedJobs.mangas.totalCount + status.completeJobs.mangas.totalCount;
|
|
|
|
|
const totalMangas = finishedUpdates + status.pendingJobs.mangas.totalCount + status.runningJobs.mangas.totalCount;
|
|
|
|
|
|
|
|
|
|
const progress = 100 * (finishedUpdates / totalMangas);
|
|
|
|
|
|
|
|
|
|
return Number.isNaN(progress) ? 0 : progress;
|
|
|
|
|
};
|
2023-01-06 17:35:26 +01:00
|
|
|
|
2023-11-25 15:35:36 +01:00
|
|
|
let lastRunningState = false;
|
|
|
|
|
|
|
|
|
|
export function UpdateChecker({ handleFinishedUpdate }: { handleFinishedUpdate?: () => void }) {
|
2023-03-23 13:59:32 +01:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
2023-11-25 15:35:36 +01:00
|
|
|
const { data: lastUpdateTimestampData, refetch: reFetchLastTimestamp } =
|
2023-11-19 22:43:29 +01:00
|
|
|
requestManager.useGetLastGlobalUpdateTimestamp();
|
|
|
|
|
const lastUpdateTimestamp = lastUpdateTimestampData?.lastUpdateTimestamp.timestamp;
|
2024-01-01 22:40:39 +01:00
|
|
|
const { data: updaterData } = requestManager.useGetGlobalUpdateSummary();
|
|
|
|
|
const status = updaterData?.updateStatus;
|
2023-10-04 22:34:32 +02:00
|
|
|
|
|
|
|
|
const loading = !!status?.isRunning;
|
|
|
|
|
const progress = useMemo(
|
|
|
|
|
() => calcProgress(status),
|
|
|
|
|
[
|
|
|
|
|
status?.failedJobs.mangas.totalCount,
|
|
|
|
|
status?.completeJobs.mangas.totalCount,
|
|
|
|
|
status?.pendingJobs.mangas.totalCount,
|
|
|
|
|
status?.runningJobs.mangas.totalCount,
|
|
|
|
|
],
|
|
|
|
|
);
|
2022-04-18 10:26:36 +02:00
|
|
|
|
2023-11-25 15:35:36 +01:00
|
|
|
useEffect(() => {
|
2023-11-25 18:20:49 +01:00
|
|
|
if (!lastRunningState && status?.isRunning) {
|
|
|
|
|
lastRunningState = true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-25 15:35:36 +01:00
|
|
|
const isUpdateFinished = lastRunningState && progress === 100;
|
|
|
|
|
if (!isUpdateFinished) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lastRunningState = false;
|
|
|
|
|
handleFinishedUpdate?.();
|
|
|
|
|
// this re-fetch is necessary since a running update could have been triggered by the server or another client
|
2023-12-31 01:42:54 +01:00
|
|
|
reFetchLastTimestamp().catch(defaultPromiseErrorHandler('UpdateChecker::reFetchLastTimestamp'));
|
2023-11-25 15:35:36 +01:00
|
|
|
}, [status?.isRunning]);
|
2023-10-27 20:45:03 +02:00
|
|
|
|
2022-04-18 10:26:36 +02:00
|
|
|
const onClick = async () => {
|
|
|
|
|
try {
|
2023-11-25 15:35:36 +01:00
|
|
|
lastRunningState = true;
|
2023-05-23 13:54:14 +02:00
|
|
|
await requestManager.startGlobalUpdate().response;
|
2023-12-31 01:42:54 +01:00
|
|
|
reFetchLastTimestamp().catch(defaultPromiseErrorHandler('UpdateChecker::reFetchLastTimestamp'));
|
2022-04-18 10:26:36 +02:00
|
|
|
} catch (e) {
|
2023-11-25 15:35:36 +01:00
|
|
|
lastRunningState = false;
|
2023-03-23 13:59:32 +01:00
|
|
|
makeToast(t('global.error.label.update_failed'), 'error');
|
2022-04-18 10:26:36 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2023-11-19 22:43:29 +01:00
|
|
|
<Tooltip
|
|
|
|
|
title={t('library.settings.global_update.label.last_update_tooltip', {
|
|
|
|
|
date: lastUpdateTimestamp ? new Date(+lastUpdateTimestamp).toLocaleString() : '-',
|
|
|
|
|
})}
|
|
|
|
|
>
|
2023-11-05 17:22:29 +01:00
|
|
|
<IconButton onClick={onClick} disabled={loading}>
|
|
|
|
|
{loading ? <Progress progress={progress} /> : <RefreshIcon />}
|
|
|
|
|
</IconButton>
|
|
|
|
|
</Tooltip>
|
2022-04-18 10:26:36 +02:00
|
|
|
);
|
|
|
|
|
}
|