From 412f2845ec8d34e5de8380cfeb128d01c91800ff Mon Sep 17 00:00:00 2001 From: Matej Lubej Date: Fri, 7 Jun 2024 06:10:58 +0200 Subject: [PATCH] Move useIonicRequiresUpdate state --- .changelog/1969.feature.md | 1 + .../Ionic/components/UpdateGate/index.tsx | 2 +- .../Ionic/hooks/useIonicRequiresUpdate.ts | 18 +++++++++++------- .../components/Ionic/providers/IonicContext.ts | 4 ++-- .../Ionic/providers/IonicProvider.tsx | 15 ++++----------- 5 files changed, 19 insertions(+), 21 deletions(-) create mode 100644 .changelog/1969.feature.md diff --git a/.changelog/1969.feature.md b/.changelog/1969.feature.md new file mode 100644 index 0000000000..1f7438188b --- /dev/null +++ b/.changelog/1969.feature.md @@ -0,0 +1 @@ +Add Android update screen diff --git a/src/app/components/Ionic/components/UpdateGate/index.tsx b/src/app/components/Ionic/components/UpdateGate/index.tsx index e0ba35634c..5c1d195056 100644 --- a/src/app/components/Ionic/components/UpdateGate/index.tsx +++ b/src/app/components/Ionic/components/UpdateGate/index.tsx @@ -78,7 +78,7 @@ export const UpdateGate: FC = ({ children }) => { const { t } = useTranslation() const isMobile = useContext(ResponsiveContext) === 'small' const { - state: { updateAvailability }, + requiresUpdateState: { updateAvailability }, checkForUpdateAvailability, skipUpdate, } = useContext(IonicContext) diff --git a/src/app/components/Ionic/hooks/useIonicRequiresUpdate.ts b/src/app/components/Ionic/hooks/useIonicRequiresUpdate.ts index de683b2389..a6a9e7d481 100644 --- a/src/app/components/Ionic/hooks/useIonicRequiresUpdate.ts +++ b/src/app/components/Ionic/hooks/useIonicRequiresUpdate.ts @@ -1,11 +1,15 @@ -import { Dispatch, SetStateAction, useEffect } from 'react' -import { IonicProviderState, UpdateAvailability } from '../providers/IonicContext' +import { useEffect, useState } from 'react' +import { IonicRequiresUpdateState, UpdateAvailability } from '../providers/IonicContext' import { updateAvailable } from '../utils/capacitor-app-update' -export const useIonicRequiresUpdate = ( - state: IonicProviderState, - setState: Dispatch>, -) => { +const ionicRequiresUpdateInitialState: IonicRequiresUpdateState = { + updateAvailability: UpdateAvailability.NOT_INITIALIZED, + error: null, +} + +export const useIonicRequiresUpdate = () => { + const [state, setState] = useState({ ...ionicRequiresUpdateInitialState }) + const checkForUpdateAvailability = async () => { if (state.updateAvailability === UpdateAvailability.LOADING) { return @@ -35,5 +39,5 @@ export const useIonicRequiresUpdate = ( setState(prevState => ({ ...prevState, updateAvailability: UpdateAvailability.UPDATE_NOT_AVAILABLE })) } - return { checkForUpdateAvailability, skipUpdate } + return { requiresUpdateState: state, checkForUpdateAvailability, skipUpdate } } diff --git a/src/app/components/Ionic/providers/IonicContext.ts b/src/app/components/Ionic/providers/IonicContext.ts index 9a76712628..937bb304a7 100644 --- a/src/app/components/Ionic/providers/IonicContext.ts +++ b/src/app/components/Ionic/providers/IonicContext.ts @@ -10,13 +10,13 @@ export enum UpdateAvailability { UNKNOWN, } -export interface IonicProviderState { +export interface IonicRequiresUpdateState { updateAvailability: UpdateAvailability error: Error | null } export interface IonicProviderContext { - readonly state: IonicProviderState + readonly requiresUpdateState: IonicRequiresUpdateState checkForUpdateAvailability: () => void skipUpdate: () => void } diff --git a/src/app/components/Ionic/providers/IonicProvider.tsx b/src/app/components/Ionic/providers/IonicProvider.tsx index e9461f04b8..b6107e4b48 100644 --- a/src/app/components/Ionic/providers/IonicProvider.tsx +++ b/src/app/components/Ionic/providers/IonicProvider.tsx @@ -1,23 +1,16 @@ -import { FC, PropsWithChildren, useState } from 'react' +import { FC, PropsWithChildren } from 'react' import { useIonicBackButtonListener } from '../hooks/useIonicBackButtonListener' import { useIonicAppStateChangeListener } from '../hooks/useIonicAppStateChangeListener' -import { IonicContext, IonicProviderContext, IonicProviderState, UpdateAvailability } from './IonicContext' +import { IonicContext, IonicProviderContext } from './IonicContext' import { useIonicRequiresUpdate } from '../hooks/useIonicRequiresUpdate' -const ionicProviderInitialState: IonicProviderState = { - updateAvailability: UpdateAvailability.NOT_INITIALIZED, - error: null, -} - export const IonicContextProvider: FC = ({ children }) => { - const [state, setState] = useState({ ...ionicProviderInitialState }) - - const { checkForUpdateAvailability, skipUpdate } = useIonicRequiresUpdate(state, setState) + const { requiresUpdateState, checkForUpdateAvailability, skipUpdate } = useIonicRequiresUpdate() useIonicAppStateChangeListener() useIonicBackButtonListener() const providerState: IonicProviderContext = { - state, + requiresUpdateState, checkForUpdateAvailability, skipUpdate, }