1 import { createSlice } from '@reduxjs/toolkit';
3 import type { ModelState } from '@proton/account';
4 import { getInitialModelState } from '@proton/account';
5 import { type WasmApiWalletBitcoinAddress } from '@proton/andromeda';
6 import { createAsyncModelThunk, handleAsyncModel } from '@proton/redux-utilities';
7 import { MINUTE } from '@proton/shared/lib/constants';
9 import type { WalletThunkArguments } from '../thunk';
11 const name = 'bitcoin_address_pool' as const;
13 type BitcoinAddressPoolByWalletAccountId = Partial<Record<string, WasmApiWalletBitcoinAddress[]>>;
15 export interface BitcoinAddressPoolState {
16 [name]: ModelState<BitcoinAddressPoolByWalletAccountId>;
19 type SliceState = BitcoinAddressPoolState[typeof name];
20 type Model = NonNullable<SliceState['value']>;
22 export const selectBitcoinAddressPool = (state: BitcoinAddressPoolState) => state[name];
24 const modelThunk = createAsyncModelThunk<Model, BitcoinAddressPoolState, WalletThunkArguments, [string, string]>(
27 miss: async ({ extraArgument, options, getState }) => {
28 const stateValue = getState()[name].value;
29 if (!options?.thunkArg) {
30 return stateValue ?? {};
33 const [walletId, walletAccountId] = options?.thunkArg;
35 const addresses = await extraArgument.walletApi
37 .bitcoin_address.getBitcoinAddresses(walletId, walletAccountId)
38 .then((data) => data[0].map((d): WasmApiWalletBitcoinAddress => d.Data));
42 [walletAccountId]: addresses,
46 previous: ({ getState, options }) => {
47 const state = getState()[name];
49 if (!options?.thunkArg || !state.value) {
53 const [, walletAccountId] = options?.thunkArg;
54 const value = state.value[walletAccountId];
65 const initialState = getInitialModelState<Model>();
67 const slice = createSlice({
71 extraReducers: (builder) => {
72 handleAsyncModel(builder, modelThunk);
76 export const bitcoinAddressPoolReducer = { [name]: slice.reducer };
77 export const bitcoinAddressPoolThunk = modelThunk.thunk;