Store network apiUrl affinity (#4147)
* Store network apiUrl affinity * Fix setting of url * Remove old affinities (when networks swap)
This commit is contained in:
parent
c10e88d3c7
commit
7c8c27f5e0
@ -11,6 +11,7 @@ import type { Group } from './types';
|
||||
import Network from './Network';
|
||||
|
||||
interface Props {
|
||||
affinities: Record<string, string>;
|
||||
apiUrl: string;
|
||||
children?: React.ReactNode;
|
||||
className?: string;
|
||||
@ -21,7 +22,7 @@ interface Props {
|
||||
value: Group;
|
||||
}
|
||||
|
||||
function GroupDisplay ({ apiUrl, children, className = '', index, isSelected, setApiUrl, setGroup, value: { header, networks } }: Props): React.ReactElement<Props> {
|
||||
function GroupDisplay ({ affinities, apiUrl, children, className = '', index, isSelected, setApiUrl, setGroup, value: { header, networks } }: Props): React.ReactElement<Props> {
|
||||
const _setGroup = useCallback(
|
||||
() => setGroup(isSelected ? -1 : index),
|
||||
[index, isSelected, setGroup]
|
||||
@ -41,6 +42,7 @@ function GroupDisplay ({ apiUrl, children, className = '', index, isSelected, se
|
||||
<div className='groupNetworks'>
|
||||
{networks.map((network, index): React.ReactNode => (
|
||||
<Network
|
||||
affinity={affinities[network.name]}
|
||||
apiUrl={apiUrl}
|
||||
key={index}
|
||||
setApiUrl={setApiUrl}
|
||||
|
||||
@ -11,28 +11,39 @@ import type { Network } from './types';
|
||||
import Url from './Url';
|
||||
|
||||
interface Props {
|
||||
affinity?: string;
|
||||
apiUrl: string;
|
||||
className?: string;
|
||||
setApiUrl: (apiUrl: string) => void;
|
||||
setApiUrl: (network: string, apiUrl: string) => void;
|
||||
value: Network;
|
||||
}
|
||||
|
||||
function NetworkDisplay ({ apiUrl, className = '', setApiUrl, value: { icon, isChild, name, providers } }: Props): React.ReactElement<Props> {
|
||||
function NetworkDisplay ({ affinity, apiUrl, className = '', setApiUrl, value: { icon, isChild, name, providers } }: Props): React.ReactElement<Props> {
|
||||
const isSelected = useMemo(
|
||||
() => providers.some(({ url }) => url === apiUrl),
|
||||
[apiUrl, providers]
|
||||
);
|
||||
|
||||
const _selectFirst = useCallback(
|
||||
() => setApiUrl(providers[0].url),
|
||||
[providers, setApiUrl]
|
||||
const _selectUrl = useCallback(
|
||||
() => setApiUrl(
|
||||
name,
|
||||
affinity && providers.find(({ url }) => url === affinity)
|
||||
? affinity
|
||||
: providers[0].url
|
||||
),
|
||||
[affinity, name, providers, setApiUrl]
|
||||
);
|
||||
|
||||
const _setApiUrl = useCallback(
|
||||
(apiUrl: string) => setApiUrl(name, apiUrl),
|
||||
[name, setApiUrl]
|
||||
);
|
||||
|
||||
return (
|
||||
<div className={`${className}${isSelected ? ' isSelected highlight--border' : ''}`}>
|
||||
<div
|
||||
className={`endpointSection${isChild ? ' isChild' : ''}`}
|
||||
onClick={_selectFirst}
|
||||
onClick={_selectUrl}
|
||||
>
|
||||
<ChainImg
|
||||
className='endpointIcon'
|
||||
@ -45,7 +56,7 @@ function NetworkDisplay ({ apiUrl, className = '', setApiUrl, value: { icon, isC
|
||||
apiUrl={apiUrl}
|
||||
key={url}
|
||||
label={name}
|
||||
setApiUrl={setApiUrl}
|
||||
setApiUrl={_setApiUrl}
|
||||
url={url}
|
||||
/>
|
||||
))}
|
||||
|
||||
@ -5,6 +5,7 @@
|
||||
/* eslint-disable-next-line node/no-deprecated-api */
|
||||
import punycode from 'punycode';
|
||||
import React, { useCallback, useMemo, useState } from 'react';
|
||||
import store from 'store';
|
||||
import styled from 'styled-components';
|
||||
|
||||
import type { LinkOption } from '@polkadot/apps-config/settings/types';
|
||||
@ -31,6 +32,8 @@ interface UrlState {
|
||||
isUrlValid: boolean;
|
||||
}
|
||||
|
||||
const STORAGE_AFFINITIES = 'network:affinities';
|
||||
|
||||
function isValidUrl (url: string): boolean {
|
||||
return (
|
||||
// some random length... we probably want to parse via some lib
|
||||
@ -98,12 +101,29 @@ function extractUrlState (apiUrl: string, groups: Group[]): UrlState {
|
||||
};
|
||||
}
|
||||
|
||||
function loadAffinities (groups: Group[]): Record<string, string> {
|
||||
return Object
|
||||
.entries<string>(store.get(STORAGE_AFFINITIES) || {})
|
||||
.filter(([network, apiUrl]) =>
|
||||
groups.some(({ networks }) =>
|
||||
networks.some(({ name, providers }) =>
|
||||
name === network && providers.some(({ url }) => url === apiUrl)
|
||||
)
|
||||
)
|
||||
)
|
||||
.reduce((result: Record<string, string>, [network, apiUrl]): Record<string, string> => ({
|
||||
...result,
|
||||
[network]: apiUrl
|
||||
}), {});
|
||||
}
|
||||
|
||||
function Endpoints ({ className = '', offset, onClose }: Props): React.ReactElement<Props> {
|
||||
const { t } = useTranslation();
|
||||
const linkOptions = createWsEndpoints(t);
|
||||
const [groups, setGroups] = useState(combineEndpoints(linkOptions));
|
||||
const [{ apiUrl, groupIndex, hasUrlChanged, isUrlValid }, setApiUrl] = useState<UrlState>(extractUrlState(uiSettings.get().apiUrl, groups));
|
||||
const [storedCustomEndpoints, setStoredCustomEndpoints] = useState<string[]>(getCustomEndpoints());
|
||||
const [affinities, setAffinities] = useState(loadAffinities(groups));
|
||||
|
||||
const isKnownUrl = useMemo(() => {
|
||||
let result = false;
|
||||
@ -168,7 +188,16 @@ function Endpoints ({ className = '', offset, onClose }: Props): React.ReactElem
|
||||
};
|
||||
|
||||
const _setApiUrl = useCallback(
|
||||
(apiUrl: string) => setApiUrl(extractUrlState(apiUrl, groups)),
|
||||
(network: string, apiUrl: string): void => {
|
||||
setAffinities((affinities): Record<string, string> => {
|
||||
const newValue = { ...affinities, [network]: apiUrl };
|
||||
|
||||
store.set(STORAGE_AFFINITIES, newValue);
|
||||
|
||||
return newValue;
|
||||
});
|
||||
setApiUrl(extractUrlState(apiUrl, groups));
|
||||
},
|
||||
[groups]
|
||||
);
|
||||
|
||||
@ -212,6 +241,7 @@ function Endpoints ({ className = '', offset, onClose }: Props): React.ReactElem
|
||||
>
|
||||
{groups.map((group, index): React.ReactNode => (
|
||||
<GroupDisplay
|
||||
affinities={affinities}
|
||||
apiUrl={apiUrl}
|
||||
index={index}
|
||||
isSelected={groupIndex === index}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user