defmodule CartosnapAppWeb.MapLive do
@moduledoc """
Every photo with a location, on one map. Pins cluster as you zoom out;
the panel beside the map lists what's in view, and hovering a photo there
lifts its pin.
The map reports each pan and zoom (`"bounds"`), and the panel is
re-streamed from the pins already loaded, so moving around never queries
the database.
"""
use CartosnapAppWeb, :live_view
import CartosnapAppWeb.Components.PhotoMap
alias CartosnapApp.Photos
alias CartosnapAppWeb.Components.PhotoMap
# The panel shows this many of what's in view; the map shows them all.
@panel_limit 60
@impl true
def mount(_params, _session, socket) do
if connected?(socket), do: Photos.subscribe(socket.assigns.current_scope)
{:ok,
socket
|> assign(:page_title, "Map")
|> assign(:promoted_only, false)
|> assign(:bounds, nil)
|> assign(:unmapped, 0)
|> load_points()}
end
@impl true
def render(assigns) do
~H"""
<Layouts.app flash={@flash} current_scope={@current_scope} active={:map} wide>
<div class="grid h-[calc(100dvh-4rem)] grid-rows-[minmax(0,1fr)_auto] lg:grid-cols-[minmax(0,1fr)_22rem] lg:grid-rows-1">
<div class="relative min-h-0">
<.photo_map
id="photos-map"
points={@points}
bounds_event="bounds"
focus_from="#map-panel-photos"
class="h-full"
rounded={false}
label="Map of your photos"
/>
<div class="absolute right-3 top-3 z-[500] flex gap-2">
<button
type="button"
id="map-toggle-promoted"
phx-click="toggle-promoted"
aria-pressed={to_string(@promoted_only)}
class={[
"btn btn-sm gap-1.5 shadow-md",
if(@promoted_only, do: "btn-warning", else: "bg-base-100/90 backdrop-blur")
]}
>
<.icon
name={if @promoted_only, do: "hero-star-solid", else: "hero-star"}
class="size-4"
/> Promoted
</button>
</div>
</div>
<aside
id="map-panel"
class="flex max-h-[40dvh] min-h-0 flex-col border-t border-base-300 bg-base-100 lg:max-h-none lg:border-l lg:border-t-0"
>
<div class="flex items-baseline justify-between gap-2 border-b border-base-300 px-4 py-3">
<h1 class="font-semibold">In view</h1>
<p class="text-xs text-base-content/60" id="map-panel-count">
{@in_view} of {length(@points)}{if @in_view > @panel_limit,
do: " · showing #{@panel_limit}"}
</p>
</div>
<div
id="map-panel-photos"
phx-update="stream"
class="grid min-h-0 flex-1 auto-rows-min grid-cols-3 gap-1.5 overflow-y-auto p-3 lg:grid-cols-2"
>
<div
id="map-panel-empty"
class="col-span-full hidden py-10 text-center text-sm text-base-content/60 only:block"
>
<%= if @points == [] do %>
Photos with GPS data land here.
<.link navigate={~p"/upload"} class="link link-primary">Upload some</.link>
<% else %>
Nothing here. Zoom out or pan around.
<% end %>
</div>
<.link
:for={{dom_id, point} <- @streams.visible}
id={dom_id}
navigate={point.href}
data-photo-id={point.id}
class="group relative block aspect-square overflow-hidden rounded-field bg-base-300 focus-visible:outline-2 focus-visible:outline-primary"
>
<img
:if={point.thumb}
src={point.thumb}
alt={point.title}
loading="lazy"
class="h-full w-full object-cover transition duration-300 group-hover:scale-105"
/>
<span class="pointer-events-none absolute inset-x-0 bottom-0 truncate bg-gradient-to-t from-black/70 to-transparent px-1.5 pb-1 pt-4 text-[10px] text-white opacity-0 transition group-hover:opacity-100">
{point.date || point.title}
</span>
<span
:if={point.promoted}
class="absolute right-1 top-1 grid size-5 place-items-center rounded-full bg-warning text-warning-content"
>
<.icon name="hero-star-solid" class="size-3" />
</span>
</.link>
</div>
<p
:if={@unmapped > 0}
id="map-unmapped"
class="border-t border-base-300 px-4 py-2 text-xs text-base-content/60"
>
{@unmapped} {if @unmapped == 1, do: "photo has", else: "photos have"} no location.
</p>
</aside>
</div>
</Layouts.app>
"""
end
@impl true
def handle_event("bounds", bounds, socket) do
{:noreply, socket |> assign(:bounds, bounds) |> stream_visible()}
end
def handle_event("toggle-promoted", _params, socket) do
{:noreply, socket |> update(:promoted_only, &(!&1)) |> load_points()}
end
@impl true
def handle_info({event, _photo}, socket)
when event in [:photo_created, :photo_updated, :photo_deleted] do
# A new location, a new thumbnail, a promotion or a deletion: redraw.
{:noreply, load_points(socket)}
end
defp load_points(socket) do
scope = socket.assigns.current_scope
opts = [promoted: socket.assigns.promoted_only]
photos = Photos.list_photos(scope, opts)
points = PhotoMap.points(photos)
socket
|> assign(:points, points)
|> assign(:unmapped, length(photos) - length(points))
|> stream_visible()
end
defp stream_visible(socket) do
visible =
case socket.assigns.bounds do
nil -> socket.assigns.points
bounds -> Enum.filter(socket.assigns.points, &PhotoMap.in_bounds?(&1, bounds))
end
socket
|> assign(:in_view, length(visible))
|> assign(:panel_limit, @panel_limit)
|> stream(:visible, Enum.take(visible, @panel_limit), reset: true)
end
end
neiam
/cartosnap_ex
public · Issues · Pulls · Labels · Forks · Compare · Actions queued · Badges · Packages
⭐
Log in to mark this repository.
6.1 KiB · text
History
e569b6d