Use source loader for email sprite icons
[ProtonMail-WebClient.git] / packages / wallet / store / slices / bitcoinNetwork.ts
blob4194b7fe36564f9270e1a2cf853671310f90a652
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
23             .clients()
24             .network.getNetwork()
25             .then((network) => network);
26     },
27     previous: previousSelector(selectBitcoinNetwork),
28 });
30 const initialState = getInitialModelState<Model>();
32 const slice = createSlice({
33     name,
34     initialState,
35     reducers: {},
36     extraReducers: (builder) => {
37         handleAsyncModel(builder, modelThunk);
38     },
39 });
41 export const bitcoinNetworkReducer = { [name]: slice.reducer };
42 export const bitcoinNetworkThunk = modelThunk.thunk;