Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force-toggle testnets visibility in network switcher #3793

Merged
merged 3 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions background/redux-slices/ui.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createSlice, createSelector } from "@reduxjs/toolkit"
import Emittery from "emittery"
import { AddressOnNetwork } from "../accounts"
import { ETHEREUM } from "../constants"
import { ETHEREUM, TEST_NETWORK_BY_CHAIN_ID } from "../constants"
import { AnalyticsEvent, OneTimeAnalyticsEvent } from "../lib/posthog"
import { EVMNetwork } from "../networks"
import { AnalyticsPreferences, DismissableItem } from "../services/preferences"
Expand All @@ -11,11 +11,12 @@ import { AccountState, addAddressNetwork } from "./accounts"
import { createBackgroundAsyncThunk } from "./utils"
import { UNIXTime } from "../types"
import { DEFAULT_AUTOLOCK_INTERVAL } from "../services/preferences/defaults"
import type { RootState } from "."

export const defaultSettings = {
hideDust: false,
defaultWallet: false,
showTestNetworks: false,
showTestNetworks: true,
showNotifications: undefined,
collectAnalytics: false,
showAnalyticsNotification: false,
Expand Down Expand Up @@ -77,6 +78,7 @@ export type Events = {
updateAnalyticsPreferences: Partial<AnalyticsPreferences>
addCustomNetworkResponse: [string, boolean]
updateAutoLockInterval: number
toggleShowTestNetworks: boolean
}

export const emitter = new Emittery<Events>()
Expand Down Expand Up @@ -392,6 +394,22 @@ export const setSelectedNetwork = createBackgroundAsyncThunk(
},
)

