prop/lib/microwaveprop_web/live/contact_live/index.ex
Graham McIntire fc245367e3
User profiles at /u/:callsign, flexible band input, assorted UX cleanup
Profile page:
  * New MicrowavepropWeb.UserProfileLive at /u/:callsign is a public
    page showing a user's contacts and beacons. Resolves case-
    insensitively so /u/w5isp and /u/W5ISP are the same thing; unknown
    callsigns redirect to /. Uses daisyUI card / stats / table
    components with an avatar-placeholder initial and hero icons.
  * Accounts.get_user_by_callsign/1 (case-insensitive) plus
    Radio.list_contacts_for_user/1 and Beacons.list_beacons_for_user/1
    back the page. Beacons list includes both approved and pending so
    owners see their drafts.
  * The top nav bar and the three LiveView sidebars (MapLive,
    WeatherMapLive, ContactMapLive) now render the logged-in callsign
    as a navigate link to /u/:callsign instead of a static label.
  * Nine new tests cover the lookup, the LiveView render, and the
    ownership-scoped queries.

Flexible band input:
  * New Microwaveprop.Radio.BandResolver module converts any of:
    ADIF wavelength labels ("33cm", "1.25cm", "6mm", case/whitespace
    insensitive), numeric frequency strings ("903.100", "10368.000"),
    and canonical MHz integers into the one of the site's known bands.
    Returns the nearest allowed band for numeric inputs >= 900 MHz,
    nil otherwise.
  * 902 MHz is added to Contact.@allowed_bands, ContactEdit.@allowed_bands,
    AdifImport.@allowed_bands, and the BandResolver list so "33cm"
    round-trips end-to-end.
  * AdifImport and CsvImport now delegate band resolution to
    BandResolver, and Radio.create_contact/2 normalizes the :band attr
    on the way in so the manual form and any API callers benefit too.
    CsvImport's "invalid band" tests previously used 99999 MHz which
    the new resolver snaps to the nearest allowed band; swapped to
    "notaband" which is truly unresolvable.

Contacts and beacons list UX:
  * Remove the "Submitted" column from /contacts — it duplicated info
    already visible on the detail page and was pushing the real
    columns off narrow viewports. submitted_cell/1 and its three
    column-specific tests go with it.
  * Hide the Lat / Lon columns from /beacons — six decimal places of
    coordinates weren't useful next to the grid square and took a
    disproportionate amount of row width.
2026-04-12 16:13:08 -05:00

241 lines
7.8 KiB
Elixir

