forked from polkadot-js/apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChainLock.tsx
69 lines (57 loc) · 1.61 KB
/
ChainLock.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
66
67
68
69
// Copyright 2017-2022 @polkadot/app-accounts authors & contributors
// SPDX-License-Identifier: Apache-2.0
import React, { useCallback, useMemo } from 'react';
import styled from 'styled-components';
import { useApi } from '@polkadot/react-hooks';
import { chains } from '@polkadot/ui-settings/defaults/chains';
import Toggle from './Toggle';
import { useTranslation } from './translate';
interface Props {
className?: string;
genesisHash: string | null;
isDisabled?: boolean;
onChange: (genesisHash: string | null) => void;
}
function calcLock (apiGenesis: string, genesisHash: string | null): boolean {
if (!genesisHash) {
return false;
}
return (
Object.values(chains).find((hashes): boolean =>
hashes.includes(apiGenesis)
) || [apiGenesis]
).includes(genesisHash);
}
function ChainLock ({ className = '', genesisHash, isDisabled, onChange }: Props): React.ReactElement<Props> | null {
const { t } = useTranslation();
const { api, isDevelopment } = useApi();
const isTiedToChain = useMemo(
() => calcLock(api.genesisHash.toHex(), genesisHash),
[api, genesisHash]
);
const _onChange = useCallback(
(isTiedToChain: boolean) =>
onChange(
isTiedToChain
? api.genesisHash.toHex()
: null
),
[api, onChange]
);
if (isDevelopment) {
return null;
}
return (
<Toggle
className={className}
isDisabled={isDisabled}
label={t<string>('only this network')}
onChange={_onChange}
preventDefault
value={isTiedToChain}
/>
);
}
export default React.memo(styled(ChainLock)`
text-align: right;
`);