-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.tsx
65 lines (51 loc) · 1.83 KB
/
util.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2017-2021 @polkadot/app-accounts authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { KeyringAddress } from '@polkadot/ui-keyring/types';
import type { SortedAccount } from './types';
import React from 'react';
import { Menu } from '@polkadot/react-components';
import { keyring } from '@polkadot/ui-keyring';
export function createMenuGroup (key: string, items: (React.ReactNode | false | undefined | null)[]): React.ReactNode | null {
const filtered = items.filter((item): item is React.ReactNode => !!item);
return filtered.length
? <React.Fragment key={key}><Menu.Divider />{filtered}</React.Fragment>
: null;
}
function expandList (mapped: SortedAccount[], entry: SortedAccount): SortedAccount[] {
mapped.push(entry);
entry.children.forEach((entry): void => {
expandList(mapped, entry);
});
return mapped;
}
export function sortAccounts (addresses: string[], favorites: string[]): SortedAccount[] {
const mapped = addresses
.map((address) => keyring.getAccount(address))
.filter((account): account is KeyringAddress => !!account)
.map((account): SortedAccount => ({
account,
children: [],
isFavorite: favorites.includes(account.address)
}))
.sort((a, b) => (a.account.meta.whenCreated || 0) - (b.account.meta.whenCreated || 0));
return mapped
.filter((entry): boolean => {
const parentAddress = entry.account.meta.parentAddress;
if (parentAddress) {
const parent = mapped.find(({ account: { address } }) => address === parentAddress);
if (parent) {
parent.children.push(entry);
return false;
}
}
return true;
})
.reduce(expandList, [])
.sort((a, b): number =>
a.isFavorite === b.isFavorite
? 0
: b.isFavorite
? 1
: -1
);
}