diff --git a/lib/reducers/alert-reducer.js b/lib/reducers/alert-reducer.js --- a/lib/reducers/alert-reducer.js +++ b/lib/reducers/alert-reducer.js @@ -4,12 +4,40 @@ incrementColdStartCountActionType, recordAlertActionType, } from '../actions/alert-actions.js'; +import { setClientDBStoreActionType } from '../actions/client-db-store-actions.js'; +import type { SyncedMetadataStoreOperation } from '../ops/synced-metadata-store-ops.js'; import type { AlertStore } from '../types/alert-types.js'; import type { BaseAction } from '../types/redux-types'; +import { syncedMetadataNames } from '../types/synced-metadata-types.js'; -function reduceAlertStore(state: AlertStore, action: BaseAction): AlertStore { +type ReduceAlertStoreResult = { + +alertStore: AlertStore, + +syncedMetadataStoreOperations: $ReadOnlyArray, +}; + +function getUpdatedAlertStoreResult( + alertStore: AlertStore, +): ReduceAlertStoreResult { + return { + alertStore, + syncedMetadataStoreOperations: [ + { + type: 'replace_synced_metadata_entry', + payload: { + name: syncedMetadataNames.ALERT_STORE, + data: JSON.stringify(alertStore), + }, + }, + ], + }; +} + +function reduceAlertStore( + state: AlertStore, + action: BaseAction, +): ReduceAlertStoreResult { if (action.type === recordAlertActionType) { - return { + return getUpdatedAlertStoreResult({ ...state, alertInfos: { ...state.alertInfos, @@ -20,7 +48,7 @@ lastAlertTime: action.payload.time, }, }, - }; + }); } else if (action.type === incrementColdStartCountActionType) { const newAlertInfos = Object.fromEntries( Object.entries(state.alertInfos).map(([alertType, info]) => [ @@ -32,13 +60,30 @@ ]), ); - return { + return getUpdatedAlertStoreResult({ ...state, alertInfos: newAlertInfos, + }); + } else if (action.type === setClientDBStoreActionType) { + let alertStore = { ...state }; + const alertStoreString = + action.payload.syncedMetadata?.[syncedMetadataNames.ALERT_STORE]; + if (alertStoreString) { + alertStore = { + ...alertStore, + ...JSON.parse(alertStoreString), + }; + } + return { + alertStore, + syncedMetadataStoreOperations: [], }; } - return state; + return { + alertStore: state, + syncedMetadataStoreOperations: [], + }; } export { reduceAlertStore }; diff --git a/lib/reducers/master-reducer.js b/lib/reducers/master-reducer.js --- a/lib/reducers/master-reducer.js +++ b/lib/reducers/master-reducer.js @@ -187,6 +187,8 @@ action, ); + const { alertStore } = reduceAlertStore(state.alertStore, action); + const { syncedMetadataStore, syncedMetadataStoreOperations } = reduceSyncedMetadataStore(state.syncedMetadataStore, action); @@ -248,7 +250,7 @@ action, threadStore, ), - alertStore: reduceAlertStore(state.alertStore, action), + alertStore, lifecycleState: reduceLifecycleState(state.lifecycleState, action), enabledApps, reportStore, diff --git a/lib/types/synced-metadata-types.js b/lib/types/synced-metadata-types.js --- a/lib/types/synced-metadata-types.js +++ b/lib/types/synced-metadata-types.js @@ -11,6 +11,7 @@ STORE_VERSION: 'store_version', ENABLED_APPS: 'enabled_apps', GLOBAL_THEME_INFO: 'global_theme_info', + ALERT_STORE: 'alert_store', }); type SyncedMetadataName = $Values;