PathsList.tsx 4.77 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
17
// 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, { useMemo } from 'react';
18
import { ScrollView, Text, View } from 'react-native';
19
20

import { PathDetailsView } from './PathDetails';
Hanwen Cheng's avatar
Hanwen Cheng committed
21

22
import { useUnlockSeed } from 'utils/navigationHelpers';
23
import { useSeedRef } from 'utils/seedRefHooks';
24
import { SafeAreaViewContainer } from 'components/SafeAreaContainer';
25
import { NETWORK_LIST, UnknownNetworkKeys } from 'constants/networkSpecs';
26
27
import testIDs from 'e2e/testIDs';
import { PathGroup } from 'types/identityTypes';
28
29
30
31
import {
	isEthereumNetworkParams,
	isUnknownNetworkParams
} from 'types/networkSpecsTypes';
32
33
import { NavigationAccountIdentityProps } from 'types/props';
import { withAccountStore, withCurrentIdentity } from 'utils/HOC';
Hanwen Cheng's avatar
Hanwen Cheng committed
34
import {
35
	getPathsWithSubstrateNetworkKey,
Hanwen Cheng's avatar
Hanwen Cheng committed
36
37
	groupPaths,
	removeSlash
38
} from 'utils/identitiesUtils';
39
import QRScannerAndDerivationTab from 'components/QRScannerAndDerivationTab';
40
41
42
43
44
45
46
import PathCard from 'components/PathCard';
import Separator from 'components/Separator';
import fontStyles from 'styles/fontStyles';
import { LeftScreenHeading } from 'components/ScreenHeading';

function PathsList({
	accounts,
47
48
	navigation,
	route
49
}: NavigationAccountIdentityProps<'PathsList'>): React.ReactElement {
50
	const networkKey = route.params.networkKey ?? UnknownNetworkKeys.UNKNOWN;
51
	const networkParams = NETWORK_LIST[networkKey];
Hanwen Cheng's avatar
Hanwen Cheng committed
52
53

	const { currentIdentity } = accounts.state;
54
	const isEthereumPath = isEthereumNetworkParams(networkParams);
55
	const isUnknownNetworkPath = isUnknownNetworkParams(networkParams);
56
	const pathsGroups = useMemo((): PathGroup[] | null => {
57
		if (!currentIdentity || isEthereumPath) return null;
58
59
60
61
		const listedPaths = getPathsWithSubstrateNetworkKey(
			currentIdentity,
			networkKey
		);
Hanwen Cheng's avatar
Hanwen Cheng committed
62
		return groupPaths(listedPaths);
63
	}, [currentIdentity, isEthereumPath, networkKey]);
64
	const { isSeedRefValid } = useSeedRef(currentIdentity.encryptedSeed);
65
	const { unlockWithoutPassword } = useUnlockSeed(isSeedRefValid);
Hanwen Cheng's avatar
Hanwen Cheng committed
66

67
	if (isEthereumNetworkParams(networkParams)) {
Hanwen Cheng's avatar
Hanwen Cheng committed
68
69
70
71
72
73
74
75
76
77
78
		return (
			<PathDetailsView
				networkKey={networkKey}
				path={networkKey}
				navigation={navigation}
				accounts={accounts}
			/>
		);
	}

	const { navigate } = navigation;
79
	const rootPath = `//${networkParams.pathId}`;
Hanwen Cheng's avatar
Hanwen Cheng committed
80

81
82
83
84
85
86
	const onTapDeriveButton = (): Promise<void> =>
		unlockWithoutPassword({
			name: 'PathDerivation',
			params: { parentPath: isUnknownNetworkPath ? '' : rootPath }
		});

87
	const renderSinglePath = (pathsGroup: PathGroup): React.ReactElement => {
Hanwen Cheng's avatar
Hanwen Cheng committed
88
89
90
91
92
93
94
		const path = pathsGroup.paths[0];
		return (
			<PathCard
				key={path}
				testID={testIDs.PathsList.pathCard + path}
				identity={currentIdentity}
				path={path}
95
				onPress={(): void => navigate('PathDetails', { path })}
Hanwen Cheng's avatar
Hanwen Cheng committed
96
97
98
99
			/>
		);
	};

100
	const renderGroupPaths = (pathsGroup: PathGroup): React.ReactElement => (
Hanwen Cheng's avatar
Hanwen Cheng committed
101
		<View key={`group${pathsGroup.title}`} style={{ marginTop: 24 }}>
102
103
104
105
106
107
108
			<Separator
				shadow={true}
				style={{
					height: 0,
					marginVertical: 0
				}}
			/>
Hanwen Cheng's avatar
Hanwen Cheng committed
109
110
			<View
				style={{
111
112
					marginVertical: 16,
					paddingHorizontal: 16
Hanwen Cheng's avatar
Hanwen Cheng committed
113
114
				}}
			>
115
116
117
118
119
				<Text style={fontStyles.t_prefix}>{removeSlash(pathsGroup.title)}</Text>
				<Text style={fontStyles.t_codeS}>
					{networkParams.pathId}
					{pathsGroup.title}
				</Text>
Hanwen Cheng's avatar
Hanwen Cheng committed
120
121
			</View>
			{pathsGroup.paths.map(path => (
122
123
124
125
126
127
128
				<PathCard
					key={path}
					testID={testIDs.PathsList.pathCard + path}
					identity={currentIdentity}
					path={path}
					onPress={(): void => navigate('PathDetails', { path })}
				/>
Hanwen Cheng's avatar
Hanwen Cheng committed
129
130
131
			))}
		</View>
	);
132

Hanwen Cheng's avatar
Hanwen Cheng committed
133
	return (
134
135
136
137
138
139
140
141
142
143
144
145
146
147
		<SafeAreaViewContainer>
			<ScrollView testID={testIDs.PathsList.screen}>
				<LeftScreenHeading
					title={networkParams.title}
					hasSubtitleIcon={true}
					networkKey={networkKey}
				/>
				{(pathsGroups as PathGroup[]).map(pathsGroup =>
					pathsGroup.paths.length === 1
						? renderSinglePath(pathsGroup)
						: renderGroupPaths(pathsGroup)
				)}
				<Separator style={{ backgroundColor: 'transparent' }} />
			</ScrollView>
148
149
			<QRScannerAndDerivationTab
				derivationTestID={testIDs.PathsList.deriveButton}
150
				title="Derive New Account"
151
				onPress={onTapDeriveButton}
152
			/>
153
		</SafeAreaViewContainer>
Hanwen Cheng's avatar
Hanwen Cheng committed
154
155
156
	);
}

157
export default withAccountStore(withCurrentIdentity(PathsList));