@unisat/wallet-state 1.0.0 → 1.0.2

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.
Files changed (40) hide show
  1. package/lib/index.d.mts +934 -0
  2. package/lib/index.d.ts +934 -0
  3. package/lib/index.js +2225 -0
  4. package/lib/index.js.map +1 -0
  5. package/lib/index.mjs +2124 -0
  6. package/lib/index.mjs.map +1 -0
  7. package/package.json +19 -5
  8. package/src/actions/global.ts +5 -0
  9. package/src/context/I18nContext.tsx +191 -0
  10. package/src/context/PriceContext.tsx +81 -0
  11. package/src/context/WalletContext.tsx +703 -0
  12. package/src/context/index.ts +3 -0
  13. package/src/hooks/accounts.ts +23 -21
  14. package/src/hooks/approval.ts +72 -0
  15. package/src/hooks/base.ts +6 -0
  16. package/src/hooks/discovery.ts +29 -0
  17. package/src/hooks/global.ts +129 -5
  18. package/src/hooks/i18n.ts +53 -0
  19. package/src/hooks/index.ts +9 -15
  20. package/src/hooks/keyrings.ts +14 -5
  21. package/src/hooks/settings.ts +318 -5
  22. package/src/hooks/transactions.ts +44 -38
  23. package/src/hooks/ui.ts +133 -5
  24. package/src/index.ts +42 -5
  25. package/src/{slices → reducers}/accounts.ts +12 -12
  26. package/src/reducers/discovery.ts +73 -0
  27. package/src/reducers/global.ts +51 -0
  28. package/src/{slices → reducers}/keyrings.ts +7 -6
  29. package/src/{slices → reducers}/settings.ts +5 -5
  30. package/src/{slices → reducers}/transactions.ts +4 -5
  31. package/src/{slices → reducers}/ui.ts +4 -5
  32. package/src/updater/accounts.ts +107 -0
  33. package/src/updater/index.ts +1 -0
  34. package/src/utils/bitcoin-utils.ts +81 -0
  35. package/src/utils/eventBus.ts +49 -0
  36. package/src/utils/i18n.ts +41 -0
  37. package/src/slices/global.ts +0 -52
  38. package/src/slices/index.ts +0 -10
  39. package/src/store/index.ts +0 -43
  40. package/src/types/index.ts +0 -37
@@ -1,7 +1,320 @@
1
- import { useWalletSelector } from './index';
1
+ import compareVersions from 'compare-versions'
2
+ import { useCallback } from 'react'
2
3
 
3
- export const useSettingsState = () => {
4
- return useWalletSelector((state) => state.settings);
5
- };
4
+ import { CHAINS_MAP, CAT_VERSION, VERSION } from '@unisat/wallet-shared'
5
+ import { AddressType, ChainType, NetworkType } from '@unisat/wallet-types'
6
+ import { useWallet } from '../context/WalletContext'
7
+ import { getAddressType } from '../utils/bitcoin-utils'
8
+ import i18n, { addResourceBundle } from '../utils/i18n'
9
+ import { BABYLON_CONFIG_MAP } from '@unisat/babylon-service/types'
10
+ import { t } from '@unisat/i18n'
6
11
 
