Use source loader for email sprite icons
[ProtonMail-WebClient.git] / packages / wallet / store / hooks / useBitcoinAddressPool.ts
blob8625d97a6d570345d3fc8b9ec08da74d9b1042d4
1 import { useCallback, useEffect } from 'react';
3 import { createSelector } from '@reduxjs/toolkit';
5 import { type WasmApiWalletBitcoinAddress } from '@proton/andromeda';
6 import { baseUseSelector } from '@proton/react-redux-store';
7 import { CacheType, createHooks } from '@proton/redux-utilities';
9 import { bitcoinAddressPoolThunk, selectBitcoinAddressPool } from '../slices';
11 const hooks = createHooks(bitcoinAddressPoolThunk, selectBitcoinAddressPool);
13 export const useGetBitcoinAddressPool = () => {
14     const get = hooks.useGet();
16     return useCallback(
17         async (walletId: string, walletAccountId: string) => {
18             const results = await get({ thunkArg: [walletId, walletAccountId], cache: CacheType.None });
19             return results[walletAccountId] ?? [];
20         },
21         [get]
22     );
25 export const useBitcoinAddressPool = (walletId: string, walletAccountId: string) => {
26     const getBitcoinAddressPool = useGetBitcoinAddressPool();
27     const [, loading] = hooks.useValue();
29     const bitcoinAddressPoolSelector = createSelector(
30         selectBitcoinAddressPool,
31         (result): [WasmApiWalletBitcoinAddress[] | undefined, boolean] => {
32             const { value } = result;
34             const addresses = value?.[walletAccountId];
36             return [addresses, loading];
37         }
38     );
40     useEffect(() => {
41         void getBitcoinAddressPool(walletId, walletAccountId);
42     }, [walletId, walletAccountId]);
44     return baseUseSelector(bitcoinAddressPoolSelector);