@unisat/wallet-state 1.0.5 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +0 -5
- package/lib/actions/global.d.ts +2 -0
- package/lib/actions/global.d.ts.map +1 -0
- package/lib/context/ApprovalContext.d.ts +9 -0
- package/lib/context/ApprovalContext.d.ts.map +1 -0
- package/lib/context/DeviceContext.d.ts +17 -0
- package/lib/context/DeviceContext.d.ts.map +1 -0
- package/lib/context/I18nContext.d.ts +13 -0
- package/lib/context/I18nContext.d.ts.map +1 -0
- package/lib/context/NavigationContext.d.ts +218 -0
- package/lib/context/NavigationContext.d.ts.map +1 -0
- package/lib/context/PriceContext.d.ts +13 -0
- package/lib/context/PriceContext.d.ts.map +1 -0
- package/lib/context/StorageContext.d.ts +60 -0
- package/lib/context/StorageContext.d.ts.map +1 -0
- package/lib/context/ToolsContext.d.ts +17 -0
- package/lib/context/ToolsContext.d.ts.map +1 -0
- package/lib/context/WalletContext.d.ts +493 -0
- package/lib/context/WalletContext.d.ts.map +1 -0
- package/lib/context/index.d.ts +9 -0
- package/lib/context/index.d.ts.map +1 -0
- package/lib/hooks/accounts.d.ts +34 -0
- package/lib/hooks/accounts.d.ts.map +1 -0
- package/lib/hooks/base.d.ts +14 -0
- package/lib/hooks/base.d.ts.map +1 -0
- package/lib/hooks/browser.d.ts +32 -0
- package/lib/hooks/browser.d.ts.map +1 -0
- package/lib/hooks/discovery.d.ts +17 -0
- package/lib/hooks/discovery.d.ts.map +1 -0
- package/lib/hooks/global.d.ts +43 -0
- package/lib/hooks/global.d.ts.map +1 -0
- package/lib/hooks/index.d.ts +10 -0
- package/lib/hooks/index.d.ts.map +1 -0
- package/lib/hooks/keyrings.d.ts +5 -0
- package/lib/hooks/keyrings.d.ts.map +1 -0
- package/lib/hooks/settings.d.ts +43 -0
- package/lib/hooks/settings.d.ts.map +1 -0
- package/lib/hooks/transactions.d.ts +67 -0
- package/lib/hooks/transactions.d.ts.map +1 -0
- package/lib/hooks/ui.d.ts +112 -0
- package/lib/hooks/ui.d.ts.map +1 -0
- package/lib/index.d.mts +2072 -199
- package/lib/index.d.ts +31 -946
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +7176 -582
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +6971 -525
- package/lib/index.mjs.map +1 -1
- package/lib/reducers/accounts.d.ts +49 -0
- package/lib/reducers/accounts.d.ts.map +1 -0
- package/lib/reducers/browser.d.ts +49 -0
- package/lib/reducers/browser.d.ts.map +1 -0
- package/lib/reducers/discovery.d.ts +22 -0
- package/lib/reducers/discovery.d.ts.map +1 -0
- package/lib/reducers/global.d.ts +35 -0
- package/lib/reducers/global.d.ts.map +1 -0
- package/lib/reducers/index.d.ts +9 -0
- package/lib/reducers/index.d.ts.map +1 -0
- package/lib/reducers/keyrings.d.ts +10 -0
- package/lib/reducers/keyrings.d.ts.map +1 -0
- package/lib/reducers/settings.d.ts +16 -0
- package/lib/reducers/settings.d.ts.map +1 -0
- package/lib/reducers/transactions.d.ts +61 -0
- package/lib/reducers/transactions.d.ts.map +1 -0
- package/lib/reducers/ui.d.ts +55 -0
- package/lib/reducers/ui.d.ts.map +1 -0
- package/lib/types/index.d.mts +7 -3
- package/lib/types/index.d.ts +3 -30
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js +7 -1
- package/lib/types/index.js.map +1 -1
- package/lib/types/index.mjs +7 -2
- package/lib/types/index.mjs.map +1 -1
- package/lib/types/ui.d.ts +31 -0
- package/lib/types/ui.d.ts.map +1 -0
- package/lib/ui-hooks/index.d.ts +55 -0
- package/lib/ui-hooks/index.d.ts.map +1 -0
- package/lib/ui-hooks/useActionOverviewSectionLogic.d.ts +12 -0
- package/lib/ui-hooks/useActionOverviewSectionLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useAddressTypeScreenLogic.d.ts +19 -0
- package/lib/ui-hooks/useAddressTypeScreenLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useAlkanesBalanceCardLogic.d.ts +19 -0
- package/lib/ui-hooks/useAlkanesBalanceCardLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useAlkanesCollectionListLogic.d.ts +11 -0
- package/lib/ui-hooks/useAlkanesCollectionListLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useAlkanesListLogic.d.ts +14 -0
- package/lib/ui-hooks/useAlkanesListLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useAlkanesNFTListLogic.d.ts +11 -0
- package/lib/ui-hooks/useAlkanesNFTListLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useAlkanesNFTScreenLogic.d.ts +9 -0
- package/lib/ui-hooks/useAlkanesNFTScreenLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useAlkanesTokenScreenLogic.d.ts +22 -0
- package/lib/ui-hooks/useAlkanesTokenScreenLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useAmountInputLogic.d.ts +22 -0
- package/lib/ui-hooks/useAmountInputLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useAnnouncementCardLogic.d.ts +11 -0
- package/lib/ui-hooks/useAnnouncementCardLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useBRC20BalanceCardLogic.d.ts +26 -0
- package/lib/ui-hooks/useBRC20BalanceCardLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useBRC20InscribeTransferLogic.d.ts +103 -0
- package/lib/ui-hooks/useBRC20InscribeTransferLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useBRC20ListLogic.d.ts +14 -0
- package/lib/ui-hooks/useBRC20ListLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useBRC20ProgListLogic.d.ts +14 -0
- package/lib/ui-hooks/useBRC20ProgListLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useBRC20SendScreenLogic.d.ts +67 -0
- package/lib/ui-hooks/useBRC20SendScreenLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useBRC20SingleStepScreenLogic.d.ts +36 -0
- package/lib/ui-hooks/useBRC20SingleStepScreenLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useBRC20TokenScreenLogic.d.ts +75 -0
- package/lib/ui-hooks/useBRC20TokenScreenLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useBalanceCardLogic.d.ts +26 -0
- package/lib/ui-hooks/useBalanceCardLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useBtcDisplayLogic.d.ts +5 -0
- package/lib/ui-hooks/useBtcDisplayLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useCAT20BalanceCardLogic.d.ts +20 -0
- package/lib/ui-hooks/useCAT20BalanceCardLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useCAT20ListLogic.d.ts +14 -0
- package/lib/ui-hooks/useCAT20ListLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useCAT20TokenScreenLogic.d.ts +19 -0
- package/lib/ui-hooks/useCAT20TokenScreenLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useCAT721ListLogic.d.ts +11 -0
- package/lib/ui-hooks/useCAT721ListLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useCAT721NFTScreenLogic.d.ts +9 -0
- package/lib/ui-hooks/useCAT721NFTScreenLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useCreatePasswordScreenLogic.d.ts +27 -0
- package/lib/ui-hooks/useCreatePasswordScreenLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useCreateWalletLogicImportWordsStep.d.ts +78 -0
- package/lib/ui-hooks/useCreateWalletLogicImportWordsStep.d.ts.map +1 -0
- package/lib/ui-hooks/useEditAccountNameScreenLogic.d.ts +18 -0
- package/lib/ui-hooks/useEditAccountNameScreenLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useEditContactScreenLogic.d.ts +24 -0
- package/lib/ui-hooks/useEditContactScreenLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useEditWalletNameScreenLogic.d.ts +15 -0
- package/lib/ui-hooks/useEditWalletNameScreenLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useExportMnemonicsScreenLogic.d.ts +18 -0
- package/lib/ui-hooks/useExportMnemonicsScreenLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useExportPrivateKeyScreenLogic.d.ts +17 -0
- package/lib/ui-hooks/useExportPrivateKeyScreenLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useFeeRateBarLogic.d.ts +34 -0
- package/lib/ui-hooks/useFeeRateBarLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useInfiniteList.d.ts +19 -0
- package/lib/ui-hooks/useInfiniteList.d.ts.map +1 -0
- package/lib/ui-hooks/useInscriptionListLogic.d.ts +11 -0
- package/lib/ui-hooks/useInscriptionListLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useLockTimePageLogic.d.ts +11 -0
- package/lib/ui-hooks/useLockTimePageLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useNotificationsLogic.d.ts +14 -0
- package/lib/ui-hooks/useNotificationsLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useOrdinalsInscriptionScreenLogic.d.ts +27 -0
- package/lib/ui-hooks/useOrdinalsInscriptionScreenLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useRunesBalanceCardLogic.d.ts +19 -0
- package/lib/ui-hooks/useRunesBalanceCardLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useRunesListLogic.d.ts +14 -0
- package/lib/ui-hooks/useRunesListLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useRunesTokenScreenLogic.d.ts +22 -0
- package/lib/ui-hooks/useRunesTokenScreenLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useSecurityCardLogic.d.ts +1 -0
- package/lib/ui-hooks/useSecurityCardLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useSendAlkanesNFTScreenLogic.d.ts +32 -0
- package/lib/ui-hooks/useSendAlkanesNFTScreenLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useSendAlkanesScreenLogic.d.ts +37 -0
- package/lib/ui-hooks/useSendAlkanesScreenLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useSendCAT20ScreenLogic.d.ts +44 -0
- package/lib/ui-hooks/useSendCAT20ScreenLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useSendCAT721ScreenLogic.d.ts +40 -0
- package/lib/ui-hooks/useSendCAT721ScreenLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useSendOrdinalsInscriptionScreenLogic.d.ts +22 -0
- package/lib/ui-hooks/useSendOrdinalsInscriptionScreenLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useSendRunesScreenLogic.d.ts +30 -0
- package/lib/ui-hooks/useSendRunesScreenLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useSettingsTabScreenLogic.d.ts +72 -0
- package/lib/ui-hooks/useSettingsTabScreenLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useSignMessageLogic.d.ts +54 -0
- package/lib/ui-hooks/useSignMessageLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useSignPsbtLogic.d.ts +82 -0
- package/lib/ui-hooks/useSignPsbtLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useSplitOrdinalsInscriptionScreenLogic.d.ts +15 -0
- package/lib/ui-hooks/useSplitOrdinalsInscriptionScreenLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useTxConfirmScreenLogic.d.ts +8 -0
- package/lib/ui-hooks/useTxConfirmScreenLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useTxCreateScreenLogic.d.ts +28 -0
- package/lib/ui-hooks/useTxCreateScreenLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useTxFailScreenLogic.d.ts +7 -0
- package/lib/ui-hooks/useTxFailScreenLogic.d.ts.map +1 -0
- package/lib/ui-hooks/useTxSuccessScreenLogic.d.ts +8 -0
- package/lib/ui-hooks/useTxSuccessScreenLogic.d.ts.map +1 -0
- package/lib/updater/accounts.d.ts +2 -0
- package/lib/updater/accounts.d.ts.map +1 -0
- package/lib/updater/index.d.ts +2 -0
- package/lib/updater/index.d.ts.map +1 -0
- package/lib/utils/bitcoin-utils.d.ts +7 -0
- package/lib/utils/bitcoin-utils.d.ts.map +1 -0
- package/lib/utils/eventBus.d.ts +12 -0
- package/lib/utils/eventBus.d.ts.map +1 -0
- package/lib/utils/password-utils.d.ts +12 -0
- package/lib/utils/password-utils.d.ts.map +1 -0
- package/lib/utils/ui-utils.d.ts +4 -0
- package/lib/utils/ui-utils.d.ts.map +1 -0
- package/package.json +19 -15
- package/src/.DS_Store +0 -0
- package/src/context/ApprovalContext.tsx +27 -0
- package/src/context/DeviceContext.tsx +36 -0
- package/src/context/I18nContext.tsx +14 -172
- package/src/context/NavigationContext.tsx +305 -0
- package/src/context/PriceContext.tsx +2 -2
- package/src/context/StorageContext.tsx +393 -0
- package/src/context/ToolsContext.tsx +50 -0
- package/src/context/WalletContext.tsx +170 -126
- package/src/context/index.ts +17 -2
- package/src/hooks/accounts.ts +11 -5
- package/src/hooks/browser.ts +11 -0
- package/src/hooks/global.ts +170 -7
- package/src/hooks/index.ts +1 -2
- package/src/hooks/settings.ts +38 -37
- package/src/hooks/transactions.ts +45 -151
- package/src/hooks/ui.ts +239 -35
- package/src/index.ts +33 -24
- package/src/reducers/accounts.ts +22 -2
- package/src/reducers/browser.ts +223 -0
- package/src/reducers/global.ts +67 -1
- package/src/reducers/index.ts +1 -0
- package/src/reducers/transactions.ts +0 -9
- package/src/reducers/ui.ts +144 -4
- package/src/types/index.ts +1 -1
- package/src/types/ui.ts +6 -1
- package/src/ui-hooks/index.ts +107 -0
- package/src/ui-hooks/useActionOverviewSectionLogic.ts +150 -0
- package/src/ui-hooks/useAddressTypeScreenLogic.ts +160 -0
- package/src/ui-hooks/useAlkanesBalanceCardLogic.ts +41 -0
- package/src/ui-hooks/useAlkanesCollectionListLogic.ts +68 -0
- package/src/ui-hooks/useAlkanesListLogic.ts +69 -0
- package/src/ui-hooks/useAlkanesNFTListLogic.ts +42 -0
- package/src/ui-hooks/useAlkanesNFTScreenLogic.ts +45 -0
- package/src/ui-hooks/useAlkanesTokenScreenLogic.ts +138 -0
- package/src/ui-hooks/useAmountInputLogic.ts +80 -0
- package/src/ui-hooks/useAnnouncementCardLogic.ts +91 -0
- package/src/ui-hooks/useBRC20BalanceCardLogic.ts +115 -0
- package/src/ui-hooks/useBRC20InscribeTransferLogic.ts +418 -0
- package/src/ui-hooks/useBRC20ListLogic.ts +79 -0
- package/src/ui-hooks/useBRC20ProgListLogic.ts +81 -0
- package/src/ui-hooks/useBRC20SendScreenLogic.ts +424 -0
- package/src/ui-hooks/useBRC20SingleStepScreenLogic.ts +208 -0
- package/src/ui-hooks/useBRC20TokenScreenLogic.ts +489 -0
- package/src/ui-hooks/useBalanceCardLogic.ts +164 -0
- package/src/ui-hooks/useBtcDisplayLogic.ts +16 -0
- package/src/ui-hooks/useCAT20BalanceCardLogic.ts +35 -0
- package/src/ui-hooks/useCAT20ListLogic.ts +83 -0
- package/src/ui-hooks/useCAT20TokenScreenLogic.ts +127 -0
- package/src/ui-hooks/useCAT721ListLogic.ts +68 -0
- package/src/ui-hooks/useCAT721NFTScreenLogic.ts +37 -0
- package/src/ui-hooks/useCreatePasswordScreenLogic.ts +92 -0
- package/src/ui-hooks/useCreateWalletLogicImportWordsStep.ts +299 -0
- package/src/ui-hooks/useEditAccountNameScreenLogic.ts +71 -0
- package/src/ui-hooks/useEditContactScreenLogic.ts +162 -0
- package/src/ui-hooks/useEditWalletNameScreenLogic.ts +60 -0
- package/src/ui-hooks/useExportMnemonicsScreenLogic.ts +75 -0
- package/src/ui-hooks/useExportPrivateKeyScreenLogic.ts +64 -0
- package/src/ui-hooks/useFeeRateBarLogic.ts +303 -0
- package/src/ui-hooks/useInfiniteList.ts +85 -0
- package/src/ui-hooks/useInscriptionListLogic.ts +68 -0
- package/src/ui-hooks/useLockTimePageLogic.ts +43 -0
- package/src/ui-hooks/useNotificationsLogic.ts +115 -0
- package/src/ui-hooks/useOrdinalsInscriptionScreenLogic.ts +130 -0
- package/src/ui-hooks/useRunesBalanceCardLogic.ts +44 -0
- package/src/ui-hooks/useRunesListLogic.ts +74 -0
- package/src/ui-hooks/useRunesTokenScreenLogic.ts +156 -0
- package/src/ui-hooks/useSecurityCardLogic.ts +0 -0
- package/src/ui-hooks/useSendAlkanesNFTScreenLogic.ts +160 -0
- package/src/ui-hooks/useSendAlkanesScreenLogic.ts +208 -0
- package/src/ui-hooks/useSendCAT20ScreenLogic.ts +297 -0
- package/src/ui-hooks/useSendCAT721ScreenLogic.ts +205 -0
- package/src/ui-hooks/useSendOrdinalsInscriptionScreenLogic.ts +152 -0
- package/src/ui-hooks/useSendRunesScreenLogic.ts +189 -0
- package/src/ui-hooks/useSettingsTabScreenLogic.ts +211 -0
- package/src/ui-hooks/useSignMessageLogic.ts +302 -0
- package/src/ui-hooks/useSignPsbtLogic.ts +517 -0
- package/src/ui-hooks/useSplitOrdinalsInscriptionScreenLogic.ts +109 -0
- package/src/ui-hooks/useTxConfirmScreenLogic.ts +47 -0
- package/src/ui-hooks/useTxCreateScreenLogic.ts +177 -0
- package/src/ui-hooks/useTxFailScreenLogic.ts +26 -0
- package/src/ui-hooks/useTxSuccessScreenLogic.ts +33 -0
- package/src/updater/accounts.ts +11 -11
- package/src/utils/bitcoin-utils.ts +17 -8
- package/src/utils/eventBus.ts +2 -1
- package/src/utils/password-utils.ts +78 -0
- package/src/utils/ui-utils.ts +28 -0
- package/src/hooks/approval.ts +0 -72
- package/src/hooks/i18n.ts +0 -53
- package/src/utils/i18n.ts +0 -41
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { useEffect, useMemo, useState } from 'react'
|
|
2
|
+
|
|
3
|
+
import { numUtils } from '@unisat/base-utils'
|
|
4
|
+
import { COIN_DUST } from '@unisat/wallet-shared'
|
|
5
|
+
import {
|
|
6
|
+
useAccountBalance,
|
|
7
|
+
useBTCUnit,
|
|
8
|
+
useChain,
|
|
9
|
+
useFeeRateBar,
|
|
10
|
+
useFetchUtxosCallback,
|
|
11
|
+
useI18n,
|
|
12
|
+
useNavigation,
|
|
13
|
+
usePrepareSendBTCCallback,
|
|
14
|
+
useTools,
|
|
15
|
+
useWallet,
|
|
16
|
+
useUiTxCreateScreen,
|
|
17
|
+
useUpdateUiTxCreateScreen,
|
|
18
|
+
useWalletConfig,
|
|
19
|
+
} from '..'
|
|
20
|
+
import { isValidAddress } from '../utils/bitcoin-utils'
|
|
21
|
+
|
|
22
|
+
export function useTxCreateScreenLogic() {
|
|
23
|
+
const { t, isSpecialLocale } = useI18n()
|
|
24
|
+
const accountBalance = useAccountBalance()
|
|
25
|
+
const nav = useNavigation()
|
|
26
|
+
const btcUnit = useBTCUnit()
|
|
27
|
+
|
|
28
|
+
const [disabled, setDisabled] = useState(true)
|
|
29
|
+
|
|
30
|
+
const setUiState = useUpdateUiTxCreateScreen()
|
|
31
|
+
const uiState = useUiTxCreateScreen()
|
|
32
|
+
const feeRateBarState = useFeeRateBar()
|
|
33
|
+
|
|
34
|
+
const toInfo = uiState.toInfo
|
|
35
|
+
const inputAmount = uiState.inputAmount
|
|
36
|
+
const enableRBF = uiState.enableRBF
|
|
37
|
+
const feeRate = feeRateBarState.feeRate
|
|
38
|
+
|
|
39
|
+
const [error, setError] = useState('')
|
|
40
|
+
|
|
41
|
+
const [autoAdjust, setAutoAdjust] = useState(false)
|
|
42
|
+
const fetchUtxos = useFetchUtxosCallback()
|
|
43
|
+
const wallet = useWallet()
|
|
44
|
+
|
|
45
|
+
const tools = useTools()
|
|
46
|
+
useEffect(() => {
|
|
47
|
+
tools.showLoading(true)
|
|
48
|
+
fetchUtxos().finally(() => {
|
|
49
|
+
tools.showLoading(false)
|
|
50
|
+
})
|
|
51
|
+
}, [])
|
|
52
|
+
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
wallet.getEnableRBF().then(enableRBF => {
|
|
55
|
+
setUiState({ enableRBF })
|
|
56
|
+
})
|
|
57
|
+
}, [wallet, setUiState])
|
|
58
|
+
|
|
59
|
+
const prepareSendBTC = usePrepareSendBTCCallback()
|
|
60
|
+
|
|
61
|
+
const toSatoshis = useMemo(() => {
|
|
62
|
+
if (!inputAmount) return 0
|
|
63
|
+
return numUtils.amountToSatoshis(inputAmount)
|
|
64
|
+
}, [inputAmount])
|
|
65
|
+
|
|
66
|
+
const dustAmount = useMemo(() => numUtils.satoshisToAmount(COIN_DUST), [COIN_DUST])
|
|
67
|
+
|
|
68
|
+
const availableAmount = numUtils.satoshisToAmount(accountBalance.availableBalance)
|
|
69
|
+
const unavailableAmount = numUtils.satoshisToAmount(accountBalance.unavailableBalance)
|
|
70
|
+
|
|
71
|
+
const showUnavailable = accountBalance.unavailableBalance > 0
|
|
72
|
+
|
|
73
|
+
const chain = useChain()
|
|
74
|
+
useEffect(() => {
|
|
75
|
+
setError('')
|
|
76
|
+
setDisabled(true)
|
|
77
|
+
if (!isValidAddress(toInfo.address)) {
|
|
78
|
+
return
|
|
79
|
+
}
|
|
80
|
+
if (!toSatoshis) {
|
|
81
|
+
return
|
|
82
|
+
}
|
|
83
|
+
if (toSatoshis < COIN_DUST) {
|
|
84
|
+
setError(`${t('amount_must_be_at_least')} ${dustAmount} ${btcUnit}`)
|
|
85
|
+
return
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (toSatoshis > accountBalance.availableBalance) {
|
|
89
|
+
setError(t('amount_exceeds_your_available_balance'))
|
|
90
|
+
return
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (feeRate <= 0) {
|
|
94
|
+
return
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
setDisabled(false)
|
|
98
|
+
}, [toInfo, inputAmount, feeRate])
|
|
99
|
+
|
|
100
|
+
const walletConfig = useWalletConfig()
|
|
101
|
+
|
|
102
|
+
const unavailableTipText = useMemo(() => {
|
|
103
|
+
let tipText = ''
|
|
104
|
+
tipText += t('unavailable_tooltip')
|
|
105
|
+
|
|
106
|
+
if (walletConfig.disableUtxoTools) {
|
|
107
|
+
tipText += t('future_versions_will_support_spending_these_assets')
|
|
108
|
+
} else {
|
|
109
|
+
tipText += t('you_can_unlock_these_assets_by_using_the_utxos_tools')
|
|
110
|
+
}
|
|
111
|
+
return tipText
|
|
112
|
+
}, [chain.enum])
|
|
113
|
+
|
|
114
|
+
const headerTitle = `${t('send')} ${btcUnit}`
|
|
115
|
+
|
|
116
|
+
const onAddressInputChange = val => setUiState({ toInfo: val })
|
|
117
|
+
|
|
118
|
+
const onAmountInputChange = amount => {
|
|
119
|
+
if (autoAdjust == true) {
|
|
120
|
+
setAutoAdjust(false)
|
|
121
|
+
}
|
|
122
|
+
setUiState({ inputAmount: amount })
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const onAmountMaxClick = () => {
|
|
126
|
+
setAutoAdjust(true)
|
|
127
|
+
setUiState({ inputAmount: availableAmount.toString() })
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const onRBFChange = (value: boolean) => {
|
|
131
|
+
setUiState({ enableRBF: value })
|
|
132
|
+
wallet.setEnableRBF(value)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const onClickNext = () => {
|
|
136
|
+
prepareSendBTC({ toAddressInfo: toInfo, toAmount: toSatoshis, feeRate, enableRBF })
|
|
137
|
+
.then(toSignData => {
|
|
138
|
+
nav.navigate('TxConfirmScreen', {
|
|
139
|
+
toSignData,
|
|
140
|
+
})
|
|
141
|
+
})
|
|
142
|
+
.catch(e => {
|
|
143
|
+
console.log(e)
|
|
144
|
+
setError(e.message)
|
|
145
|
+
})
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
headerTitle,
|
|
150
|
+
chain,
|
|
151
|
+
|
|
152
|
+
toInfo,
|
|
153
|
+
onAddressInputChange,
|
|
154
|
+
|
|
155
|
+
toSatoshis,
|
|
156
|
+
inputAmount,
|
|
157
|
+
onAmountInputChange,
|
|
158
|
+
onAmountMaxClick,
|
|
159
|
+
enableRBF,
|
|
160
|
+
onRBFChange,
|
|
161
|
+
|
|
162
|
+
showUnavailable,
|
|
163
|
+
availableAmount,
|
|
164
|
+
unavailableAmount,
|
|
165
|
+
unavailableTipText,
|
|
166
|
+
btcUnit,
|
|
167
|
+
t,
|
|
168
|
+
|
|
169
|
+
walletConfig,
|
|
170
|
+
isSpecialLocale,
|
|
171
|
+
|
|
172
|
+
error,
|
|
173
|
+
disabled,
|
|
174
|
+
|
|
175
|
+
onClickNext,
|
|
176
|
+
}
|
|
177
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useI18n, useNavigation } from 'src/context'
|
|
2
|
+
|
|
3
|
+
export function useTxFailScreenLogic() {
|
|
4
|
+
const nav = useNavigation()
|
|
5
|
+
const { error } = nav.getRouteState<'TxFailScreen'>()
|
|
6
|
+
const { t } = useI18n()
|
|
7
|
+
|
|
8
|
+
const onClickBack = () => {
|
|
9
|
+
nav.goBack()
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const onClickDone = () => {
|
|
13
|
+
nav.navigate('TabMainScreen')
|
|
14
|
+
}
|
|
15
|
+
return {
|
|
16
|
+
// info
|
|
17
|
+
error,
|
|
18
|
+
|
|
19
|
+
// actions
|
|
20
|
+
onClickBack,
|
|
21
|
+
onClickDone,
|
|
22
|
+
|
|
23
|
+
// tools
|
|
24
|
+
t,
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { useI18n, useNavigation, useTools } from 'src/context'
|
|
2
|
+
|
|
3
|
+
export function useTxSuccessScreenLogic() {
|
|
4
|
+
const nav = useNavigation()
|
|
5
|
+
const { txid } = nav.getRouteState<'TxSuccessScreen'>()
|
|
6
|
+
const { t } = useI18n()
|
|
7
|
+
const tools = useTools()
|
|
8
|
+
|
|
9
|
+
const onClickExploreTx = () => {
|
|
10
|
+
nav.navToExplorerTx(txid)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const onClickDone = () => {
|
|
14
|
+
nav.navigate('MainScreen')
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const onClickCopy = () => {
|
|
18
|
+
tools.copyToClipboard(txid)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return {
|
|
22
|
+
// data
|
|
23
|
+
txid,
|
|
24
|
+
|
|
25
|
+
// actions
|
|
26
|
+
onClickExploreTx,
|
|
27
|
+
onClickDone,
|
|
28
|
+
onClickCopy,
|
|
29
|
+
|
|
30
|
+
// tools
|
|
31
|
+
t,
|
|
32
|
+
}
|
|
33
|
+
}
|
package/src/updater/accounts.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { useCallback, useEffect, useRef } from 'react'
|
|
2
2
|
|
|
3
|
-
import { Account } from '@unisat/wallet-shared'
|
|
4
|
-
import
|
|
3
|
+
import { Account, BUS_METHODS } from '@unisat/wallet-shared'
|
|
4
|
+
import { uiEventBus } from '../utils/eventBus'
|
|
5
5
|
|
|
6
6
|
import { useIsUnlocked } from '../hooks/global'
|
|
7
7
|
import { globalActions } from '../reducers/global'
|
|
@@ -18,7 +18,7 @@ export function AccountUpdater() {
|
|
|
18
18
|
const currentAccount = useCurrentAccount()
|
|
19
19
|
const isUnlocked = useIsUnlocked()
|
|
20
20
|
const selfRef = useRef({
|
|
21
|
-
preAccountKey: '',
|
|
21
|
+
preAccountKey: '_',
|
|
22
22
|
loadingBalance: false,
|
|
23
23
|
loadingHistory: false,
|
|
24
24
|
})
|
|
@@ -61,9 +61,9 @@ export function AccountUpdater() {
|
|
|
61
61
|
dispatch((accountActions as any).setCurrent(account))
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
|
-
|
|
64
|
+
uiEventBus.addEventListener(BUS_METHODS.ACCOUNTS_CHANGED, accountChangeHandler)
|
|
65
65
|
return () => {
|
|
66
|
-
|
|
66
|
+
uiEventBus.removeEventListener(BUS_METHODS.ACCOUNTS_CHANGED, accountChangeHandler)
|
|
67
67
|
}
|
|
68
68
|
}, [dispatch])
|
|
69
69
|
|
|
@@ -77,9 +77,9 @@ export function AccountUpdater() {
|
|
|
77
77
|
|
|
78
78
|
reloadAccounts()
|
|
79
79
|
}
|
|
80
|
-
|
|
80
|
+
uiEventBus.addEventListener(BUS_METHODS.CHAIN_CHANGED, chaintChangeHandler)
|
|
81
81
|
return () => {
|
|
82
|
-
|
|
82
|
+
uiEventBus.removeEventListener(BUS_METHODS.CHAIN_CHANGED, chaintChangeHandler)
|
|
83
83
|
}
|
|
84
84
|
}, [dispatch])
|
|
85
85
|
|
|
@@ -87,9 +87,9 @@ export function AccountUpdater() {
|
|
|
87
87
|
const lockHandler = () => {
|
|
88
88
|
dispatch(globalActions.update({ isUnlocked: false }))
|
|
89
89
|
}
|
|
90
|
-
|
|
90
|
+
uiEventBus.addEventListener(BUS_METHODS.LOCKED, lockHandler)
|
|
91
91
|
return () => {
|
|
92
|
-
|
|
92
|
+
uiEventBus.removeEventListener(BUS_METHODS.LOCKED, lockHandler)
|
|
93
93
|
}
|
|
94
94
|
}, [dispatch])
|
|
95
95
|
|
|
@@ -97,9 +97,9 @@ export function AccountUpdater() {
|
|
|
97
97
|
const unlockHandler = () => {
|
|
98
98
|
dispatch(globalActions.update({ isUnlocked: true }))
|
|
99
99
|
}
|
|
100
|
-
|
|
100
|
+
uiEventBus.addEventListener(BUS_METHODS.UNLOCKED, unlockHandler)
|
|
101
101
|
return () => {
|
|
102
|
-
|
|
102
|
+
uiEventBus.removeEventListener(BUS_METHODS.UNLOCKED, unlockHandler)
|
|
103
103
|
}
|
|
104
104
|
}, [dispatch])
|
|
105
105
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { addressUtils } from '@unisat/base-utils'
|
|
1
2
|
import { AddressType, NetworkType } from '@unisat/wallet-types'
|
|
2
|
-
|
|
3
|
+
import * as bip39 from 'bip39'
|
|
3
4
|
export function getAddressType(address: string, networkType?: NetworkType) {
|
|
4
5
|
if (address.startsWith('bc1q') || address.startsWith('tb1q')) {
|
|
5
6
|
return AddressType.P2WPKH
|
|
@@ -15,18 +16,17 @@ export function getAddressType(address: string, networkType?: NetworkType) {
|
|
|
15
16
|
}
|
|
16
17
|
|
|
17
18
|
export function isValidAddress(address: string, networkType?: NetworkType) {
|
|
18
|
-
|
|
19
|
-
if (addressType === AddressType.UNKNOWN) {
|
|
20
|
-
return false
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return true
|
|
19
|
+
return addressUtils.isAddressLikelyValid(address)
|
|
24
20
|
}
|
|
25
21
|
|
|
26
22
|
export function getAddressUtxoDust(address: string) {
|
|
27
23
|
const addressType = getAddressType(address)
|
|
28
24
|
if (addressType === AddressType.P2WPKH) {
|
|
29
|
-
|
|
25
|
+
if (address.length === 42) {
|
|
26
|
+
return 294
|
|
27
|
+
} else {
|
|
28
|
+
return 330
|
|
29
|
+
}
|
|
30
30
|
} else if (addressType === AddressType.P2TR) {
|
|
31
31
|
return 330
|
|
32
32
|
} else {
|
|
@@ -79,3 +79,12 @@ export function isValidHdPath(path: string): boolean {
|
|
|
79
79
|
|
|
80
80
|
return true
|
|
81
81
|
}
|
|
82
|
+
|
|
83
|
+
export function validateMnemonic(mnemonic: string): boolean {
|
|
84
|
+
// do not use bip39.validateMnemonic here to reduce bundle size
|
|
85
|
+
// const words = mnemonic.trim().split(/\s+/);
|
|
86
|
+
// const wordCount = words.length;
|
|
87
|
+
// return [12, 15, 18, 21, 24].includes(wordCount);
|
|
88
|
+
|
|
89
|
+
return bip39.validateMnemonic(mnemonic)
|
|
90
|
+
}
|
package/src/utils/eventBus.ts
CHANGED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
export const MIN_PASSWORD_LENGTH = 8
|
|
2
|
+
export const UNRECOGNIZED_PASSWORD_STRENGTH = 'Unrecognized password strength.'
|
|
3
|
+
|
|
4
|
+
const calculatePasswordStrength = (password: string): number => {
|
|
5
|
+
let score = 0
|
|
6
|
+
|
|
7
|
+
// Length check
|
|
8
|
+
if (password.length >= 8) score += 1
|
|
9
|
+
if (password.length >= 12) score += 1
|
|
10
|
+
if (password.length >= 16) score += 1
|
|
11
|
+
|
|
12
|
+
// Character variety checks
|
|
13
|
+
if (/[a-z]/.test(password)) score += 1 // lowercase
|
|
14
|
+
if (/[A-Z]/.test(password)) score += 1 // uppercase
|
|
15
|
+
if (/[0-9]/.test(password)) score += 1 // numbers
|
|
16
|
+
if (/[^A-Za-z0-9]/.test(password)) score += 1 // special characters
|
|
17
|
+
|
|
18
|
+
// Bonus for good mix
|
|
19
|
+
if (
|
|
20
|
+
password.length >= 10 &&
|
|
21
|
+
/[a-z]/.test(password) &&
|
|
22
|
+
/[A-Z]/.test(password) &&
|
|
23
|
+
/[0-9]/.test(password) &&
|
|
24
|
+
/[^A-Za-z0-9]/.test(password)
|
|
25
|
+
) {
|
|
26
|
+
score += 1
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Penalty for common patterns
|
|
30
|
+
if (/(.)\1{2,}/.test(password)) score -= 1 // repeated characters (aaa, 111)
|
|
31
|
+
if (/123|abc|qwe|asd|zxc/i.test(password)) score -= 1 // common sequences
|
|
32
|
+
if (/password|123456|qwerty/i.test(password)) score -= 2 // common passwords
|
|
33
|
+
|
|
34
|
+
return Math.max(0, Math.min(4, score))
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const colors = {
|
|
38
|
+
red: '#ED334B',
|
|
39
|
+
green: '#41B530',
|
|
40
|
+
orange: '#FF7B21',
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export const getPasswordStrengthWord = (password: string, t) => {
|
|
44
|
+
if (password.length < MIN_PASSWORD_LENGTH) {
|
|
45
|
+
return {
|
|
46
|
+
text: t('not_long_enough'),
|
|
47
|
+
color: colors.red,
|
|
48
|
+
tip: t('password_must_be_at_least_8_characters'),
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const strength = calculatePasswordStrength(password)
|
|
53
|
+
|
|
54
|
+
if (strength <= 1) {
|
|
55
|
+
return {
|
|
56
|
+
text: t('weak'),
|
|
57
|
+
color: colors.red,
|
|
58
|
+
tip: t('strong_password_tip'),
|
|
59
|
+
}
|
|
60
|
+
} else if (strength === 2) {
|
|
61
|
+
return {
|
|
62
|
+
text: t('weak'),
|
|
63
|
+
color: colors.red,
|
|
64
|
+
tip: t('strong_password_tip'),
|
|
65
|
+
}
|
|
66
|
+
} else if (strength === 3) {
|
|
67
|
+
return {
|
|
68
|
+
text: t('average'),
|
|
69
|
+
color: colors.orange,
|
|
70
|
+
tip: t('strong_password_tip'),
|
|
71
|
+
}
|
|
72
|
+
} else {
|
|
73
|
+
return {
|
|
74
|
+
text: t('strong'),
|
|
75
|
+
color: colors.green,
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { useEffect } from 'react'
|
|
2
|
+
|
|
3
|
+
export function shortAddress(address?: string, len = 8) {
|
|
4
|
+
if (!address) return ''
|
|
5
|
+
if (address.length <= len * 2) return address
|
|
6
|
+
return address.slice(0, len) + '...' + address.slice(address.length - len)
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function useAsyncEffect(
|
|
10
|
+
effect: () => Promise<void | (() => void)>,
|
|
11
|
+
deps: React.DependencyList = []
|
|
12
|
+
) {
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
let isMounted = true
|
|
15
|
+
let cleanup: void | (() => void)
|
|
16
|
+
;(async () => {
|
|
17
|
+
cleanup = await effect()
|
|
18
|
+
})()
|
|
19
|
+
|
|
20
|
+
return () => {
|
|
21
|
+
isMounted = false
|
|
22
|
+
if (cleanup) {
|
|
23
|
+
cleanup()
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
27
|
+
}, deps)
|
|
28
|
+
}
|
package/src/hooks/approval.ts
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { useEffect, useRef, useState } from 'react'
|
|
2
|
-
import { useNavigate } from 'react-router-dom'
|
|
3
|
-
|
|
4
|
-
import { useWallet } from '../context/WalletContext'
|
|
5
|
-
|
|
6
|
-
const UI_TYPE = {
|
|
7
|
-
Tab: 'index',
|
|
8
|
-
Pop: 'popup',
|
|
9
|
-
Notification: 'notification',
|
|
10
|
-
SidePanel: 'sidepanel',
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
type UiTypeCheck = {
|
|
14
|
-
isTab: boolean
|
|
15
|
-
isNotification: boolean
|
|
16
|
-
isPop: boolean
|
|
17
|
-
isSidePanel: boolean
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export const getUiType = (): UiTypeCheck => {
|
|
21
|
-
// @ts-ignore
|
|
22
|
-
const { pathname } = window.location
|
|
23
|
-
return Object.entries(UI_TYPE).reduce((m, [key, value]) => {
|
|
24
|
-
// @ts-ignore
|
|
25
|
-
m[`is${key}`] = pathname === `/${value}.html`
|
|
26
|
-
|
|
27
|
-
return m
|
|
28
|
-
}, {} as UiTypeCheck)
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export const useApproval = () => {
|
|
32
|
-
const wallet = useWallet()
|
|
33
|
-
const navigate = useNavigate()
|
|
34
|
-
const getApproval = wallet.getApproval
|
|
35
|
-
|
|
36
|
-
const resolveApproval = async (data?: any, stay = false, forceReject = false) => {
|
|
37
|
-
const approval = await getApproval()
|
|
38
|
-
|
|
39
|
-
if (approval) {
|
|
40
|
-
wallet.resolveApproval(data, forceReject)
|
|
41
|
-
}
|
|
42
|
-
if (stay) {
|
|
43
|
-
return
|
|
44
|
-
}
|
|
45
|
-
setTimeout(() => {
|
|
46
|
-
navigate('/')
|
|
47
|
-
})
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const rejectApproval = async (err: any, stay = false, isInternal = false) => {
|
|
51
|
-
const approval = await getApproval()
|
|
52
|
-
if (approval) {
|
|
53
|
-
await wallet.rejectApproval(err, stay, isInternal)
|
|
54
|
-
}
|
|
55
|
-
if (!stay) {
|
|
56
|
-
navigate('/')
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
useEffect(() => {
|
|
61
|
-
if (!getUiType().isNotification) {
|
|
62
|
-
return () => {}
|
|
63
|
-
}
|
|
64
|
-
// @ts-ignore
|
|
65
|
-
window.addEventListener('beforeunload', rejectApproval)
|
|
66
|
-
|
|
67
|
-
// @ts-ignore
|
|
68
|
-
return () => window.removeEventListener('beforeunload', rejectApproval)
|
|
69
|
-
}, [])
|
|
70
|
-
|
|
71
|
-
return [getApproval, resolveApproval, rejectApproval] as const
|
|
72
|
-
}
|
package/src/hooks/i18n.ts
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { useContext } from 'react'
|
|
2
|
-
|
|
3
|
-
import { I18nContext } from '../context/I18nContext'
|
|
4
|
-
import { FALLBACK_LOCALE, getCurrentLocaleAsync, LOCALE_NAMES } from '@unisat/i18n'
|
|
5
|
-
|
|
6
|
-
const defaultI18nContext = {
|
|
7
|
-
t: (key: string) => key,
|
|
8
|
-
locale: FALLBACK_LOCALE,
|
|
9
|
-
supportedLocales: [FALLBACK_LOCALE],
|
|
10
|
-
localeNames: LOCALE_NAMES,
|
|
11
|
-
changeLocale: async () => {
|
|
12
|
-
/* empty implementation */
|
|
13
|
-
},
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Use i18n Hook
|
|
18
|
-
* @returns i18n context
|
|
19
|
-
*/
|
|
20
|
-
export const useI18n = () => {
|
|
21
|
-
try {
|
|
22
|
-
const context = useContext(I18nContext)
|
|
23
|
-
|
|
24
|
-
if (!context) {
|
|
25
|
-
console.warn('useI18n must be used within an I18nProvider, using default context instead')
|
|
26
|
-
return defaultI18nContext
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return context as any
|
|
30
|
-
} catch (error) {
|
|
31
|
-
console.error('Error in useI18n:', error)
|
|
32
|
-
return defaultI18nContext
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Get current language
|
|
38
|
-
* @returns current language code
|
|
39
|
-
*/
|
|
40
|
-
export const getCurrentLocale = async (): Promise<string> => {
|
|
41
|
-
return await getCurrentLocaleAsync()
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Select special languages for style adaptation
|
|
46
|
-
* @returns { currentLocale: string, isSpecialLocale: boolean }
|
|
47
|
-
*/
|
|
48
|
-
export const getSpecialLocale = async () => {
|
|
49
|
-
const currentLocale = await getCurrentLocale()
|
|
50
|
-
const specialLocales = ['es', 'ru', 'fr', 'ja']
|
|
51
|
-
const isSpecialLocale = specialLocales.includes(currentLocale)
|
|
52
|
-
return { currentLocale, isSpecialLocale }
|
|
53
|
-
}
|
package/src/utils/i18n.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import i18n from 'i18next'
|
|
2
|
-
import { initReactI18next } from 'react-i18next'
|
|
3
|
-
|
|
4
|
-
export const fetchLocale = async locale => {
|
|
5
|
-
// @ts-ignore
|
|
6
|
-
const res = await fetch(`./_locales/${locale}/messages.json`)
|
|
7
|
-
const data: Record<string, { message: string; description: string }> = await res.json()
|
|
8
|
-
return Object.keys(data).reduce((res, key) => {
|
|
9
|
-
return {
|
|
10
|
-
...res,
|
|
11
|
-
[key.replace(/__/g, ' ')]: data[key].message,
|
|
12
|
-
}
|
|
13
|
-
}, {})
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
i18n
|
|
17
|
-
.use(initReactI18next) // passes i18n down to react-i18next
|
|
18
|
-
.init({
|
|
19
|
-
fallbackLng: 'en',
|
|
20
|
-
defaultNS: 'translations',
|
|
21
|
-
interpolation: {
|
|
22
|
-
escapeValue: false, // react already safes from xss
|
|
23
|
-
},
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
export const I18N_NS = 'translations'
|
|
27
|
-
|
|
28
|
-
export const addResourceBundle = async (locale: string) => {
|
|
29
|
-
if (i18n.hasResourceBundle(locale, I18N_NS)) return
|
|
30
|
-
const bundle = await fetchLocale(locale)
|
|
31
|
-
|
|
32
|
-
i18n.addResourceBundle(locale, 'translations', bundle)
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
addResourceBundle('en')
|
|
36
|
-
|
|
37
|
-
i18n.on('languageChanged', function (lng: string) {
|
|
38
|
-
addResourceBundle(lng)
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
export default i18n
|