defmodule MicrowavepropWeb.ContactLive.Index do
@moduledoc false
use MicrowavepropWeb, :live_view
alias Microwaveprop.Radio
@sortable_fields ~w(station1 station2 band mode distance_km qso_timestamp inserted_at)
@default_sort_by "inserted_at"
@default_sort_order "desc"
@impl true
def mount(_params, _session, socket) do
{:ok, socket}
end
@impl true
def handle_params(params, _uri, socket) do
page =
case Integer.parse(Map.get(params, "page", "1")) do
{n, _} -> max(n, 1)
:error -> 1
end
sort_by = validate_sort_field(Map.get(params, "sort_by", @default_sort_by))
sort_order = validate_sort_order(Map.get(params, "sort_order", @default_sort_order))
search = Map.get(params, "search", "")
result =
Radio.list_contacts(
page: page,
sort_by: String.to_existing_atom(sort_by),
sort_order: String.to_existing_atom(sort_order),
search: search
)
{:noreply,
assign(socket,
page_title: "Contacts",
page: result.page,
total_pages: result.total_pages,
total_entries: result.total_entries,
contacts: result.entries,
grouped_contacts: result.grouped_entries,
sort_by: sort_by,
sort_order: sort_order,
search: search
)}
end
@impl true
def handle_event("search", %{"search" => search}, socket) do
{:noreply, push_patch(socket, to: ~p"/contacts?search=#{search}")}
end
@impl true
def handle_event("sort", %{"field" => field}, socket) do
field = validate_sort_field(field)
new_order =
if socket.assigns.sort_by == field && socket.assigns.sort_order == "asc",
do: "desc",
else: "asc"
params = %{sort_by: field, sort_order: new_order}
params = if socket.assigns.search == "", do: params, else: Map.put(params, :search, socket.assigns.search)
{:noreply, push_patch(socket, to: ~p"/contacts?#{params}")}
end
defp validate_sort_field(field) when field in @sortable_fields, do: field
defp validate_sort_field(_), do: @default_sort_by
defp validate_sort_order(order) when order in ~w(asc desc), do: order
defp validate_sort_order(_), do: @default_sort_order
@enrichment_fields [
{:hrrr_status, "HRRR"},
{:weather_status, "Weather"},
{:terrain_status, "Terrain"},
{:iemre_status, "IEMRE"}
]
@done_statuses [:complete, :unavailable]
defp enrichment_badge(contact) do
details =
Enum.map(@enrichment_fields, fn {field, name} ->
{name, Map.get(contact, field)}
end)
done = Enum.count(details, fn {_, s} -> s in @done_statuses end)
failed = Enum.count(details, fn {_, s} -> s == :failed end)
{label, class} =
cond do
done == 4 -> {"Complete", "badge-success"}
failed > 0 -> {"Failed", "badge-error"}
done > 0 -> {"Partial", "badge-warning"}
true -> {"Pending", "badge-ghost"}
end
tooltip =
Enum.map_join(details, ", ", fn {name, status} ->
"#{name}: #{status}"
end)
assigns = %{label: label, class: class, tooltip: tooltip}
~H"""
<span class={["badge badge-xs", @class]} title={@tooltip}>{@label}</span>
"""
end
@impl true
def render(assigns) do
~H"""
<Layouts.app flash={@flash} current_scope={@current_scope} max_width="max-w-7xl">
<.header>
Contacts
<:subtitle>{@total_entries} contacts</:subtitle>
<:actions>
<.link navigate={~p"/submit"} class="btn btn-primary">
<.icon name="hero-plus" class="w-5 h-5" /> Submit Contact
</.link>
</:actions>
</.header>
<form phx-submit="search" class="mb-4">
<div class="flex items-center gap-2">
<input
name="search"
value={@search}
placeholder="Search callsign or pair (e.g. W5LUA W5HN)..."
type="text"
class="input input-bordered input-sm w-64"
/>
<button type="submit" class="btn btn-sm btn-outline">Search</button>
<.link :if={@search != ""} patch={~p"/contacts"} class="btn btn-sm btn-ghost">Clear</.link>
</div>
</form>
<table class="table table-sm w-full">
<thead>
<tr>
<th class="w-8"></th>
<th>Station 1</th>
<th>Grid 1</th>
<th>Station 2</th>
<th>Grid 2</th>
<th>Band</th>
<th>Mode</th>
<th>Distance (km)</th>
<th>Enriched</th>
<th></th>
<th>Timestamp (UTC)</th>
<th>Added (UTC)</th>
</tr>
</thead>
<tbody>
<%= for {primary, reciprocals} <- @grouped_contacts do %>
<tr class="h-1">
<td colspan="12"></td>
</tr>
<tr
class={[
"hover:bg-base-200 cursor-pointer",
reciprocals != [] && "bg-primary/5"
]}
phx-click={JS.navigate(~p"/contacts/#{primary.id}")}
>
<td class="w-8 text-center">
<span
:if={reciprocals != []}
class="badge badge-primary badge-xs"
title="Has reciprocal contact"
>
{length(reciprocals) + 1}
</span>
</td>
<td class="font-mono font-semibold">{primary.station1}</td>
<td>{primary.grid1 || "—"}</td>
<td class="font-mono font-semibold">{primary.station2}</td>
<td>{primary.grid2 || "—"}</td>
<td>{primary.band}</td>
<td>{primary.mode}</td>
<td>{primary.distance_km}</td>
<td>{enrichment_badge(primary)}</td>
<td class="text-center">{if primary.flagged_invalid, do: "🚩"}</td>
<td>{Calendar.strftime(primary.qso_timestamp, "%Y-%m-%d %H:%M")}</td>
<td>{Calendar.strftime(primary.inserted_at, "%Y-%m-%d %H:%M")}</td>
</tr>
<%= for recip <- reciprocals do %>
<tr
class="hover:bg-base-200 cursor-pointer bg-primary/5 opacity-70 border-t-0"
phx-click={JS.navigate(~p"/contacts/#{recip.id}")}
>
<td class="w-8 text-center text-primary">↳</td>
<td class="font-mono">{recip.station1}</td>
<td>{recip.grid1 || "—"}</td>
<td class="font-mono">{recip.station2}</td>
<td>{recip.grid2 || "—"}</td>
<td>{recip.band}</td>
<td>{recip.mode}</td>
<td>{recip.distance_km}</td>
<td>{enrichment_badge(recip)}</td>
<td class="text-center">{if recip.flagged_invalid, do: "🚩"}</td>
<td>{Calendar.strftime(recip.qso_timestamp, "%Y-%m-%d %H:%M")}</td>
<td>{Calendar.strftime(recip.inserted_at, "%Y-%m-%d %H:%M")}</td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
<div class="flex items-center justify-between pt-4">
<.link
:if={@page > 1}
patch={
~p"/contacts?page=#{@page - 1}&sort_by=#{@sort_by}&sort_order=#{@sort_order}&search=#{@search}"
}
class="btn btn-sm btn-outline"
>
<.icon name="hero-chevron-left" class="w-4 h-4" /> Previous
</.link>
<span :if={@page <= 1} />
<span class="text-sm text-base-content/70">Page {@page} of {@total_pages}</span>
<.link
:if={@page < @total_pages}
patch={
~p"/contacts?page=#{@page + 1}&sort_by=#{@sort_by}&sort_order=#{@sort_order}&search=#{@search}"
}
class="btn btn-sm btn-outline"
>
Next <.icon name="hero-chevron-right" class="w-4 h-4" />
</.link>
<span :if={@page >= @total_pages} />
</div>
</Layouts.app>
"""
end
end