PathDetails.tsx 4.93 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
// 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/>.

17
import { StackNavigationProp } from '@react-navigation/stack';
Hanwen Cheng's avatar
Hanwen Cheng committed
18
import React from 'react';
19
import { StyleSheet, View } from 'react-native';
20

21
import { SafeAreaScrollViewContainer } from 'components/SafeAreaContainer';
22
23
import { defaultNetworkKey, UnknownNetworkKeys } from 'constants/networkSpecs';
import testIDs from 'e2e/testIDs';
24
25
// TODO use typescript 3.8's type import, Wait for prettier update.
import AccountsStore from 'stores/AccountsStore';
26
import { NavigationAccountProps } from 'types/props';
27
import { RootStackParamList } from 'types/routes';
28
29
30
31
32
33
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
34
import {
35
	getAddressWithPath,
36
	getNetworkKey,
37
	getPathName,
38
	getPathsWithSubstrateNetworkKey,
Hanwen Cheng's avatar
Hanwen Cheng committed
39
	isSubstratePath
40
41
42
43
44
} 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
45

46
interface Props {
47
48
	path: string;
	networkKey: string;
49
50
51
52
	navigation:
		| StackNavigationProp<RootStackParamList, 'PathDetails'>
		| StackNavigationProp<RootStackParamList, 'PathsList'>;
	accounts: AccountsStore;
53
54
55
56
57
58
59
60
}

export function PathDetailsView({
	accounts,
	navigation,
	path,
	networkKey
}: Props): React.ReactElement {
Hanwen Cheng's avatar
Hanwen Cheng committed
61
	const { currentIdentity } = accounts.state;
62
63
	const address = getAddressWithPath(path, currentIdentity);
	const accountName = getPathName(path, currentIdentity);
64
	if (!address) return <View />;
65
66
	const isUnknownNetwork = networkKey === UnknownNetworkKeys.UNKNOWN;
	const formattedNetworkKey = isUnknownNetwork ? defaultNetworkKey : networkKey;
67
68
	const accountId = generateAccountId({
		address,
69
		networkKey: formattedNetworkKey
70
	});
Hanwen Cheng's avatar
Hanwen Cheng committed
71

72
	const onOptionSelect = (value: string): void => {
73
74
75
76
		switch (value) {
			case 'PathDelete':
				alertDeleteAccount('this account', async () => {
					await unlockSeedPhrase(navigation);
77
78
					try {
						await accounts.deletePath(path);
79
80
						if (isSubstratePath(path)) {
							const listedPaths = getPathsWithSubstrateNetworkKey(
81
82
								accounts.state.currentIdentity!,
								networkKey
83
84
85
							);
							const hasOtherPaths = listedPaths.length > 0;
							hasOtherPaths
86
								? navigateToPathsList(navigation, networkKey)
87
88
89
90
								: navigation.navigate('AccountNetworkChooser');
						} else {
							navigation.navigate('AccountNetworkChooser');
						}
91
92
					} catch (err) {
						alertPathDeletionError(err);
93
94
95
96
97
98
99
100
101
					}
				});
				break;
			case 'PathDerivation':
				navigation.navigate('PathDerivation', { parentPath: path });
				break;
			case 'PathManagement':
				navigation.navigate('PathManagement', { path });
				break;
Hanwen Cheng's avatar
Hanwen Cheng committed
102
103
104
105
		}
	};

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

139
140
function PathDetails({
	accounts,
141
142
143
144
	navigation,
	route
}: NavigationAccountProps<'PathDetails'>): React.ReactElement {
	const path = route.params.path ?? '';
145
	const networkKey = getNetworkKey(path, accounts.state.currentIdentity!);
Hanwen Cheng's avatar
Hanwen Cheng committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
	return (
		<PathDetailsView
			accounts={accounts}
			navigation={navigation}
			path={path}
			networkKey={networkKey}
		/>
	);
}

const styles = StyleSheet.create({
	deleteText: {
		color: colors.bg_alert
	}
});

162
export default withAccountStore(PathDetails);