5.5 KiB · text History e569b6d
defmodule CartosnapAppWeb.PhotoComponents do
  @moduledoc "Pieces of photo UI shared by the photo, map and trip pages."

  use CartosnapAppWeb, :html

  alias CartosnapApp.Photos.Photo
  alias CartosnapApp.Storage

  @doc """
  A photo at one of its sizes, over its blurhash. Until the image pipeline
  has made the variant (or while it loads), the blurhash shows through.
  """
  attr :id, :string, required: true
  attr :photo, Photo, required: true
  attr :variant, :atom, default: :thumb, values: [:thumb, :preview, :full, :original]
  attr :class, :any, default: nil
  attr :img_class, :any, default: "object-cover"
  attr :rest, :global

  def photo_image(assigns) do
    assigns = assign(assigns, :src, src(assigns.photo, assigns.variant))

    ~H"""
    <div class={["relative overflow-hidden bg-base-300", @class]} {@rest}>
      <canvas
        :if={@photo.blurhash}
        id={"#{@id}-hash"}
        phx-hook="Blurhash"
        data-hash={@photo.blurhash}
        width="32"
        height="32"
        class="blurhash-canvas absolute inset-0 h-full w-full"
        aria-hidden="true"
      ></canvas>
      <img
        :if={@src}
        src={@src}
        alt={Photo.title(@photo)}
        loading="lazy"
        decoding="async"
        class={["relative h-full w-full", @img_class]}
      />
      <div
        :if={is_nil(@src)}
        class="absolute inset-0 grid place-items-center text-base-content/50"
        title="Still processing"
      >
        <.icon name="hero-arrow-path" class="size-5 motion-safe:animate-spin" />
      </div>
    </div>
    """
  end

  @doc """
  A photo sphere or panorama, seen as one: a sphere in the 360° viewer
  (`assets/js/photo_sphere.js`) to look around in, a panorama at full
  height to pan across (`assets/js/panorama.js`). For an ordinary photo,
  use `photo_image/1`; `immersive?/1` says which.

  The id carries the photo's, so stepping to another photo mounts a fresh
  viewer rather than reusing this one.
  """
  attr :id, :string, required: true
  attr :photo, Photo, required: true
  attr :class, :any, default: nil

  def photo_stage(%{photo: %Photo{kind: "sphere"}} = assigns) do
    ~H"""
    <div
      id={"#{@id}-#{@photo.id}"}
      phx-hook="PhotoSphere"
      phx-update="ignore"
      data-src={src(@photo, :full)}
      data-preview={src(@photo, :preview)}
      data-pano={Jason.encode!(@photo.pano)}
      class={["photo-sphere relative overflow-hidden bg-neutral", @class]}
    >
    </div>
    """
  end

  def photo_stage(%{photo: %Photo{kind: "panorama"}} = assigns) do
    ~H"""
    <div class={["relative", @class]}>
      <div
        id={"#{@id}-#{@photo.id}"}
        phx-hook="Panorama"
        phx-update="ignore"
        class="h-full w-full cursor-grab overflow-x-auto overflow-y-hidden overscroll-x-contain [scrollbar-width:none]"
        aria-label={"Panorama: #{Photo.title(@photo)}. Drag or scroll sideways to look along it."}
      >
        <img
          src={src(@photo, :full)}
          alt={Photo.title(@photo)}
          draggable="false"
          class="block h-full w-auto max-w-none select-none"
        />
      </div>
      <span class="pointer-events-none absolute left-3 top-3 rounded-full bg-black/45 px-2 py-0.5 text-[11px] font-semibold text-white backdrop-blur">
        Panorama
      </span>
      <div class="pointer-events-none absolute inset-x-6 bottom-3 h-1 rounded-full bg-black/30">
        <div data-role="pano-progress" class="absolute inset-y-0 rounded-full bg-white/80"></div>
      </div>
    </div>
    """
  end

  @doc "Whether a photo is shown in `photo_stage/1` rather than flat: a sphere or panorama, processed."
  def immersive?(%Photo{} = photo),
    do: (Photo.sphere?(photo) or Photo.panorama?(photo)) and not is_nil(photo.url_full)

  def immersive?(_photo), do: false

  @doc "A small label on a tile saying a photo is a 360° sphere or a panorama."
  attr :photo, Photo, required: true
  attr :class, :any, default: nil

  def shape_badge(assigns) do
    ~H"""
    <span
      :if={@photo.kind in ["sphere", "panorama"]}
      class={[
        "pointer-events-none inline-flex items-center gap-0.5 rounded-full bg-black/55 px-1.5 py-0.5 text-[10px] font-semibold text-white backdrop-blur",
        @class
      ]}
      title={if @photo.kind == "sphere", do: "360° photo sphere", else: "Panorama"}
    >
      <.icon
        name={
          if @photo.kind == "sphere", do: "hero-globe-alt-micro", else: "hero-arrows-right-left-micro"
        }
        class="size-3"
      />
      {if @photo.kind == "sphere", do: "360°", else: "Pano"}
    </span>
    """
  end

  @doc "The star a promoted photo wears."
  attr :photo, Photo, required: true
  attr :class, :any, default: nil

  def promoted_badge(assigns) do
    ~H"""
    <span
      :if={@photo.promoted}
      class={[
        "inline-grid size-6 place-items-center rounded-full bg-warning text-warning-content shadow",
        @class
      ]}
      title="Promoted"
    >
      <.icon name="hero-star-solid" class="size-3.5" />
    </span>
    """
  end

  @doc "A photo's date, short."
  def photo_date(%Photo{date: nil}), do: "Undated"
  def photo_date(%Photo{date: date}), do: Calendar.strftime(date, "%b %-d, %Y")

  @doc "The URL for a photo variant, falling back to larger ones still to be made."
  def src(%Photo{} = photo, variant) do
    key =
      case variant do
        :thumb -> photo.url_thumb
        :preview -> photo.url_preview || photo.url_thumb
        :full -> photo.url_full || photo.url_original
        :original -> photo.url_original
      end

    Storage.public_url(key)
  end
end