1 import { useCallback, useEffect } from 'react';
3 import { createSelector } from '@reduxjs/toolkit';
5 import type { WasmApiExchangeRate, WasmFiatCurrencySymbol } from '@proton/andromeda';
6 import { baseUseSelector } from '@proton/react-redux-store';
7 import { createHooks } from '@proton/redux-utilities';
9 import { exchangeRateThunk, selectExchangeRate } from '../slices';
10 import { getKeyAndTs } from '../slices/exchangeRate';
12 export const exchangeRateHooks = createHooks(exchangeRateThunk, selectExchangeRate);
14 export const useGetExchangeRate = () => {
15 const get = exchangeRateHooks.useGet();
17 async (fiat: WasmFiatCurrencySymbol, date?: Date) => {
18 const results = await get({ thunkArg: [fiat, date] });
19 const [key] = getKeyAndTs(fiat, date);
27 export const useExchangeRate = (fiat: WasmFiatCurrencySymbol) => {
28 const getExchangeRate = useGetExchangeRate();
29 const [, loading] = exchangeRateHooks.useValue();
31 const exchangeRateSimpleSelector = createSelector(
33 (result): [WasmApiExchangeRate | undefined, boolean] => {
34 const { value } = result;
36 const rate = value?.[fiat];
38 return [rate, loading];
43 void getExchangeRate(fiat);
44 }, [fiat, getExchangeRate]);
46 return baseUseSelector(exchangeRateSimpleSelector);