diff --git a/app/src/state/reducers/rootReducer.ts b/app/src/state/reducers/rootReducer.ts index db094f940..9eec729f8 100644 --- a/app/src/state/reducers/rootReducer.ts +++ b/app/src/state/reducers/rootReducer.ts @@ -2,7 +2,7 @@ import { combineReducers } from 'redux'; import localForage from 'localforage'; import autoMergeLevel1 from 'redux-persist/lib/stateReconciler/autoMergeLevel1'; import autoMergeLevel2 from 'redux-persist/lib/stateReconciler/autoMergeLevel2'; -import { persistReducer } from 'redux-persist'; +import { persistReducer, createTransform } from 'redux-persist'; import appMode from './appMode'; import { ActivityState, createActivityReducer } from './activity'; import { AuthState, createAuthReducer } from './auth'; @@ -23,6 +23,8 @@ import { AppConfig } from 'state/config'; import { CURRENT_MIGRATION_VERSION, MIGRATION_VERSION_KEY } from 'constants/offline_state_version'; import { createTileCacheReducer } from 'state/reducers/tile_cache'; import { MOBILE } from 'state/build-time-config'; +import { UserRecordCacheStatus } from 'interfaces/UserRecordSet'; +import { CacheDownloadMode } from 'utils/record-cache'; // it will try indexdb first, then fall back to localstorage if not available. @@ -49,6 +51,27 @@ const purgeOldStateOnVersionUpgrade = async (state: any) => { } }; +// executes during app restart or when the page reloads +const pauseDownloadOnRehydration = createTransform( + (inboundState) => inboundState, + + (outboundState) => { + if (outboundState && typeof outboundState === 'object') { + Object.keys(outboundState).forEach((key) => { + // updates state correctly when page reloads during an active download + if (outboundState[key]?.cacheMetadataStatus === UserRecordCacheStatus.DOWNLOADING) { + outboundState[key].cacheMetadataStatus = UserRecordCacheStatus.PAUSED; + outboundState[key].cacheDownloadProgress.downloadMode = CacheDownloadMode.PAUSE; + outboundState[key].cacheDownloadProgress.message = + `Mode: ${CacheDownloadMode.PAUSE.toLocaleString().toUpperCase()} Caching`; + } + }); + } + return outboundState; + }, + { whitelist: ['recordSets'] } +); + function createRootReducer(config: AppConfig) { return combineReducers({ AppMode: appMode, @@ -103,7 +126,8 @@ function createRootReducer(config: AppConfig) { 'boundaries', 'layerPickerIsAccordion', 'mapCenter' - ] + ], + transforms: [pauseDownloadOnRehydration] }, createUserSettingsReducer(config) ),