From 488e96b20bb80a9754c75045f2aa33692e06abc7 Mon Sep 17 00:00:00 2001 From: Jorge Manrubia Date: Fri, 4 Apr 2025 14:35:24 +0200 Subject: [PATCH] Handle times on the client to make this cache-friendly --- .../controllers/local_time_controller.js | 21 +++++++++++++ app/views/bubbles/_card_mini.html.erb | 11 ++----- app/views/bubbles/_meta.html.erb | 30 +++++-------------- 3 files changed, 32 insertions(+), 30 deletions(-) diff --git a/app/javascript/controllers/local_time_controller.js b/app/javascript/controllers/local_time_controller.js index bd02d8598..512169949 100644 --- a/app/javascript/controllers/local_time_controller.js +++ b/app/javascript/controllers/local_time_controller.js @@ -11,6 +11,7 @@ export default class extends Controller { this.shortDateFormatter = new Intl.DateTimeFormat(undefined, { month: "short", day: "numeric" }) this.dateTimeFormatter = new Intl.DateTimeFormat(undefined, { timeStyle: "short", dateStyle: "short" }) this.agoFormatter = new AgoFormatter() + this.daysAgoFormatter = new DaysAgoFormatter() this.indaysFormatter = new InDaysFormatter() } @@ -42,6 +43,10 @@ export default class extends Controller { this.#formatTime(this.agoFormatter, target) } + daysAgoTargetConnected(target) { + this.#formatTime(this.daysAgoFormatter, target) + } + indaysTargetConnected(target) { this.#formatTime(this.indaysFormatter, target) } @@ -87,6 +92,22 @@ class AgoFormatter { } } +class DaysAgoFormatter { + format(dt) { + const now = new Date() + + const startOfToday = new Date(now.getFullYear(), now.getMonth(), now.getDate()) + const startOfGivenDay = new Date(dt.getFullYear(), dt.getMonth(), dt.getDate()) + + const msPerDay = 1000 * 60 * 60 * 24 + const dayDiff = Math.floor((startOfToday - startOfGivenDay) / msPerDay) + + if (dayDiff === 0) return "Today" + if (dayDiff === 1) return "Yesterday" + return `in ${dayDiff} days` + } +} + class InDaysFormatter { format(dt) { const target = this.#beginningOfDay(dt) diff --git a/app/views/bubbles/_card_mini.html.erb b/app/views/bubbles/_card_mini.html.erb index 2bb4cdf07..18211e6c8 100644 --- a/app/views/bubbles/_card_mini.html.erb +++ b/app/views/bubbles/_card_mini.html.erb @@ -6,14 +6,9 @@

<%= bubble.title %>

-
<%= "Added to #{bubble.bucket.name} by #{bubble.creator.name}" %> <%= - days = (Date.current - bubble.created_at.to_date).to_i - case days - when 0 then "today" - when 1 then "yesterday" - else "#{days} days ago".html_safe - end - %>
+
<%= "Added to #{bubble.bucket.name} by #{bubble.creator.name}" %> + <%= local_datetime_tag(bubble.created_at, style: :daysAgo) %> +
<%= render "bubbles/assignees", bubble: bubble %>
<%= render "bubbles/tags", bubble: bubble %>
diff --git a/app/views/bubbles/_meta.html.erb b/app/views/bubbles/_meta.html.erb index e5d5881eb..b1b1c4ddc 100644 --- a/app/views/bubbles/_meta.html.erb +++ b/app/views/bubbles/_meta.html.erb @@ -1,32 +1,18 @@
- <%= bubble.creating? ? "Added" : "Updated" %> - <%= days = (Date.current - bubble.updated_at.to_date).to_i - case days - when 0 then "Today".html_safe - when 1 then "Yesterday".html_safe - else "#{days} days ago".html_safe - end %> + <%= bubble.creating? ? "Added" : "Updated" %> + <%= local_datetime_tag(bubble.updated_at, style: :daysAgo) %>
<% if bubble.creating? %> - Expires - <%= days = (bubble.auto_pop_at.to_date - Date.current).to_i - case days - when 0 then "Today".html_safe - else "in #{days} days".html_safe - end %> + Expires + <%= local_datetime_tag(bubble.auto_pop_at, style: :daysAgo) %> <% else %> - Added - <%= days = (Date.current - bubble.created_at.to_date).to_i - case days - when 0 then "Today".html_safe - when 1 then "Yesterday".html_safe - else "#{days} days ago".html_safe - end %> + Added + <%= local_datetime_tag(bubble.created_at, style: :daysAgo) %> <% end %>
@@ -39,7 +25,7 @@
- + <%= render "bubbles/boosts", bubble: bubble %>
-
\ No newline at end of file +