Merge pull request #721 from basecamp/time-or-date-formate

Show date instead of time for notifications older than 1 day
This commit is contained in:
Jason Zimdars
2025-07-07 17:34:04 -05:00
committed by GitHub
3 changed files with 20 additions and 3 deletions
@@ -2,7 +2,7 @@ import { Controller } from "@hotwired/stimulus"
import { differenceInDays } from "helpers/date_helpers"
export default class extends Controller {
static targets = [ "time", "date", "datetime", "shortdate", "ago", "indays", "daysago", "agoorweekday" ]
static targets = [ "time", "date", "datetime", "shortdate", "ago", "indays", "daysago", "agoorweekday", "timeordate" ]
static values = { refreshInterval: Number }
static classes = [ "local-time-value"]
@@ -19,6 +19,7 @@ export default class extends Controller {
this.datewithweekdayFormatter = new Intl.DateTimeFormat(undefined, { weekday: "long", month: "long", day: "numeric" })
this.indaysFormatter = new InDaysFormatter()
this.agoorweekdayFormatter = new DaysAgoOrWeekdayFormatter()
this.timeordateFormatter = new TimeOrDateFormatter()
}
connect() {
@@ -75,6 +76,10 @@ export default class extends Controller {
this.#formatTime(this.agoorweekdayFormatter, target)
}
timeordateTargetConnected(target) {
this.#formatTime(this.timeordateFormatter, target)
}
#refreshRelativeTimes() {
this.agoTargets.forEach(target => {
this.#formatTime(this.agoFormatter, target)
@@ -148,6 +153,18 @@ class InDaysFormatter {
}
}
class TimeOrDateFormatter {
format(date) {
const days = differenceInDays(date, new Date())
if (days >= 1) {
return new Intl.DateTimeFormat(undefined, { month: "short", day: "numeric" }).format(date)
} else {
return new Intl.DateTimeFormat(undefined, { timeStyle: "short" }).format(date)
}
}
}
function styleableValue(value) {
return `<span class="local-time-value">${value}</span>`
}
@@ -10,7 +10,7 @@
</div>
<div class="card__notification-meta overflow-ellipsis flex-item-no-shrink">
<span class="card__timestamp"><%= local_datetime_tag(notification.created_at) %></span>
<span class="card__timestamp"><%= local_datetime_tag(notification.created_at, style: :timeordate) %></span>
</div>
<div class="card__notification-meta flex-item-no-shrink">
@@ -10,7 +10,7 @@
</div>
<div class="card__notification-meta overflow-ellipsis flex-item-no-shrink">
<span class="card__timestamp"><%= local_datetime_tag(notification.created_at) %></span>
<span class="card__timestamp"><%= local_datetime_tag(notification.created_at, style: :timeordate) %></span>
</div>
<div class="card__notification-meta flex-item-no-shrink">