forked from polkadot-js/apps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevelopment.ts
More file actions
107 lines (91 loc) · 2.72 KB
/
development.ts
File metadata and controls
107 lines (91 loc) · 2.72 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
// 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';
export const CUSTOM_ENDPOINT_KEY = 'polkadot-app-custom-endpoints';
interface EnvWindow {
// eslint-disable-next-line camelcase
process_env?: {
WS_URL: string;
}
}
function findUiForUrl (url: string, endpoints: EndpointOption[]): LinkOption['ui'] {
// Search through all endpoints to find a matching provider URL
for (const endpoint of endpoints) {
if (endpoint.providers) {
for (const providerUrl of Object.values(endpoint.providers)) {
if (providerUrl === url) {
return endpoint.ui;
}
}
}
// Also check linked endpoints (parachains)
if (endpoint.linked) {
const linkedUi = findUiForUrl(url, endpoint.linked);
if (linkedUi && (linkedUi.color || linkedUi.logo || linkedUi.identityIcon)) {
return linkedUi;
}
}
}
return {};
}
export function createCustom (t: TFunction, endpoints: EndpointOption[] = []): LinkOption[] {
const WS_URL = (
(typeof process !== 'undefined' ? process.env?.WS_URL : undefined) ||
(typeof window !== 'undefined' ? (window as EnvWindow).process_env?.WS_URL : undefined)
);
if (!WS_URL) {
return [];
}
// Try to find UI configuration from existing endpoint with matching URL
const ui = findUiForUrl(WS_URL, endpoints);
return [
{
isHeader: true,
text: t('rpc.dev.custom', 'Custom environment', { ns: 'apps-config' }),
textBy: '',
ui: {},
value: ''
},
{
info: 'WS_URL',
text: t('rpc.dev.custom.entry', 'Custom {{WS_URL}}', { ns: 'apps-config', replace: { WS_URL } }),
textBy: WS_URL,
ui,
value: WS_URL
}
];
}
export function createOwn (t: TFunction): LinkOption[] {
try {
// this may not be available, e.g. when running via script
const storedItems = typeof localStorage === 'object' && typeof localStorage.getItem === 'function'
? localStorage.getItem(CUSTOM_ENDPOINT_KEY)
: null;
if (storedItems) {
const items = JSON.parse(storedItems) as string[];
return items.map((textBy) => ({
info: 'local',
text: t('rpc.dev.custom.own', 'Custom', { ns: 'apps-config' }),
textBy,
ui: {},
value: textBy
}));
}
} catch (e) {
console.error(e);
}
return [];
}
export function createDev (t: TFunction): LinkOption[] {
return [
{
dnslink: 'local',
info: 'local',
text: t('rpc.dev.local', 'Local Node', { ns: 'apps-config' }),
textBy: '127.0.0.1:9944',
ui: {},
value: 'ws://127.0.0.1:9944'
}
];
}