A Metamask fork with Infura removed and default networks editable
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
ciphermask/ui/app/ducks/alerts/unconnected-account.js

90 lines
2.2 KiB

import { createSlice } from '@reduxjs/toolkit'
import { captureException } from '@sentry/browser'
import actionConstants from '../../store/actionConstants'
import { addPermittedAccount } from '../../store/actions'
import { getOriginOfCurrentTab, getSelectedAddress } from '../../selectors/selectors'
// Constants
export const ALERT_STATE = {
CLOSED: 'CLOSED',
ERROR: 'ERROR',
LOADING: 'LOADING',
OPEN: 'OPEN',
}
const name = 'unconnectedAccount'
const initialState = {
state: ALERT_STATE.CLOSED,
}
// Slice (reducer plus auto-generated actions and action creators)
const slice = createSlice({
name,
initialState,
reducers: {
connectAccountFailed: (state) => {
state.state = ALERT_STATE.ERROR
},
connectAccountRequested: (state) => {
state.state = ALERT_STATE.LOADING
},
connectAccountSucceeded: (state) => {
state.state = ALERT_STATE.CLOSED
},
dismissAlert: (state) => {
state.state = ALERT_STATE.CLOSED
},
switchedToUnconnectedAccount: (state) => {
state.state = ALERT_STATE.OPEN
},
},
extraReducers: {
[actionConstants.SELECTED_ADDRESS_CHANGED]: (state) => {
// close the alert if the account is switched while it's open
if (state.state === ALERT_STATE.OPEN) {
state.state = ALERT_STATE.CLOSED
}
},
},
})
const { actions, reducer } = slice
export default reducer
// Selectors
export const getAlertState = (state) => state[name].state
export const alertIsOpen = (state) => state[name].state !== ALERT_STATE.CLOSED
// Actions / action-creators
export const {
connectAccountFailed,
connectAccountRequested,
connectAccountSucceeded,
dismissAlert,
switchedToUnconnectedAccount,
} = actions
export const connectAccount = () => {
return async (dispatch, getState) => {
const state = getState()
const selectedAddress = getSelectedAddress(state)
const origin = getOriginOfCurrentTab(state)
try {
await dispatch(connectAccountRequested())
await dispatch(addPermittedAccount(origin, selectedAddress))
await dispatch(connectAccountSucceeded())
} catch (error) {
console.error(error)
captureException(error)
await dispatch(connectAccountFailed())
}
}
}