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/.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-03-16 21:01:24 +01:00
|
|
|
import { useEffect, useState } 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-06-26 12:42:03 +02:00
|
|
|
import PopupState, { bindMenu, bindTrigger } from 'material-ui-popup-state';
|
|
|
|
|
import Menu from '@mui/material/Menu';
|
|
|
|
|
import MenuItem from '@mui/material/MenuItem';
|
2024-06-26 13:19:02 +02:00
|
|
|
import ClearIcon from '@mui/icons-material/Clear';
|
|
|
|
|
import Stack from '@mui/material/Stack';
|
2025-02-01 14:24:38 +01:00
|
|
|
import { CustomTooltip } from '@/modules/core/components/CustomTooltip.tsx';
|
2024-10-06 01:27:51 +02:00
|
|
|
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
2024-10-05 23:22:42 +02:00
|
|
|
import { makeToast } from '@/modules/core/utils/Toast.ts';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { Progress } from '@/modules/core/components/Progress.tsx';
|
|
|
|
|
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
|
|
|
|
import { dateTimeFormatter } from '@/util/DateHelper.ts';
|
2024-10-05 23:22:42 +02:00
|
|
|
import { MediaQuery } from '@/modules/core/utils/MediaQuery.tsx';
|
2024-10-05 21:39:58 +02:00
|
|
|
|
|
|
|
|
import { CategoryIdInfo } from '@/modules/category/Category.types.ts';
|
2024-12-20 17:24:40 +01:00
|
|
|
import { getErrorMessage } from '@/lib/HelperFunctions.ts';
|
2022-04-18 10:26:36 +02:00
|
|
|
|
2023-11-25 15:35:36 +01:00
|
|
|
let lastRunningState = false;
|
|
|
|
|
|
2024-06-26 12:42:03 +02:00
|
|
|
export function UpdateChecker({
|
|
|
|
|
categoryId,
|
|
|
|
|
handleFinishedUpdate,
|
|
|
|
|
}: {
|
2024-07-01 21:40:27 +02:00
|
|
|
categoryId?: CategoryIdInfo['id'];
|
2024-06-26 12:42:03 +02:00
|
|
|
handleFinishedUpdate?: () => void;
|
|
|
|
|
}) {
|
2023-03-23 13:59:32 +01:00
|
|
|
const { t } = useTranslation();
|
2024-06-26 13:19:02 +02:00
|
|
|
const isTouchDevice = MediaQuery.useIsTouchDevice();
|
|
|
|
|
|
|
|
|
|
const [isHovered, setIsHovered] = useState(false);
|
2023-03-23 13:59:32 +01:00
|
|
|
|
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();
|
2025-03-16 21:01:24 +01:00
|
|
|
const status = updaterData?.libraryUpdateStatus;
|
2023-10-04 22:34:32 +02:00
|
|
|
|
2025-03-16 21:01:24 +01:00
|
|
|
const isRunning = !!status?.jobsInfo.isRunning;
|
|
|
|
|
const progress = status ? (status.jobsInfo.finishedJobs / status.jobsInfo.totalJobs) * 100 : 0;
|
2022-04-18 10:26:36 +02:00
|
|
|
|
2023-11-25 15:35:36 +01:00
|
|
|
useEffect(() => {
|
2025-03-16 21:01:24 +01:00
|
|
|
if (!lastRunningState && isRunning) {
|
2023-11-25 18:20:49 +01:00
|
|
|
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'));
|
2025-03-16 21:01:24 +01:00
|
|
|
}, [isRunning]);
|
2023-10-27 20:45:03 +02:00
|
|
|
|
2024-07-01 21:40:27 +02:00
|
|
|
const startUpdate = async (category?: CategoryIdInfo['id']) => {
|
2022-04-18 10:26:36 +02:00
|
|
|
try {
|
2023-11-25 15:35:36 +01:00
|
|
|
lastRunningState = true;
|
2024-06-26 12:42:03 +02:00
|
|
|
await requestManager.startGlobalUpdate(category !== undefined ? [category] : undefined).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;
|
2024-12-20 17:24:40 +01:00
|
|
|
makeToast(t('global.error.label.update_failed'), 'error', getErrorMessage(e));
|
2022-04-18 10:26:36 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-06-26 13:19:02 +02:00
|
|
|
const stopUpdate = async () => {
|
|
|
|
|
try {
|
|
|
|
|
await requestManager.resetGlobalUpdate();
|
|
|
|
|
} catch (e) {
|
2024-12-20 17:24:40 +01:00
|
|
|
makeToast(t('library.error.label.stop_global_update'), 'error', getErrorMessage(e));
|
2024-06-26 13:19:02 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-01 21:40:27 +02:00
|
|
|
const onClick = async (category?: CategoryIdInfo['id']) => {
|
2024-06-26 13:19:02 +02:00
|
|
|
if (isRunning) {
|
|
|
|
|
stopUpdate();
|
|
|
|
|
} else {
|
|
|
|
|
startUpdate(category);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-04-18 10:26:36 +02:00
|
|
|
return (
|
2024-06-26 13:19:02 +02:00
|
|
|
<PopupState variant="popover" popupId="library-update-checker-menu">
|
|
|
|
|
{(popupState) => (
|
|
|
|
|
<>
|
2025-02-01 14:24:38 +01:00
|
|
|
<CustomTooltip
|
2024-06-26 13:19:02 +02:00
|
|
|
title={
|
|
|
|
|
isRunning
|
|
|
|
|
? t('library.action.label.stop_update')
|
|
|
|
|
: t('library.settings.global_update.label.last_update_tooltip', {
|
|
|
|
|
date: lastUpdateTimestamp ? dateTimeFormatter.format(+lastUpdateTimestamp) : '-',
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
>
|
2024-06-26 12:42:03 +02:00
|
|
|
<IconButton
|
2024-06-26 13:19:02 +02:00
|
|
|
sx={{ position: 'relative' }}
|
|
|
|
|
{...(categoryId !== undefined && !isRunning
|
|
|
|
|
? bindTrigger(popupState)
|
|
|
|
|
: { onClick: () => onClick() })}
|
|
|
|
|
onMouseEnter={() => setIsHovered(true)}
|
|
|
|
|
onMouseLeave={() => setIsHovered(false)}
|
2024-09-06 16:59:27 +02:00
|
|
|
color="inherit"
|
2024-06-26 12:42:03 +02:00
|
|
|
>
|
2024-06-26 13:19:02 +02:00
|
|
|
{!isRunning ? (
|
|
|
|
|
<RefreshIcon />
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
<ClearIcon sx={{ opacity: Number(isTouchDevice || isHovered) }} />
|
|
|
|
|
<Stack sx={{ position: 'absolute' }}>
|
2024-09-06 16:59:27 +02:00
|
|
|
<Progress
|
|
|
|
|
progress={progress}
|
|
|
|
|
showText={!isTouchDevice && !isHovered}
|
|
|
|
|
progressProps={{ color: 'inherit' }}
|
|
|
|
|
/>
|
2024-06-26 13:19:02 +02:00
|
|
|
</Stack>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
2024-06-26 12:42:03 +02:00
|
|
|
</IconButton>
|
2025-02-01 14:24:38 +01:00
|
|
|
</CustomTooltip>
|
2024-06-26 13:19:02 +02:00
|
|
|
<Menu {...bindMenu(popupState)}>
|
|
|
|
|
<MenuItem
|
|
|
|
|
onClick={() => {
|
|
|
|
|
popupState.close();
|
|
|
|
|
onClick();
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{t('library.action.label.update_library')}
|
|
|
|
|
</MenuItem>
|
|
|
|
|
<MenuItem
|
|
|
|
|
onClick={() => {
|
|
|
|
|
popupState.close();
|
|
|
|
|
onClick(categoryId);
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{t('library.action.label.update_category')}
|
|
|
|
|
</MenuItem>
|
|
|
|
|
</Menu>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</PopupState>
|
2022-04-18 10:26:36 +02:00
|
|
|
);
|
|
|
|
|
}
|