|
| 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 { useCallback, useEffect, useRef, useState } from 'react'; |
| 19 | +import { useTranslation } from 'react-i18next'; |
| 20 | +import { CoinCode, IAccount } from '@/api/account'; |
| 21 | +import * as backendAPI from '@/api/backend'; |
| 22 | +import { i18n } from '@/i18n/i18n'; |
| 23 | +import { Guide } from '@/components/guide/guide'; |
| 24 | +import { Entry } from '@/components/guide/entry'; |
| 25 | +import { Button, ButtonLink } from '@/components/forms'; |
| 26 | +import { Header } from '@/components/layout'; |
| 27 | +import { getConfig, setConfig } from '@/utils/config'; |
| 28 | +import { MobileHeader } from './components/mobile-header'; |
| 29 | +import { BlockExplorers } from './block-explorers'; |
| 30 | + |
| 31 | +type TSelectExplorerSettingsProps = { |
| 32 | + accounts: IAccount[]; |
| 33 | +} |
| 34 | + |
| 35 | +export const SelectExplorerSettings = ({ accounts }: TSelectExplorerSettingsProps) => { |
| 36 | + const { t } = useTranslation(); |
| 37 | + |
| 38 | + const initialConfig = useRef<any>(); |
| 39 | + const [config, setConfigState] = useState<any>(); |
| 40 | + |
| 41 | + const availableCoins = new Set(accounts.map(account => account.coinCode)); |
| 42 | + const [allSelections, setAllSelections] = useState<backendAPI.TAvailableExplorers>(); |
| 43 | + |
| 44 | + const [saveDisabled, setSaveDisabled] = useState(true); |
| 45 | + |
| 46 | + const loadConfig = () => { |
| 47 | + getConfig().then(setConfigState); |
| 48 | + }; |
| 49 | + |
| 50 | + |
| 51 | + const updateConfigState = useCallback((newConfig: any) => { |
| 52 | + if (JSON.stringify(initialConfig.current) !== JSON.stringify(newConfig)) { |
| 53 | + setConfigState(newConfig); |
| 54 | + setSaveDisabled(false); |
| 55 | + } else { |
| 56 | + setSaveDisabled(true); |
| 57 | + } |
| 58 | + }, []); |
| 59 | + |
| 60 | + const handleChange = useCallback((selectedTxPrefix: string, coin: CoinCode) => { |
| 61 | + if (config.backend.blockExplorers[coin] && config.backend.blockExplorers[coin] !== selectedTxPrefix) { |
| 62 | + config.backend.blockExplorers[coin] = selectedTxPrefix; |
| 63 | + updateConfigState(config); |
| 64 | + } |
| 65 | + }, [config, updateConfigState]); |
| 66 | + |
| 67 | + const save = async () => { |
| 68 | + setSaveDisabled(true); |
| 69 | + await setConfig(config); |
| 70 | + initialConfig.current = await getConfig(); |
| 71 | + }; |
| 72 | + |
| 73 | + useEffect(() => { |
| 74 | + const fetchData = async () => { |
| 75 | + const allExplorerSelection = await backendAPI.getAvailableExplorers(); |
| 76 | + |
| 77 | + // if set alongside config it will 'update' with it, but we want it to stay the same after initialization. |
| 78 | + initialConfig.current = await getConfig(); |
| 79 | + |
| 80 | + setAllSelections(allExplorerSelection); |
| 81 | + }; |
| 82 | + |
| 83 | + loadConfig(); |
| 84 | + fetchData().catch(console.error); |
| 85 | + }, []); |
| 86 | + |
| 87 | + if (config === undefined) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + return ( |
| 92 | + <div className="contentWithGuide"> |
| 93 | + <div className="container"> |
| 94 | + <div className="innerContainer scrollableContainer"> |
| 95 | + <Header |
| 96 | + hideSidebarToggler |
| 97 | + title={ |
| 98 | + <> |
| 99 | + <h2 className={'hide-on-small'}>{t('settings.expert.explorer.title')}</h2> |
| 100 | + <MobileHeader withGuide title={t('settings.expert.explorer.title')}/> |
| 101 | + </> |
| 102 | + }/> |
| 103 | + <div className="content padded"> |
| 104 | + { Array.from(availableCoins).map(coin => { |
| 105 | + return <BlockExplorers |
| 106 | + key={coin} |
| 107 | + coin={coin} |
| 108 | + explorerOptions={allSelections?.[coin] ?? []} |
| 109 | + handleOnChange={handleChange} |
| 110 | + selectedPrefix={config.backend.blockExplorers?.[coin]}/>; |
| 111 | + }) } |
| 112 | + </div> |
| 113 | + <div className="content padded" style={{ display: 'flex', justifyContent: 'space-between' }}> |
| 114 | + <ButtonLink |
| 115 | + secondary |
| 116 | + className={'hide-on-small'} |
| 117 | + to={'/settings'}> |
| 118 | + {t('button.back')} |
| 119 | + </ButtonLink> |
| 120 | + <Button primary disabled={saveDisabled} onClick={() => save()}>{t('settings.save')}</Button> |
| 121 | + </div> |
| 122 | + </div> |
| 123 | + </div> |
| 124 | + <Guide> |
| 125 | + <Entry key="guide.settingsBlockExplorer.what" entry={t('guide.settingsBlockExplorer.what', { returnObjects: true })} /> |
| 126 | + <Entry key="guide.settingsBlockExplorer.why" entry={t('guide.settingsBlockExplorer.why', { returnObjects: true })} /> |
| 127 | + <Entry key="guide.settingsBlockExplorer.options" entry={t('guide.settingsBlockExplorer.options', { returnObjects: true })} /> |
| 128 | + <Entry key="guide.settings-electrum.instructions" entry={{ |
| 129 | + link: { |
| 130 | + text: t('guide.settingsBlockExplorer.instructions.link.text'), |
| 131 | + url: (i18n.resolvedLanguage === 'de') |
| 132 | + // TODO: DE guide. |
| 133 | + ? 'https://shiftcrypto.support/help/en-us/23-bitcoin/205-how-to-use-a-block-explorer' |
| 134 | + : 'https://shiftcrypto.support/help/en-us/23-bitcoin/205-how-to-use-a-block-explorer' |
| 135 | + }, |
| 136 | + text: t('guide.settingsBlockExplorer.instructions.text'), |
| 137 | + title: t('guide.settingsBlockExplorer.instructions.title') |
| 138 | + }} /> |
| 139 | + </Guide> |
| 140 | + </div> |
| 141 | + ); |
| 142 | +}; |
0 commit comments