PathDetails.tsx 5.22 KB
Newer Older
1
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
Hanwen Cheng's avatar
Hanwen Cheng committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 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 { ScrollView, StyleSheet, View } from 'react-native';
20

21
import { SafeAreaViewContainer } from 'components/SafeAreaContainer';
22
23
import { defaultNetworkKey, UnknownNetworkKeys } from 'constants/networkSpecs';
import testIDs from 'e2e/testIDs';
24
// TODO use typescript 3.8's type import, Wait for prettier update.
25
26
import { AccountsStoreStateWithIdentity } from 'types/identityTypes';
import { NavigationAccountIdentityProps } from 'types/props';
27
import { RootStackParamList } from 'types/routes';
28
import { withAccountStore, withCurrentIdentity } from 'utils/HOC';
29
30
31
32
33
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
} from 'utils/identitiesUtils';
import { alertDeleteAccount, alertPathDeletionError } from 'utils/alertUtils';
42
43
44
45
import {
	navigateToPathDerivation,
	navigateToPathsList
} from 'utils/navigationHelpers';
46
47
import { generateAccountId } from 'utils/account';
import UnknownAccountWarning from 'components/UnknownAccountWarning';
48
import { useSeedRef } from 'utils/seedRefHooks';
49
import QrScannerTab from 'components/QrScannerTab';
Hanwen Cheng's avatar
Hanwen Cheng committed
50

51
interface Props {
52
53
	path: string;
	networkKey: string;
54
55
56
	navigation:
		| StackNavigationProp<RootStackParamList, 'PathDetails'>
		| StackNavigationProp<RootStackParamList, 'PathsList'>;
57
	accounts: AccountsStoreStateWithIdentity;
58
59
60
61
62
63
64
65
}

export function PathDetailsView({
	accounts,
	navigation,
	path,
	networkKey
}: Props): React.ReactElement {
Hanwen Cheng's avatar
Hanwen Cheng committed
66
	const { currentIdentity } = accounts.state;
67
68
	const address = getAddressWithPath(path, currentIdentity);
	const accountName = getPathName(path, currentIdentity);
69
	const { isSeedRefValid } = useSeedRef(currentIdentity.encryptedSeed);
70
	if (!address) return <View />;
71
72
	const isUnknownNetwork = networkKey === UnknownNetworkKeys.UNKNOWN;
	const formattedNetworkKey = isUnknownNetwork ? defaultNetworkKey : networkKey;
73
74
	const accountId = generateAccountId({
		address,
75
		networkKey: formattedNetworkKey
76
	});
Hanwen Cheng's avatar
Hanwen Cheng committed
77

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

	return (
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
		<SafeAreaViewContainer>
			<ScrollView testID={testIDs.PathDetail.screen} bounces={false}>
				<LeftScreenHeading
					title="Public Address"
					networkKey={formattedNetworkKey}
					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'
								}
							]}
						/>
					}
				/>
				<PathCard identity={currentIdentity} path={path} />
				<QrView data={`${accountId}:${accountName}`} />
				{isUnknownNetwork && <UnknownAccountWarning isPath />}
			</ScrollView>
			<QrScannerTab />
		</SafeAreaViewContainer>
Hanwen Cheng's avatar
Hanwen Cheng committed
144
145
146
	);
}

147
148
function PathDetails({
	accounts,
149
150
	navigation,
	route
151
}: NavigationAccountIdentityProps<'PathDetails'>): React.ReactElement {
152
	const path = route.params.path ?? '';
153
	const networkKey = getNetworkKey(path, accounts.state.currentIdentity);
Hanwen Cheng's avatar
Hanwen Cheng committed
154
155
156
157
158
159
160
161
162
163
164
165
	return (
		<PathDetailsView
			accounts={accounts}
			navigation={navigation}
			path={path}
			networkKey={networkKey}
		/>
	);
}

const styles = StyleSheet.create({
	deleteText: {
166
		color: colors.signal.error
Hanwen Cheng's avatar
Hanwen Cheng committed
167
168
169
	}
});

170
export default withAccountStore(withCurrentIdentity(PathDetails));