@@ -54,87 +54,8 @@ export const useBitcoinRatesWithRange = (options: BitcoinRatesOptions) => {
5454 const abortControllerRef = useRef < AbortController | null > ( null ) ;
5555 const refreshIntervalRef = useRef < NodeJS . Timeout | null > ( null ) ;
5656
57- const fetchBitcoinRates = useCallback ( async ( ) => {
58- // Check cache first for this specific range
59- const cached = rangeCache . get ( options . timeRange ) ;
60- const now = Date . now ( ) ;
61-
62- // Always check cache duration
63- const shouldUseFreshData = ! cached ||
64- ( now - cached . timestamp ) > CACHE_DURATION ;
65-
66- if ( ! shouldUseFreshData && cached ) {
67- console . log ( `[useBitcoinRatesWithRange] Using cached data for ${ options . timeRange } ` ) ;
68- return cached . data ;
69- }
70-
71- // Cancel any existing request
72- if ( abortControllerRef . current ) {
73- abortControllerRef . current . abort ( ) ;
74- }
75-
76- // Create new abort controller for this request
77- abortControllerRef . current = new AbortController ( ) ;
78-
79- try {
80- const token = readToken ( ) ;
81- if ( ! token ) {
82- throw new Error ( 'No authentication token found' ) ;
83- }
84-
85- const params = getApiParams ( options . timeRange ) ;
86-
87- // Try range-specific endpoint first, fallback to last-30-days
88- let endpoint = `${ config . baseURL } /api/bitcoin-rates/range/${ options . timeRange } ` ;
89-
90- // For now, keep using the last-30-days endpoint until backend is updated
91- // Once backend supports the /range/:range endpoint, remove this line
92- endpoint = `${ config . baseURL } /api/bitcoin-rates/last-30-days` ;
93-
94- console . log ( `[useBitcoinRatesWithRange] Fetching ${ options . timeRange } data from ${ endpoint } ` ) ;
95-
96- const response = await fetch ( endpoint , {
97- method : 'GET' ,
98- headers : {
99- 'Content-Type' : 'application/json' ,
100- 'Authorization' : `Bearer ${ token } ` ,
101- } ,
102- signal : abortControllerRef . current . signal ,
103- } ) ;
104-
105- if ( ! response . ok ) {
106- if ( response . status === 401 ) {
107- handleLogout ( ) ;
108- throw new Error ( 'Authentication failed. You have been logged out.' ) ;
109- }
110-
111- throw new Error ( `Network response was not ok (status: ${ response . status } )` ) ;
112- }
113-
114- const data = await response . json ( ) ;
115- console . log ( `[useBitcoinRatesWithRange] Data received for ${ options . timeRange } ` ) ;
116-
117- const processedData = processBitcoinData ( data , options . timeRange ) ;
118-
119- // Cache the result
120- rangeCache . set ( options . timeRange , {
121- data : processedData ,
122- timestamp : now ,
123- } ) ;
124-
125- setLastUpdate ( new Date ( ) ) ;
126- return processedData ;
127- } catch ( err : any ) {
128- if ( err . name === 'AbortError' ) {
129- console . log ( '[useBitcoinRatesWithRange] Request was aborted' ) ;
130- return [ ] ;
131- }
132- throw err ;
133- }
134- } , [ options . timeRange , handleLogout ] ) ;
135-
13657 // Process and filter data based on time range
137- const processBitcoinData = ( data : any [ ] , range : TimeRange ) : Earning [ ] => {
58+ const processBitcoinData = useCallback ( ( data : any [ ] , range : TimeRange ) : Earning [ ] => {
13859 console . log ( `[processBitcoinData] Processing ${ data . length } data points for range: ${ range } ` ) ;
13960
14061 if ( data . length === 0 ) {
@@ -266,7 +187,87 @@ export const useBitcoinRatesWithRange = (options: BitcoinRatesOptions) => {
266187 console . log ( `[processBitcoinData] Final: ${ aggregated . length } points for ${ range } view` ) ;
267188
268189 return aggregated ;
269- } ;
190+ } , [ ] ) ;
191+
192+ // Smart aggregation that maintains data shape while reducing points
193+ const fetchBitcoinRates = useCallback ( async ( ) => {
194+ // Check cache first for this specific range
195+ const cached = rangeCache . get ( options . timeRange ) ;
196+ const now = Date . now ( ) ;
197+
198+ // Always check cache duration
199+ const shouldUseFreshData = ! cached ||
200+ ( now - cached . timestamp ) > CACHE_DURATION ;
201+
202+ if ( ! shouldUseFreshData && cached ) {
203+ console . log ( `[useBitcoinRatesWithRange] Using cached data for ${ options . timeRange } ` ) ;
204+ return cached . data ;
205+ }
206+
207+ // Cancel any existing request
208+ if ( abortControllerRef . current ) {
209+ abortControllerRef . current . abort ( ) ;
210+ }
211+
212+ // Create new abort controller for this request
213+ abortControllerRef . current = new AbortController ( ) ;
214+
215+ try {
216+ const token = readToken ( ) ;
217+ if ( ! token ) {
218+ throw new Error ( 'No authentication token found' ) ;
219+ }
220+
221+ const params = getApiParams ( options . timeRange ) ;
222+
223+ // Try range-specific endpoint first, fallback to last-30-days
224+ let endpoint = `${ config . baseURL } /api/bitcoin-rates/range/${ options . timeRange } ` ;
225+
226+ // For now, keep using the last-30-days endpoint until backend is updated
227+ // Once backend supports the /range/:range endpoint, remove this line
228+ endpoint = `${ config . baseURL } /api/bitcoin-rates/last-30-days` ;
229+
230+ console . log ( `[useBitcoinRatesWithRange] Fetching ${ options . timeRange } data from ${ endpoint } ` ) ;
231+
232+ const response = await fetch ( endpoint , {
233+ method : 'GET' ,
234+ headers : {
235+ 'Content-Type' : 'application/json' ,
236+ 'Authorization' : `Bearer ${ token } ` ,
237+ } ,
238+ signal : abortControllerRef . current . signal ,
239+ } ) ;
240+
241+ if ( ! response . ok ) {
242+ if ( response . status === 401 ) {
243+ handleLogout ( ) ;
244+ throw new Error ( 'Authentication failed. You have been logged out.' ) ;
245+ }
246+
247+ throw new Error ( `Network response was not ok (status: ${ response . status } )` ) ;
248+ }
249+
250+ const data = await response . json ( ) ;
251+ console . log ( `[useBitcoinRatesWithRange] Data received for ${ options . timeRange } ` ) ;
252+
253+ const processedData = processBitcoinData ( data , options . timeRange ) ;
254+
255+ // Cache the result
256+ rangeCache . set ( options . timeRange , {
257+ data : processedData ,
258+ timestamp : now ,
259+ } ) ;
260+
261+ setLastUpdate ( new Date ( ) ) ;
262+ return processedData ;
263+ } catch ( err : any ) {
264+ if ( err . name === 'AbortError' ) {
265+ console . log ( '[useBitcoinRatesWithRange] Request was aborted' ) ;
266+ return [ ] ;
267+ }
268+ throw err ;
269+ }
270+ } , [ options . timeRange , handleLogout , processBitcoinData ] ) ;
270271
271272 // Smart aggregation that maintains data shape while reducing points
272273 const smartAggregateData = ( data : Earning [ ] , targetPoints : number ) : Earning [ ] => {
0 commit comments