Skip to content

Commit a0be54f

Browse files
authored
Add periodic timestamp updates to timeline events (#7286)
* Initial plan * Plan: Add periodic time updates to timeline timestamps Co-authored-by: alexr00 <[email protected]> * Implement periodic timestamp updates in timeline Co-authored-by: alexr00 <[email protected]> * Optimize timestamp updates with variable intervals and visibility detection Co-authored-by: alexr00 <[email protected]> * Update timestamp intervals per feedback: 20s for <1min, 2min for <1hr, no updates for >1day Co-authored-by: alexr00 <[email protected]> * Change update frequencies
1 parent 6a9d5cc commit a0be54f

File tree

1 file changed

+82
-3
lines changed

1 file changed

+82
-3
lines changed

webviews/components/timestamp.tsx

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,98 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import * as React from 'react';
6+
import React, { useEffect, useState } from 'react';
77

88
import { dateFromNow } from '../../src/common/utils';
99

1010
export const Timestamp = ({ date, href }: { date: Date | string; href?: string }) => {
11+
const [timeString, setTimeString] = useState(dateFromNow(date));
1112
const title = typeof date === 'string' ? new Date(date).toLocaleString() : date.toLocaleString();
13+
14+
useEffect(() => {
15+
// Update the time string immediately
16+
setTimeString(dateFromNow(date));
17+
18+
// Calculate appropriate update interval based on how old the timestamp is
19+
const getUpdateInterval = () => {
20+
const now = Date.now();
21+
const timestamp = typeof date === 'string' ? new Date(date).getTime() : date.getTime();
22+
const ageInMinutes = (now - timestamp) / (1000 * 60);
23+
24+
// For very recent timestamps (< 1 minute), update every 20 seconds
25+
if (ageInMinutes < 1) {
26+
return 20000; // 20 seconds
27+
}
28+
// For timestamps < 1 hour old, update every 2 minutes
29+
else if (ageInMinutes < 60) {
30+
return 2 * 60000; // 2 minutes
31+
}
32+
// For timestamps < 1 day old, update every 10 minutes
33+
else if (ageInMinutes < 60 * 24) {
34+
return 10 * 60000; // 10 minutes
35+
}
36+
// Older timestamps shouldn't be updated
37+
return null;
38+
};
39+
40+
const intervalDuration = getUpdateInterval();
41+
42+
// If intervalDuration is null, don't set up any updates for very old timestamps
43+
if (intervalDuration === null) {
44+
return;
45+
}
46+
47+
let intervalId: number;
48+
49+
const updateTimeString = () => {
50+
// Only update if the page is visible
51+
if (document.visibilityState === 'visible') {
52+
setTimeString(dateFromNow(date));
53+
}
54+
};
55+
56+
const startInterval = () => {
57+
intervalId = window.setInterval(updateTimeString, intervalDuration);
58+
};
59+
60+
const handleVisibilityChange = () => {
61+
if (document.visibilityState === 'visible') {
62+
// Page became visible, update immediately and restart interval
63+
setTimeString(dateFromNow(date));
64+
if (intervalId) {
65+
clearInterval(intervalId);
66+
}
67+
startInterval();
68+
} else {
69+
// Page became hidden, pause the interval
70+
if (intervalId) {
71+
clearInterval(intervalId);
72+
}
73+
}
74+
};
75+
76+
// Start the interval
77+
startInterval();
78+
79+
// Listen for visibility changes
80+
document.addEventListener('visibilitychange', handleVisibilityChange);
81+
82+
// Clean up on component unmount
83+
return () => {
84+
if (intervalId) {
85+
clearInterval(intervalId);
86+
}
87+
document.removeEventListener('visibilitychange', handleVisibilityChange);
88+
};
89+
}, [date]);
90+
1291
return href ? (
1392
<a href={href} className="timestamp" title={title}>
14-
{dateFromNow(date)}
93+
{timeString}
1594
</a>
1695
) : (
1796
<div className="timestamp" title={title}>
18-
{dateFromNow(date)}
97+
{timeString}
1998
</div>
2099
);
21100
};

0 commit comments

Comments
 (0)