1 import { createSlice } from '@reduxjs/toolkit';
3 import { type ModelState, getInitialModelState } from '@proton/account';
4 import { type WasmNetwork } from '@proton/andromeda';
5 import { createAsyncModelThunk, handleAsyncModel, previousSelector } from '@proton/redux-utilities';
7 import { type WalletThunkArguments } from '../thunk';
9 const name = 'bitcoin_network' as const;
11 export interface BitcoinNetworkState {
12 [name]: ModelState<WasmNetwork>;
15 type SliceState = BitcoinNetworkState[typeof name];
16 type Model = NonNullable<SliceState['value']>;
18 export const selectBitcoinNetwork = (state: BitcoinNetworkState) => state[name];
20 const modelThunk = createAsyncModelThunk<Model, BitcoinNetworkState, WalletThunkArguments>(`${name}/fetch`, {
21 miss: ({ extraArgument }) => {
22 return extraArgument.walletApi
25 .then((network) => network);
27 previous: previousSelector(selectBitcoinNetwork),
30 const initialState = getInitialModelState<Model>();
32 const slice = createSlice({
36 extraReducers: (builder) => {
37 handleAsyncModel(builder, modelThunk);
41 export const bitcoinNetworkReducer = { [name]: slice.reducer };
42 export const bitcoinNetworkThunk = modelThunk.thunk;