Extract automated update check into hook
This commit is contained in:
@@ -12,20 +12,15 @@ import DialogContent from '@mui/material/DialogContent';
|
|||||||
import DialogTitle from '@mui/material/DialogTitle';
|
import DialogTitle from '@mui/material/DialogTitle';
|
||||||
import Button from '@mui/material/Button';
|
import Button from '@mui/material/Button';
|
||||||
import DialogContentText from '@mui/material/DialogContentText';
|
import DialogContentText from '@mui/material/DialogContentText';
|
||||||
import { useEffect, useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
||||||
import { getVersion } from '@/screens/settings/About.tsx';
|
import { getVersion } from '@/screens/settings/About.tsx';
|
||||||
import { useLocalStorage } from '@/util/useStorage.tsx';
|
import { useUpdateChecker } from '@/util/useUpdateChecker.tsx';
|
||||||
import { defaultPromiseErrorHandler } from '@/util/defaultPromiseErrorHandler.ts';
|
|
||||||
|
|
||||||
const UPDATE_CHECK_INTERVAL = 1000 * 60 * 60 * 24; // 1 day
|
|
||||||
|
|
||||||
export const ServerUpdateChecker = () => {
|
export const ServerUpdateChecker = () => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
|
||||||
const [lastUpdateCheck, setLastUpdateCheck] = useLocalStorage('lastAutomatedServerUpdateCheck', 0);
|
|
||||||
|
|
||||||
const {
|
const {
|
||||||
data: serverUpdateCheckData,
|
data: serverUpdateCheckData,
|
||||||
loading: isCheckingForServerUpdate,
|
loading: isCheckingForServerUpdate,
|
||||||
@@ -48,23 +43,7 @@ export const ServerUpdateChecker = () => {
|
|||||||
setOpen(false);
|
setOpen(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useUpdateChecker('server', checkForUpdate);
|
||||||
const remainingTimeTillNextUpdateCheck =
|
|
||||||
(UPDATE_CHECK_INTERVAL - (Date.now() - lastUpdateCheck)) % UPDATE_CHECK_INTERVAL;
|
|
||||||
|
|
||||||
let timeout: NodeJS.Timeout | undefined;
|
|
||||||
const scheduleUpdateCheck = (timeoutMS: number) => {
|
|
||||||
timeout = setTimeout(() => {
|
|
||||||
checkForUpdate().catch(defaultPromiseErrorHandler('ServerUpdateChecker::checkForUpdate'));
|
|
||||||
setLastUpdateCheck(Date.now());
|
|
||||||
scheduleUpdateCheck(UPDATE_CHECK_INTERVAL);
|
|
||||||
}, timeoutMS);
|
|
||||||
};
|
|
||||||
|
|
||||||
scheduleUpdateCheck(remainingTimeTillNextUpdateCheck);
|
|
||||||
|
|
||||||
return () => clearTimeout(timeout);
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
if (isCheckingForServerUpdate) {
|
if (isCheckingForServerUpdate) {
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
38
src/util/useUpdateChecker.tsx
Normal file
38
src/util/useUpdateChecker.tsx
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* 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 { useEffect } from 'react';
|
||||||
|
import { defaultPromiseErrorHandler } from '@/util/defaultPromiseErrorHandler.ts';
|
||||||
|
import { useLocalStorage } from '@/util/useStorage.tsx';
|
||||||
|
|
||||||
|
const UPDATE_CHECK_INTERVAL = 1000 * 60 * 60 * 24; // 1 day
|
||||||
|
|
||||||
|
export const useUpdateChecker = (
|
||||||
|
storageKey: string,
|
||||||
|
checkForUpdate: () => Promise<unknown>,
|
||||||
|
interval: number = UPDATE_CHECK_INTERVAL,
|
||||||
|
): void => {
|
||||||
|
const [lastUpdateCheck, setLastUpdateCheck] = useLocalStorage(`UpdateChecker::${storageKey}`, 0);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const remainingTimeTillNextUpdateCheck = (interval - (Date.now() - lastUpdateCheck)) % interval;
|
||||||
|
|
||||||
|
let timeout: NodeJS.Timeout | undefined;
|
||||||
|
const scheduleUpdateCheck = (timeoutMS: number) => {
|
||||||
|
timeout = setTimeout(() => {
|
||||||
|
checkForUpdate().catch(defaultPromiseErrorHandler(`UpdateChecker(${storageKey})::checkForUpdate`));
|
||||||
|
setLastUpdateCheck(Date.now());
|
||||||
|
scheduleUpdateCheck(interval);
|
||||||
|
}, timeoutMS);
|
||||||
|
};
|
||||||
|
|
||||||
|
scheduleUpdateCheck(remainingTimeTillNextUpdateCheck);
|
||||||
|
|
||||||
|
return () => clearTimeout(timeout);
|
||||||
|
}, [storageKey, checkForUpdate, interval]);
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user