4.0 KiB · text History 573fef1
defmodule VigilWeb.ShareLive.Issue do
  @moduledoc """
  An issue its project's members made public (`Vigil.Issues.set_public/3`),
  for anyone with the link, signed in or not. Read-only, and deliberately
  thin: the title, culprit, level, counts and when it was seen, and of the
  latest event only the exception, stack trace and message
  (`VigilWeb.EventComponents.event_body/1` with `public`). The request,
  user, breadcrumbs, contexts, extra, tags and frame variables stay
  private: they may hold personal data. An issue that isn't public (or
  doesn't exist) answers the same "not found".
  """
  use VigilWeb, :live_view

  import VigilWeb.EventComponents

  alias Vigil.Issues

  @impl true
  def mount(%{"id" => id}, _session, socket) do
    case Issues.get_public_issue(id) do
      nil ->
        {:ok, socket |> assign(:issue, nil) |> assign(:page_title, "Not found")}

      issue ->
        {:ok,
         socket
         |> assign(:issue, issue)
         |> assign(:event, Issues.latest_event(issue))
         |> assign(:page_title, issue.title)}
    end
  end

  @impl true
  def render(assigns) do
    ~H"""
    <Layouts.app flash={@flash} current_scope={@current_scope}>
      <%= if @issue do %>
        <header id="share-header" class="space-y-4">
          <div class="flex flex-wrap items-center gap-2 text-sm">
            <span class="rounded-md bg-base-200 px-2 py-0.5 font-mono text-xs font-semibold">
              {String.upcase(@issue.project.slug)}-{@issue.short_id}
            </span>
            <.level_badge level={@issue.level} />
            <span class="badge badge-ghost badge-sm">{@issue.status}</span>
            <span class="ml-auto inline-flex items-center gap-1 text-xs text-base-content/50">
              <.icon name="hero-globe-alt-micro" class="size-3.5" /> Shared issue
            </span>
          </div>
          <div class="flex gap-3">
            <span class={["mt-1.5 w-1 shrink-0 self-stretch rounded-full", level_dot(@issue.level)]} />
            <div class="min-w-0">
              <h1 id="share-title" class="break-words text-xl font-semibold sm:text-2xl">
                {@issue.title}
              </h1>
              <p
                :if={@issue.culprit not in [nil, ""]}
                class="mt-1 break-all font-mono text-sm text-base-content/60"
              >
                {@issue.culprit}
              </p>
            </div>
          </div>
          <dl id="share-stats" class="grid grid-cols-3 gap-3 sm:max-w-xl">
            <div class="rounded-box border border-base-300 bg-base-100 px-3 py-2">
              <dt class="text-xs text-base-content/60">Events</dt>
              <dd class="text-lg font-semibold tabular-nums">{@issue.count}</dd>
            </div>
            <div class="rounded-box border border-base-300 bg-base-100 px-3 py-2">
              <dt class="text-xs text-base-content/60">First seen</dt>
              <dd class="text-lg font-semibold"><.time_ago at={@issue.first_seen} /></dd>
            </div>
            <div class="rounded-box border border-base-300 bg-base-100 px-3 py-2">
              <dt class="text-xs text-base-content/60">Last seen</dt>
              <dd class="text-lg font-semibold"><.time_ago at={@issue.last_seen} /></dd>
            </div>
          </dl>
        </header>

        <.event_body :if={@event} id="share-event" event={@event} public />
        <p :if={!@event} id="share-no-event" class="text-sm text-base-content/60">
          This issue's events have aged out.
        </p>
      <% else %>
        <div
          id="share-not-found"
          class="mx-auto max-w-md rounded-box border border-dashed border-base-300 p-10 text-center"
        >
          <.icon name="hero-eye-slash" class="mx-auto size-8 text-base-content/40" />
          <h1 class="mt-3 font-semibold">Not found, or not public</h1>
          <p class="mt-1 text-sm text-base-content/60">
            This issue doesn't exist, or its project no longer shares it.
          </p>
        </div>
      <% end %>
    </Layouts.app>
    """
  end
end