export const toggleShowTestNetworks = createBackgroundAsyncThunk(
"ui/toggleShowTestNetworks",
async (value: boolean, { dispatch, getState }) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
async (value: boolean, { dispatch, getState }) => {
async (updatedValue: boolean, { dispatch, getState }) => {

Perhaps for clarity? Or even updatedShowTestNetworksValue or similar?

const state = getState() as RootState

const currentNetwork = state.ui.selectedAccount.network

// If user is on one of the built-in test networks, don't leave them stranded
if (TEST_NETWORK_BY_CHAIN_ID.has(currentNetwork.chainID)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also guard on !value right? Like, if the user is toggling from off to on, we shouldn't push them into Ethereum—in theory they wouldn't be there anyway because they wouldn't be on a test network, but in fact they might be if they've added the testnet as a custom chain, for example.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I think this should also happen if the production build has the testnet as a built in network that's still under a feature flag.

dispatch(setSelectedNetwork(ETHEREUM))
}

await emitter.emit("toggleShowTestNetworks", value)
},
)

export const refreshBackgroundPage = createBackgroundAsyncThunk(
"ui/refreshBackgroundPage",
async () => {
Expand Down
24 changes: 24 additions & 0 deletions background/services/preferences/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export type Preferences = {
analytics: AnalyticsPreferences
autoLockInterval: UNIXTime
shouldShowNotifications: boolean
showTestNetworks: boolean
}

/**
Expand Down Expand Up @@ -439,6 +440,19 @@ export class PreferenceDatabase extends Dexie {
}),
)

this.version(22).upgrade((tx) =>
tx
.table("preferences")
.toCollection()
.modify((storedPreferences: Omit<Preferences, "showTestNetworks">) => {
const update: Partial<Preferences> = {
showTestNetworks: true,
}

Object.assign(storedPreferences, update)
}),
)

// This is the old version for populate
// https://dexie.org/docs/Dexie/Dexie.on.populate-(old-version)
// The this does not behave according the new docs, but works
Expand Down Expand Up @@ -468,6 +482,16 @@ export class PreferenceDatabase extends Dexie {
})
}

async setShowTestNetworks(newValue: boolean): Promise<void> {
await this.preferences
.toCollection()
.modify((storedPreferences: Preferences) => {
const update: Partial<Preferences> = { showTestNetworks: newValue }

Object.assign(storedPreferences, update)
})
}

async setShouldShowNotifications(newValue: boolean): Promise<void> {
await this.preferences
.toCollection()
Expand Down
1 change: 1 addition & 0 deletions background/services/preferences/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const defaultPreferences = {
},
autoLockInterval: DEFAULT_AUTOLOCK_INTERVAL,
shouldShowNotifications: false,
showTestNetworks: true,
}

export default defaultPreferences
14 changes: 14 additions & 0 deletions background/services/preferences/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ interface Events extends ServiceLifecycleEvents {
initializeSelectedAccount: AddressOnNetwork
initializeShownDismissableItems: DismissableItem[]
initializeNotificationsPreferences: boolean
initializeShowTestNetworks: boolean
updateAnalyticsPreferences: AnalyticsPreferences
addressBookEntryModified: AddressBookEntry
updatedSignerSettings: AccountSignerSettings[]
Expand Down Expand Up @@ -162,6 +163,11 @@ export default class PreferenceService extends BaseService<Events> {
"initializeNotificationsPreferences",
await this.getShouldShowNotificationsPreferences(),
)

this.emitter.emit(
"initializeShowTestNetworks",
await this.getShowTestNetworks(),
)
}

protected override async internalStopService(): Promise<void> {
Expand Down Expand Up @@ -290,6 +296,14 @@ export default class PreferenceService extends BaseService<Events> {
return (await this.db.getPreferences())?.currency
}

async setShowTestNetworks(value: boolean): Promise<void> {
await this.db.setShowTestNetworks(value)
}

async getShowTestNetworks(): Promise<boolean> {
return (await this.db.getPreferences()).showTestNetworks
}

async getTokenListPreferences(): Promise<TokenListPreferences> {
return (await this.db.getPreferences())?.tokenLists
}
Expand Down
18 changes: 14 additions & 4 deletions background/services/redux/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import {
dismissableItemMarkedAsShown,
MezoClaimStatus,
updateCampaignState,
toggleTestNetworks,
} from "../../redux-slices/ui"
import {
estimatedFeesPerGas,
Expand Down Expand Up @@ -1354,16 +1355,25 @@ export default class ReduxService extends BaseService<never> {
},
)

this.preferenceService.emitter.on(
"initializeShowTestNetworks",
async (showTestNetworks: boolean) => {
await this.store.dispatch(toggleTestNetworks(showTestNetworks))
},
)

uiSliceEmitter.on("toggleShowTestNetworks", async (value) => {
await this.preferenceService.setShowTestNetworks(value)
await this.store.dispatch(toggleTestNetworks(value))
})

this.preferenceService.emitter.on(
"initializeSelectedAccount",
async (dbAddressNetwork: AddressOnNetwork) => {
if (dbAddressNetwork) {
// Wait until chain service starts and populates supported networks
await this.chainService.started()
// TBD: naming the normal reducer and async thunks
// Initialize redux from the db
// !!! Important: this action belongs to a regular reducer.
// NOT to be confused with the setNewCurrentAddress asyncThunk

const { address, network } = dbAddressNetwork
let supportedNetwork = this.chainService.supportedNetworks.find(
(net) => sameNetwork(network, net),
Expand Down
8 changes: 4 additions & 4 deletions ui/pages/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
selectShowNotifications,
setShouldShowNotifications,
selectShowTestNetworks,
toggleTestNetworks,
toggleHideBanners,
toggleShowTestNetworks,
selectHideBanners,
selectShowUnverifiedAssets,
toggleShowUnverifiedAssets,
Expand Down Expand Up @@ -186,8 +186,8 @@ export default function Settings(): ReactElement {
)
}

const toggleShowTestNetworks = (defaultWalletValue: boolean) => {
dispatch(toggleTestNetworks(defaultWalletValue))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably kill this old action in the UI slice yeah?

Copy link
Collaborator Author

@hyphenized hyphenized Mar 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait this old action is still used to update the "cached" store value 👀 (in the redux service)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh I see. Ok, so we have moved from, “if you want to toggle test networks you update directly in redux” to “if you want to toggle test networks you thunk in redux, which updates the preference service, which then updates in redux”, and toggleShowTestNetworks is the thunk and toggleTestNetworks is the direct state update.

Ok well that's clear as mud as in other cases that we've done this heh, and we have the same problem in the whole slice. We should figure out a clearer way to name these IMO—something like setShow... and updateShow... or something—and we should clearly document at the top of the slice.

This isn't a blocker though, just going to continue to be a pain point. The other option is abstracting preference settings a little more, but I remember I did a deep pass at that a couple of years ago and the typing is (or was) a near impossibility.

const toggleShowTestnets = (defaultWalletValue: boolean) => {
dispatch(toggleShowTestNetworks(defaultWalletValue))
}

const toggleShowUnverified = (toggleValue: boolean) => {
Expand Down Expand Up @@ -243,7 +243,7 @@ export default function Settings(): ReactElement {
title: t("settings.enableTestNetworks"),
component: () => (
<SharedToggleButton
onChange={(toggleValue) => toggleShowTestNetworks(toggleValue)}
onChange={(toggleValue) => toggleShowTestnets(toggleValue)}
value={showTestNetworks}
/>
),
Expand Down