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 { useTranslation } from 'react-i18next';
|
|
|
|
|
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';
|
2024-05-03 02:15:11 +02:00
|
|
|
import { VersionUpdateInfoDialog } from '@/components/util/VersionUpdateInfoDialog.tsx';
|
2024-05-07 00:37:01 +02:00
|
|
|
import { useMetadataServerSettings } from '@/lib/metadata/metadataServerSettings.ts';
|
|
|
|
|
|
|
|
|
|
const disabledUpdateCheck = () => Promise.resolve();
|
2023-12-09 23:20:52 +01:00
|
|
|
|
|
|
|
|
export const ServerUpdateChecker = () => {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
2024-05-07 00:37:01 +02:00
|
|
|
const {
|
|
|
|
|
settings: { serverInformAvailableUpdate },
|
|
|
|
|
} = 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({
|
|
|
|
|
notifyOnNetworkStatusChange: true,
|
|
|
|
|
fetchPolicy: 'cache-only',
|
|
|
|
|
});
|
2023-12-09 23:20:52 +01:00
|
|
|
|
|
|
|
|
const { data } = requestManager.useGetAbout();
|
|
|
|
|
const { aboutServer } = data ?? {};
|
|
|
|
|
|
|
|
|
|
const selectedServerChannelInfo = serverUpdateCheckData?.checkForServerUpdates?.find(
|
|
|
|
|
(channel) => channel.channel === aboutServer?.buildType,
|
|
|
|
|
);
|
|
|
|
|
const version = aboutServer ? getVersion(aboutServer) : undefined;
|
|
|
|
|
const isServerUpdateAvailable = !!selectedServerChannelInfo?.tag && selectedServerChannelInfo.tag !== version;
|
|
|
|
|
|
2024-05-07 00:37:01 +02:00
|
|
|
const updateChecker = useUpdateChecker(
|
|
|
|
|
'server',
|
|
|
|
|
serverInformAvailableUpdate ? checkForUpdate : disabledUpdateCheck,
|
|
|
|
|
selectedServerChannelInfo?.tag,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!serverInformAvailableUpdate) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
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-05-03 02:15:11 +02:00
|
|
|
<VersionUpdateInfoDialog
|
|
|
|
|
info={t('global.update.label.info', {
|
|
|
|
|
channel: selectedServerChannelInfo.channel,
|
|
|
|
|
version: selectedServerChannelInfo.tag,
|
|
|
|
|
})}
|
|
|
|
|
actionTitle={t('chapter.action.download.add.label.action')}
|
|
|
|
|
actionUrl={selectedServerChannelInfo.url}
|
|
|
|
|
updateCheckerProps={['server', checkForUpdate, selectedServerChannelInfo?.tag]}
|
|
|
|
|
/>
|
2023-12-09 23:20:52 +01:00
|
|
|
);
|
|
|
|
|
};
|