2023-12-09 23:20:52 +01: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/.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import Dialog from '@mui/material/Dialog';
|
|
|
|
|
import DialogActions from '@mui/material/DialogActions';
|
|
|
|
|
import DialogContent from '@mui/material/DialogContent';
|
|
|
|
|
import DialogTitle from '@mui/material/DialogTitle';
|
|
|
|
|
import Button from '@mui/material/Button';
|
|
|
|
|
import DialogContentText from '@mui/material/DialogContentText';
|
2024-04-21 14:01:47 +02:00
|
|
|
import { useState } from 'react';
|
2023-12-09 23:20:52 +01:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2024-05-03 01:48:12 +02:00
|
|
|
import PopupState, { bindMenu, bindTrigger } from 'material-ui-popup-state';
|
|
|
|
|
import Menu from '@mui/material/Menu';
|
|
|
|
|
import MenuItem from '@mui/material/MenuItem';
|
2023-12-09 23:20:52 +01:00
|
|
|
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
|
|
|
|
import { getVersion } from '@/screens/settings/About.tsx';
|
2024-04-21 14:01:47 +02:00
|
|
|
import { useUpdateChecker } from '@/util/useUpdateChecker.tsx';
|
2023-12-09 23:20:52 +01:00
|
|
|
|
|
|
|
|
export const ServerUpdateChecker = () => {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
data: serverUpdateCheckData,
|
|
|
|
|
loading: isCheckingForServerUpdate,
|
|
|
|
|
error: serverUpdateCheckError,
|
|
|
|
|
refetch: checkForUpdate,
|
|
|
|
|
} = requestManager.useCheckForServerUpdate({ notifyOnNetworkStatusChange: true, fetchPolicy: 'cache-only' });
|
|
|
|
|
|
|
|
|
|
const { data } = requestManager.useGetAbout();
|
|
|
|
|
const { aboutServer } = data ?? {};
|
|
|
|
|
|
|
|
|
|
const [open, setOpen] = useState(true);
|
|
|
|
|
|
|
|
|
|
const selectedServerChannelInfo = serverUpdateCheckData?.checkForServerUpdates?.find(
|
|
|
|
|
(channel) => channel.channel === aboutServer?.buildType,
|
|
|
|
|
);
|
|
|
|
|
const version = aboutServer ? getVersion(aboutServer) : undefined;
|
|
|
|
|
const isServerUpdateAvailable = !!selectedServerChannelInfo?.tag && selectedServerChannelInfo.tag !== version;
|
|
|
|
|
|
|
|
|
|
const handleClose = () => {
|
|
|
|
|
setOpen(false);
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-21 15:05:12 +02:00
|
|
|
const updateChecker = useUpdateChecker('server', checkForUpdate, selectedServerChannelInfo?.tag);
|
2023-12-09 23:20:52 +01:00
|
|
|
|
|
|
|
|
if (isCheckingForServerUpdate) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (serverUpdateCheckError) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isServerUpdateAvailable) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const isAboutPage = window.location.pathname === '/settings/about';
|
|
|
|
|
if (isAboutPage) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-21 15:05:12 +02:00
|
|
|
if (!updateChecker.handleUpdate) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-09 23:20:52 +01:00
|
|
|
return (
|
2024-04-21 15:05:12 +02:00
|
|
|
<Dialog open={open} aria-labelledby="alert-dialog-title" aria-describedby="alert-dialog-description">
|
2023-12-09 23:20:52 +01:00
|
|
|
<DialogTitle id="alert-dialog-title">{t('global.update.label.available')}</DialogTitle>
|
|
|
|
|
<DialogContent>
|
|
|
|
|
<DialogContentText id="alert-dialog-description">
|
|
|
|
|
{t('global.update.label.info', {
|
|
|
|
|
channel: selectedServerChannelInfo.channel,
|
|
|
|
|
version: selectedServerChannelInfo.tag,
|
|
|
|
|
})}
|
|
|
|
|
</DialogContentText>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
<DialogActions>
|
2024-05-03 01:48:12 +02:00
|
|
|
<PopupState variant="popover" popupId="server-update-checker-close-menu">
|
|
|
|
|
{(popupState) => (
|
|
|
|
|
<>
|
|
|
|
|
<Button {...bindTrigger(popupState)}>{t('global.label.close')}</Button>
|
|
|
|
|
<Menu {...bindMenu(popupState)}>
|
|
|
|
|
<MenuItem
|
|
|
|
|
onClick={() => {
|
|
|
|
|
updateChecker.remindLater();
|
|
|
|
|
popupState.close();
|
|
|
|
|
handleClose();
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{t('global.button.remind_later')}
|
|
|
|
|
</MenuItem>
|
|
|
|
|
<MenuItem
|
|
|
|
|
onClick={() => {
|
|
|
|
|
updateChecker.ignoreUpdate();
|
|
|
|
|
popupState.close();
|
|
|
|
|
handleClose();
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{t('global.button.ignore')}
|
|
|
|
|
</MenuItem>
|
|
|
|
|
</Menu>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</PopupState>
|
2023-12-09 23:20:52 +01:00
|
|
|
<Button onClick={handleClose} variant="contained" href={selectedServerChannelInfo.url} target="_blank">
|
|
|
|
|
{t('chapter.action.download.add.label.action')}
|
|
|
|
|
</Button>
|
|
|
|
|
</DialogActions>
|
|
|
|
|
</Dialog>
|
|
|
|
|
);
|
|
|
|
|
};
|