|
1 | 1 | import { useEffect, useState } from 'react';
|
2 | 2 | import { __E2E__ } from '../constants/env';
|
3 | 3 | import { tradingPairs } from '../constants/widgets';
|
| 4 | +import { widgetsCache } from '../storage/widgets-cache'; |
4 | 5 | import { TGraphPeriod } from '../store/types/widgets';
|
5 | 6 | import { IThemeColors } from '../styles/themes';
|
6 | 7 |
|
@@ -94,13 +95,42 @@ export const formatPrice = (pair: TradingPair, price: number): string => {
|
94 | 95 | }
|
95 | 96 | };
|
96 | 97 |
|
| 98 | +const cacheData = ( |
| 99 | + pairName: string, |
| 100 | + period: TGraphPeriod, |
| 101 | + data: TWidgetData, |
| 102 | +) => { |
| 103 | + const cacheKey = `${pairName}_${period}`; |
| 104 | + widgetsCache.set(cacheKey, data); |
| 105 | +}; |
| 106 | + |
| 107 | +const getCachedData = ( |
| 108 | + pairs: string[], |
| 109 | + period: TGraphPeriod, |
| 110 | +): TWidgetData[] | null => { |
| 111 | + const data = pairs.map((pairName) => { |
| 112 | + const cacheKey = `${pairName}_${period}`; |
| 113 | + const cached = widgetsCache.get<TWidgetData>(cacheKey); |
| 114 | + return cached; |
| 115 | + }); |
| 116 | + |
| 117 | + const allCached = data.every((d) => d !== null); |
| 118 | + if (allCached) { |
| 119 | + return data; |
| 120 | + } |
| 121 | + |
| 122 | + return null; |
| 123 | +}; |
| 124 | + |
97 | 125 | const usePriceWidget = (
|
98 | 126 | pairs: string[],
|
99 | 127 | period: TGraphPeriod,
|
100 | 128 | ): TWidgetState => {
|
101 |
| - const [state, setState] = useState<TWidgetState>({ |
102 |
| - status: EWidgetStatus.Loading, |
103 |
| - data: null, |
| 129 | + const [state, setState] = useState<TWidgetState>(() => { |
| 130 | + const cached = getCachedData(pairs, period); |
| 131 | + return cached |
| 132 | + ? { status: EWidgetStatus.Ready, data: cached } |
| 133 | + : { status: EWidgetStatus.Loading, data: null }; |
104 | 134 | });
|
105 | 135 |
|
106 | 136 | // biome-ignore lint/correctness/useExhaustiveDependencies: pairs is an array so deep check it
|
@@ -146,12 +176,16 @@ const usePriceWidget = (
|
146 | 176 | const change = getChange(updatedPastValues);
|
147 | 177 | const price = formatPrice(pair, latestPrice);
|
148 | 178 |
|
149 |
| - return { |
| 179 | + const data = { |
150 | 180 | name: pairName,
|
151 | 181 | price,
|
152 | 182 | change,
|
153 | 183 | pastValues: updatedPastValues,
|
154 | 184 | };
|
| 185 | + |
| 186 | + cacheData(pairName, period, data); |
| 187 | + |
| 188 | + return data; |
155 | 189 | });
|
156 | 190 | const data = await Promise.all(promises);
|
157 | 191 | setState({ status: EWidgetStatus.Ready, data });
|
@@ -179,12 +213,16 @@ const usePriceWidget = (
|
179 | 213 | const change = getChange(newPastValues);
|
180 | 214 | const price = formatPrice(pair, latestPrice);
|
181 | 215 |
|
182 |
| - return { |
| 216 | + const data = { |
183 | 217 | ...pairData,
|
184 | 218 | price,
|
185 | 219 | change,
|
186 | 220 | pastValues: newPastValues,
|
187 | 221 | };
|
| 222 | + |
| 223 | + cacheData(pairData.name, period, data); |
| 224 | + |
| 225 | + return data; |
188 | 226 | }),
|
189 | 227 | );
|
190 | 228 | setState({ status: EWidgetStatus.Ready, data: updatedData });
|
|
0 commit comments