4.7 KiB · text History e569b6d
defmodule CartosnapAppWeb.TripLive.Index do
  @moduledoc """
  Your trips (the ones you made, and the ones you were invited to), each
  with a cover from its album.
  """
  use CartosnapAppWeb, :live_view

  alias CartosnapApp.Trips
  alias CartosnapApp.Storage

  @impl true
  def mount(_params, _session, socket) do
    scope = socket.assigns.current_scope
    trips = Trips.list_trips(scope)

    {:ok,
     socket
     |> assign(:page_title, "Trips")
     |> assign(:summaries, Trips.trip_summaries(trips))
     |> assign(:owners, owner_names(trips, scope))
     |> assign(:invite_count, scope |> Trips.list_my_invites() |> length())
     |> assign(:count, length(trips))
     |> stream(:trips, trips)}
  end

  @impl true
  def render(assigns) do
    ~H"""
    <Layouts.app flash={@flash} current_scope={@current_scope} active={:trips}>
      <.header>
        Trips
        <:subtitle>File photos under a trip to step through them together.</:subtitle>
        <:actions>
          <.link navigate={~p"/trips/new"} class="btn btn-primary btn-sm" id="new-trip">
            <.icon name="hero-plus-micro" class="size-4" /> New trip
          </.link>
        </:actions>
      </.header>

      <.link
        :if={@invite_count > 0}
        navigate={~p"/users/invites"}
        id="invites-waiting"
        class="alert alert-info alert-soft flex items-center justify-between transition hover:brightness-95"
      >
        <span class="flex items-center gap-2">
          <.icon name="hero-envelope" class="size-5" />
          {if @invite_count == 1,
            do: "You've been invited to a trip.",
            else: "You've been invited to #{@invite_count} trips."}
        </span>
        <span class="font-semibold">See invites →</span>
      </.link>

      <div
        id="trips"
        phx-update="stream"
        class="grid gap-4 sm:grid-cols-2 lg:grid-cols-3"
      >
        <div
          id="trips-empty"
          class="col-span-full hidden rounded-box border border-dashed border-base-300 py-16 text-center only:block"
        >
          <.icon name="hero-paper-airplane" class="mx-auto size-10 text-base-content/30" />
          <p class="mt-3 font-medium">No trips yet</p>
          <.link navigate={~p"/trips/new"} class="btn btn-primary btn-sm mt-4">Make your first</.link>
        </div>
        <.link
          :for={{dom_id, trip} <- @streams.trips}
          id={dom_id}
          navigate={~p"/trips/#{trip}"}
          class="card group overflow-hidden border border-base-300 bg-base-100 shadow-sm transition duration-200 hover:-translate-y-1 hover:shadow-xl"
        >
          <figure class="aspect-[16/9] bg-base-300">
            <%= case @summaries[trip.id] do %>
              <% %{cover: cover} when is_binary(cover) -> %>
                <img
                  src={Storage.public_url(cover)}
                  alt=""
                  loading="lazy"
                  class="h-full w-full object-cover transition duration-500 group-hover:scale-105"
                />
              <% _ -> %>
                <div class="grid h-full w-full place-items-center text-base-content/30">
                  <.icon name="hero-photo" class="size-10" />
                </div>
            <% end %>
          </figure>
          <div class="card-body gap-1 p-4">
            <div class="flex items-start justify-between gap-2">
              <h2 class="card-title text-base">{trip.name}</h2>
              <span
                :if={@owners[trip.id]}
                class="badge badge-soft badge-info badge-sm shrink-0"
                title="Someone else's trip you're in"
              >
                with {@owners[trip.id]}
              </span>
            </div>
            <p class="text-sm text-base-content/60">{date_range(trip)}</p>
            <p class="text-xs text-base-content/50">
              {photo_count(@summaries[trip.id])}
            </p>
          </div>
        </.link>
      </div>
    </Layouts.app>
    """
  end

  # For trips someone else owns: whose they are.
  defp owner_names(trips, scope) do
    theirs = Enum.reject(trips, &(&1.user == scope.user.id))
    names = theirs |> Enum.map(& &1.user) |> CartosnapApp.Accounts.display_names()
    Map.new(theirs, &{&1.id, names[&1.user]})
  end

  defp photo_count(%{count: 1}), do: "1 photo"
  defp photo_count(%{count: n}), do: "#{n} photos"
  defp photo_count(_), do: "No photos yet"

  @doc false
  def date_range(%{date_start: start, date_end: finish}) do
    cond do
      start == finish ->
        Calendar.strftime(start, "%b %-d, %Y")

      start.year == finish.year ->
        "#{Calendar.strftime(start, "%b %-d")} – #{Calendar.strftime(finish, "%b %-d, %Y")}"

      true ->
        "#{Calendar.strftime(start, "%b %-d, %Y")} – #{Calendar.strftime(finish, "%b %-d, %Y")}"
    end
  end
end