-
Notifications
You must be signed in to change notification settings - Fork 180
/
WalletViewsSwitch.tsx
163 lines (153 loc) · 4.85 KB
/
WalletViewsSwitch.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { ArrowBackIcon } from '@chakra-ui/icons'
import {
Flex,
IconButton,
Modal,
ModalCloseButton,
ModalContent,
ModalOverlay,
useToast,
} from '@chakra-ui/react'
import { AnimatePresence } from 'framer-motion'
import { useCallback, useEffect, useMemo } from 'react'
import { useTranslate } from 'react-polyglot'
import { Route, Switch, useHistory, useLocation, useRouteMatch } from 'react-router-dom'
import { SlideTransition } from 'components/SlideTransition'
import { WalletActions } from 'context/WalletProvider/actions'
import { useWallet } from 'hooks/useWallet/useWallet'
import { localWalletSlice } from 'state/slices/localWalletSlice/localWalletSlice'
import { store } from 'state/store'
import { SUPPORTED_WALLETS } from './config'
import { SelectModal } from './SelectModal'
const arrowBackIcon = <ArrowBackIcon />
export const WalletViewsSwitch = () => {
const history = useHistory()
const location = useLocation()
const toast = useToast()
const translate = useTranslate()
const match = useRouteMatch('/')
const {
state: {
wallet,
modal,
showBackButton,
initialRoute,
modalType,
disconnectOnCloseModal,
deviceState: { disposition },
},
dispatch,
disconnect,
} = useWallet()
const cancelWalletRequests = useCallback(async () => {
await wallet?.cancel().catch(e => {
console.error(e)
toast({
title: translate('common.error'),
description: e?.message ?? translate('common.somethingWentWrong'),
status: 'error',
isClosable: true,
})
})
}, [toast, translate, wallet])
const onClose = useCallback(async () => {
if (disposition === 'initializing' || disposition === 'recovering') {
await wallet?.cancel()
disconnect()
store.dispatch(localWalletSlice.actions.clearLocalWallet())
dispatch({ type: WalletActions.OPEN_KEEPKEY_DISCONNECT })
} else {
history.replace('/')
if (disconnectOnCloseModal) {
disconnect()
dispatch({ type: WalletActions.RESET_STATE })
store.dispatch(localWalletSlice.actions.clearLocalWallet())
} else {
dispatch({ type: WalletActions.SET_WALLET_MODAL, payload: false })
}
await cancelWalletRequests()
}
}, [
cancelWalletRequests,
disconnect,
disconnectOnCloseModal,
dispatch,
disposition,
history,
wallet,
])
const handleBack = useCallback(async () => {
history.goBack()
// If we're back at the select wallet modal, remove the initial route
// otherwise clicking the button for the same wallet doesn't do anything
if (history.location.pathname === '/select') {
dispatch({ type: WalletActions.SET_INITIAL_ROUTE, payload: '' })
}
await cancelWalletRequests()
}, [cancelWalletRequests, dispatch, history])
useEffect(() => {
if (initialRoute) {
history.push(initialRoute)
}
}, [history, initialRoute])
/**
* Memoize the routes list to avoid unnecessary re-renders unless the wallet changes
*/
const walletRoutesList = useMemo(
() =>
modalType
? SUPPORTED_WALLETS[modalType].routes.map(route => {
const Component = route.component
return !Component ? null : (
<Route
exact
key={'route'}
path={route.path}
// we need to pass an arg here, so we need an anonymous function wrapper
// eslint-disable-next-line react-memo/require-usememo
render={routeProps => <Component {...routeProps} />}
/>
)
})
: [],
[modalType],
)
const renderSelectModal = useCallback(() => <SelectModal />, [])
return (
<>
<Modal
isOpen={modal}
onClose={onClose}
isCentered
trapFocus={false}
closeOnOverlayClick={false}
>
<ModalOverlay />
<ModalContent justifyContent='center' px={3} pt={3} pb={6}>
<Flex justifyContent='space-between' alignItems='center' position='relative'>
{!match?.isExact && showBackButton && (
<IconButton
icon={arrowBackIcon}
aria-label={translate('common.back')}
variant='ghost'
fontSize='xl'
size='sm'
isRound
onClick={handleBack}
/>
)}
<ModalCloseButton ml='auto' borderRadius='full' position='static' />
</Flex>
<AnimatePresence mode='wait' initial={false}>
<SlideTransition key={location.key}>
<Switch key={location.pathname} location={location}>
{walletRoutesList}
<Route path={'/'} children={renderSelectModal} />
</Switch>
</SlideTransition>
</AnimatePresence>
</ModalContent>
</Modal>
</>
)
}