Skip to content

Commit e8b4548

Browse files
authored
Merge pull request #57 from anxkhn/fix/update-cycle
feat: implement dynamic last updated phrase
2 parents 66e2d3e + 8388945 commit e8b4548

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
<cite title="Source Title">
5353
<a href="https://leetcode.com/discuss/compensation">leetcode/compensation</a>
5454
</cite>
55-
/ <span style="color: green;">updated weekly</span>
55+
/ <span id="lastUpdatedPhrase" style="color: green;">updated recently</span>
5656
</footer>
5757
</blockquote>
5858
</div>

script.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,31 @@ function sortAndSliceData(companyCounts) {
372372

373373
document.addEventListener('DOMContentLoaded', async function () {
374374

375-
async function fetchOffers() {
375+
async function fetchLastUpdatedPhrase() {
376+
const span = document.getElementById('lastUpdatedPhrase');
377+
if (!span) return;
378+
const url = 'https://api.github.com/repos/kuutsav/leetcode-compensation/commits?sha=master&path=data&per_page=1';
379+
try {
380+
const res = await fetch(url, { headers: { Accept: 'application/vnd.github+json' } });
381+
if (!res.ok) throw new Error();
382+
const commit = (await res.json())[0];
383+
const date = commit?.commit?.committer?.date || commit?.commit?.author?.date;
384+
span.textContent = `updated ${date ? formatTimeAgo(new Date(date)) : 'recently'} ↺`;
385+
} catch {
386+
span.textContent = 'updated recently ↺';
387+
}
388+
}
389+
390+
function formatTimeAgo(date) {
391+
const mins = Math.floor((Date.now() - date) / 60000);
392+
const rtf = Intl?.RelativeTimeFormat && new Intl.RelativeTimeFormat(undefined, { numeric: 'auto' });
393+
const fmt = (v, u) => rtf ? rtf.format(-v, u) : `${v} ${u}${v !== 1 ? 's' : ''} ago`;
394+
if (mins < 60) return fmt(mins, 'minute');
395+
const hrs = Math.floor(mins / 60);
396+
if (hrs < 24) return fmt(hrs, 'hour');
397+
return fmt(Math.floor(hrs / 24), 'day');
398+
}
399+
async function fetchOffers() {
376400
const response = await fetch('data/parsed_comps.json');
377401
const data = await response.json();
378402
offers = data;
@@ -382,7 +406,8 @@ document.addEventListener('DOMContentLoaded', async function () {
382406
}
383407

384408
await fetchOffers();
385-
409+
// Fetch and show last updated phrase
410+
fetchLastUpdatedPhrase();
386411
// Initialize charts and stats
387412
setStatsStr(filteredOffers);
388413
plotHistogram(filteredOffers, 'total');

0 commit comments

Comments
 (0)