From 280461f28124021f84faa2128e13383a3498a685 Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Mon, 7 Jul 2025 10:01:14 -0500 Subject: [PATCH] add time ago on info page --- lib/aprsme_web/live/info_live/show.ex | 37 ++++++++++++++++++-- lib/aprsme_web/live/info_live/show.html.heex | 22 ++++++++++-- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/lib/aprsme_web/live/info_live/show.ex b/lib/aprsme_web/live/info_live/show.ex index c0e2ded..60c2495 100644 --- a/lib/aprsme_web/live/info_live/show.ex +++ b/lib/aprsme_web/live/info_live/show.ex @@ -124,7 +124,7 @@ defmodule AprsmeWeb.InfoLive.Show do callsign: p.sender, distance: format_distance(dist), course: course, - last_heard: PacketUtils.get_timestamp(p), + last_heard: format_timestamp_for_display(p), packet: p } end) @@ -182,6 +182,39 @@ defmodule AprsmeWeb.InfoLive.Show do defp to_float(_), do: 0.0 + defp format_timestamp_for_display(packet) do + received_at = get_received_at(packet) + + if received_at do + timestamp_dt = AprsmeWeb.TimeHelpers.to_datetime(received_at) + + if timestamp_dt do + %{ + time_ago: AprsmeWeb.TimeHelpers.time_ago_in_words(timestamp_dt), + formatted: Calendar.strftime(timestamp_dt, "%Y-%m-%d %H:%M:%S UTC") + } + else + %{ + time_ago: gettext("Unknown"), + formatted: "" + } + end + else + %{ + time_ago: gettext("Unknown"), + formatted: "" + } + end + end + + defp get_received_at(packet) do + cond do + Map.has_key?(packet, :received_at) -> packet.received_at + Map.has_key?(packet, "received_at") -> packet["received_at"] + true -> nil + end + end + defp format_distance(km) when km < 1.0 do "#{Float.round(km * 1000, 0)} m" end @@ -237,7 +270,7 @@ defmodule AprsmeWeb.InfoLive.Show do %{ callsign: p.sender, ssid: p.ssid, - last_heard: PacketUtils.get_timestamp(p), + last_heard: format_timestamp_for_display(p), packet: p } end) diff --git a/lib/aprsme_web/live/info_live/show.html.heex b/lib/aprsme_web/live/info_live/show.html.heex index 2ff961b..72340e1 100644 --- a/lib/aprsme_web/live/info_live/show.html.heex +++ b/lib/aprsme_web/live/info_live/show.html.heex @@ -346,7 +346,16 @@ <% end %> - {ssid_info.last_heard || ""} + <%= if ssid_info.last_heard do %> +
+ {ssid_info.last_heard.time_ago} +
+
+ {ssid_info.last_heard.formatted} +
+ <% else %> + {gettext("Unknown")} + <% end %> <% end %> @@ -441,7 +450,16 @@ <% end %> - {neighbor.last_heard || ""} + <%= if neighbor.last_heard do %> +
+ {neighbor.last_heard.time_ago} +
+
+ {neighbor.last_heard.formatted} +
+ <% else %> + {gettext("Unknown")} + <% end %> <% end %>