-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenable-ethereum-workflow.tsx
More file actions
159 lines (144 loc) · 5.07 KB
/
enable-ethereum-workflow.tsx
File metadata and controls
159 lines (144 loc) · 5.07 KB
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
import React, {useContext} from 'react';
import {EventData} from 'xstate';
import './wallet.scss';
import {Button, Box, Flex, Icon, Text, MetaMaskButton, Flash, Heading} from 'rimble-ui';
import ConnectionBanner from '@rimble/connection-banner';
import RimbleUtils from '@rimble/utils';
import {WorkflowState} from '../workflows/ethereum-enable';
import {WindowContext} from './window-context';
import {CHAIN_NETWORK_ID} from '../config';
import {track} from '../segment-analytics';
interface Props {
current: WorkflowState;
send: (event: any, payload?: EventData | undefined) => WorkflowState;
}
export const EnableEthereum = (props: Props) => {
const {current: currentState, send: _send} = props;
const send = (event: 'USER_APPROVES_ENABLE' | 'USER_REJECTS_ENABLE') => () => {
track(event, {enabledAddress: currentState.context.enabledAddress});
_send(event);
};
const targetNetwork = Number(CHAIN_NETWORK_ID);
const window = useContext(WindowContext);
const chainId = window?.ethereum?.chainId;
const currentNetwork = chainId && Number(chainId);
const metaMaskButton = (disabled, message) => (
<MetaMaskButton.Outline
disabled={disabled}
id="connect-with-metamask-button"
onClick={send('USER_APPROVES_ENABLE')}
>
{message}
</MetaMaskButton.Outline>
);
const NoNetwork = () => (
<div>
<Flash variant={'danger'}>
<Flex alignItems="center" justifyContent="space-between" flexDirection="column">
<Flex alignItems="center" pb={3} flexDirection="column">
<Box>
<Icon name="Warning" size="44" />
</Box>
<Flex flexDirection="column">
<Text fontWeight="bold" color={'inherit'} textAlign="center">
Install the MetaMask browser extension to use our blockchain features in your
current browser
</Text>
</Flex>
</Flex>
<MetaMaskButton as="a" href="https://metamask.io/" target="_blank" color={'white'}>
Install MetaMask
</MetaMaskButton>
</Flex>
</Flash>
</div>
);
const WrongNetwork = () => (
<div>
<Flash variant={'danger'}>
<Flex alignItems="center" flexDirection="column">
<Box>
<Icon name="Warning" size="44" />
</Box>
<Flex flexDirection="column">
<Text fontWeight="bold" color={'inherit'} textAlign="center" pb={3}>
Switch to the {RimbleUtils.getEthNetworkNameById(targetNetwork)} network in MetaMask
</Text>
<Text color={'inherit'} textAlign="center">
To use our blockchain features, you need to be on the{' '}
{RimbleUtils.getEthNetworkNameById(targetNetwork)} network. You are currently on{' '}
{RimbleUtils.getEthNetworkNameById(currentNetwork)}.
</Text>
</Flex>
</Flex>
</Flash>
</div>
);
const NotWeb3Browser = () => (
<div>
<Flash variant={'danger'}>
<Flex alignItems="center" flexDirection="column">
<Box>
<Icon name="Warning" size="44" />
</Box>
<Flex flexDirection="column">
<Text fontWeight="bold" color={'inherit'}>
Your browser does not support our blockchain features
</Text>
{RimbleUtils.isMobileDevice() ? (
<Text color={'inherit'}>
Try a mobile wallet browser like Status, Coinbase wallet or Cipher
</Text>
) : (
<Text color={'inherit'}>
Switch to either Brave, FireFox, Opera, or Chrome to continue
</Text>
)}
</Flex>
</Flex>
</Flash>
</div>
);
const button = () => {
switch (currentState.value.toString()) {
case 'explainToUser':
case 'retry':
return metaMaskButton(false, 'Connect with MetaMask');
case 'enabling':
return metaMaskButton(true, 'Connecting..');
case 'done':
return metaMaskButton(true, 'Connected!');
case 'failure':
return metaMaskButton(true, 'Connection failed :(');
default:
return '';
}
};
const connectionBanner = (
<ConnectionBanner currentNetwork={currentNetwork} requiredNetwork={targetNetwork}>
{{
notWeb3CapableBrowserMessage: <NotWeb3Browser />,
noNetworkAvailableMessage: <NoNetwork />,
onWrongNetworkMessage: <WrongNetwork />
}}
</ConnectionBanner>
);
const prompt = (
<Flex flexDirection="column" alignItems="center">
<Heading>Connect to Blockchain</Heading>
<Text pb={3}>
This app uses state channels. It order to continue you need to connect to the blockchain.
</Text>
<div>{button()}</div>
<div>
<Button.Text onClick={send('USER_REJECTS_ENABLE')}>Cancel</Button.Text>
</div>
</Flex>
);
// need currentNetwork to be defined, and equal to the targetNetwork
return currentNetwork === targetNetwork ? (
prompt
) : (
<div style={{paddingTop: '10px'}}>{connectionBanner}</div>
);
};