diff --git a/lib/microwaveprop_web/live/contact_live/show.ex b/lib/microwaveprop_web/live/contact_live/show.ex index 2202217d..2f1392de 100644 --- a/lib/microwaveprop_web/live/contact_live/show.ex +++ b/lib/microwaveprop_web/live/contact_live/show.ex @@ -721,6 +721,9 @@ defmodule MicrowavepropWeb.ContactLive.Show do <.icon name="hero-flag" class="w-4 h-4" /> {if @contact.flagged_invalid, do: "Unflag", else: "Flag as Invalid"} + <.link navigate={path_calc_url(@contact)} class="btn btn-sm btn-outline"> + <.icon name="hero-calculator" class="w-4 h-4" /> Open in Path Calculator + <.link navigate={~p"/contacts"} class="btn btn-sm btn-ghost"> <.icon name="hero-arrow-left" class="w-4 h-4" /> Back to Contacts @@ -2045,4 +2048,26 @@ defmodule MicrowavepropWeb.ContactLive.Show do true -> "badge badge-sm badge-ghost" end end + + defp path_calc_url(contact) do + source = prefer_grid(contact.grid1, contact.station1) + dest = prefer_grid(contact.grid2, contact.station2) + + params = maybe_put_band(%{"source" => source, "destination" => dest}, contact.band) + + "/path?" <> URI.encode_query(params) + end + + defp prefer_grid(grid, _fallback) when is_binary(grid) and grid != "", do: grid + defp prefer_grid(_grid, fallback), do: fallback || "" + + defp maybe_put_band(params, nil), do: params + + defp maybe_put_band(params, %Decimal{} = band) do + Map.put(params, "band", Decimal.to_string(band, :normal)) + end + + defp maybe_put_band(params, band) when is_integer(band), do: Map.put(params, "band", Integer.to_string(band)) + defp maybe_put_band(params, band) when is_binary(band), do: Map.put(params, "band", band) + defp maybe_put_band(params, _), do: params end