aboutsummaryrefslogtreecommitdiffstats
path: root/packages/website/ts/components/top_bar/provider_display.tsx
blob: cbbd39c70e796950bec41a7c92d5b145f479d6fc (plain) (blame)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { Styles } from '@0xproject/react-shared';
import * as _ from 'lodash';
import CircularProgress from 'material-ui/CircularProgress';
import RaisedButton from 'material-ui/RaisedButton';
import ActionAccountBalanceWallet from 'material-ui/svg-icons/action/account-balance-wallet';
import Lock from 'material-ui/svg-icons/action/lock';
import * as React from 'react';

import { Blockchain } from 'ts/blockchain';
import { ProviderPicker } from 'ts/components/top_bar/provider_picker';
import { Circle } from 'ts/components/ui/circle';
import { Container } from 'ts/components/ui/container';
import { DropDown } from 'ts/components/ui/drop_down';
import { Identicon } from 'ts/components/ui/identicon';
import { Image } from 'ts/components/ui/image';
import { Island } from 'ts/components/ui/island';
import { Text } from 'ts/components/ui/text';
import { Dispatcher } from 'ts/redux/dispatcher';
import { colors } from 'ts/style/colors';
import { AccountState, ProviderType } from 'ts/types';
import { constants } from 'ts/utils/constants';
import { utils } from 'ts/utils/utils';

const ROOT_HEIGHT = 24;

export interface ProviderDisplayProps {
    dispatcher: Dispatcher;
    userAddress: string;
    networkId: number;
    injectedProviderName: string;
    providerType: ProviderType;
    onToggleLedgerDialog: () => void;
    blockchain?: Blockchain;
    blockchainIsLoaded: boolean;
}

interface ProviderDisplayState {}

const styles: Styles = {
    root: {
        height: ROOT_HEIGHT,
        borderRadius: ROOT_HEIGHT,
    },
};

