From 827f2349f2f90fc2ede25f03fcd9c7d3f7bd9bf8 Mon Sep 17 00:00:00 2001 From: lukaw3d Date: Thu, 3 Nov 2022 04:53:00 +0100 Subject: [PATCH] Fix switching network and opening a second tab Wait for tabs to sync, and only skipUnlocking if tabs don't sync. ValidatorList test relied on skipUnlocking being called immediately and triggering selectNetwork. --- .../__snapshots__/index.test.tsx.snap | 6 +++--- .../ValidatorList/__tests__/index.test.tsx | 10 +++++++++- src/app/state/network/saga.ts | 18 ++++++++++++------ src/app/state/persist/index.ts | 2 +- src/app/state/persist/selectors.ts | 4 ++++ 5 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/app/pages/StakingPage/Features/ValidatorList/__tests__/__snapshots__/index.test.tsx.snap b/src/app/pages/StakingPage/Features/ValidatorList/__tests__/__snapshots__/index.test.tsx.snap index 6a1174da3e..f0643a3979 100644 --- a/src/app/pages/StakingPage/Features/ValidatorList/__tests__/__snapshots__/index.test.tsx.snap +++ b/src/app/pages/StakingPage/Features/ValidatorList/__tests__/__snapshots__/index.test.tsx.snap @@ -746,7 +746,7 @@ exports[` list should match snapshot 1`] = ` - TEST + ROSE @@ -808,7 +808,7 @@ exports[` list should match snapshot 1`] = ` - TEST + ROSE @@ -870,7 +870,7 @@ exports[` list should match snapshot 1`] = ` - TEST + ROSE diff --git a/src/app/pages/StakingPage/Features/ValidatorList/__tests__/index.test.tsx b/src/app/pages/StakingPage/Features/ValidatorList/__tests__/index.test.tsx index b8dcfe4a9c..fa4a134e44 100644 --- a/src/app/pages/StakingPage/Features/ValidatorList/__tests__/index.test.tsx +++ b/src/app/pages/StakingPage/Features/ValidatorList/__tests__/index.test.tsx @@ -58,7 +58,15 @@ describe('', () => { let store: ReturnType beforeEach(() => { - store = configureAppStore() + store = configureAppStore({ + network: { + chainContext: '', + epoch: 300, + selectedNetwork: 'mainnet', + ticker: 'ROSE', + minimumStakingAmount: 100, + }, + }) jest.mocked(NodeInternal).mockReturnValue({ async stakingAccount(query: any) { diff --git a/src/app/state/network/saga.ts b/src/app/state/network/saga.ts index 8451c9011b..e568cb0cf3 100644 --- a/src/app/state/network/saga.ts +++ b/src/app/state/network/saga.ts @@ -1,9 +1,10 @@ import * as oasis from '@oasisprotocol/client' import { PayloadAction } from '@reduxjs/toolkit' import { persistActions } from 'app/state/persist' -import { selectNeedsPassword } from 'app/state/persist/selectors' +import { selectSkipUnlockingOnInit } from 'app/state/persist/selectors' import { config } from 'config' -import { all, call, put, select, takeLatest } from 'typed-redux-saga' +import { RECEIVE_INIT_STATE } from 'redux-state-sync' +import { all, call, delay, put, race, select, take, takeLatest } from 'typed-redux-saga' import { backend, backendApi } from 'vendors/backend' import { networkActions } from '.' @@ -59,14 +60,19 @@ export function* networkSaga() { put(networkActions.selectNetwork(process.env.REACT_APP_LOCALNET ? 'local' : 'mainnet')), ) yield* takeLatest(persistActions.resetRootState, function* () { - const needsPassword = yield* select(selectNeedsPassword) - if (!needsPassword) { + const skipUnlockOnInit = yield* select(selectSkipUnlockingOnInit) + if (skipUnlockOnInit) { yield* put(persistActions.skipUnlocking()) } }) - const needsPassword = yield* select(selectNeedsPassword) - if (!needsPassword) { + // Wait for tabs to sync state. >5ms should be enough. + const maybeSynced = yield* race({ + tabsSynced: take(RECEIVE_INIT_STATE), + thereAreNoOtherTabs: delay(50), + }) + const skipUnlockOnInit = yield* select(selectSkipUnlockingOnInit) + if (!maybeSynced.tabsSynced && skipUnlockOnInit) { yield* put(persistActions.skipUnlocking()) } } diff --git a/src/app/state/persist/index.ts b/src/app/state/persist/index.ts index f73b029823..b9a9fca4ae 100644 --- a/src/app/state/persist/index.ts +++ b/src/app/state/persist/index.ts @@ -26,7 +26,7 @@ export function getInitialState(): PersistState { // Disable persistence if tabs would override each other. isPersistenceUnsupported: needsSyncingTabs && !isSyncingTabsSupported, loading: false, - stringifiedEncryptionKey: localStorage.getItem(STORAGE_FIELD) ? undefined : 'skipped', + stringifiedEncryptionKey: undefined, enteredWrongPassword: false, } } diff --git a/src/app/state/persist/selectors.ts b/src/app/state/persist/selectors.ts index e3e6fb824c..650e78429f 100644 --- a/src/app/state/persist/selectors.ts +++ b/src/app/state/persist/selectors.ts @@ -11,6 +11,10 @@ export const selectNeedsPassword = createSelector( [selectSlice], state => state.hasPersistedProfiles && !state.stringifiedEncryptionKey, ) +export const selectSkipUnlockingOnInit = createSelector( + [selectSlice], + state => !state.hasPersistedProfiles && !state.stringifiedEncryptionKey, +) export const selectHasPersistedProfiles = createSelector([selectSlice], state => state.hasPersistedProfiles) export const selectIsPersistenceUnsupported = createSelector( [selectSlice],