|
| 1 | +/** |
| 2 | + * Copyright 2018 Shift Devices AG |
| 3 | + * Copyright 2022 Shift Crypto AG |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { useTranslation } from 'react-i18next'; |
| 19 | +import { i18n } from '../../i18n/i18n'; |
| 20 | +import { Guide } from '../../components/guide/guide'; |
| 21 | +import { Entry } from '../../components/guide/entry'; |
| 22 | +import { Button, ButtonLink } from '../../components/forms'; |
| 23 | +import { Header } from '../../components/layout'; |
| 24 | +import { BlockExplorers } from './block-explorers'; |
| 25 | +import * as backendAPI from '../../api/backend'; |
| 26 | +import { apiGet } from '../../utils/request'; |
| 27 | +import { getConfig, setConfig } from '../../utils/config'; |
| 28 | +import { CoinCode } from '../../api/account'; |
| 29 | +import { MobileHeader } from './components/mobile-header'; |
| 30 | +import { useCallback, useEffect, useRef, useState } from 'react'; |
| 31 | + |
| 32 | + |
| 33 | +type TBlockExplorer = { name: string; url: string }; |
| 34 | +type TSelectionData = Record<CoinCode, TBlockExplorer[]>; |
| 35 | + |
| 36 | +export const SelectExplorerSettings = () => { |
| 37 | + const { t } = useTranslation(); |
| 38 | + |
| 39 | + const initialConfig = useRef<any>(); |
| 40 | + const [config, setConfigState] = useState<any>(); |
| 41 | + |
| 42 | + const [supportedCoins, setSupportedCoins] = useState<backendAPI.ICoin[]>([]); |
| 43 | + const [allSelections, setAllSelections] = useState<TSelectionData>(); |
| 44 | + |
| 45 | + const [saveDisabled, setSaveDisabled] = useState(true); |
| 46 | + |
| 47 | + const loadConfig = () => { |
| 48 | + getConfig().then(setConfigState); |
| 49 | + }; |
| 50 | + |
| 51 | + |
| 52 | + const updateConfigState = useCallback((newConfig: any) => { |
| 53 | + if (JSON.stringify(initialConfig.current) !== JSON.stringify(newConfig)) { |
| 54 | + setConfigState(newConfig); |
| 55 | + setSaveDisabled(false); |
| 56 | + } else { |
| 57 | + setSaveDisabled(true); |
| 58 | + } |
| 59 | + }, []); |
| 60 | + |
| 61 | + const handleChange = useCallback((selectedTxPrefix: string, coin: CoinCode) => { |
| 62 | + if (config.backend[coin]) { |
| 63 | + // if you select another explorer and then change it back to the previous state, |
| 64 | + // save will be enabled...it would be nicer if we know the initial state and enable |
| 65 | + // might refactor later but this is ok imo. |
| 66 | + if (config.backend[coin].blockExplorerTxPrefix !== selectedTxPrefix) { |
| 67 | + config.backend[coin].blockExplorerTxPrefix = selectedTxPrefix; |
| 68 | + // setSaveDisabled(false); |
| 69 | + // setConfigState(config); |
| 70 | + updateConfigState(config); |
| 71 | + } |
| 72 | + } |
| 73 | + }, [config, updateConfigState]); |
| 74 | + |
| 75 | + const save = async () => { |
| 76 | + setSaveDisabled(true); |
| 77 | + await setConfig(config); |
| 78 | + initialConfig.current = await getConfig(); |
| 79 | + }; |
| 80 | + |
| 81 | + useEffect(() => { |
| 82 | + const fetchData = async () => { |
| 83 | + const coins = await backendAPI.getSupportedCoins(); |
| 84 | + const allExplorerSelection = await apiGet('available-explorers'); |
| 85 | + |
| 86 | + // if set alongside config it will 'update' with it, but we want it to stay the same after initialization. |
| 87 | + initialConfig.current = await getConfig(); |
| 88 | + |
| 89 | + setSupportedCoins(coins); |
| 90 | + setAllSelections(allExplorerSelection); |
| 91 | + }; |
| 92 | + |
| 93 | + loadConfig(); |
| 94 | + fetchData().catch(console.error); |
| 95 | + }, []); |
| 96 | + |
| 97 | + if (config === undefined) { |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + return ( |
| 102 | + <div className="contentWithGuide"> |
| 103 | + <div className="container"> |
| 104 | + <div className="innerContainer scrollableContainer"> |
| 105 | + <Header |
| 106 | + hideSidebarToggler |
| 107 | + title={ |
| 108 | + <> |
| 109 | + <h2 className={'hide-on-small'}>{t('settings.expert.explorer.title')}</h2> |
| 110 | + <MobileHeader withGuide title={t('settings.expert.explorer.title-small')}/> |
| 111 | + </> |
| 112 | + }/> |
| 113 | + <div className="content padded"> |
| 114 | + {supportedCoins.map(coin => { |
| 115 | + return <BlockExplorers |
| 116 | + key={coin.coinCode} |
| 117 | + coin={coin.coinCode} |
| 118 | + explorerOptions={allSelections?.[coin.coinCode] ?? []} |
| 119 | + handleOnChange={handleChange} |
| 120 | + selectedPrefix={config?.backend?.[coin.coinCode]?.blockExplorerTxPrefix}/>; |
| 121 | + })} |
| 122 | + </div> |
| 123 | + <div className="content padded" style={{ display: 'flex', justifyContent: 'space-between' }}> |
| 124 | + <ButtonLink |
| 125 | + secondary |
| 126 | + className={'hide-on-small'} |
| 127 | + to={'/settings'}> |
| 128 | + {t('button.back')} |
| 129 | + </ButtonLink> |
| 130 | + <Button primary disabled={saveDisabled} onClick={() => save()}>{t('settings.save')}</Button> |
| 131 | + </div> |
| 132 | + </div> |
| 133 | + </div> |
| 134 | + <Guide> |
| 135 | + <Entry key="guide.settings-block-explorer.what" entry={t('guide.settings-block-explorer.what')} /> |
| 136 | + <Entry key="guide.settings-block-explorer.why" entry={t('guide.settings-block-explorer.why')} /> |
| 137 | + <Entry key="guide.settings-block-explorer.options" entry={t('guide.settings-block-explorer.options')} /> |
| 138 | + <Entry key="guide.settings-electrum.instructions" entry={{ |
| 139 | + link: { |
| 140 | + text: t('guide.settings-block-explorer.instructions.link.text'), |
| 141 | + url: (i18n.resolvedLanguage === 'de') |
| 142 | + // TODO: DE guide. |
| 143 | + ? 'https://shiftcrypto.support/help/en-us/23-bitcoin/205-how-to-use-a-block-explorer' |
| 144 | + : 'https://shiftcrypto.support/help/en-us/23-bitcoin/205-how-to-use-a-block-explorer' |
| 145 | + }, |
| 146 | + text: t('guide.settings-block-explorer.instructions.text'), |
| 147 | + title: t('guide.settings-block-explorer.instructions.title') |
| 148 | + }} /> |
| 149 | + </Guide> |
| 150 | + </div> |
| 151 | + ); |
| 152 | +}; |
0 commit comments