Skip to content

Commit 2e3f340

Browse files
committed
[e.cash] Update live transaction times in realtime
Summary: Currently the time-since value on the transactions is static. Adding an interval to update the times every 5s so the values stay more accurate. Test Plan: preview and check Reviewers: #bitcoin_abc, bytesofman Reviewed By: #bitcoin_abc, bytesofman Subscribers: bytesofman, Mengerian Differential Revision: https://reviews.bitcoinabc.org/D19355
1 parent 827bbc8 commit 2e3f340

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

web/e.cash/app/components/Home/LiveCard.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@ function calculateAmount(tx: Tx): bigint {
2222
/**
2323
* Helper function to format time since sent
2424
*/
25-
function formatTimeSince(timeFirstSeen: number): string {
25+
function formatTimeSince(timeFirstSeen: number, currentTime: number): string {
2626
if (!timeFirstSeen || timeFirstSeen === 0) {
2727
return "--";
2828
}
29-
const now = Math.floor(Date.now() / 1000);
30-
const secondsAgo = now - timeFirstSeen;
29+
const secondsAgo = currentTime - timeFirstSeen;
3130

3231
if (secondsAgo < 60) {
3332
return `${secondsAgo}s ago`;
@@ -71,6 +70,18 @@ function AnimatedEllipsis() {
7170

7271
export default function LiveCard() {
7372
const { mempool, isLoadingMempool } = useChronik();
73+
const [currentTime, setCurrentTime] = useState(() =>
74+
Math.floor(Date.now() / 1000),
75+
);
76+
77+
// Update current time to refresh time displays
78+
useEffect(() => {
79+
const interval = setInterval(() => {
80+
setCurrentTime(Math.floor(Date.now() / 1000));
81+
}, 1000);
82+
83+
return () => clearInterval(interval);
84+
}, []);
7485

7586
// Get the most recent MAX_TRANSACTIONS transactions
7687
const transactions = useMemo(() => {
@@ -86,7 +97,10 @@ export default function LiveCard() {
8697
<div className="flex flex-col gap-1">
8798
{transactions.map((tx, index) => {
8899
const amount = calculateAmount(tx);
89-
const timeSince = formatTimeSince(tx.timeFirstSeen);
100+
const timeSince = formatTimeSince(
101+
tx.timeFirstSeen,
102+
currentTime,
103+
);
90104
return (
91105
<motion.div
92106
key={tx.txid}

0 commit comments

Comments
 (0)