-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
index.tsx
152 lines (132 loc) · 4.17 KB
/
index.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
// Copyright 2017-2019 @polkadot/app-contracts authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { AppProps, I18nProps } from '@polkadot/react-components/types';
import { TabItem } from '@polkadot/react-components/Tabs';
import { ComponentProps } from './types';
import React from 'react';
import { Route, Switch, RouteComponentProps } from 'react-router';
import { withRouter } from 'react-router-dom';
import { HelpOverlay, Tabs } from '@polkadot/react-components';
import { withMulti, withObservable } from '@polkadot/react-api/hoc';
import keyring from '@polkadot/ui-keyring';
import { SubjectInfo } from '@polkadot/ui-keyring/observable/types';
import introMd from './md/intro.md';
import store from './store';
import translate from './translate';
import Contracts from './Contracts';
import Codes from './Codes';
import Deploy from './Deploy';
interface Props extends AppProps, I18nProps, RouteComponentProps {
accounts: SubjectInfo[];
contracts: SubjectInfo[];
}
interface State {
codeHash?: string;
constructorIndex: number;
hasContracts: boolean;
isDeployOpen: boolean;
updated: number;
}
class ContractsApp extends React.PureComponent<Props, State> {
public state: State = {
constructorIndex: 0,
hasContracts: false,
isDeployOpen: false,
updated: 0
};
constructor (props: Props) {
super(props);
store.on('new-code', this.triggerUpdate);
store.on('removed-code', this.triggerUpdate);
// since we have a dep on the async API, we load here
store.loadAll().catch((): void => {
// noop, handled internally
});
}
public static getDerivedStateFromProps ({ contracts }: Props): Pick<State, never> {
const hasContracts = !!contracts && Object.keys(contracts).length >= 1;
return {
hasContracts
};
}
public render (): React.ReactNode {
const { basePath, t } = this.props;
const { codeHash, constructorIndex, isDeployOpen } = this.state;
const hidden: string[] = [];
return (
<main className='contracts--App'>
<HelpOverlay md={introMd} />
<header>
<Tabs
basePath={basePath}
hidden={hidden}
items={[
{
name: 'code',
text: 'Code'
},
{
isRoot: true,
name: 'contracts',
text: 'Contracts'
}
].map((tab): TabItem => ({ ...tab, text: t(tab.text) }))
}
/>
</header>
<Switch>
<Route path={`${basePath}/code`} render={this.renderComponent(Codes)} />
<Route render={this.renderComponent(Contracts)} exact />
</Switch>
<Deploy
basePath={basePath}
codeHash={codeHash}
constructorIndex={constructorIndex}
isOpen={isDeployOpen}
onClose={this.hideDeploy}
/>
</main>
);
}
private renderComponent (Component: React.ComponentType<ComponentProps>): () => React.ReactNode {
return (): React.ReactNode => {
const { accounts, basePath, contracts, onStatusChange } = this.props;
const { updated } = this.state;
if (!contracts) {
return null;
}
return (
<Component
accounts={accounts}
basePath={basePath}
contracts={contracts}
hasCode={store.hasCode}
onStatusChange={onStatusChange}
showDeploy={this.showDeploy}
updated={updated}
/>
);
};
}
private showDeploy = (codeHash?: string, constructorIndex = 0): () => void =>
(): void => {
this.setState({
codeHash: codeHash || undefined,
constructorIndex,
isDeployOpen: true
});
}
private hideDeploy = (): void => {
this.setState({ isDeployOpen: false });
}
private triggerUpdate = (): void => {
this.setState({ updated: Date.now() });
}
}
export default withMulti(
withRouter(ContractsApp),
translate,
withObservable(keyring.accounts.subject, { propName: 'accounts' }),
withObservable(keyring.contracts.subject, { propName: 'contracts' })
);