Skip to content

Commit 304679d

Browse files
Copilot0xrinegade
andcommitted
Address code review feedback: fix retry logic, remove unused props, improve time tracking, throttle onRead calls, and add logging
Co-authored-by: 0xrinegade <[email protected]>
1 parent 18f610d commit 304679d

File tree

7 files changed

+212
-126
lines changed

7 files changed

+212
-126
lines changed

src/components/RewardDashboard.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
getRemainingFailedClaimCooldown,
1313
getCooldownStats
1414
} from '../utils/rewardTransactions';
15-
import { useAutoClaimManager } from '../utils/autoClaimManager';
15+
import { useAutoClaimManager } from '../hooks/useAutoClaimManager';
1616
import {
1717
REWARD_CONSTANTS,
1818
CONVERSION_HELPERS,

src/components/common/RealTimeFeedback.js

Lines changed: 75 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useEffect, useRef } from 'react';
1+
import React, { useState, useEffect, useRef, useCallback } from 'react';
22
import PropTypes from 'prop-types';
33

44
/**
@@ -27,77 +27,10 @@ const RealTimeFeedback = ({
2727

2828
const intervalRef = useRef(null);
2929
const retryTimeoutRef = useRef(null);
30-
const wsRef = useRef(null);
31-
32-
// Simulate WebSocket connection
33-
useEffect(() => {
34-
if (!transactionId && !networkEndpoint) return;
35-
36-
const connect = () => {
37-
setConnectionStatus('connecting');
38-
39-
// Simulate connection delay
40-
setTimeout(() => {
41-
setConnectionStatus('connected');
42-
setRetryCount(0);
43-
startPolling();
44-
}, 500);
45-
};
46-
47-
const disconnect = () => {
48-
setConnectionStatus('disconnected');
49-
stopPolling();
50-
};
51-
52-
const reconnect = () => {
53-
if (retryCount < maxRetries) {
54-
setConnectionStatus('reconnecting');
55-
setRetryCount(prev => prev + 1);
56-
57-
retryTimeoutRef.current = setTimeout(() => {
58-
connect();
59-
}, retryDelay * Math.pow(2, retryCount)); // Exponential backoff
60-
} else {
61-
setConnectionStatus('failed');
62-
}
63-
};
64-
65-
connect();
66-
67-
return () => {
68-
disconnect();
69-
if (retryTimeoutRef.current) {
70-
clearTimeout(retryTimeoutRef.current);
71-
}
72-
};
73-
}, [transactionId, networkEndpoint, maxRetries, retryDelay, retryCount]);
74-
75-
// Polling simulation for real-time updates
76-
const startPolling = () => {
77-
if (intervalRef.current) return;
78-
79-
intervalRef.current = setInterval(() => {
80-
// Simulate network status updates
81-
updateNetworkStatus();
82-
83-
// Simulate transaction updates if tracking a transaction
84-
if (transactionId) {
85-
updateTransactionStatus();
86-
}
87-
88-
setLastUpdate(new Date().toISOString());
89-
}, updateInterval);
90-
};
91-
92-
const stopPolling = () => {
93-
if (intervalRef.current) {
94-
clearInterval(intervalRef.current);
95-
intervalRef.current = null;
96-
}
97-
};
30+
const retryCountRef = useRef(0);
9831

9932
// Simulate network status updates
100-
const updateNetworkStatus = () => {
33+
const updateNetworkStatus = useCallback(() => {
10134
const newNetworkStatus = {
10235
health: Math.max(85, Math.min(100, networkStatus.health + (Math.random() - 0.5) * 10)),
10336
latency: Math.max(10, Math.min(500, networkStatus.latency + (Math.random() - 0.5) * 50)),
@@ -110,10 +43,10 @@ const RealTimeFeedback = ({
11043
if (onNetworkChange) {
11144
onNetworkChange(newNetworkStatus);
11245
}
113-
};
46+
}, [networkStatus.health, networkStatus.latency, onNetworkChange]);
11447

11548
// Simulate transaction status updates
116-
const updateTransactionStatus = () => {
49+
const updateTransactionStatus = useCallback(() => {
11750
const statuses = ['submitted', 'pending', 'confirming', 'confirmed', 'finalized'];
11851
const currentIndex = transactionStatus ? statuses.indexOf(transactionStatus.status) : -1;
11952

@@ -156,7 +89,76 @@ const RealTimeFeedback = ({
15689
sendPushNotification(newStatus);
15790
}
15891
}
159-
};
92+
}, [transactionStatus, networkStatus.blockHeight, queuePosition, onStatusUpdate, enableSound, enablePushNotifications]);
93+
94+
// Polling simulation for real-time updates
95+
const startPolling = useCallback(() => {
96+
if (intervalRef.current) return;
97+
98+
intervalRef.current = setInterval(() => {
99+
// Simulate network status updates
100+
updateNetworkStatus();
101+
102+
// Simulate transaction updates if tracking a transaction
103+
if (transactionId) {
104+
updateTransactionStatus();
105+
}
106+
107+
setLastUpdate(new Date().toISOString());
108+
}, updateInterval);
109+
}, [updateInterval, transactionId, updateNetworkStatus, updateTransactionStatus]);
110+
111+
const stopPolling = useCallback(() => {
112+
if (intervalRef.current) {
113+
clearInterval(intervalRef.current);
114+
intervalRef.current = null;
115+
}
116+
}, []);
117+
118+
// Simulate WebSocket connection
119+
useEffect(() => {
120+
if (!transactionId && !networkEndpoint) return;
121+
122+
const connect = () => {
123+
setConnectionStatus('connecting');
124+
125+
// Simulate connection delay
126+
setTimeout(() => {
127+
setConnectionStatus('connected');
128+
retryCountRef.current = 0;
129+
setRetryCount(0);
130+
startPolling();
131+
}, 500);
132+
};
133+
134+
const disconnect = () => {
135+
setConnectionStatus('disconnected');
136+
stopPolling();
137+
};
138+
139+
const reconnect = () => {
140+
if (retryCountRef.current < maxRetries) {
141+
setConnectionStatus('reconnecting');
142+
retryCountRef.current += 1;
143+
setRetryCount(retryCountRef.current);
144+
145+
retryTimeoutRef.current = setTimeout(() => {
146+
connect();
147+
}, retryDelay * Math.pow(2, retryCountRef.current - 1)); // Exponential backoff
148+
} else {
149+
setConnectionStatus('failed');
150+
}
151+
};
152+
153+
connect();
154+
155+
return () => {
156+
disconnect();
157+
if (retryTimeoutRef.current) {
158+
clearTimeout(retryTimeoutRef.current);
159+
}
160+
};
161+
}, [transactionId, networkEndpoint, maxRetries, retryDelay, startPolling, stopPolling]);
160162

161163
// Play notification sound
162164
const playNotificationSound = () => {

src/components/common/TransactionAnalytics.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ import PropTypes from 'prop-types';
77
* Provides transparency and builds user confidence
88
*/
99
const TransactionAnalytics = ({
10-
userStats = {},
1110
globalStats = {},
1211
recentTransactions = [],
13-
showPersonalStats = true,
1412
showGlobalStats = true,
1513
showRecentActivity = true,
1614
timeframe = '7d',
@@ -314,7 +312,6 @@ const TransactionAnalytics = ({
314312
};
315313

316314
TransactionAnalytics.propTypes = {
317-
userStats: PropTypes.object,
318315
globalStats: PropTypes.object,
319316
recentTransactions: PropTypes.arrayOf(
320317
PropTypes.shape({
@@ -326,7 +323,6 @@ TransactionAnalytics.propTypes = {
326323
value: PropTypes.number
327324
})
328325
),
329-
showPersonalStats: PropTypes.bool,
330326
showGlobalStats: PropTypes.bool,
331327
showRecentActivity: PropTypes.bool,
332328
timeframe: PropTypes.oneOf(['24h', '7d', '30d', '90d']),

src/components/common/TransactionProgressIndicator.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,20 @@ const TransactionProgressIndicator = ({
1919
showStepDetails = true,
2020
compact = false
2121
}) => {
22-
const [elapsedTime, setElapsedTime] = useState(0);
2322
const [startTime] = useState(Date.now());
23+
const [, forceRender] = useState({});
2424

25-
// Update elapsed time every second
25+
// Force re-render every second to update elapsed time
2626
useEffect(() => {
2727
const interval = setInterval(() => {
28-
setElapsedTime(Math.floor((Date.now() - startTime) / 1000));
28+
forceRender({});
2929
}, 1000);
3030

3131
return () => clearInterval(interval);
32-
}, [startTime]);
32+
}, []);
33+
34+
// Calculate elapsed time directly during render for accuracy
35+
const elapsedTime = Math.floor((Date.now() - startTime) / 1000);
3336

3437
// Calculate progress percentage
3538
const progressPercentage = totalSteps

src/components/notifications/EnhancedNotification.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useEffect } from 'react';
1+
import React, { useState, useEffect, useRef } from 'react';
22
import PropTypes from 'prop-types';
33

44
/**
@@ -33,6 +33,7 @@ const EnhancedNotification = ({
3333
const [isExpanded, setIsExpanded] = useState(expanded);
3434
const [isVisible, setIsVisible] = useState(true);
3535
const [timeRemaining, setTimeRemaining] = useState(autoClose ? autoCloseTime / 1000 : null);
36+
const readCalledRef = useRef(false);
3637

3738
// Auto-close timer
3839
useEffect(() => {
@@ -52,10 +53,15 @@ const EnhancedNotification = ({
5253
}
5354
}, [autoClose, persistent, type, read, id, onDelete]);
5455

55-
// Mark as read when interacted with
56+
// Mark as read when interacted with (with throttling to prevent multiple calls)
5657
const handleRead = () => {
57-
if (!read && onRead) {
58+
if (!read && onRead && !readCalledRef.current) {
59+
readCalledRef.current = true;
5860
onRead(id);
61+
// Reset the flag after a short delay to allow future reads if needed
62+
setTimeout(() => {
63+
readCalledRef.current = false;
64+
}, 100);
5965
}
6066
};
6167

src/hooks/useAutoClaimManager.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* React Hook for Auto-Claim Manager
3+
*
4+
* Provides React hook interface for the AutoClaimManager class
5+
*/
6+
7+
import { useEffect } from 'react';
8+
import { getAutoClaimManager } from '../utils/autoClaimManager';
9+
10+
/**
11+
* React hook for using auto-claim manager
12+
*/
13+
export const useAutoClaimManager = (wallet, connection) => {
14+
const manager = getAutoClaimManager(wallet, connection);
15+
16+
// Auto-start if enabled
17+
useEffect(() => {
18+
if (manager.getConfig().enabled && !manager.isRunning && wallet?.connected) {
19+
manager.start();
20+
}
21+
22+
return () => {
23+
if (manager.isRunning) {
24+
manager.stop();
25+
}
26+
};
27+
}, [manager, wallet?.connected]);
28+
29+
return manager;
30+
};
31+
32+
export default useAutoClaimManager;

0 commit comments

Comments
 (0)