7
- // Additional settings hooks will be migrated here
12
+ import { AppState } from '..'
13
+ import { useCurrentAccount } from '../hooks/accounts'
14
+ import { useAppDispatch, useAppSelector } from './base'
15
+ import { settingsActions } from '../reducers/settings'
16
+
17
+ export function useSettingsState(): AppState['settings'] {
18
+ return useAppSelector(state => state.settings)
19
+ }
20
+
21
+ export function useLocale() {
22
+ const settings = useSettingsState()
23
+ return settings.locale
24
+ }
25
+
26
+ export function useChangeLocaleCallback() {
27
+ const dispatch = useAppDispatch()
28
+ const wallet = useWallet()
29
+ return useCallback(
30
+ async (locale: string) => {
31
+ await wallet.setLocale(locale)
32
+ await addResourceBundle(locale)
33
+ i18n.changeLanguage(locale)
34
+ dispatch(
35
+ (settingsActions as any).updateSettings({
36
+ locale,
37
+ })
38
+ )
39
+
40
+ // @ts-ignore
41
+ window.location.reload()
42
+ },
43
+ [dispatch, wallet]
44
+ )
45
+ }
46
+
47
+ export function useNetworkType() {
48
+ const accountsState = useSettingsState()
49
+ const chain = CHAINS_MAP[accountsState.chainType]
50
+ if (chain) {
51
+ return chain.networkType
52
+ } else {
53
+ return NetworkType.TESTNET
54
+ }
55
+ }
56
+
57
+ export function useChangeNetworkTypeCallback() {
58
+ const dispatch = useAppDispatch()
59
+ const wallet = useWallet()
60
+ return useCallback(
61
+ async (type: NetworkType) => {
62
+ if (type === NetworkType.MAINNET) {
63
+ await wallet.setChainType(ChainType.BITCOIN_MAINNET)
64
+ dispatch(
65
+ (settingsActions as any).updateSettings({
66
+ chainType: ChainType.BITCOIN_MAINNET,
67
+ })
68
+ )
69
+ } else if (type === NetworkType.TESTNET) {
70
+ await wallet.setChainType(ChainType.BITCOIN_TESTNET)
71
+ dispatch(
72
+ (settingsActions as any).updateSettings({
73
+ chainType: ChainType.BITCOIN_TESTNET,
74
+ })
75
+ )
76
+ }
77
+ },
78
+ [dispatch]
79
+ )
80
+ }
81
+
82
+ export function useChainType() {
83
+ const accountsState = useSettingsState()
84
+ return accountsState.chainType
85
+ }
86
+
87
+ export function useChain() {
88
+ const accountsState = useSettingsState()
89
+ return CHAINS_MAP[accountsState.chainType]
90
+ }
91
+
92
+ export function useChangeChainTypeCallback() {
93
+ const dispatch = useAppDispatch()
94
+ const wallet = useWallet()
95
+ return useCallback(
96
+ async (type: ChainType) => {
97
+ dispatch(
98
+ (settingsActions as any).updateSettings({
99
+ chainType: type,
100
+ })
101
+ )
102
+ await wallet.setChainType(type)
103
+ },
104
+ [dispatch]
105
+ )
106
+ }
107
+
108
+ export function useBTCUnit() {
109
+ const chainType = useChainType()
110
+ return CHAINS_MAP[chainType]!.unit
111
+ }
112
+
113
+ export function useTxExplorerUrl(txid: string) {
114
+ const chain = useChain()!
115
+ if (chain.enum === ChainType.BITCOIN_MAINNET) {
116
+ return `${chain.unisatExplorerUrl}/tx/${txid}`
117
+ } else if (chain.defaultExplorer === 'mempool-space') {
118
+ return `${chain.mempoolSpaceUrl}/tx/${txid}`
119
+ } else {
120
+ return `${chain.unisatExplorerUrl}/tx/${txid}`
121
+ }
122
+ }
123
+
124
+ export function useGetTxExplorerUrlCallback() {
125
+ const chain = useChain()!
126
+ return useCallback(
127
+ (txid: string) => {
128
+ if (chain.enum === ChainType.BITCOIN_MAINNET) {
129
+ return `${chain.unisatExplorerUrl}/tx/${txid}`
130
+ } else if (chain.defaultExplorer === 'mempool-space') {
131
+ return `${chain.mempoolSpaceUrl}/tx/${txid}`
132
+ } else {
133
+ return `${chain.unisatExplorerUrl}/tx/${txid}`
134
+ }
135
+ },
136
+ [chain]
137
+ )
138
+ }
139
+
140
+ export function useAddressExplorerUrl(address: string) {
141
+ const chain = useChain()!
142
+ if (chain.enum === ChainType.BITCOIN_MAINNET) {
143
+ return `${chain.unisatExplorerUrl}/address/${address}`
144
+ } else if (chain.defaultExplorer === 'mempool-space') {
145
+ return `${chain.mempoolSpaceUrl}/address/${address}`
146
+ } else {
147
+ return `${chain.unisatExplorerUrl}/address/${address}`
148
+ }
149
+ }
150
+
151
+ export function useCAT20TokenInfoExplorerUrl(version: CAT_VERSION, tokenId: string) {
152
+ const chain = useChain()!
153
+ if (version === CAT_VERSION.V1) {
154
+ return `${chain.unisatExplorerUrl}/cat20/${tokenId}`
155
+ } else {
156
+ return `${chain.unisatExplorerUrl}/cat20-v2/${tokenId}`
157
+ }
158
+ }
159
+
160
+ export function useUnisatWebsite() {
161
+ const chainType = useChainType()
162
+ return CHAINS_MAP[chainType]!.unisatUrl
163
+ }
164
+
165
+ export function useOrdinalsWebsite() {
166
+ const chainType = useChainType()
167
+ return CHAINS_MAP[chainType]!.ordinalsUrl
168
+ }
169
+
170
+ export function useWalletConfig() {
171
+ const accountsState = useSettingsState()
172
+ return accountsState.walletConfig
173
+ }
174
+
175
+ export function useVersionInfo() {
176
+ const accountsState = useSettingsState()
177
+ const walletConfig = accountsState.walletConfig
178
+ const newVersion = walletConfig.version
179
+ const skippedVersion = accountsState.skippedVersion
180
+ const currentVesion = VERSION!
181
+ let skipped = false
182
+ let latestVersion = ''
183
+ // skip if new version is empty
184
+ if (!newVersion) {
185
+ skipped = true
186
+ }
187
+
188
+ // skip if skipped
189
+ if (newVersion == skippedVersion) {
190
+ skipped = true
191
+ }
192
+
193
+ // skip if current version is greater or equal to new version
194
+ if (newVersion) {
195
+ if (compareVersions(currentVesion, newVersion) >= 0) {
196
+ skipped = true
197
+ } else {
198
+ latestVersion = newVersion
199
+ }
200
+ }
201
+
202
+ // skip if current version is 0.0.0
203
+ if (currentVesion === '0.0.0') {
204
+ skipped = true
205
+ }
206
+ return {
207
+ currentVesion,
208
+ newVersion,
209
+ latestVersion,
210
+ skipped,
211
+ }
212
+ }
213
+
214
+ export function useSkipVersionCallback() {
215
+ const wallet = useWallet()
216
+ const dispatch = useAppDispatch()
217
+ return useCallback((version: string) => {
218
+ wallet.setSkippedVersion(version).then(v => {
219
+ dispatch((settingsActions as any).updateSettings({ skippedVersion: version }))
220
+ })
221
+ }, [])
222
+ }
223
+
224
+ export function useAutoLockTimeId() {
225
+ const state = useSettingsState()
226
+ return state.autoLockTimeId
227
+ }
228
+
229
+ export function getAddressTips(address: string, chanEnum: ChainType) {
230
+ let ret = {
231
+ homeTip: '',
232
+ sendTip: '',
233
+ }
234
+ try {
235
+ const chain = CHAINS_MAP[chanEnum]!
236
+ const addressType = getAddressType(address, chain.networkType)
237
+ if (chain.isFractal && addressType === AddressType.P2PKH) {
238
+ ret = {
239
+ homeTip: t('legacy_address_warning_3'),
240
+ sendTip: t('legacy_address_warning_4'),
241
+ }
242
+ }
243
+ } catch (e) {
244
+ console.log(e)
245
+ }
246
+
247
+ return ret
248
+ }
249
+
250
+ export function useAddressTips() {
251
+ const chain = useChain()!
252
+ const account = useCurrentAccount()
253
+ return getAddressTips(account.address, chain.enum)
254
+ }
255
+
256
+ export function useCAT721NFTContentBaseUrl(version: CAT_VERSION) {
257
+ const chainType = useChainType()
258
+ if (chainType === ChainType.FRACTAL_BITCOIN_MAINNET) {
259
+ if (version === CAT_VERSION.V1) {
260
+ return 'https://tracker-fractal-mainnet.catprotocol.org'
261
+ } else {
262
+ return 'https://tracker2-fractal-mainnet.catprotocol.org'
263
+ }
264
+ } else if (chainType === ChainType.FRACTAL_BITCOIN_TESTNET) {
265
+ return 'https://tracker-fractal-testnet.catprotocol.org'
266
+ } else {
267
+ return ''
268
+ }
269
+ }
270
+
271
+ export function useBRC20MarketPlaceWebsite(ticker: string) {
272
+ const chainType = useChainType()
273
+ if (chainType === ChainType.BITCOIN_MAINNET) {
274
+ if (ticker.length == 6) {
275
+ return `${CHAINS_MAP[chainType]!.unisatUrl}/market/brc20_prog?tick=${ticker}`
276
+ }
277
+ }
278
+ return `${CHAINS_MAP[chainType]!.unisatUrl}/market/brc20?tick=${ticker}`
279
+ }
280
+
281
+ export function useRunesMarketPlaceWebsite(ticker: string) {
282
+ const chainType = useChainType()
283
+ return `${CHAINS_MAP[chainType]!.unisatUrl}/runes/market?tick=${ticker}`
284
+ }
285
+
286
+ export function useCAT20MarketPlaceWebsite(tokenId: string) {
287
+ const chainType = useChainType()
288
+ return `${CHAINS_MAP[chainType]!.unisatUrl}/dex/cat20/${tokenId}`
289
+ }
290
+
291
+ export function useBabylonConfig() {
292
+ const chainType = useChainType()
293
+ return BABYLON_CONFIG_MAP[chainType] || BABYLON_CONFIG_MAP[ChainType.BITCOIN_MAINNET]
294
+ }
295
+
296
+ export function useIsMainnetChain() {
297
+ const chainType = useChainType()
298
+ return chainType === ChainType.BITCOIN_MAINNET || chainType === ChainType.FRACTAL_BITCOIN_MAINNET
299
+ }
300
+
301
+ export function useDeveloperMode() {
302
+ const settings = useSettingsState()
303
+ return settings.developerMode
304
+ }
305
+
306
+ export function useSetDeveloperModeCallback() {
307
+ const dispatch = useAppDispatch()
308
+ const wallet = useWallet()
309
+ return useCallback(
310
+ async (developerMode: boolean) => {
311
+ await wallet.setDeveloperMode(developerMode)
312
+ dispatch(
313
+ (settingsActions as any).updateSettings({
314
+ developerMode,
315
+ })
316
+ )
317
+ },
318
+ [dispatch, wallet]
319
+ )
320
+ }
@@ -1,17 +1,23 @@
1
1
  import { useCallback, useMemo } from 'react'
