PathDetails.tsx 5.08 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
// 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';
Hanwen Cheng's avatar
Hanwen Cheng committed
49

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

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

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

	return (
110
		<SafeAreaScrollViewContainer testID={testIDs.PathDetail.screen}>
111
			<LeftScreenHeading
112
113
				title="Public Address"
				networkKey={formattedNetworkKey}
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
				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'
							}
						]}
					/>
				}
135
			/>
136
			<PathCard identity={currentIdentity} path={path} />
137
138
139
			<QrView data={`${accountId}:${accountName}`} />
			{isUnknownNetwork && <UnknownAccountWarning isPath />}
		</SafeAreaScrollViewContainer>
Hanwen Cheng's avatar
Hanwen Cheng committed
140
141
142
	);
}

143
144
function PathDetails({
	accounts,
145
146
	navigation,
	route
147
}: NavigationAccountIdentityProps<'PathDetails'>): React.ReactElement {
148
	const path = route.params.path ?? '';
149
	const networkKey = getNetworkKey(path, accounts.state.currentIdentity);
Hanwen Cheng's avatar
Hanwen Cheng committed
150
151
152
153
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: {
		color: colors.bg_alert
	}
});

166
export default withAccountStore(withCurrentIdentity(PathDetails));