refactor: improve code quality and fix potential issues

- Remove duplicate time formatting function in components.ex
  - Use existing time_ago_in_words from TimeHelpers instead of custom format_time_ago
- Remove unnecessary Float multiplication in format_coordinates
  - Changed Float.round(lat * 1.0, 4) to Float.round(lat, 4)
- Fix TypeScript type safety issues
  - Add pendingMarkers to LiveViewHookContext interface
  - Add initializationTimeout property for proper cleanup
- Add cleanup for map initialization retry timeout
  - Prevent memory leak by clearing timeout in destroyed() method
- Format code with mix format

All changes maintain backward compatibility while improving code quality.
This commit is contained in:
Graham McIntire 2025-10-12 14:02:27 -05:00
parent 8a70011679
commit 62ada761cd
No known key found for this signature in database
4 changed files with 14 additions and 15 deletions

View file

@ -1968,6 +1968,12 @@ let MapAPRSMap = {
self.programmaticMoveTimeout = undefined;
}
// Clear initialization timeout
if (self.initializationTimeout !== undefined) {
clearTimeout(self.initializationTimeout);
self.initializationTimeout = undefined;
}
// Clear any dimension check timeouts
if (self.cleanupTimeouts !== undefined) {
self.cleanupTimeouts.forEach((timeout: number) => {

View file

@ -36,6 +36,7 @@ export interface LiveViewHookContext {
markerClusterGroup?: MarkerClusterGroup;
cleanupTimeouts?: ReturnType<typeof setTimeout>[];
pendingMarkers?: MarkerData[];
initializationTimeout?: ReturnType<typeof setTimeout>;
}
export interface MarkerData {

View file

@ -5,6 +5,8 @@ defmodule AprsmeWeb.MapLive.Components do
"""
use AprsmeWeb, :html
import AprsmeWeb.TimeHelpers, only: [time_ago_in_words: 1]
attr :flash, :map, default: %{}
attr :slideover_open, :boolean, default: false
attr :map_center, :map, required: true
@ -194,7 +196,7 @@ defmodule AprsmeWeb.MapLive.Components do
</form>
<%= if @tracked_callsign_latest_packet do %>
<div class="mt-2 text-xs text-gray-600">
Last seen: {format_time_ago(@tracked_callsign_latest_packet.received_at)}
Last seen: {time_ago_in_words(@tracked_callsign_latest_packet.received_at)}
</div>
<% end %>
</div>
@ -235,7 +237,7 @@ defmodule AprsmeWeb.MapLive.Components do
</div>
<% end %>
<div class="text-xs text-gray-500 mt-2">
via {@packet.path || "direct"} {format_time_ago(@packet.received_at)}
via {@packet.path || "direct"} {time_ago_in_words(@packet.received_at)}
</div>
</div>
<%= if @packet.has_position do %>
@ -256,18 +258,8 @@ defmodule AprsmeWeb.MapLive.Components do
end
# Helper functions
defp format_time_ago(datetime) do
# Implement time ago formatting
case DateTime.diff(DateTime.utc_now(), datetime, :second) do
seconds when seconds < 60 -> "#{seconds}s ago"
seconds when seconds < 3600 -> "#{div(seconds, 60)}m ago"
seconds when seconds < 86_400 -> "#{div(seconds, 3600)}h ago"
seconds -> "#{div(seconds, 86_400)}d ago"
end
end
defp format_coordinates(lat, lon) when is_number(lat) and is_number(lon) do
"#{Float.round(lat * 1.0, 4)}, #{Float.round(lon * 1.0, 4)}"
"#{Float.round(lat, 4)}, #{Float.round(lon, 4)}"
end
defp format_coordinates(_, _), do: "Unknown location"

View file

@ -1007,8 +1007,8 @@ defmodule AprsmeWeb.MapLive.Index do
~H"""
<.map_styles />
<.map_container
slideover_open={@slideover_open}
<.map_container
slideover_open={@slideover_open}
map_center={@map_center}
map_zoom={@map_zoom}
/>