aboutsummaryrefslogtreecommitdiffstats
path: root/packages/website/ts/components/top_bar/provider_picker.tsx
blob: 7937f2e9db68bd740060d5a249b3de6a3aafe4cb (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
import { colors, constants as sharedConstants } from '@0xproject/react-shared';
import { RadioButton, RadioButtonGroup } from 'material-ui/RadioButton';
import * as React from 'react';
import { Blockchain } from 'ts/blockchain';
import { Dispatcher } from 'ts/redux/dispatcher';
import { ProviderType } from 'ts/types';

interface ProviderPickerProps {
    networkId: number;
    injectedProviderName: string;
    providerType: ProviderType;
    onToggleLedgerDialog: () => void;
    dispatcher: Dispatcher;
    blockchain: Blockchain;
}

interface ProviderPickerState {}

export class ProviderPicker extends React.Component<ProviderPickerProps, ProviderPickerState> {
    public render(): React.ReactNode {
        const isLedgerSelected = this.props.providerType === ProviderType.Ledger;
        const menuStyle = {
            padding: 10,
            paddingTop: 15,
            paddingBottom: 15,
        };
        // Show dropdown with two options
        return (
            <div style={{ width: 225, overflow: 'hidden' }}>
                <RadioButtonGroup name="provider" defaultSelected={this.props.providerType}>
                    <RadioButton
                        onClick={this._onProviderRadioChanged.bind(this, ProviderType.Injected)}
                        style={{ ...menuStyle, backgroundColor: !isLedgerSelected && colors.grey50 }}
                        value={ProviderType.Injected}
                        label={this._renderLabel(this.props.injectedProviderName, !isLedgerSelected)}
                    />
                    <RadioButton
                        onClick={this._onProviderRadioChanged.bind(this, ProviderType.Ledger)}
                        style={{ ...menuStyle, backgroundColor: isLedgerSelected && colors.grey50 }}
                        value={ProviderType.Ledger}
                        label={this._renderLabel('Ledger Nano S', isLedgerSelected)}
                    />
                </RadioButtonGroup>
            </div>
        );
    }
    private _renderLabel(title: string, shouldShowNetwork: boolean): React.ReactNode {
        const label = (
            <div className="flex">
                <div style={{ fontSize: 14 }}>{title}</div>
                {shouldShowNetwork && this._renderNetwork()}
            </div>
        );
        return label;
    }
    private _renderNetwork(): React.ReactNode {
        const networkName = sharedConstants.NETWORK_NAME_BY_ID[this.props.networkId];
        return (
            <div className="flex" style={{ marginTop: 1 }}>
                <div className="relative" style={{ width: 14, paddingLeft: 14 }}>
                    <img
                        src={`/images/network_icons/${networkName.toLowerCase()}.png`}
                        className="absolute"
                        style={{ top: 6, width: 10 }}
                    />
                </div>
                <div style={{ color: colors.lightGrey, fontSize: 11 }}>{networkName}</div>
            </div>
        );
    }
    private _onProviderRadioChanged(value: string): void {
        if (value === ProviderType.Ledger) {
            this.props.onToggleLedgerDialog();
        } else {
            // tslint:disable-next-line:no-floating-promises
            this.props.blockchain.updateProviderToInjectedAsync();
        }
    }
}