forked from polkadot-js/apps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.ts
More file actions
130 lines (112 loc) · 3.68 KB
/
util.ts
File metadata and controls
130 lines (112 loc) · 3.68 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2017-2026 @polkadot/apps-config authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { TFunction } from '../types.js';
import type { EndpointOption, LinkOption } from './types.js';
interface SortOption {
isUnreachable?: boolean;
}
let dummyId = 0;
function sortNoop (): number {
return 0;
}
function sortLinks (a: SortOption, b: SortOption): number {
return !!a.isUnreachable !== !!b.isUnreachable
? a.isUnreachable
? 1
: -1
: 0;
}
function expandLinked (input: LinkOption[]): LinkOption[] {
const valueRelay = input.map(({ value }) => value);
return input.reduce((result: LinkOption[], entry): LinkOption[] => {
result.push(entry);
return entry.linked
? result.concat(
expandLinked(entry.linked).map((child): LinkOption => {
child.genesisHashRelay = entry.genesisHash;
child.isChild = true;
child.textRelay = input.length
? input[0].text
: undefined;
child.valueRelay = valueRelay;
if (entry.ui?.identityIcon && child.paraId && child.paraId < 2000) {
if (!child.ui) {
child.ui = { identityIcon: entry.ui.identityIcon };
} else if (!child.ui.identityIcon) {
child.ui.identityIcon = entry.ui.identityIcon;
}
}
return child;
})
)
: result;
}, []);
}
function expandEndpoint (t: TFunction, { dnslink, genesisHash, homepage, info, isChild, isDisabled, isPeople, isPeopleForIdentity, isUnreachable, linked, paraId, providers, relayName, teleport, text, ui }: EndpointOption, firstOnly: boolean, withSort: boolean): LinkOption[] {
const hasProviders = Object.keys(providers).length !== 0;
const base = {
genesisHash,
homepage,
info,
isChild,
isDisabled,
isPeople,
isPeopleForIdentity,
isUnreachable: isUnreachable || !hasProviders,
paraId,
providers: Object.keys(providers).map((k) => providers[k]),
relayName,
teleport,
text,
ui
};
const result = Object
.entries(
hasProviders
? providers
: { Placeholder: `wss://${++dummyId}` }
)
.filter((_, index) => !firstOnly || index === 0)
.map(([host, value], index): LinkOption => ({
...base,
dnslink: index === 0 ? dnslink : undefined,
isLightClient: value.startsWith('light://'),
isRelay: false,
textBy: value.startsWith('light://')
? t('lightclient.experimental', 'light client (experimental)', { ns: 'apps-config' })
: t('rpc.hosted.via', 'via {{host}}', { ns: 'apps-config', replace: { host } }),
value
}))
.sort((a, b) =>
a.isLightClient
? 1
: b.isLightClient
? -1
: a.textBy.toLocaleLowerCase().localeCompare(b.textBy.toLocaleLowerCase())
);
if (linked) {
const last = result[result.length - 1];
const options: LinkOption[] = [];
linked
.sort(withSort ? sortLinks : sortNoop)
.filter(({ paraId }) => paraId)
.forEach((o) =>
options.push(...expandEndpoint(t, o, firstOnly, withSort))
);
last.isRelay = true;
last.linked = options;
}
return expandLinked(result);
}
export function expandEndpoints (t: TFunction, input: EndpointOption[], firstOnly: boolean, withSort: boolean): LinkOption[] {
return input
.sort(withSort ? sortLinks : sortNoop)
.reduce((all: LinkOption[], e) =>
all.concat(expandEndpoint(t, e, firstOnly, withSort)), []);
}
export function getTeleports (input: EndpointOption[]): number[] {
return input
.filter(({ teleport }) => !!teleport && teleport[0] === -1)
.map(({ paraId }) => paraId)
.filter((id): id is number => !!id);
}