2
2
 
3
- import { RawTxInfo, ToAddressInfo } from '@/shared/types'
4
- import { useTools } from '@/ui/components/ActionComponent'
5
- import { useI18n } from '@/ui/hooks/useI18n'
6
- import { useBTCUnit } from '@/ui/state/settings/hooks'
7
- import { satoshisToBTC, sleep, useWallet } from '@/ui/utils'
8
- import { UnspentOutput } from '@unisat/tx-helpers/types'
3
+ import { RawTxInfo, ToAddressInfo, UnspentOutput } from '@unisat/wallet-shared'
4
+
5
+ import { useI18n } from './i18n'
6
+ import { useBTCUnit } from '../hooks/settings'
9
7
 
10
8
  import { AppState } from '..'
11
- import { useAccountAddress, useCurrentAccount } from '../accounts/hooks'
12
- import { accountActions } from '../accounts/reducer'
13
- import { useAppDispatch, useAppSelector } from '../hooks'
14
- import { transactionsActions } from './reducer'
9
+ import { useAccountAddress, useCurrentAccount } from '../hooks/accounts'
10
+ import { accountActions } from '../reducers/accounts'
11
+ import { useAppDispatch, useAppSelector } from './base'
12
+ import { transactionsActions } from '../reducers/transactions'
13
+ import { numUtils, timeUtils } from '@unisat/base-utils'
14
+ import { useWallet } from '../context/WalletContext'
15
+
16
+ function useTools() {
17
+ return {
18
+ showLoading: (show: boolean) => {},
19
+ }
20
+ }
15
21
 
