Remove client-side isLoggedIn value
[ProtonMail-WebClient.git] / packages / wallet / store / slices / bitcoinAddressPool.ts
blobb11ae83f83127a3bdc321f97f3e27cffddebf7fd
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]>(
25     `${name}/fetch`,
26     {
27         miss: async ({ extraArgument, options, getState }) => {
28             const stateValue = getState()[name].value;
29             if (!options?.thunkArg) {
30                 return stateValue ?? {};
31             }
33             const [walletId, walletAccountId] = options?.thunkArg;
35             const addresses = await extraArgument.walletApi
36                 .clients()
37                 .bitcoin_address.getBitcoinAddresses(walletId, walletAccountId)
38                 .then((data) => data[0].map((d): WasmApiWalletBitcoinAddress => d.Data));
40             return {
41                 ...stateValue,
42                 [walletAccountId]: addresses,
43             };
44         },
45         expiry: 10 * MINUTE,
46         previous: ({ getState, options }) => {
47             const state = getState()[name];
49             if (!options?.thunkArg || !state.value) {
50                 return undefined;
51             }
53             const [, walletAccountId] = options?.thunkArg;
54             const value = state.value[walletAccountId];
56             if (!value) {
57                 return undefined;
58             }
60             return state;
61         },
62     }
65 const initialState = getInitialModelState<Model>();
67 const slice = createSlice({
68     name,
69     initialState,
70     reducers: {},
71     extraReducers: (builder) => {
72         handleAsyncModel(builder, modelThunk);
73     },
74 });
76 export const bitcoinAddressPoolReducer = { [name]: slice.reducer };
77 export const bitcoinAddressPoolThunk = modelThunk.thunk;