/
UriPromptPage.js
147 lines (132 loc) · 4.59 KB
/
UriPromptPage.js
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
// @flow
import type { Node } from 'react';
import { Component } from 'react';
import { observer } from 'mobx-react';
import { computed, observable, runInAction } from 'mobx';
import { intlShape } from 'react-intl';
import StaticTopbarTitle from '../../components/topbar/StaticTopbarTitle';
import TopBar from '../../components/topbar/TopBar';
import TopBarLayout from '../../components/layout/TopBarLayout';
import UriPromptForm from '../../components/profile/uri-prompt/UriPromptForm';
import UriAccept from '../../components/profile/uri-prompt/UriAccept';
import type { InjectedOrGenerated } from '../../types/injectedPropsType';
import TestnetWarningBanner from '../../components/topbar/banners/TestnetWarningBanner';
import ServerErrorBanner from '../../components/topbar/banners/ServerErrorBanner';
import { ServerStatusErrors } from '../../types/serverStatusErrorType';
import registerProtocols from '../../uri-protocols';
import globalMessages from '../../i18n/global-messages';
import type { $npm$ReactIntl$IntlFormat } from 'react-intl';
import type { ServerStatusErrorType } from '../../types/serverStatusErrorType';
import { PublicDeriver } from '../../api/ada/lib/storage/models/PublicDeriver/index';
import { isTestnet } from '../../api/ada/lib/storage/database/prepackaged/networks';
import { trackUriPrompt } from '../../api/analytics';
type GeneratedData = typeof UriPromptPage.prototype.generated;
@observer
export default class UriPromptPage extends Component<InjectedOrGenerated<GeneratedData>> {
@observable
isAccepted: boolean = false;
static contextTypes: {|intl: $npm$ReactIntl$IntlFormat|} = {
intl: intlShape.isRequired,
};
onAccept: void => void = () => {
registerProtocols();
runInAction(() => {
this.isAccepted = true;
});
trackUriPrompt('allow');
};
onSkip: void => void = () => {
this.generated.actions.profile.acceptUriScheme.trigger()
trackUriPrompt('skip');
};
onBack: void => void = () => {
runInAction(() => {
this.isAccepted = false;
});
};
_getContent: (() => Node) = () => {
if (!this.isAccepted) {
return <UriPromptForm
onAccept={this.onAccept}
onSkip={this.onSkip}
classicTheme={this.generated.stores.profile.isClassicTheme}
/>;
}
if (this.isAccepted) {
return <UriAccept
onConfirm={this.generated.actions.profile.acceptUriScheme.trigger}
onBack={this.onBack}
classicTheme={this.generated.stores.profile.isClassicTheme}
/>;
}
throw new Error('UriPromptPage::_getContent Should never happen');
}
render(): Node {
const { checkAdaServerStatus } = this.generated.stores.serverConnectionStore;
const { selected } = this.generated.stores.wallets;
const isWalletTestnet = selected == null
? false
: isTestnet(selected.getParent().getNetworkInfo());
const displayedBanner = checkAdaServerStatus === ServerStatusErrors.Healthy
? <TestnetWarningBanner isTestnet={isWalletTestnet} />
: <ServerErrorBanner errorType={checkAdaServerStatus} />;
const topbarTitle = (
<StaticTopbarTitle title={this.context.intl.formatMessage(globalMessages.uriSchemeLabel)} />
);
const topbarElement = (
<TopBar
title={topbarTitle}
/>);
return (
<TopBarLayout
topbar={topbarElement}
banner={displayedBanner}
>
{this._getContent()}
</TopBarLayout>
);
}
@computed get generated(): {|
actions: {|
profile: {|
acceptUriScheme: {|
trigger: (params: void) => Promise<void>
|}
|}
|},
stores: {|
profile: {| isClassicTheme: boolean |},
serverConnectionStore: {|
checkAdaServerStatus: ServerStatusErrorType
|},
wallets: {| selected: null | PublicDeriver<> |},
|}
|} {
if (this.props.generated !== undefined) {
return this.props.generated;
}
if (this.props.stores == null || this.props.actions == null) {
throw new Error(`${nameof(UriPromptPage)} no way to generated props`);
}
const { stores, actions } = this.props;
const profileStore = stores.profile;
return Object.freeze({
stores: {
profile: {
isClassicTheme: profileStore.isClassicTheme,
},
serverConnectionStore: {
checkAdaServerStatus: stores.serverConnectionStore.checkAdaServerStatus,
},
wallets: {
selected: stores.wallets.selected,
},
},
actions: {
profile: {
acceptUriScheme: { trigger: actions.profile.acceptUriScheme.trigger },
},
},
});
}
}