PathDetails.tsx 4.97 KB
Newer Older
Hanwen Cheng's avatar
Hanwen Cheng committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity.  If not, see <http://www.gnu.org/licenses/>.

import React from 'react';
import { withNavigation } from 'react-navigation';
import { ScrollView, StyleSheet, View } from 'react-native';
20
21
22
23
24
25
26
27
28
29

import { defaultNetworkKey, UnknownNetworkKeys } from 'constants/networkSpecs';
import testIDs from 'e2e/testIDs';
import { NavigationAccountProps } from 'types/props';
import { withAccountStore } from 'utils/HOC';
import PathCard from 'components/PathCard';
import PopupMenu from 'components/PopupMenu';
import { LeftScreenHeading } from 'components/ScreenHeading';
import colors from 'styles/colors';
import QrView from 'components/QrView';
Hanwen Cheng's avatar
Hanwen Cheng committed
30
import {
31
	getAddressWithPath,
32
	getNetworkKey,
33
	getNetworkKeyByPath,
34
	getPathName,
35
	getPathsWithSubstrateNetworkKey,
Hanwen Cheng's avatar
Hanwen Cheng committed
36
	isSubstratePath
37
38
39
40
41
} from 'utils/identitiesUtils';
import { alertDeleteAccount, alertPathDeletionError } from 'utils/alertUtils';
import { navigateToPathsList, unlockSeedPhrase } from 'utils/navigationHelpers';
import { generateAccountId } from 'utils/account';
import UnknownAccountWarning from 'components/UnknownAccountWarning';
Hanwen Cheng's avatar
Hanwen Cheng committed
42

43
44
45
46
47
48
49
50
51
52
53
interface Props extends NavigationAccountProps<{}> {
	path: string;
	networkKey: string;
}

export function PathDetailsView({
	accounts,
	navigation,
	path,
	networkKey
}: Props): React.ReactElement {
Hanwen Cheng's avatar
Hanwen Cheng committed
54
	const { currentIdentity } = accounts.state;
55
56
57
	const address = getAddressWithPath(path, currentIdentity!);
	const accountName = getPathName(path, currentIdentity!);
	if (!address) return <View />;
58
59
	const isUnknownNetwork = networkKey === UnknownNetworkKeys.UNKNOWN;
	const formattedNetworkKey = isUnknownNetwork ? defaultNetworkKey : networkKey;
60
61
	const accountId = generateAccountId({
		address,
62
		networkKey: formattedNetworkKey
63
	});
Hanwen Cheng's avatar
Hanwen Cheng committed
64

65
	const onOptionSelect = (value: string): void => {
66
67
68
69
70
		switch (value) {
			case 'PathDelete':
				alertDeleteAccount('this account', async () => {
					await unlockSeedPhrase(navigation);
					const deleteSucceed = await accounts.deletePath(path);
71
					const paths = Array.from(accounts.state.currentIdentity!.meta.keys());
72
					const pathIndicatedNetworkKey = getNetworkKeyByPath(path);
73
					if (deleteSucceed) {
74
75
76
77
78
79
80
81
82
83
84
85
						if (isSubstratePath(path)) {
							const listedPaths = getPathsWithSubstrateNetworkKey(
								paths,
								pathIndicatedNetworkKey
							);
							const hasOtherPaths = listedPaths.length > 0;
							hasOtherPaths
								? navigateToPathsList(navigation, pathIndicatedNetworkKey)
								: navigation.navigate('AccountNetworkChooser');
						} else {
							navigation.navigate('AccountNetworkChooser');
						}
86
87
88
89
90
91
92
93
94
95
96
					} else {
						alertPathDeletionError();
					}
				});
				break;
			case 'PathDerivation':
				navigation.navigate('PathDerivation', { parentPath: path });
				break;
			case 'PathManagement':
				navigation.navigate('PathManagement', { path });
				break;
Hanwen Cheng's avatar
Hanwen Cheng committed
97
98
99
100
		}
	};

	return (
101
		<View style={styles.body} testID={testIDs.PathDetail.screen}>
102
			<LeftScreenHeading
103
104
105
				title="Public Address"
				networkKey={formattedNetworkKey}
			/>
Hanwen Cheng's avatar
Hanwen Cheng committed
106
107
108
109
110
111
			<View style={styles.menuView}>
				<PopupMenu
					testID={testIDs.PathDetail.popupMenuButton}
					onSelect={onOptionSelect}
					menuTriggerIconName={'more-vert'}
					menuItems={[
112
						{ text: 'Edit', value: 'PathManagement' },
113
114
115
116
117
						{
							hide: !isSubstratePath(path),
							text: 'Derive Account',
							value: 'PathDerivation'
						},
Hanwen Cheng's avatar
Hanwen Cheng committed
118
119
120
121
122
123
124
125
126
127
						{
							testID: testIDs.PathDetail.deleteButton,
							text: 'Delete',
							textStyle: styles.deleteText,
							value: 'PathDelete'
						}
					]}
				/>
			</View>
			<ScrollView>
128
				<PathCard identity={currentIdentity!} path={path} />
129
				<QrView data={`${accountId}:${accountName}`} />
130
				{isUnknownNetwork && <UnknownAccountWarning isPath />}
Hanwen Cheng's avatar
Hanwen Cheng committed
131
132
133
134
135
			</ScrollView>
		</View>
	);
}

136
137
138
139
function PathDetails({
	accounts,
	navigation
}: NavigationAccountProps<{ path?: string }>): React.ReactElement {
Hanwen Cheng's avatar
Hanwen Cheng committed
140
	const path = navigation.getParam('path', '');
141
	const networkKey = getNetworkKey(path, accounts.state.currentIdentity!);
Hanwen Cheng's avatar
Hanwen Cheng committed
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
	return (
		<PathDetailsView
			accounts={accounts}
			navigation={navigation}
			path={path}
			networkKey={networkKey}
		/>
	);
}

const styles = StyleSheet.create({
	body: {
		backgroundColor: colors.bg,
		flex: 1,
		flexDirection: 'column'
	},
	deleteText: {
		color: colors.bg_alert
	},
	menuView: {
		alignItems: 'flex-end',
		flex: 1,
		position: 'absolute',
		right: 16,
		top: 5
	}
});

export default withAccountStore(withNavigation(PathDetails));