This repository has been archived by the owner on Jul 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Saved.tsx
153 lines (141 loc) · 5.51 KB
/
Saved.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
153
// Copyright 2018-2019 @paritytech/substrate-light-ui 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 { Balance } from '@polkadot/types';
import { SingleAddress, SubjectInfo } from '@polkadot/ui-keyring/observable/types';
import accountObservable from '@polkadot/ui-keyring/observable/accounts';
import addressObservable from '@polkadot/ui-keyring/observable/addresses';
import { ApiContext, Subscribe } from '@substrate/ui-api';
import { AddressSummary, BalanceDisplay, Grid, Margin, NavLink, Stacked, StackedHorizontal, SubHeader, WalletCard, WithSpace } from '@substrate/ui-components';
import React from 'react';
import { Link, RouteComponentProps } from 'react-router-dom';
import { map } from 'rxjs/operators';
import { MatchParams } from './types';
interface Props extends RouteComponentProps<MatchParams> {
}
export class Saved extends React.PureComponent<Props> {
static contextType = ApiContext;
context!: React.ContextType<typeof ApiContext>; // http://bit.ly/typescript-and-react-context
render () {
return (
<WalletCard
header='Saved Identities'
overflow='scroll'
subheader='Quickly select an identity that you have previously saved to transfer balance to'>
<Margin top='huge' />
<Grid>
<Grid.Column width={8}>
<Stacked>
<SubHeader> My Unlocked Accounts </SubHeader>
<WithSpace>
{this.renderMyAccounts()}
</WithSpace>
</Stacked>
</Grid.Column>
<Grid.Column width={8}>
<Stacked justify='flex-start'>
<SubHeader> Saved Addresses </SubHeader>
<WithSpace>
{this.renderMyAddresses()}
</WithSpace>
</Stacked>
</Grid.Column>
</Grid>
</WalletCard>
);
}
renderBalance = (balance: Balance) => {
return (
<BalanceDisplay balance={balance} />
);
}
renderEmpty () {
const { match: { params: { currentAddress } } } = this.props;
return (
<React.Fragment>
<p> It looks like you don't have any saved accounts. You can add them from your address book in Identity app. </p>
<NavLink to={`/identity/${currentAddress}`}> Take me there </NavLink>
</React.Fragment>
);
}
renderMyAccounts () {
const { api } = this.context;
const { match: { params: { currentAddress } } } = this.props;
return (
<Subscribe>
{accountObservable.subject.pipe(
map((allAccounts: SubjectInfo) =>
!allAccounts
? this.renderEmpty()
: Object.values(allAccounts).map((account: SingleAddress) =>
<React.Fragment key={account.json.address}>
<Margin top />
<AddressSummary
address={account.json.address}
name={
<Link to={{
pathname: `/transfer/${currentAddress}`,
state: {
recipientAddress: account.json.address
}
}}>
{account.json.meta.name}
</Link>
}
orientation='horizontal'
size='small'
/>
<Subscribe>
{
// FIXME using any because freeBalance gives a Codec here, not a Balance
// Wait for @polkadot/api to have TS support for all query.*
api.query.balances.freeBalance(account.json.address).pipe(map(this.renderBalance as any))
}
</Subscribe>
</React.Fragment>
)
))}
</Subscribe>
);
}
renderMyAddresses () {
const { api } = this.context;
const { match: { params: { currentAddress } } } = this.props;
return (
<Subscribe>
{addressObservable.subject.pipe(
map((allAddresses: SubjectInfo) =>
!Object.keys(allAddresses).length
? this.renderEmpty()
: Object.values(allAddresses).map((address: SingleAddress) =>
<React.Fragment key={`__locked_${address.json.address}`}>
<Margin top />
<StackedHorizontal>
<Link to={{
pathname: `/transfer/${currentAddress}`,
state: {
recipientAddress: address.json.address
}
}}>
<AddressSummary
address={address.json.address}
data-address={address.json.address} // Trick to avoid creating a new React component
name={address.json.meta.name}
orientation='horizontal'
size='small' />
</Link>
<Subscribe>
{
// FIXME using any because freeBalance gives a Codec here, not a Balance
// Wait for @polkadot/api to have TS support for all query.*
api.query.balances.freeBalance(address.json.address).pipe(map(this.renderBalance as any))
}
</Subscribe>
</StackedHorizontal>
</React.Fragment>
)
))}
</Subscribe>
);
}
}