1 import { createSlice } from '@reduxjs/toolkit';
3 import type { ModelState } from '@proton/account';
4 import { getInitialModelState } from '@proton/account';
5 import type { WasmFiatCurrencySymbol, WasmPriceGraph, WasmTimeframe } from '@proton/andromeda';
6 import { createAsyncModelThunk, handleAsyncModel } from '@proton/redux-utilities';
7 import { type SimpleMap } from '@proton/shared/lib/interfaces';
9 import type { WalletThunkArguments } from '../thunk';
11 const name = 'price_graph' as const;
13 type WasmPriceGraphByFiatAndTimeframe = SimpleMap<WasmPriceGraph>;
15 export interface PriceGraphDataState {
16 [name]: ModelState<WasmPriceGraphByFiatAndTimeframe>;
19 type SliceState = PriceGraphDataState[typeof name];
20 type Model = NonNullable<SliceState['value']>;
22 export const selectPriceGraphData = (state: PriceGraphDataState) => {
26 export const getKey = (fiat: WasmFiatCurrencySymbol, timeframe: WasmTimeframe) => `${fiat}-${timeframe}`;
28 const modelThunk = createAsyncModelThunk<
32 [WasmFiatCurrencySymbol, WasmTimeframe?]
34 miss: async ({ extraArgument, options, getState }) => {
35 const stateValue = getState()[name].value;
36 if (!options?.thunkArg) {
37 return stateValue ?? {};
40 const fiat = options?.thunkArg?.[0] ?? 'USD';
41 const timeframe = options?.thunkArg?.[1] ?? 'OneDay';
43 const key = getKey(fiat, timeframe);
46 const priceGraph = await extraArgument.walletApi
48 .price_graph.getGraphData(fiat, timeframe)
58 return stateValue ?? {};
61 previous: ({ getState, options }) => {
62 const state = getState()[name];
68 const fiat = options?.thunkArg?.[0] ?? 'USD';
69 const timeframe = options?.thunkArg?.[1] ?? 'OneDay';
71 const key = getKey(fiat, timeframe);
73 if (state.value[key]) {
81 const initialState = getInitialModelState<Model>();
83 const slice = createSlice({
87 extraReducers: (builder) => {
88 handleAsyncModel(builder, modelThunk);
92 export const priceGraphDataReducer = { [name]: slice.reducer };
93 export const priceGraphDataThunk = modelThunk.thunk;