16
22
  export function useTransactionsState(): AppState['transactions'] {
17
23
  return useAppSelector(state => state.transactions)
@@ -62,7 +68,7 @@ export function usePrepareSendBTCCallback() {
62
68
 
63
69
  if (!feeRate) {
64
70
  const summary = await wallet.getFeeSummary()
65
- feeRate = summary.list[1].feeRate
71
+ feeRate = summary.list[1]!.feeRate
66
72
  }
67
73
  let res: {
68
74
  psbtHex: string
@@ -84,13 +90,13 @@ export function usePrepareSendBTCCallback() {
84
90
  btcUtxos: _utxos,
85
91
  enableRBF,
86
92
  feeRate,
87
- memo,
88
- memos,
93
+ memo: memo!,
94
+ memos: memos!,
89
95
  })
90
96
  }
91
97
 
92
98
  dispatch(
93
- transactionsActions.updateBitcoinTx({
99
+ (transactionsActions as any).updateBitcoinTx({
94
100
  rawtx: res.rawtx,
95
101
  psbtHex: res.psbtHex,
96
102
  fromAddress,
@@ -137,7 +143,7 @@ export function usePrepareSendBypassHeadOffsetsCallback() {
137
143
  )
138
144
 
139
145
  dispatch(
140
- transactionsActions.updateBitcoinTx({
146
+ (transactionsActions as any).updateBitcoinTx({
141
147
  rawtx: res.rawtx,
142
148
  psbtHex: res.psbtHex,
143
149
  fromAddress,
@@ -170,15 +176,15 @@ export function usePushBitcoinTxCallback() {
170
176
  try {
171
177
  tools.showLoading(true)
172
178
  const txid = await wallet.pushTx(rawtx)
173
- await sleep(3) // Wait for transaction synchronization
179
+ await timeUtils.sleep(3) // Wait for transaction synchronization
174
180
  tools.showLoading(false)
175
- dispatch(transactionsActions.updateBitcoinTx({ txid }))
176
- dispatch(accountActions.expireBalance())
181
+ dispatch((transactionsActions as any).updateBitcoinTx({ txid }))
182
+ dispatch((accountActions as any).expireBalance())
177
183
  setTimeout(() => {
178
- dispatch(accountActions.expireBalance())
184
+ dispatch((accountActions as any).expireBalance())
179
185
  }, 2000)
180
186
  setTimeout(() => {
181
- dispatch(accountActions.expireBalance())
187
+ dispatch((accountActions as any).expireBalance())
182
188
  }, 5000)
183
189
 
184
190
  ret.success = true
@@ -222,7 +228,7 @@ export function usePrepareSendOrdinalsInscriptionCallback() {
222
228
  }) => {
223
229
  if (!feeRate) {
224
230
  const summary = await wallet.getFeeSummary()
225
- feeRate = summary.list[1].feeRate
231
+ feeRate = summary.list[1]!.feeRate
226
232
  }
227
233
 
228
234
  let btcUtxos = utxos
@@ -234,12 +240,12 @@ export function usePrepareSendOrdinalsInscriptionCallback() {
234
240
  to: toAddressInfo.address,
235
241
  inscriptionId,
236
242
  feeRate,
237
- outputValue,
243
+ outputValue: outputValue!,
238
244
  enableRBF,
239
245
  btcUtxos,
240
246
  })
241
247
  dispatch(
242
- transactionsActions.updateOrdinalsTx({
248
+ (transactionsActions as any).updateOrdinalsTx({
243
249
  rawtx: res.rawtx,
244
250
  psbtHex: res.psbtHex,
245
251
  fromAddress,
@@ -281,7 +287,7 @@ export function usePrepareSendOrdinalsInscriptionsCallback() {
281
287
  }) => {
282
288
  if (!feeRate) {
283
289
  const summary = await wallet.getFeeSummary()
284
- feeRate = summary.list[1].feeRate
290
+ feeRate = summary.list[1]!.feeRate
285
291
  }
286
292
 
287
293
  let btcUtxos = utxos
@@ -296,7 +302,7 @@ export function usePrepareSendOrdinalsInscriptionsCallback() {
296
302
  btcUtxos,
297
303
  })
298
304
  dispatch(
299
- transactionsActions.updateOrdinalsTx({
305
+ (transactionsActions as any).updateOrdinalsTx({
300
306
  rawtx: res.rawtx,
301
307
  psbtHex: res.psbtHex,
302
308
  fromAddress,
@@ -347,7 +353,7 @@ export function useCreateSplitTxCallback() {
347
353
  btcUtxos,
348
354
  })
349
355
  dispatch(
350
- transactionsActions.updateOrdinalsTx({
356
+ (transactionsActions as any).updateOrdinalsTx({
351
357
  rawtx: res.rawtx,
352
358
  psbtHex: res.psbtHex,
353
359
  fromAddress,
@@ -384,16 +390,16 @@ export function usePushOrdinalsTxCallback() {
384
390
  try {
385
391
  tools.showLoading(true)
386
392
  const txid = await wallet.pushTx(rawtx)
387
- await sleep(3) // Wait for transaction synchronization
393
+ await timeUtils.sleep(3) // Wait for transaction synchronization
388
394
  tools.showLoading(false)
389
- dispatch(transactionsActions.updateOrdinalsTx({ txid }))
395
+ dispatch((transactionsActions as any).updateOrdinalsTx({ txid }))
390
396
 
391
- dispatch(accountActions.expireBalance())
397
+ dispatch((accountActions as any).expireBalance())
392
398
  setTimeout(() => {
393
- dispatch(accountActions.expireBalance())
399
+ dispatch((accountActions as any).expireBalance())
394
400
  }, 2000)
395
401
  setTimeout(() => {
396
- dispatch(accountActions.expireBalance())
402
+ dispatch((accountActions as any).expireBalance())
397
403
  }, 5000)
398
404
 
399
405
  ret.success = true
@@ -421,7 +427,7 @@ export function useFetchUtxosCallback() {
421
427
  const account = useCurrentAccount()
422
428
  return useCallback(async () => {
423
429
  const data = await wallet.getBTCUtxos()
424
- dispatch(transactionsActions.setUtxos(data))
430
+ dispatch((transactionsActions as any).setUtxos(data))
425
431
  return data
426
432
  }, [wallet, account])
427
433
  }
@@ -435,7 +441,7 @@ export function useSetSpendUnavailableUtxosCallback() {
435
441
  const dispatch = useAppDispatch()
436
442
  return useCallback(
437
443
  (utxos: UnspentOutput[]) => {
438
- dispatch(transactionsActions.setSpendUnavailableUtxos(utxos))
444
+ dispatch((transactionsActions as any).setSpendUnavailableUtxos(utxos))
439
445
  },
440
446
  [dispatch]
441
447
  )
@@ -447,7 +453,7 @@ export function useSafeBalance() {
447
453
  const satoshis = utxos
448
454
  .filter(v => v.inscriptions.length === 0)
449
455
  .reduce((pre, cur) => pre + cur.satoshis, 0)
450
- return satoshisToBTC(satoshis)
456
+ return numUtils.satoshisToAmount(satoshis)
451
457
  }, [utxos])
452
458
  }
453
459
 
@@ -463,7 +469,7 @@ export function useFetchAssetUtxosRunesCallback() {
463
469
  return useCallback(
464
470
  async (rune: string) => {
465
471
  const data = await wallet.getAssetUtxosRunes(rune)
466
- dispatch(transactionsActions.setAssetUtxosRunes(data))
472
+ dispatch((transactionsActions as any).setAssetUtxosRunes(data))
467
473
  return data
468
474
  },
469
475
  [wallet, account]
@@ -497,7 +503,7 @@ export function usePrepareSendRunesCallback() {
497
503
  }) => {
498
504
  if (!feeRate) {
499
505
  const summary = await wallet.getFeeSummary()
500
- feeRate = summary.list[1].feeRate
506
+ feeRate = summary.list[1]!.feeRate
501
507
  }
502
508
 
503
509
  let btcUtxos = utxos
@@ -514,7 +520,7 @@ export function usePrepareSendRunesCallback() {
514
520
  to: toAddressInfo.address,
515
521
  runeid,
516
522
  runeAmount,
517
- outputValue,
523
+ outputValue: outputValue!,
518
524
  feeRate,
519
525
  enableRBF,
520
526
  btcUtxos,
@@ -522,7 +528,7 @@ export function usePrepareSendRunesCallback() {
522
528
  })
523
529
 
524
530
  dispatch(
525
- transactionsActions.updateRunesTx({
531
+ (transactionsActions as any).updateRunesTx({
526
532
  rawtx: res.rawtx,
527
533
  psbtHex: res.psbtHex,
528
534
  fromAddress,
package/src/hooks/ui.ts CHANGED
@@ -1,7 +1,135 @@
1
- import { useWalletSelector } from './index';
1
+ import { useMemo } from 'react'
2
2
 
3
- export const useUIState = () => {
4
- return useWalletSelector((state) => state.ui);
5
- };
3
+ import { ChainType } from '@unisat/wallet-types'
4
+ import { Inscription } from '@unisat/wallet-shared'
5
+ import { getAddressType } from '../utils/bitcoin-utils'
6
+ import { AddressType } from '@unisat/wallet-types'
6
7
 
7
- // Additional UI hooks will be migrated here
8
+ import { AppState } from '..'
9
+ import { useCurrentAccount, useCurrentAddress } from '../hooks/accounts'
10
+ import { useAppDispatch, useAppSelector } from '../hooks/base'
11
+ import { useChainType, useNetworkType } from '../hooks/settings'
12
+ import { AssetTabKey, uiActions } from '../reducers/ui'
13
+ import { TypeChain } from '@unisat/wallet-shared'
14
+ export function useUIState(): AppState['ui'] {
15
+ return useAppSelector(state => state.ui)
16
+ }
17
+
18
+ export function useAssetTabKey() {
19
+ const uiState = useUIState()
20
+ return uiState.assetTabKey
21
+ }
22
+
23
+ export function useOrdinalsAssetTabKey() {
24
+ const uiState = useUIState()
25
+ return uiState.ordinalsAssetTabKey
26
+ }
27
+
28
+ export function useCATAssetTabKey() {
29
+ const uiState = useUIState()
30
+ return uiState.catAssetTabKey
31
+ }
32
+
33
+ export function useAlkanesAssetTabKey() {
34
+ const uiState = useUIState()
35
+ return uiState.alkanesAssetTabKey
36
+ }
37
+
38
+ export function useUiTxCreateScreen() {
39
+ const uiState = useUIState()
40
+ return uiState.uiTxCreateScreen
41
+ }
42
+
43
+ export function useUpdateUiTxCreateScreen() {
44
+ const dispatch = useAppDispatch()
45
+ return ({
46
+ toInfo,
47
+ inputAmount,
48
+ enableRBF,
49
+ feeRate,
50
+ }: {
51
+ toInfo?: { address: string; domain: string; inscription?: Inscription }
52
+ inputAmount?: string
53
+ enableRBF?: boolean
54
+ feeRate?: number
55
+ }) => {
56
+ dispatch((uiActions as any).updateTxCreateScreen({ toInfo, inputAmount, enableRBF, feeRate }))
57
+ }
58
+ }
59
+
60
+ export function useResetUiTxCreateScreen() {
61
+ const dispatch = useAppDispatch()
62
+ return () => {
63
+ dispatch((uiActions as any).resetTxCreateScreen())
64
+ }
65
+ }
66
+
67
+ export function useSupportedAssets() {
68
+ const chainType = useChainType()
69
+ const currentAddress = useCurrentAddress()
70
+ const networkType = useNetworkType()
71
+ const currentAccount = useCurrentAccount()
72
+
73
+ const assetTabKeys: AssetTabKey[] = []
74
+ const assets = {
75
+ ordinals: false,
76
+ runes: false,
77
+ CAT20: false,
78
+ alkanes: false,
79
+ }
80
+
81
+ assets.ordinals = true
82
+ assetTabKeys.push(AssetTabKey.ORDINALS)
83
+
84
+ assets.runes = true
85
+ assetTabKeys.push(AssetTabKey.RUNES)
86
+
87
+ if (
88
+ chainType === ChainType.FRACTAL_BITCOIN_MAINNET ||
89
+ chainType === ChainType.FRACTAL_BITCOIN_TESTNET
90
+ ) {
91
+ const addressType = getAddressType(currentAddress, networkType)
92
+ if (addressType == AddressType.P2TR || addressType == AddressType.P2WPKH) {
93
+ assets.CAT20 = true
94
+ assetTabKeys.push(AssetTabKey.CAT)
95
+ }
96
+ }
97
+
98
+ if (chainType === ChainType.BITCOIN_SIGNET || chainType === ChainType.BITCOIN_MAINNET) {
99
+ assets.alkanes = true
100
+ assetTabKeys.push(AssetTabKey.ALKANES)
101
+ }
102
+
103
+ return {
104
+ tabKeys: assetTabKeys,
105
+ assets,
106
+ key: assetTabKeys.join(','),
107
+ }
108
+ }
109
+
110
+ export const useIsInExpandView = () => {
111
+ // @ts-ignore
112
+ if (typeof window === 'undefined') {
113
+ return false
114
+ }
115
+ return useMemo(() => {
116
+ // @ts-ignore
117
+ if (window.innerWidth > 156 * 3) {
118
+ return true
119
+ } else {
120
+ return false
121
+ }
122
+ // @ts-ignore
123
+ }, [window.innerWidth])
124
+ }
125
+
126
+ export const useUtxoTools = (chain: TypeChain) => {
127
+ const openUtxoTools = () => {
128
+ // @ts-ignore
129
+ window.open(`${chain.unisatUrl}/utxo?tab=all`)
130
+ }
131
+
132
+ return {
133
+ openUtxoTools,
134
+ }
135
+ }