From af7413c899caeab7d047899a1acc49daa3e575b1 Mon Sep 17 00:00:00 2001 From: Dan Milne Date: Mon, 10 Nov 2025 08:04:35 +1100 Subject: [PATCH] Better layout --- .../controllers/timeline_controller.js | 101 +++++++++++++++++- app/views/events/index.html.erb | 9 +- 2 files changed, 105 insertions(+), 5 deletions(-) diff --git a/app/javascript/controllers/timeline_controller.js b/app/javascript/controllers/timeline_controller.js index 3a53404..4acdaa4 100644 --- a/app/javascript/controllers/timeline_controller.js +++ b/app/javascript/controllers/timeline_controller.js @@ -2,11 +2,20 @@ import { Controller } from "@hotwired/stimulus" export default class extends Controller { - static targets = ["row", "time", "bar"] + static targets = ["row", "time", "bar", "timestamp", "date"] + static values = { + mode: { type: String, default: "timeline" } // "timeline", "events", or "individual" + } connect() { - this.maxTotal = this.calculateMaxTotal() - this.updateTimeline() + if (this.modeValue === "timeline") { + this.maxTotal = this.calculateMaxTotal() + this.updateTimeline() + } else if (this.modeValue === "events") { + this.updateEventsTimes() + } else { + this.updateIndividualTimes() + } } calculateMaxTotal() { @@ -51,4 +60,90 @@ export default class extends Controller { barElement.style.width = `${barWidth}%` }, 100 + (index * 50)) } + + updateEventsTimes() { + this.timestampTargets.forEach(element => { + const iso = element.dataset.iso + if (iso) { + this.convertToLocalTime(element, iso, "time") + } + }) + + this.dateTargets.forEach(element => { + const iso = element.dataset.iso + if (iso) { + this.convertToLocalTime(element, iso, "date") + } + }) + } + + updateIndividualTimes() { + const iso = this.element.dataset.iso + if (iso) { + this.convertToLocalTime(this.element, iso, this.element.dataset.format || "both") + } + } + + convertToLocalTime(element, isoString, format) { + const date = new Date(isoString) + + switch (format) { + case "time": + element.textContent = date.toLocaleTimeString(undefined, { + hour: '2-digit', + minute: '2-digit', + second: '2-digit', + hour12: false + }) + element.title = date.toLocaleString(undefined, { + weekday: 'short', + year: 'numeric', + month: 'short', + day: 'numeric', + hour: '2-digit', + minute: '2-digit', + second: '2-digit', + timeZoneName: 'short' + }) + break + case "date": + element.textContent = date.toLocaleDateString(undefined, { + year: 'numeric', + month: '2-digit', + day: '2-digit' + }) + element.title = date.toLocaleString(undefined, { + weekday: 'short', + year: 'numeric', + month: 'short', + day: 'numeric', + hour: '2-digit', + minute: '2-digit', + second: '2-digit', + timeZoneName: 'short' + }) + break + case "both": + element.textContent = date.toLocaleString(undefined, { + year: 'numeric', + month: '2-digit', + day: '2-digit', + hour: '2-digit', + minute: '2-digit', + second: '2-digit', + hour12: false + }) + element.title = date.toLocaleString(undefined, { + weekday: 'short', + year: 'numeric', + month: 'short', + day: 'numeric', + hour: '2-digit', + minute: '2-digit', + second: '2-digit', + timeZoneName: 'short' + }) + break + } + } } \ No newline at end of file diff --git a/app/views/events/index.html.erb b/app/views/events/index.html.erb index 6e490bc..c855788 100644 --- a/app/views/events/index.html.erb +++ b/app/views/events/index.html.erb @@ -45,7 +45,7 @@ -
+

Events (<%= number_with_delimiter(@events.count) %>)

@@ -85,7 +85,12 @@ <% @events.each do |event| %> - <%= event.timestamp.strftime("%Y-%m-%d %H:%M:%S") %> +
+ <%= event.timestamp.strftime("%H:%M:%S") %> +
+
+ <%= event.timestamp.strftime("%Y-%m-%d") %> +
<%= event.ip_address %>