6.3 KiB · text History e569b6d
defmodule CartosnapAppWeb.HomeLive do
  @moduledoc """
  The front page, for everyone, signed in or not: a map of the photos in
  every public trip, with the trips listed beside it. A pin opens its trip's
  public view at that photo (`/t/<id>?photo=<id>`); a trip opens at the
  start.

  Only public trips appear here; trips shared by link are for the people
  the link was sent to and are never listed. The page follows trips being
  made public or private while it's open.
  """
  use CartosnapAppWeb, :live_view

  import CartosnapAppWeb.Components.PhotoMap

  alias CartosnapApp.Storage
  alias CartosnapApp.Trips
  alias CartosnapAppWeb.Components.PhotoMap
  alias CartosnapAppWeb.TripLive

  @impl true
  def mount(_params, _session, socket) do
    if connected?(socket), do: Trips.subscribe_public()

    {:ok,
     socket
     |> assign(:page_title, "Shared photos, on the map")
     |> load_trips()}
  end

  @impl true
  def render(assigns) do
    ~H"""
    <Layouts.app flash={@flash} current_scope={@current_scope} wide>
      <div class="flex flex-col lg:h-[calc(100dvh-4rem)]">
        <%= if signed_in?(@current_scope) do %>
          <div class="flex flex-wrap items-center justify-between gap-2 border-b border-base-300 px-4 py-3 sm:px-6">
            <div>
              <h1 class="text-lg font-bold tracking-tight">Public trips</h1>
              <p class="text-xs text-base-content/60">
                Trips people have opened to everyone. Yours are private unless you make them public.
              </p>
            </div>
            <.link navigate={~p"/photos"} class="btn btn-ghost btn-sm" id="home-my-photos">
              Your photos <.icon name="hero-arrow-right-micro" class="size-4" />
            </.link>
          </div>
        <% else %>
          <section
            id="landing"
            class="flex flex-wrap items-center justify-between gap-4 border-b border-base-300 px-4 py-5 sm:px-6"
          >
            <div class="max-w-2xl space-y-1">
              <p class="text-xs font-semibold uppercase tracking-[0.2em] text-primary">
                Shared photos, on the map
              </p>
              <h1 class="text-2xl font-extrabold tracking-tight sm:text-3xl">
                Every photo, from everyone on the trip, in one album.
              </h1>
              <p class="text-sm text-base-content/70">
                CartoSnap gathers photos from you and your friends into shared trip albums, and puts each one back where it was taken. Here are some trips people have made public.
              </p>
            </div>
            <div class="flex gap-2">
              <.link
                :if={CartosnapApp.Accounts.registration_open?() and NeiamAuth.local_login_enabled?()}
                navigate={~p"/users/register"}
                class="btn btn-primary"
                id="landing-register"
              >
                Get started <.icon name="hero-arrow-right-micro" class="size-4" />
              </.link>
              <.link navigate={~p"/users/log-in"} class="btn btn-ghost" id="landing-log-in">
                Log in
              </.link>
            </div>
          </section>
        <% end %>

        <div class="grid min-h-0 flex-1 grid-rows-[minmax(0,1fr)_auto] lg:grid-cols-[minmax(0,1fr)_22rem] lg:grid-rows-1">
          <div class="relative min-h-[60dvh] lg:min-h-0">
            <.photo_map
              id="public-map"
              points={@points}
              class="h-full"
              rounded={false}
              empty_label="No public trips yet"
              label="Map of public trips"
            />
          </div>

          <aside class="flex min-h-0 flex-col border-t border-base-300 bg-base-100 lg:border-l lg:border-t-0">
            <div
              id="public-trips"
              phx-update="stream"
              class="grid min-h-0 flex-1 auto-rows-min gap-3 overflow-y-auto p-3 sm:grid-cols-2 lg:grid-cols-1"
            >
              <div
                id="public-trips-empty"
                class="col-span-full hidden py-10 text-center text-sm text-base-content/60 only:block"
              >
                No public trips yet.
                <span :if={signed_in?(@current_scope)}>
                  Make one of yours public from its page.
                </span>
              </div>
              <.link
                :for={{dom_id, entry} <- @streams.trips}
                id={dom_id}
                navigate={~p"/t/#{entry.trip.id}"}
                class="group flex gap-3 rounded-box border border-base-300 bg-base-100 p-2 transition hover:-translate-y-0.5 hover:shadow-lg"
              >
                <div class="size-20 shrink-0 overflow-hidden rounded-field bg-base-300">
                  <img
                    :if={entry.cover}
                    src={Storage.public_url(entry.cover)}
                    alt=""
                    loading="lazy"
                    class="h-full w-full object-cover transition duration-500 group-hover:scale-105"
                  />
                </div>
                <div class="min-w-0 py-0.5">
                  <p class="truncate font-semibold">{entry.trip.name}</p>
                  <p class="truncate text-xs text-base-content/60">
                    {TripLive.Index.date_range(entry.trip)}
                  </p>
                  <p class="mt-1 text-xs text-base-content/50">
                    {entry.count} {if entry.count == 1, do: "photo", else: "photos"} · by {entry.owner}
                  </p>
                </div>
              </.link>
            </div>
          </aside>
        </div>
      </div>
    </Layouts.app>
    """
  end

  @impl true
  def handle_info(:public_trips_changed, socket), do: {:noreply, load_trips(socket)}

  defp load_trips(socket) do
    entries = Trips.list_public_trips()

    # Each photo once, opening into the (first) public trip it's in.
    points =
      entries
      |> Enum.flat_map(fn %{trip: trip, photos: photos} -> Enum.map(photos, &{trip, &1}) end)
      |> Enum.uniq_by(fn {_trip, photo} -> photo.id end)
      |> Enum.flat_map(fn {trip, photo} ->
        photo
        |> List.wrap()
        |> PhotoMap.points()
        |> Enum.map(&%{&1 | href: "/t/#{trip.id}?photo=#{photo.id}"})
      end)

    socket
    |> assign(:points, points)
    |> stream(:trips, Enum.map(entries, &Map.put(&1, :id, &1.trip.id)), reset: true)
  end

  defp signed_in?(%{user: %{}}), do: true
  defp signed_in?(_scope), do: false
end