git » git-arr » next » tree

[next] / static / git-arr.js

/* Miscellaneous javascript functions for git-arr. */

/* Return the current timestamp. */
function now() {
    return (new Date().getTime() / 1000);
}

/* Return a human readable string telling "how long ago" for a timestamp. */
function how_long_ago(timestamp) {
    if (timestamp < 0)
        return "never";

    var seconds = Math.floor(now() - timestamp);

    var interval = Math.floor(seconds / (365 * 24 * 60 * 60));
    if (interval > 1)
        return interval + " years ago";

    interval = Math.floor(seconds / (30 * 24 * 60 * 60));
    if (interval > 1)
        return interval + " months ago";

    interval = Math.floor(seconds / (24 * 60 * 60));

    if (interval > 1)
        return interval + " days ago";
    interval = Math.floor(seconds / (60 * 60));

    if (interval > 1)
        return interval + " hours ago";

    interval = Math.floor(seconds / 60);
    if (interval > 1)
        return interval + " minutes ago";

    if (seconds > 1)
        return Math.floor(seconds) + " seconds ago";

    return "about now";
}

/* Load the timestamps from the modified_ts.json file, and then
 * insert the human-friendly representation into the corresponding span.age
 * elements. */
async function load_timestamps() {
    const response = await fetch("modified_ts.json");
    if (!response.ok) {
        throw new Error(`fetch error, status: ${response.status}`);
    }

    const json = await response.json();
    console.log("Loaded timestamps:", json);

    for (const [repo_name, timestamp] of Object.entries(json)) {
        const e = document.getElementById("age:" + repo_name);
        if (!e) {
            console.warn(`No element found for repo: ${repo_name}`);
            continue;
        }
        e.innerHTML = how_long_ago(timestamp);
        e.style.display = "inline";

        if (timestamp > 0) {
            var age = now() - timestamp;
            if (age < (2 * 60 * 60))
                e.className = e.className + " age-band0";
            else if (age < (3 * 24 * 60 * 60))
                e.className = e.className + " age-band1";
            else if (age < (30 * 24 * 60 * 60))
                e.className = e.className + " age-band2";
        }
    }
}

function toggle(id) {
    var e = document.getElementById(id);

    if (e.style.display == "") {
        e.style.display = "none"
    } else if (e.style.display == "none") {
        e.style.display = ""
    }
}