export class ProviderDisplay extends React.Component<ProviderDisplayProps, ProviderDisplayState> {
    public render(): React.ReactNode {
        const isExternallyInjectedProvider = utils.isExternallyInjected(
            this.props.providerType,
            this.props.injectedProviderName,
        );
        const hoverActiveNode = (
            <Island className="flex items-center py1 px2" style={styles.root}>
                {this._renderIcon()}
                <Container marginLeft="12px" marginRight="12px">
                    {this._renderDisplayMessage()}
                </Container>
                {this._renderInjectedProvider()}
            </Island>
        );
        const hasLedgerProvider = this.props.providerType === ProviderType.Ledger;
        const horizontalPosition = isExternallyInjectedProvider || hasLedgerProvider ? 'left' : 'middle';
        return (
            <div style={{ width: 'fit-content', height: 48, float: 'right' }}>
                <DropDown
                    hoverActiveNode={hoverActiveNode}
                    popoverContent={this.renderPopoverContent(isExternallyInjectedProvider, hasLedgerProvider)}
                    anchorOrigin={{ horizontal: horizontalPosition, vertical: 'bottom' }}
                    targetOrigin={{ horizontal: horizontalPosition, vertical: 'top' }}
                    zDepth={1}
                />
            </div>
        );
    }
    public renderPopoverContent(hasInjectedProvider: boolean, hasLedgerProvider: boolean): React.ReactNode {
        if (!this._isBlockchainReady()) {
            return null;
        } else if (hasInjectedProvider || hasLedgerProvider) {
            return (
                <ProviderPicker
                    dispatcher={this.props.dispatcher}
                    networkId={this.props.networkId}
                    injectedProviderName={this.props.injectedProviderName}
                    providerType={this.props.providerType}
                    onToggleLedgerDialog={this.props.onToggleLedgerDialog}
                    blockchain={this.props.blockchain}
                />
            );
        } else {
            // Nothing to connect to, show install/info popover
            return (
                <div className="px2" style={{ maxWidth: 420 }}>
                    <div className="center h4 py2" style={{ color: colors.grey700 }}>
                        Choose a wallet:
                    </div>
                    <div className="flex pb3">
                        <div className="center px2">
                            <div style={{ color: colors.darkGrey }}>Install a browser wallet</div>
                            <div className="py2">
                                <img src="/images/metamask_or_parity.png" width="135" />
                            </div>
                            <div>
                                Use{' '}
                                <a
                                    href={constants.URL_METAMASK_CHROME_STORE}
                                    target="_blank"
                                    style={{ color: colors.lightBlueA700 }}
                                >
                                    Metamask
                                </a>{' '}
                                or{' '}
                                <a
                                    href={constants.URL_PARITY_CHROME_STORE}
                                    target="_blank"
                                    style={{ color: colors.lightBlueA700 }}
                                >
                                    Parity Signer
                                </a>
                            </div>
                        </div>
                        <div>
                            <div
                                className="pl1 ml1"
                                style={{ borderLeft: `1px solid ${colors.grey300}`, height: 65 }}
                            />
                            <div className="py1">or</div>
                            <div
                                className="pl1 ml1"
                                style={{ borderLeft: `1px solid ${colors.grey300}`, height: 68 }}
                            />
                        </div>
                        <div className="px2 center">
                            <div style={{ color: colors.darkGrey }}>Connect to a ledger hardware wallet</div>
                            <div style={{ paddingTop: 21, paddingBottom: 29 }}>
                                <img src="/images/ledger_icon.png" style={{ width: 80 }} />
                            </div>
                            <div>
                                <RaisedButton
                                    style={{ width: '100%' }}
                                    label="Use Ledger"
                                    onClick={this.props.onToggleLedgerDialog}
                                />
                            </div>
                        </div>
                    </div>
                </div>
            );
        }
    }
    private _renderIcon(): React.ReactNode {
        const accountState = this._getAccountState();
        switch (accountState) {
            case AccountState.Ready:
                return <Identicon address={this.props.userAddress} diameter={ROOT_HEIGHT} />;
            case AccountState.Loading:
                return <CircularProgress size={ROOT_HEIGHT} thickness={2} />;
            case AccountState.Locked:
                return <Lock color={colors.black} />;
            case AccountState.Unconnected:
                return <ActionAccountBalanceWallet color={colors.mediumBlue} />;
            default:
                return null;
        }
    }
    private _renderDisplayMessage(): React.ReactNode {
        const accountState = this._getAccountState();
        const displayMessage = utils.getReadableAccountState(accountState, this.props.userAddress);
        const fontColor = this._getDisplayMessageFontColor();
        return (
            <Text fontSize="16px" fontColor={fontColor} fontWeight={500}>
                {displayMessage}
            </Text>
        );
    }
    private _getDisplayMessageFontColor(): string {
        const accountState = this._getAccountState();
        switch (accountState) {
            case AccountState.Loading:
                return colors.darkGrey;
            case AccountState.Ready:
            case AccountState.Locked:
            case AccountState.Unconnected:
            default:
                return colors.black;
        }
    }
    private _renderInjectedProvider(): React.ReactNode {
        const accountState = this._getAccountState();
        switch (accountState) {
            case AccountState.Ready:
            case AccountState.Locked:
                const circleColor = this._getInjectedProviderColor();
                return (
                    <Container className="flex items-center">
                        <Circle diameter={6} fillColor={circleColor} />
                        <Container marginLeft="6px">
                            <Text fontSize="12px" lineHeight="14px" fontColor={colors.darkGrey}>
                                {this.props.injectedProviderName}
                            </Text>
                        </Container>
                    </Container>
                );
            case AccountState.Unconnected:
            case AccountState.Loading:
            default:
                return null;
        }
    }
    private _getInjectedProviderColor(): string {
        const accountState = this._getAccountState();
        switch (accountState) {
            case AccountState.Ready:
                return colors.green;
            case AccountState.Locked:
            case AccountState.Loading:
            case AccountState.Unconnected:
            default:
                return colors.red;
        }
    }
    private _isBlockchainReady(): boolean {
        return this.props.blockchainIsLoaded && !_.isUndefined(this.props.blockchain);
    }
    private _getAccountState(): AccountState {
        return utils.getAccountState(
            this._isBlockchainReady(),
            this.props.providerType,
            this.props.injectedProviderName,
            this.props.userAddress,
        );
    }
}