diff --git a/src/core/state/userChains/index.ts b/src/core/state/userChains/index.ts index 3d16b13a3f..e3f0dbfda3 100644 --- a/src/core/state/userChains/index.ts +++ b/src/core/state/userChains/index.ts @@ -2,6 +2,7 @@ import create from 'zustand'; import { SUPPORTED_MAINNET_CHAINS } from '~/core/references/chains'; import { ChainId } from '~/core/types/chains'; +import { persistOptions } from '~/core/utils/persistOptions'; import { createStore } from '../internal/createStore'; import { withSelectors } from '../internal/withSelectors'; @@ -38,7 +39,7 @@ export interface UserChainsState { removeUserChain: ({ chainId }: { chainId: ChainId }) => void; } -const chains = SUPPORTED_MAINNET_CHAINS.reduce( +const initialChains = SUPPORTED_MAINNET_CHAINS.reduce( (acc, chain) => ({ ...acc, [chain.id]: true, @@ -46,12 +47,14 @@ const chains = SUPPORTED_MAINNET_CHAINS.reduce( {} as Record, ); -const userChainsOrder = Object.keys(chains).map((id) => Number(id) as number); +const initialUserChainsOrder = SUPPORTED_MAINNET_CHAINS.map( + (id) => Number(id) as number, +); export const userChainsStore = createStore( (set, get) => ({ - userChains: chains, - userChainsOrder, + userChains: initialChains, + userChainsOrder: initialUserChainsOrder, updateUserChains: ({ chainIds, enabled }) => { const { userChains } = get(); const chainsUpdated = chainIds.reduce( @@ -108,10 +111,76 @@ export const userChainsStore = createStore( }, }), { - persist: { + persist: persistOptions({ name: 'userChains', - version: 5, - }, + version: 6, + migrations: [ + // previous naive migrations reset user custom networks and ordering + function v1(state: UserChainsState) { + return { + ...state, + userChains: initialChains, + userChainsOrder: initialUserChainsOrder, + }; + }, + + function v2(state: UserChainsState) { + return { + ...state, + userChains: initialChains, + userChainsOrder: initialUserChainsOrder, + }; + }, + + function v3(state: UserChainsState) { + return { + ...state, + userChains: initialChains, + userChainsOrder: initialUserChainsOrder, + }; + }, + + function v4(state: UserChainsState) { + return { + ...state, + userChains: initialChains, + userChainsOrder: initialUserChainsOrder, + }; + }, + + // v5 adds apechain support + function v5(state: UserChainsState) { + // previous october migration #1738 inadvertenly reset state + // this will only apply to users who have not yet migrated to v5 + return { + ...state, + userChains: { ...state.userChains, [ChainId.apechain]: true }, + userChainsOrder: state.userChainsOrder.includes(ChainId.apechain) + ? state.userChainsOrder + : [...state.userChainsOrder, ChainId.apechain], + }; + }, + + // v6 adds apechainCurtis, ink, inkSepolia support + function v6(state: UserChainsState) { + const newChains = [ + ChainId.apechainCurtis, + ChainId.ink, + ChainId.inkSepolia, + ]; + return { + ...state, + userChains: { + ...state.userChains, + ...Object.fromEntries(newChains.map((id) => [id, true])), + }, + userChainsOrder: state.userChainsOrder.concat( + newChains.filter((id) => !state.userChainsOrder.includes(id)), + ), + }; + }, + ], + }), }, );