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/.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-05-07 01:35:19 +02:00
|
|
|
import { useState } from 'react';
|
|
|
|
|
import Dialog from '@mui/material/Dialog';
|
|
|
|
|
import DialogTitle from '@mui/material/DialogTitle';
|
|
|
|
|
import DialogContent from '@mui/material/DialogContent';
|
|
|
|
|
import DialogContentText from '@mui/material/DialogContentText';
|
|
|
|
|
import DialogActions from '@mui/material/DialogActions';
|
|
|
|
|
import Button from '@mui/material/Button';
|
2026-01-10 19:45:02 +01:00
|
|
|
import { useLingui } from '@lingui/react/macro';
|
2024-10-06 01:27:51 +02:00
|
|
|
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
2025-08-15 22:02:58 +02:00
|
|
|
import { useUpdateChecker } from '@/features/app-updates/hooks/useUpdateChecker.tsx';
|
|
|
|
|
import { VersionUpdateInfoDialog } from '@/features/app-updates/components/VersionUpdateInfoDialog.tsx';
|
|
|
|
|
import { useMetadataServerSettings } from '@/features/settings/services/ServerSettingsMetadata.ts';
|
2025-08-16 02:48:46 +02:00
|
|
|
import { useLocalStorage } from '@/base/hooks/useStorage.tsx';
|
|
|
|
|
import { AppRoutes } from '@/base/AppRoute.constants.ts';
|
2026-03-20 03:29:45 +01:00
|
|
|
import { STABLE_EMPTY_OBJECT } from '@/base/Base.constants.ts';
|
2024-05-07 00:37:01 +02:00
|
|
|
|
|
|
|
|
const disabledUpdateCheck = () => Promise.resolve();
|
2023-12-09 23:20:52 +01:00
|
|
|
|
|
|
|
|
export const ServerUpdateChecker = () => {
|
2026-01-10 19:45:02 +01:00
|
|
|
const { t } = useLingui();
|
2023-12-09 23:20:52 +01:00
|
|
|
|
2024-05-07 01:35:19 +02:00
|
|
|
const [serverVersion, setServerVersion] = useLocalStorage<string>('serverVersion');
|
|
|
|
|
const [open, setOpen] = useState(false);
|
|
|
|
|
|
2024-05-07 00:37:01 +02:00
|
|
|
const {
|
2026-01-25 17:57:30 +01:00
|
|
|
settings: { serverInformAvailableUpdate, serverInformVersionUpdated },
|
|
|
|
|
loading: areMetadataServerSettingsLoading,
|
2024-05-07 00:37:01 +02:00
|
|
|
} = useMetadataServerSettings();
|
|
|
|
|
|
2023-12-09 23:20:52 +01:00
|
|
|
const {
|
|
|
|
|
data: serverUpdateCheckData,
|
|
|
|
|
loading: isCheckingForServerUpdate,
|
|
|
|
|
error: serverUpdateCheckError,
|
|
|
|
|
refetch: checkForUpdate,
|
2024-05-07 00:37:01 +02:00
|
|
|
} = requestManager.useCheckForServerUpdate({
|
|
|
|
|
fetchPolicy: 'cache-only',
|
|
|
|
|
});
|
2023-12-09 23:20:52 +01:00
|
|
|
|
|
|
|
|
const { data } = requestManager.useGetAbout();
|
2026-03-20 03:29:45 +01:00
|
|
|
const { aboutServer } = data ?? STABLE_EMPTY_OBJECT;
|
2023-12-09 23:20:52 +01:00
|
|
|
|
|
|
|
|
const selectedServerChannelInfo = serverUpdateCheckData?.checkForServerUpdates?.find(
|
|
|
|
|
(channel) => channel.channel === aboutServer?.buildType,
|
|
|
|
|
);
|
2025-04-06 23:58:58 +02:00
|
|
|
const version = aboutServer ? aboutServer.version : undefined;
|
2023-12-09 23:20:52 +01:00
|
|
|
const isServerUpdateAvailable = !!selectedServerChannelInfo?.tag && selectedServerChannelInfo.tag !== version;
|
|
|
|
|
|
2024-05-07 00:37:01 +02:00
|
|
|
const updateChecker = useUpdateChecker(
|
|
|
|
|
'server',
|
|
|
|
|
serverInformAvailableUpdate ? checkForUpdate : disabledUpdateCheck,
|
|
|
|
|
selectedServerChannelInfo?.tag,
|
|
|
|
|
);
|
|
|
|
|
|
2024-05-07 01:35:19 +02:00
|
|
|
const changelogUrl =
|
|
|
|
|
aboutServer?.buildType.toLowerCase() === 'stable'
|
|
|
|
|
? `https://github.com/Suwayomi/Suwayomi-Server/releases/tag/${aboutServer.version}`
|
|
|
|
|
: undefined;
|
|
|
|
|
|
2024-05-08 13:50:27 +02:00
|
|
|
const isSameAsCurrent = !version || !serverVersion || serverVersion === version;
|
2024-09-01 00:57:42 +02:00
|
|
|
|
|
|
|
|
const saveInitialVersion = !serverVersion && !!version;
|
|
|
|
|
if (saveInitialVersion) {
|
|
|
|
|
setServerVersion(version);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-25 17:57:30 +01:00
|
|
|
if (!areMetadataServerSettingsLoading && !isSameAsCurrent && !open) {
|
|
|
|
|
if (serverInformVersionUpdated) {
|
|
|
|
|
setOpen(true);
|
|
|
|
|
} else {
|
|
|
|
|
setServerVersion(version);
|
|
|
|
|
}
|
2024-05-07 01:35:19 +02:00
|
|
|
}
|
|
|
|
|
|
2023-12-09 23:20:52 +01:00
|
|
|
if (isCheckingForServerUpdate) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (serverUpdateCheckError) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-07 01:35:19 +02:00
|
|
|
if (isServerUpdateAvailable) {
|
2024-07-28 22:56:39 +02:00
|
|
|
if (!serverInformAvailableUpdate) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-05 15:34:58 +02:00
|
|
|
const isAboutPage = window.location.pathname === AppRoutes.about.path;
|
2024-05-07 01:35:19 +02:00
|
|
|
if (isAboutPage) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-12-09 23:20:52 +01:00
|
|
|
|
2024-05-07 01:35:19 +02:00
|
|
|
if (!updateChecker.handleUpdate) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<VersionUpdateInfoDialog
|
2026-01-10 19:45:02 +01:00
|
|
|
info={t`Server version ${selectedServerChannelInfo.tag} (${selectedServerChannelInfo.channel}) available for download`}
|
|
|
|
|
actionTitle={t`Download`}
|
2024-05-07 01:35:19 +02:00
|
|
|
actionUrl={selectedServerChannelInfo.url}
|
|
|
|
|
updateCheckerProps={['server', checkForUpdate, selectedServerChannelInfo?.tag]}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2023-12-09 23:20:52 +01:00
|
|
|
}
|
|
|
|
|
|
2024-05-07 01:35:19 +02:00
|
|
|
if (!open) {
|
2024-04-21 15:05:12 +02:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-09 23:20:52 +01:00
|
|
|
return (
|
2024-05-07 01:35:19 +02:00
|
|
|
<Dialog open={open}>
|
2026-01-10 19:45:02 +01:00
|
|
|
<DialogTitle>{t`Updated version`}</DialogTitle>
|
2024-05-07 01:35:19 +02:00
|
|
|
<DialogContent>
|
2026-01-10 19:45:02 +01:00
|
|
|
<DialogContentText>{t`Server was updated to version ${version} (${aboutServer?.buildType})`}</DialogContentText>
|
2024-05-07 01:35:19 +02:00
|
|
|
</DialogContent>
|
|
|
|
|
<DialogActions>
|
|
|
|
|
{changelogUrl && (
|
|
|
|
|
<Button href={changelogUrl} target="_blank" rel="noreferrer">
|
2026-01-10 19:45:02 +01:00
|
|
|
{t`Changelog`}
|
2024-05-07 01:35:19 +02:00
|
|
|
</Button>
|
|
|
|
|
)}
|
|
|
|
|
<Button
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setServerVersion(version);
|
|
|
|
|
setOpen(false);
|
|
|
|
|
}}
|
|
|
|
|
variant="contained"
|
|
|
|
|
>
|
2026-01-10 19:45:02 +01:00
|
|
|
{t`Ok`}
|
2024-05-07 01:35:19 +02:00
|
|
|
</Button>
|
|
|
|
|
</DialogActions>
|
|
|
|
|
</Dialog>
|
2023-12-09 23:20:52 +01:00
|
|
|
);
|
|
|
|
|
};
|