From d41e79ca7861c2aa83f5ee924a39466cc86026f5 Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Sat, 5 Oct 2024 21:44:08 +0200 Subject: [PATCH] Move app-update-checker files into new folder --- src/App.tsx | 4 +- .../app-updates/AppUpdateChecker.types.ts | 12 ++ .../components}/ServerUpdateChecker.tsx | 6 +- .../app-updates/components/VersionInfo.tsx | 166 +++++++++++++++++ .../components}/VersionUpdateInfoDialog.tsx | 2 +- .../components}/WebUIUpdateChecker.tsx | 4 +- .../app-updates/hooks}/useUpdateChecker.tsx | 0 .../app-updates/services/AppUpdateChecker.tsx | 19 ++ src/screens/settings/About.tsx | 174 +----------------- src/screens/settings/ServerSettings.tsx | 3 +- src/screens/settings/WebUISettings.tsx | 3 +- src/typings.ts | 6 +- 12 files changed, 213 insertions(+), 186 deletions(-) create mode 100644 src/modules/app-updates/AppUpdateChecker.types.ts rename src/{components/util => modules/app-updates/components}/ServerUpdateChecker.tsx (94%) create mode 100644 src/modules/app-updates/components/VersionInfo.tsx rename src/{components/util => modules/app-updates/components}/VersionUpdateInfoDialog.tsx (98%) rename src/{components/util => modules/app-updates/components}/WebUIUpdateChecker.tsx (97%) rename src/{util => modules/app-updates/hooks}/useUpdateChecker.tsx (100%) create mode 100644 src/modules/app-updates/services/AppUpdateChecker.tsx diff --git a/src/App.tsx b/src/App.tsx index 5e810641..e222f91e 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -16,8 +16,8 @@ import { AppContext } from '@/modules/core/contexts/AppContext.tsx'; import '@/i18n'; import { DefaultNavBar } from '@/components/navbar/DefaultNavBar'; import { requestManager } from '@/lib/requests/requests/RequestManager.ts'; -import { WebUIUpdateChecker } from '@/components/util/WebUIUpdateChecker.tsx'; -import { ServerUpdateChecker } from '@/components/util/ServerUpdateChecker.tsx'; +import { WebUIUpdateChecker } from '@/modules/app-updates/components/WebUIUpdateChecker.tsx'; +import { ServerUpdateChecker } from '@/modules/app-updates/components/ServerUpdateChecker.tsx'; import { lazyLoadFallback } from '@/modules/core/utils/LazyLoad.tsx'; import { ErrorBoundary } from '@/modules/core/components/ErrorBoundary.tsx'; import { useNavBarContext } from '@/components/context/NavbarContext.tsx'; diff --git a/src/modules/app-updates/AppUpdateChecker.types.ts b/src/modules/app-updates/AppUpdateChecker.types.ts new file mode 100644 index 00000000..d9926a0e --- /dev/null +++ b/src/modules/app-updates/AppUpdateChecker.types.ts @@ -0,0 +1,12 @@ +/* + * 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/. + */ + +export type MetadataUpdateSettings = { + webUIInformAvailableUpdate: boolean; + serverInformAvailableUpdate: boolean; +}; diff --git a/src/components/util/ServerUpdateChecker.tsx b/src/modules/app-updates/components/ServerUpdateChecker.tsx similarity index 94% rename from src/components/util/ServerUpdateChecker.tsx rename to src/modules/app-updates/components/ServerUpdateChecker.tsx index 0721e353..dfa1d263 100644 --- a/src/components/util/ServerUpdateChecker.tsx +++ b/src/modules/app-updates/components/ServerUpdateChecker.tsx @@ -15,11 +15,11 @@ import DialogContentText from '@mui/material/DialogContentText'; import DialogActions from '@mui/material/DialogActions'; import Button from '@mui/material/Button'; import { requestManager } from '@/lib/requests/requests/RequestManager.ts'; -import { getVersion } from '@/screens/settings/About.tsx'; -import { useUpdateChecker } from '@/util/useUpdateChecker.tsx'; -import { VersionUpdateInfoDialog } from '@/components/util/VersionUpdateInfoDialog.tsx'; +import { useUpdateChecker } from '@/modules/app-updates/hooks/useUpdateChecker.tsx'; +import { VersionUpdateInfoDialog } from '@/modules/app-updates/components/VersionUpdateInfoDialog.tsx'; import { useMetadataServerSettings } from '@/lib/metadata/metadataServerSettings.ts'; import { useLocalStorage } from '@/modules/core/hooks/useStorage.tsx'; +import { getVersion } from '@/modules/app-updates/services/AppUpdateChecker.tsx'; const disabledUpdateCheck = () => Promise.resolve(); diff --git a/src/modules/app-updates/components/VersionInfo.tsx b/src/modules/app-updates/components/VersionInfo.tsx new file mode 100644 index 00000000..110c8b68 --- /dev/null +++ b/src/modules/app-updates/components/VersionInfo.tsx @@ -0,0 +1,166 @@ +/* + * 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 Button from '@mui/material/Button'; +import CircularProgress from '@mui/material/CircularProgress'; +import Stack from '@mui/material/Stack'; +import Typography from '@mui/material/Typography'; +import RefreshIcon from '@mui/icons-material/Refresh'; +import DownloadIcon from '@mui/icons-material/Download'; +import OpenInNewIcon from '@mui/icons-material/OpenInNew'; +import { t as translate } from 'i18next'; +import DownloadingIcon from '@mui/icons-material/Downloading'; +import { UpdateState } from '@/lib/graphql/generated/graphql.ts'; + +export type BaseVersionInfoProps = { + version: string; + isCheckingForUpdate: boolean; + isUpdateAvailable: boolean; + updateCheckError: any; + checkForUpdate: () => void; +}; +export type LinkVersionInfoProps = { + downloadAsLink: true; + url: string; +}; +export type TriggerVersionInfoProps = { + triggerUpdate: () => void; + updateState: UpdateState; + progress: number; +}; +export type VersionInfoProps = + | (BaseVersionInfoProps & PropertiesNever & LinkVersionInfoProps) + | (BaseVersionInfoProps & TriggerVersionInfoProps & PropertiesNever); + +const getUpdateCheckButtonIcon = ( + isLoading: boolean, + isUpdateAvailable: boolean, + updateState?: UpdateState, + asLink: boolean = false, +) => { + const isUpdateInProgress = updateState === UpdateState.Downloading; + if (isUpdateInProgress) { + return ; + } + + if (isLoading) { + return ; + } + + const isRefreshRequired = !isUpdateAvailable || updateState === UpdateState.Error; + if (isRefreshRequired) { + return ; + } + + return asLink ? : ; +}; + +const getUpdateCheckButtonText = ( + isLoading: boolean, + isUpdateAvailable: boolean, + error: any, + updateState?: UpdateState, + progress: number = 0, +) => { + const isUpdating = updateState === UpdateState.Downloading; + if (isUpdating) { + return translate('global.update.label.updating', { progress }); + } + + const didUpdateFail = updateState === UpdateState.Error; + if (didUpdateFail) { + return translate('global.update.label.update_failure'); + } + + if (isLoading) { + return translate('global.update.label.checking'); + } + + if (error) { + return translate('global.update.label.check_failure'); + } + + if (isUpdateAvailable) { + return translate('global.update.label.available'); + } + + return translate('global.update.label.up_to_date'); +}; + +export const VersionInfo = ({ + version, + isCheckingForUpdate, + isUpdateAvailable, + updateCheckError, + checkForUpdate, + triggerUpdate, + updateState, + progress, + downloadAsLink, + url, +}: VersionInfoProps) => { + const isUpdateInProgress = updateState === UpdateState.Downloading; + + const onClick = () => { + if (isUpdateInProgress) { + return; + } + + const shouldCheckForUpdate = !isUpdateAvailable || updateCheckError || updateState === UpdateState.Error; + if (shouldCheckForUpdate) { + checkForUpdate(); + return; + } + + if (isUpdateAvailable) { + triggerUpdate?.(); + } + }; + + return ( + + + {version} + + + + ); +}; diff --git a/src/components/util/VersionUpdateInfoDialog.tsx b/src/modules/app-updates/components/VersionUpdateInfoDialog.tsx similarity index 98% rename from src/components/util/VersionUpdateInfoDialog.tsx rename to src/modules/app-updates/components/VersionUpdateInfoDialog.tsx index 37bec751..3ebbdd94 100644 --- a/src/components/util/VersionUpdateInfoDialog.tsx +++ b/src/modules/app-updates/components/VersionUpdateInfoDialog.tsx @@ -17,7 +17,7 @@ import Menu from '@mui/material/Menu'; import MenuItem from '@mui/material/MenuItem'; import { useTranslation } from 'react-i18next'; import Stack from '@mui/material/Stack'; -import { useUpdateChecker } from '@/util/useUpdateChecker.tsx'; +import { useUpdateChecker } from '@/modules/app-updates/hooks/useUpdateChecker.tsx'; interface BaseProps { info: string; diff --git a/src/components/util/WebUIUpdateChecker.tsx b/src/modules/app-updates/components/WebUIUpdateChecker.tsx similarity index 97% rename from src/components/util/WebUIUpdateChecker.tsx rename to src/modules/app-updates/components/WebUIUpdateChecker.tsx index 953d094a..34fe7444 100644 --- a/src/components/util/WebUIUpdateChecker.tsx +++ b/src/modules/app-updates/components/WebUIUpdateChecker.tsx @@ -20,8 +20,8 @@ import { useLocalStorage } from '@/modules/core/hooks/useStorage.tsx'; import { requestManager } from '@/lib/requests/requests/RequestManager.ts'; import { makeToast } from '@/lib/ui/Toast.ts'; import { ABOUT_WEBUI, WEBUI_UPDATE_CHECK } from '@/lib/graphql/fragments/InfoFragments.ts'; -import { VersionUpdateInfoDialog } from '@/components/util/VersionUpdateInfoDialog.tsx'; -import { useUpdateChecker } from '@/util/useUpdateChecker'; +import { VersionUpdateInfoDialog } from '@/modules/app-updates/components/VersionUpdateInfoDialog.tsx'; +import { useUpdateChecker } from '@/modules/app-updates/hooks/useUpdateChecker.tsx'; import { useMetadataServerSettings } from '@/lib/metadata/metadataServerSettings.ts'; const disabledUpdateCheck = () => Promise.resolve(); diff --git a/src/util/useUpdateChecker.tsx b/src/modules/app-updates/hooks/useUpdateChecker.tsx similarity index 100% rename from src/util/useUpdateChecker.tsx rename to src/modules/app-updates/hooks/useUpdateChecker.tsx diff --git a/src/modules/app-updates/services/AppUpdateChecker.tsx b/src/modules/app-updates/services/AppUpdateChecker.tsx new file mode 100644 index 00000000..bb8446d1 --- /dev/null +++ b/src/modules/app-updates/services/AppUpdateChecker.tsx @@ -0,0 +1,19 @@ +/* + * 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 { epochToDate } from '@/util/DateHelper.ts'; +import { GetAboutQuery } from '@/lib/graphql/generated/graphql.ts'; + +type AboutServer = GetAboutQuery['aboutServer']; + +export const getVersion = (aboutServer: AboutServer) => { + if (aboutServer.buildType === 'Stable') return `${aboutServer.version}`; + return `${aboutServer.version}-${aboutServer.revision}`; +}; + +export const getBuildTime = (aboutServer: AboutServer) => epochToDate(Number(aboutServer.buildTime)).toString(); diff --git a/src/screens/settings/About.tsx b/src/screens/settings/About.tsx index d752192d..39135f40 100644 --- a/src/screens/settings/About.tsx +++ b/src/screens/settings/About.tsx @@ -12,184 +12,16 @@ import ListItem from '@mui/material/ListItem'; import ListItemText from '@mui/material/ListItemText'; import { useTranslation } from 'react-i18next'; import ListSubheader from '@mui/material/ListSubheader'; -import Button from '@mui/material/Button'; -import CircularProgress from '@mui/material/CircularProgress'; import Divider from '@mui/material/Divider'; -import Stack from '@mui/material/Stack'; -import Typography from '@mui/material/Typography'; -import RefreshIcon from '@mui/icons-material/Refresh'; -import DownloadIcon from '@mui/icons-material/Download'; -import OpenInNewIcon from '@mui/icons-material/OpenInNew'; -import { t as translate } from 'i18next'; -import DownloadingIcon from '@mui/icons-material/Downloading'; import { requestManager } from '@/lib/requests/requests/RequestManager.ts'; import { ListItemLink } from '@/modules/core/components/ListItemLink.tsx'; import { NavBarContext } from '@/components/context/NavbarContext'; import { LoadingPlaceholder } from '@/modules/core/components/placeholder/LoadingPlaceholder.tsx'; -import { GetAboutQuery, UpdateState } from '@/lib/graphql/generated/graphql.ts'; +import { UpdateState } from '@/lib/graphql/generated/graphql.ts'; import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts'; import { EmptyViewAbsoluteCentered } from '@/modules/core/components/placeholder/EmptyViewAbsoluteCentered.tsx'; -import { epochToDate } from '@/util/DateHelper.ts'; - -type AboutServer = GetAboutQuery['aboutServer']; - -export const getVersion = (aboutServer: AboutServer) => { - if (aboutServer.buildType === 'Stable') return `${aboutServer.version}`; - return `${aboutServer.version}-${aboutServer.revision}`; -}; - -const getBuildTime = (aboutServer: AboutServer) => epochToDate(Number(aboutServer.buildTime)).toString(); - -const getUpdateCheckButtonIcon = ( - isLoading: boolean, - isUpdateAvailable: boolean, - updateState?: UpdateState, - asLink: boolean = false, -) => { - const isUpdateInProgress = updateState === UpdateState.Downloading; - if (isUpdateInProgress) { - return ; - } - - if (isLoading) { - return ; - } - - const isRefreshRequired = !isUpdateAvailable || updateState === UpdateState.Error; - if (isRefreshRequired) { - return ; - } - - return asLink ? : ; -}; - -const getUpdateCheckButtonText = ( - isLoading: boolean, - isUpdateAvailable: boolean, - error: any, - updateState?: UpdateState, - progress: number = 0, -) => { - const isUpdating = updateState === UpdateState.Downloading; - if (isUpdating) { - return translate('global.update.label.updating', { progress }); - } - - const didUpdateFail = updateState === UpdateState.Error; - if (didUpdateFail) { - return translate('global.update.label.update_failure'); - } - - if (isLoading) { - return translate('global.update.label.checking'); - } - - if (error) { - return translate('global.update.label.check_failure'); - } - - if (isUpdateAvailable) { - return translate('global.update.label.available'); - } - - return translate('global.update.label.up_to_date'); -}; - -type BaseVersionInfoProps = { - version: string; - isCheckingForUpdate: boolean; - isUpdateAvailable: boolean; - updateCheckError: any; - checkForUpdate: () => void; -}; - -type LinkVersionInfoProps = { - downloadAsLink: true; - url: string; -}; - -type TriggerVersionInfoProps = { - triggerUpdate: () => void; - updateState: UpdateState; - progress: number; -}; - -type VersionInfoProps = - | (BaseVersionInfoProps & PropertiesNever & LinkVersionInfoProps) - | (BaseVersionInfoProps & TriggerVersionInfoProps & PropertiesNever); - -const VersionInfo = ({ - version, - isCheckingForUpdate, - isUpdateAvailable, - updateCheckError, - checkForUpdate, - triggerUpdate, - updateState, - progress, - downloadAsLink, - url, -}: VersionInfoProps) => { - const isUpdateInProgress = updateState === UpdateState.Downloading; - - const onClick = () => { - if (isUpdateInProgress) { - return; - } - - const shouldCheckForUpdate = !isUpdateAvailable || updateCheckError || updateState === UpdateState.Error; - if (shouldCheckForUpdate) { - checkForUpdate(); - return; - } - - if (isUpdateAvailable) { - triggerUpdate?.(); - } - }; - - return ( - - - {version} - - - - ); -}; +import { getBuildTime, getVersion } from '@/modules/app-updates/services/AppUpdateChecker.tsx'; +import { VersionInfo } from '@/modules/app-updates/components/VersionInfo.tsx'; export function About() { const { t } = useTranslation(); diff --git a/src/screens/settings/ServerSettings.tsx b/src/screens/settings/ServerSettings.tsx index 4219cc00..bd881e5d 100644 --- a/src/screens/settings/ServerSettings.tsx +++ b/src/screens/settings/ServerSettings.tsx @@ -19,7 +19,7 @@ import { NavBarContext } from '@/components/context/NavbarContext.tsx'; import { requestManager } from '@/lib/requests/requests/RequestManager.ts'; import { useLocalStorage } from '@/modules/core/hooks/useStorage.tsx'; import { TextSetting } from '@/modules/core/components/settings/text/TextSetting.tsx'; -import { MetadataUpdateSettings, ServerSettings as GqlServerSettings } from '@/typings.ts'; +import { ServerSettings as GqlServerSettings } from '@/typings.ts'; import { NumberSetting } from '@/modules/core/components/settings/NumberSetting.tsx'; import { SelectSetting } from '@/modules/core/components/settings/SelectSetting.tsx'; import { LoadingPlaceholder } from '@/modules/core/components/placeholder/LoadingPlaceholder.tsx'; @@ -30,6 +30,7 @@ import { useMetadataServerSettings, } from '@/lib/metadata/metadataServerSettings.ts'; import { makeToast } from '@/lib/ui/Toast.ts'; +import { MetadataUpdateSettings } from '@/modules/app-updates/AppUpdateChecker.types.ts'; type ServerSettingsType = Pick< GqlServerSettings, diff --git a/src/screens/settings/WebUISettings.tsx b/src/screens/settings/WebUISettings.tsx index 7931945e..ddfe35f0 100644 --- a/src/screens/settings/WebUISettings.tsx +++ b/src/screens/settings/WebUISettings.tsx @@ -13,7 +13,7 @@ import ListItem from '@mui/material/ListItem'; import ListItemText from '@mui/material/ListItemText'; import Switch from '@mui/material/Switch'; import { NavBarContext } from '@/components/context/NavbarContext.tsx'; -import { MetadataUpdateSettings, ServerSettings } from '@/typings.ts'; +import { ServerSettings } from '@/typings.ts'; import { requestManager } from '@/lib/requests/requests/RequestManager.ts'; import { WebUIUpdateIntervalSetting } from '@/components/settings/webUI/WebUIUpdateIntervalSetting.tsx'; import { TextSetting } from '@/modules/core/components/settings/text/TextSetting.tsx'; @@ -31,6 +31,7 @@ import { useMetadataServerSettings, } from '@/lib/metadata/metadataServerSettings.ts'; import { makeToast } from '@/lib/ui/Toast.ts'; +import { MetadataUpdateSettings } from '@/modules/app-updates/AppUpdateChecker.types.ts'; type WebUISettingsType = Pick< ServerSettings, diff --git a/src/typings.ts b/src/typings.ts index 829979b8..1db5e6cb 100644 --- a/src/typings.ts +++ b/src/typings.ts @@ -17,6 +17,7 @@ import { MetadataLibrarySettings } from '@/modules/library/Library.types.ts'; import { SourceMetadataKeys } from '@/modules/source/Source.types.ts'; import { MetadataTrackingSettings } from '@/modules/tracker/Tracker.types.ts'; import { CategoryMetadataKeys } from '@/modules/category/Category.types.ts'; +import { MetadataUpdateSettings } from '@/modules/app-updates/AppUpdateChecker.types.ts'; export interface IMetadataMigration { appKeyPrefix?: { oldPrefix: string; newPrefix: string }; @@ -77,11 +78,6 @@ export type MetadataBrowseSettings = { hideLibraryEntries: boolean; }; -export type MetadataUpdateSettings = { - webUIInformAvailableUpdate: boolean; - serverInformAvailableUpdate: boolean; -}; - export type MetadataThemeSettings = { customThemes: Record; mangaThumbnailBackdrop: boolean;