9.0 KiB · text History 44bc4f7
defmodule CartosnapAppWeb.Layouts do
  @moduledoc """
  The page frame: root HTML (`layouts/root.html.heex`), and `app/1`, the nav
  bar and content area every LiveView renders inside.
  """
  use CartosnapAppWeb, :html

  embed_templates "layouts/*"

  @doc """
  Renders the app layout.

      <Layouts.app flash={@flash} current_scope={@current_scope} active={:photos}>
        <h1>Content</h1>
      </Layouts.app>
  """
  attr :flash, :map, required: true, doc: "the map of flash messages"

  attr :current_scope, :map,
    default: nil,
    doc: "the current [scope](https://hexdocs.pm/phoenix/scopes.html)"

  attr :active, :atom,
    default: nil,
    values: [nil, :photos, :map, :trips, :upload],
    doc: "the nav section this page belongs to"

  attr :wide, :boolean,
    default: false,
    doc: "when true, the inner block fills the width and manages its own padding"

  slot :inner_block, required: true

  def app(assigns) do
    ~H"""
    <header class="navbar sticky top-0 z-40 gap-2 border-b border-base-300/60 bg-base-100/80 px-4 backdrop-blur-md sm:px-6 lg:px-8">
      <div class="flex-1">
        <.link
          navigate={~p"/"}
          class="group flex w-fit items-center gap-2"
          id="nav-home"
        >
          <span class="grid size-8 place-items-center rounded-lg bg-primary text-primary-content shadow-sm transition-transform group-hover:-rotate-6">
            <.icon name="hero-map-pin-solid" class="size-4" />
          </span>
          <span class="text-base font-bold tracking-tight">CartoSnap</span>
        </.link>
      </div>
      <nav class="flex-none" aria-label="Main">
        <ul class="flex flex-row items-center gap-1">
          <%= if @current_scope && @current_scope.user do %>
            <li>
              <.nav_link
                id="nav-photos"
                navigate={~p"/photos"}
                icon="hero-photo-micro"
                active={@active == :photos}
              >
                Photos
              </.nav_link>
            </li>
            <li>
              <.nav_link
                id="nav-map"
                navigate={~p"/map"}
                icon="hero-map-micro"
                active={@active == :map}
              >
                Map
              </.nav_link>
            </li>
            <li>
              <.nav_link
                id="nav-trips"
                navigate={~p"/trips"}
                icon="hero-paper-airplane-micro"
                active={@active == :trips}
              >
                Trips
              </.nav_link>
            </li>
            <li>
              <.link
                navigate={~p"/upload"}
                id="nav-upload"
                class={[
                  "btn btn-sm gap-1.5",
                  if(@active == :upload, do: "btn-primary", else: "btn-primary btn-soft")
                ]}
              >
                <.icon name="hero-arrow-up-tray-micro" class="size-4" />
                <span class="hidden sm:inline">Upload</span>
              </.link>
            </li>
            <li>
              <.theme_toggle />
            </li>
            <li>
              <details class="dropdown dropdown-end" id="nav-account">
                <summary class="btn btn-ghost btn-sm">
                  <.icon name="hero-user-circle-micro" class="size-4" />
                  <span class="hidden max-w-[12ch] truncate md:inline">
                    {@current_scope.user.email}
                  </span>
                </summary>
                <ul class="menu dropdown-content z-50 mt-1 w-52 rounded-box border border-base-300 bg-base-200 p-1 shadow-lg">
                  <li class="menu-title truncate font-mono text-xs">
                    {@current_scope.user.email}
                  </li>
                  <li>
                    <.link navigate={~p"/users/invites"} id="nav-invites">
                      <.icon name="hero-envelope-micro" class="size-4" /> Invites
                    </.link>
                  </li>
                  <li>
                    <.link navigate={~p"/users/settings"}>
                      <.icon name="hero-cog-6-tooth-micro" class="size-4" /> Settings
                    </.link>
                  </li>
                  <li>
                    <.link href={~p"/users/log-out"} method="delete" id="nav-log-out">
                      <.icon name="hero-arrow-right-on-rectangle-micro" class="size-4" /> Log out
                    </.link>
                  </li>
                </ul>
              </details>
            </li>
          <% else %>
            <li>
              <.theme_toggle />
            </li>
            <li>
              <.link navigate={~p"/users/log-in"} class="btn btn-ghost btn-sm" id="nav-log-in">
                Log in
              </.link>
            </li>
            <li>
              <.link navigate={~p"/users/register"} class="btn btn-primary btn-sm" id="nav-register">
                Register
              </.link>
            </li>
          <% end %>
        </ul>
      </nav>
    </header>

    <%= if @wide do %>
      <main class="w-full">
        {render_slot(@inner_block)}
      </main>
    <% else %>
      <main class="px-4 py-10 sm:px-6 lg:px-8">
        <div class="mx-auto max-w-6xl space-y-6">
          {render_slot(@inner_block)}
        </div>
      </main>
    <% end %>

    <.flash_group flash={@flash} />
    """
  end

  attr :navigate, :string, required: true
  attr :icon, :string, required: true
  attr :active, :boolean, default: false
  attr :id, :string, required: true
  slot :inner_block, required: true

  defp nav_link(assigns) do
    ~H"""
    <.link
      navigate={@navigate}
      id={@id}
      aria-current={@active && "page"}
      class={[
        "btn btn-ghost btn-sm gap-1.5 transition-colors",
        @active && "bg-base-200 text-primary"
      ]}
    >
      <.icon name={@icon} class="size-4" />
      <span class="hidden sm:inline">{render_slot(@inner_block)}</span>
    </.link>
    """
  end

  @doc """
  Shows the flash group with standard titles and content.

  ## Examples

      <.flash_group flash={@flash} />
  """
  attr :flash, :map, required: true, doc: "the map of flash messages"
  attr :id, :string, default: "flash-group", doc: "the optional id of flash container"

  def flash_group(assigns) do
    ~H"""
    <div id={@id} aria-live="polite">
      <.flash kind={:info} flash={@flash} />
      <.flash kind={:error} flash={@flash} />

      <.flash
        id="client-error"
        kind={:error}
        title={gettext("We can't find the internet")}
        phx-disconnected={
          show(".phx-client-error #client-error")
          |> JS.remove_attribute("hidden", to: ".phx-client-error #client-error")
        }
        phx-connected={hide("#client-error") |> JS.set_attribute({"hidden", ""})}
        hidden
      >
        {gettext("Attempting to reconnect")}
        <.icon name="hero-arrow-path" class="ml-1 size-3 motion-safe:animate-spin" />
      </.flash>

      <.flash
        id="server-error"
        kind={:error}
        title={gettext("Something went wrong!")}
        phx-disconnected={
          show(".phx-server-error #server-error")
          |> JS.remove_attribute("hidden", to: ".phx-server-error #server-error")
        }
        phx-connected={hide("#server-error") |> JS.set_attribute({"hidden", ""})}
        hidden
      >
        {gettext("Attempting to reconnect")}
        <.icon name="hero-arrow-path" class="ml-1 size-3 motion-safe:animate-spin" />
      </.flash>
    </div>
    """
  end

  @doc """
  Theme picker listing every theme in `CartosnapApp.Themes` (defined in
  `app.css`). The choice is kept in localStorage by the script in
  `root.html.heex`, which also handles the `phx:set-theme` event sent here.
  """
  def theme_toggle(assigns) do
    assigns = assign_new(assigns, :themes, fn -> CartosnapApp.Themes.all() end)

    ~H"""
    <div class="dropdown dropdown-end" id="theme-picker">
      <div tabindex="0" role="button" class="btn btn-ghost btn-sm gap-2" aria-label="Choose theme">
        <.icon name="hero-swatch-micro" class="size-4" />
        <span class="hidden lg:inline">Theme</span>
      </div>
      <ul
        tabindex="0"
        class="menu dropdown-content z-50 mt-1 w-44 rounded-box border border-base-300 bg-base-200 p-1 shadow-lg"
      >
        <li>
          <button
            type="button"
            phx-click={JS.dispatch("phx:set-theme")}
            data-phx-theme="system"
            class="flex items-center gap-2"
          >
            <.icon name="hero-computer-desktop-micro" class="size-4" /> System
          </button>
        </li>
        <li :for={theme <- @themes}>
          <button
            type="button"
            phx-click={JS.dispatch("phx:set-theme")}
            data-phx-theme={theme}
            class="flex items-center justify-between"
          >
            <span class="capitalize">{theme}</span>
            <span
              class="size-3 rounded-full border border-base-content/20"
              data-theme={theme}
              style="background: var(--color-primary)"
            />
          </button>
        </li>
      </ul>
    </div>
    """
  end
end