1
1
import { createSlice , createSelector } from "@reduxjs/toolkit"
2
2
import Emittery from "emittery"
3
3
import { AddressOnNetwork } from "../accounts"
4
- import { ETHEREUM } from "../constants"
4
+ import { ETHEREUM , TEST_NETWORK_BY_CHAIN_ID } from "../constants"
5
5
import { AnalyticsEvent , OneTimeAnalyticsEvent } from "../lib/posthog"
6
6
import { EVMNetwork } from "../networks"
7
7
import { AnalyticsPreferences , DismissableItem } from "../services/preferences"
@@ -11,18 +11,25 @@ import { AccountState, addAddressNetwork } from "./accounts"
11
11
import { createBackgroundAsyncThunk } from "./utils"
12
12
import { UNIXTime } from "../types"
13
13
import { DEFAULT_AUTOLOCK_INTERVAL } from "../services/preferences/defaults"
14
+ import type { RootState } from "."
15
+ import {
16
+ CampaignIds ,
17
+ Campaigns ,
18
+ FilterCampaignsById ,
19
+ } from "../services/campaign/types"
14
20
15
21
export const defaultSettings = {
16
22
hideDust : false ,
17
23
defaultWallet : false ,
18
- showTestNetworks : false ,
24
+ showTestNetworks : true ,
19
25
showNotifications : undefined ,
20
26
collectAnalytics : false ,
21
27
showAnalyticsNotification : false ,
22
28
showUnverifiedAssets : false ,
23
29
hideBanners : false ,
24
30
useFlashbots : false ,
25
31
autoLockInterval : DEFAULT_AUTOLOCK_INTERVAL ,
32
+ campaigns : { } ,
26
33
}
27
34
28
35
export type UIState = {
@@ -47,6 +54,16 @@ export type UIState = {
47
54
routeHistoryEntries ?: Partial < Location > [ ]
48
55
slippageTolerance : number
49
56
accountSignerSettings : AccountSignerSettings [ ]
57
+ // Active user campaigns
58
+ campaigns : {
59
+ /**
60
+ * Some hash used to invalidate cached data and update UI
61
+ */
62
+ [ campaignId in CampaignIds ] ?: FilterCampaignsById <
63
+ Campaigns ,
64
+ campaignId
65
+ > [ "data" ]
66
+ }
50
67
}
51
68
52
69
export type Events = {
@@ -63,6 +80,8 @@ export type Events = {
63
80
updateAnalyticsPreferences : Partial < AnalyticsPreferences >
64
81
addCustomNetworkResponse : [ string , boolean ]
65
82
updateAutoLockInterval : number
83
+ toggleShowTestNetworks : boolean
84
+ clearNotification : string
66
85
}
67
86
68
87
export const emitter = new Emittery < Events > ( )
@@ -78,6 +97,7 @@ export const initialState: UIState = {
78
97
snackbarMessage : "" ,
79
98
slippageTolerance : 0.01 ,
80
99
accountSignerSettings : [ ] ,
100
+ campaigns : { } ,
81
101
}
82
102
83
103
const uiSlice = createSlice ( {
@@ -222,6 +242,16 @@ const uiSlice = createSlice({
222
242
...state ,
223
243
settings : { ...state . settings , autoLockInterval : payload } ,
224
244
} ) ,
245
+ updateCampaignsState : (
246
+ immerState : UIState ,
247
+ {
248
+ payload,
249
+ } : {
250
+ payload : UIState [ "campaigns" ]
251
+ } ,
252
+ ) => {
253
+ immerState . campaigns = payload
254
+ } ,
225
255
} ,
226
256
} )
227
257
@@ -246,6 +276,7 @@ export const {
246
276
setSlippageTolerance,
247
277
setAccountsSignerSettings,
248
278
setAutoLockInterval,
279
+ updateCampaignsState,
249
280
} = uiSlice . actions
250
281
251
282
export default uiSlice . reducer
@@ -273,6 +304,13 @@ export const deleteAnalyticsData = createBackgroundAsyncThunk(
273
304
} ,
274
305
)
275
306
307
+ export const clearNotification = createBackgroundAsyncThunk (
308
+ "ui/clearNotification" ,
309
+ async ( id : string ) => {
310
+ await emitter . emit ( "clearNotification" , id )
311
+ } ,
312
+ )
313
+
276
314
// Async thunk to bubble the setNewDefaultWalletValue action from store to emitter.
277
315
export const setNewDefaultWalletValue = createBackgroundAsyncThunk (
278
316
"ui/setNewDefaultWalletValue" ,
@@ -360,6 +398,22 @@ export const setSelectedNetwork = createBackgroundAsyncThunk(
360
398
} ,
361
399
)
362
400
401
+ export const toggleShowTestNetworks = createBackgroundAsyncThunk (
402
+ "ui/toggleShowTestNetworks" ,
403
+ async ( updatedValue : boolean , { dispatch, getState } ) => {
404
+ const state = getState ( ) as RootState
405
+
406
+ const currentNetwork = state . ui . selectedAccount . network
407
+
408
+ // If user is on one of the built-in test networks, don't leave them stranded
409
+ if ( ! updatedValue && TEST_NETWORK_BY_CHAIN_ID . has ( currentNetwork . chainID ) ) {
410
+ dispatch ( setSelectedNetwork ( ETHEREUM ) )
411
+ }
412
+
413
+ await emitter . emit ( "toggleShowTestNetworks" , updatedValue )
414
+ } ,
415
+ )
416
+
363
417
export const refreshBackgroundPage = createBackgroundAsyncThunk (
364
418
"ui/refreshBackgroundPage" ,
365
419
async ( ) => {
0 commit comments