diff --git a/assets/js/clubstatus.js b/assets/js/clubstatus.js new file mode 100644 index 0000000..d87eb04 --- /dev/null +++ b/assets/js/clubstatus.js @@ -0,0 +1,67 @@ +const spaceapiUrl = "https://spaceapi.hamburg.ccc.de" +const interval_ms = 60000 + +console.log("clubstatus.js geladen!"); /* TODO: ENTFERNEN */ + +function timeDistanceDE(pastDate) { + const seconds = Math.floor((Date.now() - pastDate.getTime()) / 1000) + const minutes = Math.floor(seconds / 60) + const hours = Math.floor(minutes / 60) + const days = Math.floor(hours / 24) + + if (days = 1) return `since ${days}d` + if (days > 1) return `since ${days}d` + if (hours = 1) return `since ${hours}h` + if (hours > 1) return `since ${hours}h` + if (minutes = 1) return `since ${minutes}min` + if (minutes > 1) return `since ${minutes}min` + return `gerade eben` +} + +function updateRoomState(openState, lastChangeTimestamp) { + const roomstate = document.querySelector('#clubstatus-badge') + const statusItem = roomstate.querySelector(".state") + const durationItem = roomstate.querySelector(".duration") + + if (!statusItem || !durationItem) return + + roomstate.classList.remove("open", "closed", "unknown") + statusItem.classList.remove("open", "closed", "unknown") + + if (openState === null || lastChangeTimestamp === null) { + statusItem.textContent = "unbekannt" + statusItem.classList.add("unknown") + durationItem.textContent = "(since unbekannt)" + } else { + const changeDate = new Date(lastChangeTimestamp * 1000) + const sinceText = timeDistanceDE(changeDate) + + if (openState) { + statusItem.textContent = "open" + statusItem.classList.add("open") + } else { + statusItem.textContent = "closed" + statusItem.classList.add("closed") + } + + durationItem.textContent = `(${sinceText})` + } +} + +function fetchRoomState() { + fetch(spaceapiUrl) + .then(response => response.json()) + .then(data => { + const openState = data?.state?.open ?? null + const lastChange = data?.state?.lastchange ?? null + updateRoomState(openState, lastChange) + }) + .catch(err => { + console.error("Fehler beim Laden der SpaceAPI:", err) + updateRoomState(null, null) + }) +} + +// Initialer Abruf + dann regelmäßig +fetchRoomState() +setInterval(fetchRoomState, interval_ms) diff --git a/content/_index.md b/content/_index.md index 872be10..6945629 100644 --- a/content/_index.md +++ b/content/_index.md @@ -1,6 +1,27 @@ --- description: "Startseite CCCB mit Kurzkalender" --- +/* this section will be enabled when the cccb spaceapi is public + +
+
spacestatus unknown
+
+ + + +*/ ### Nächste Veranstaltungen: diff --git a/layouts/partials/clubstatus.html b/layouts/partials/clubstatus.html new file mode 100644 index 0000000..8288367 --- /dev/null +++ b/layouts/partials/clubstatus.html @@ -0,0 +1,3 @@ +{{ $js := resources.Get "js/clubstatus.js" | resources.Minify | resources.Fingerprint }} + + diff --git a/layouts/partials/footer.html b/layouts/partials/footer.html new file mode 100644 index 0000000..12a2638 --- /dev/null +++ b/layouts/partials/footer.html @@ -0,0 +1,70 @@ +