11.8 KiB · text History 11accb3
defmodule VigilWeb.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 VigilWeb, :html

  embed_templates "layouts/*"

  @doc """
  Renders the app layout.

      <Layouts.app flash={@flash} current_scope={@current_scope} active={:home}>
        <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,
      :home,
      :invites,
      :settings,
      :orgs,
      :projects,
      :uptime,
      :org_settings,
      :tokens,
      :shaders
    ],
    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-eye-solid" class="size-4" />
          </span>
          <span class="text-base font-bold tracking-tight">Vigil</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 %>
            <%= if @current_scope.organization do %>
              <li>
                <.nav_link
                  id="nav-projects"
                  navigate={~p"/orgs/#{@current_scope.organization.slug}"}
                  icon="hero-squares-2x2-micro"
                  active={@active == :projects}
                >
                  Projects
                </.nav_link>
              </li>
              <li>
                <.nav_link
                  id="org-uptime"
                  navigate={~p"/orgs/#{@current_scope.organization.slug}/uptime"}
                  icon="hero-signal-micro"
                  active={@active == :uptime}
                >
                  Uptime
                </.nav_link>
              </li>
              <li>
                <%!-- Which organization this is: its settings, and the way to the others. --%>
                <details class="dropdown dropdown-end" id="nav-org">
                  <summary class={[
                    "btn btn-ghost btn-sm gap-1.5",
                    @active == :org_settings && "bg-base-200 text-primary"
                  ]}>
                    <span class="grid size-5 place-items-center rounded bg-primary/15 text-[0.65rem] font-bold text-primary">
                      {@current_scope.organization.name |> String.first() |> String.upcase()}
                    </span>
                    <span class="hidden max-w-[14ch] truncate sm:inline">
                      {@current_scope.organization.name}
                    </span>
                    <.icon name="hero-chevron-down-micro" class="size-3.5 opacity-60" />
                  </summary>
                  <ul class="menu dropdown-content z-50 mt-1 w-56 rounded-box border border-base-300 bg-base-200 p-1 shadow-lg">
                    <li class="menu-title truncate">{@current_scope.organization.name}</li>
                    <li>
                      <.link
                        navigate={~p"/orgs/#{@current_scope.organization.slug}/settings"}
                        id="nav-org-settings"
                      >
                        <.icon name="hero-cog-6-tooth-micro" class="size-4" /> Settings
                      </.link>
                    </li>
                    <li>
                      <.link
                        navigate={~p"/orgs/#{@current_scope.organization.slug}/settings/members"}
                        id="nav-org-members"
                      >
                        <.icon name="hero-users-micro" class="size-4" /> Members
                      </.link>
                    </li>
                    <li>
                      <.link navigate={~p"/orgs"} id="nav-org-switch">
                        <.icon name="hero-arrows-right-left-micro" class="size-4" />
                        Switch organization
                      </.link>
                    </li>
                  </ul>
                </details>
              </li>
            <% else %>
              <li>
                <.nav_link
                  id="nav-orgs"
                  navigate={~p"/orgs"}
                  icon="hero-building-office-2-micro"
                  active={@active == :orgs}
                >
                  Organizations
                </.nav_link>
              </li>
            <% end %>
            <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/tokens"} id="nav-tokens">
                      <.icon name="hero-key-micro" class="size-4" /> API tokens
                    </.link>
                  </li>
                  <li>
                    <.link navigate={~p"/users/settings"} id="nav-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>
            <%!-- Invite-only (the default): an invite link is the way to sign up. --%>
            <li :if={Vigil.Accounts.registration_open?() and NeiamAuth.local_login_enabled?()}>
              <.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 `Vigil.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.
  Below them, the way to the background shaders (/shaders), as on
  neiam-org.
  """
  def theme_toggle(assigns) do
    assigns = assign_new(assigns, :themes, fn -> Vigil.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>
        <li class="menu-title pt-2">Background</li>
        <li>
          <.link navigate={~p"/shaders"} id="nav-shaders" class="flex items-center gap-2">
            <.icon name="hero-sparkles-micro" class="size-4" /> Shaders
          </.link>
        </li>
      </ul>
    </div>
    """
  end
end