From 48cbf40b9da8200acdfbb58710007231389d46fe Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Fri, 17 Apr 2026 13:08:35 -0500 Subject: [PATCH] feat(contacts): show total contact count in list header --- .../live/contact_live/index.ex | 22 +++++++++++++++++-- .../live/contact_live_test.exs | 9 ++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/microwaveprop_web/live/contact_live/index.ex b/lib/microwaveprop_web/live/contact_live/index.ex index a532fdc2..33709ab8 100644 --- a/lib/microwaveprop_web/live/contact_live/index.ex +++ b/lib/microwaveprop_web/live/contact_live/index.ex @@ -3,6 +3,9 @@ defmodule MicrowavepropWeb.ContactLive.Index do use MicrowavepropWeb, :live_view use LiveTable.LiveResource, schema: Microwaveprop.Radio.Contact + alias Microwaveprop.Radio.Contact + alias Microwaveprop.Repo + def table_options do %{ sorting: %{default_sort: [inserted_at: :desc]}, @@ -45,7 +48,12 @@ defmodule MicrowavepropWeb.ContactLive.Index do @impl true def mount(_params, _session, socket) do - {:ok, assign(socket, :page_title, "Contacts")} + total = Repo.aggregate(Contact, :count, :id) + + {:ok, + socket + |> assign(:page_title, "Contacts") + |> assign(:total_contacts, total)} end @enrichment_fields [ @@ -96,6 +104,14 @@ defmodule MicrowavepropWeb.ContactLive.Index do defp flag_cell(_), do: "" + defp format_count(n) when is_integer(n) do + n + |> Integer.to_string() + |> String.reverse() + |> String.replace(~r/(\d{3})(?=\d)/, "\\1,") + |> String.reverse() + end + defp format_ts(nil), do: "" defp format_ts(%DateTime{} = dt), do: Calendar.strftime(dt, "%Y-%m-%d %H:%M") defp format_ts(%NaiveDateTime{} = dt), do: Calendar.strftime(dt, "%Y-%m-%d %H:%M") @@ -107,7 +123,9 @@ defmodule MicrowavepropWeb.ContactLive.Index do <.header> Contacts - <:subtitle>Sort or search by callsign, grid, or mode. + <:subtitle> + {format_count(@total_contacts)} total — sort or search by callsign, grid, or mode. + <:actions> <.link navigate={~p"/submit"} class="btn btn-primary"> <.icon name="hero-plus" class="w-5 h-5" /> Submit Contact diff --git a/test/microwaveprop_web/live/contact_live_test.exs b/test/microwaveprop_web/live/contact_live_test.exs index 238bf0ad..5e823a48 100644 --- a/test/microwaveprop_web/live/contact_live_test.exs +++ b/test/microwaveprop_web/live/contact_live_test.exs @@ -55,6 +55,15 @@ defmodule MicrowavepropWeb.ContactLiveTest do assert html =~ "Contacts" end + test "shows total contact count in subtitle", %{conn: conn} do + create_contact() + create_contact(%{station1: "N5AC"}) + create_contact(%{station1: "K5TR"}) + + {:ok, _lv, html} = live(conn, ~p"/contacts") + assert html =~ "3 total" + end + test "table shows contact data", %{conn: conn} do create_contact()