defmodule VigilWeb.StatusPageLive.Index do
@moduledoc """
An organization's status pages (`Vigil.Uptime`): each one's name, where
it lives (`/status/:org/:slug`), whether anyone may see it or only
members, and which monitors it shows. Someone with `org:write` (managers
and owners) can make, edit, publish and delete them; everyone else sees
the list and the links.
"""
use VigilWeb, :live_view
alias Vigil.Accounts.Scope
alias Vigil.Uptime
alias Vigil.Uptime.StatusPage
@impl true
def mount(_params, _session, socket) do
scope = socket.assigns.current_scope
{:ok,
socket
|> assign(:page_title, "#{scope.organization.name} · Status pages")
|> assign(:can_manage?, Scope.can?(scope, "org:write"))
|> assign(:monitors, Uptime.list_monitors(scope))
|> assign(:editing, nil)
|> assign(:form, nil)
|> assign(:selected, MapSet.new())
|> stream_configure(:pages, dom_id: &"status-page-#{&1.id}")
|> load()}
end
defp load(socket) do
pages = Uptime.list_status_pages_with_monitors(socket.assigns.current_scope.organization.id)
socket
|> assign(:count, length(pages))
|> stream(:pages, pages, reset: true)
end
defp form_for(page, params, action \\ nil),
do: page |> Uptime.change_status_page(params) |> to_form(action: action)
defp monitor_ids(params),
do: params |> Map.get("monitor_ids", []) |> List.wrap() |> Enum.reject(&(&1 == ""))
defp base(%StatusPage{} = page), do: page
defp base(_), do: %StatusPage{}
defp forbidden(socket),
do: put_flash(socket, :error, "Only managers and owners can change status pages.")
@impl true
def handle_event("new", _params, socket) do
{:noreply,
assign(socket,
editing: :new,
form: form_for(%StatusPage{}, %{}),
selected: MapSet.new()
)}
end
def handle_event("edit", %{"id" => id}, socket) do
case Uptime.get_org_status_page(socket.assigns.current_scope.organization.id, id) do
nil ->
{:noreply, put_flash(socket, :error, "That status page is gone.")}
page ->
{:noreply,
assign(socket,
editing: page,
form: form_for(page, %{}),
selected: MapSet.new(page.monitors, & &1.id)
)}
end
end
def handle_event("cancel", _params, socket),
do: {:noreply, assign(socket, editing: nil, form: nil)}
def handle_event("validate", %{"status_page" => attrs} = params, socket) do
{:noreply,
assign(socket,
form: form_for(base(socket.assigns.editing), attrs, :validate),
selected: MapSet.new(monitor_ids(params))
)}
end
def handle_event("save", %{"status_page" => attrs} = params, socket) do
scope = socket.assigns.current_scope
ids = monitor_ids(params)
result =
case socket.assigns.editing do
%StatusPage{} = page -> Uptime.update_status_page(scope, page, attrs, ids)
_ -> Uptime.create_status_page(scope, attrs, ids)
end
case result do
{:ok, page} ->
{:noreply,
socket
|> assign(editing: nil, form: nil)
|> put_flash(:info, "Status page #{page.name} saved.")
|> load()}
{:error, %Ecto.Changeset{} = cs} ->
{:noreply, assign(socket, form: to_form(cs, action: :insert), selected: MapSet.new(ids))}
{:error, _} ->
{:noreply, forbidden(socket)}
end
end
def handle_event("toggle_public", %{"id" => id}, socket) do
scope = socket.assigns.current_scope
with %StatusPage{} = page <- Uptime.get_org_status_page(scope.organization.id, id),
{:ok, page} <-
Uptime.update_status_page(
scope,
page,
%{is_public: !page.is_public},
Enum.map(page.monitors, & &1.id)
) do
{:noreply,
socket
|> put_flash(
:info,
if(page.is_public,
do: "#{page.name} is public now.",
else: "#{page.name} is for members only now."
)
)
|> load()}
else
nil -> {:noreply, put_flash(socket, :error, "That status page is gone.")}
{:error, _} -> {:noreply, forbidden(socket)}
end
end
def handle_event("delete", %{"id" => id}, socket) do
scope = socket.assigns.current_scope
with %StatusPage{} = page <- Uptime.get_org_status_page(scope.organization.id, id),
{:ok, page} <- Uptime.delete_status_page(scope, page) do
{:noreply,
socket
|> put_flash(:info, "Status page #{page.name} deleted.")
|> stream_delete(:pages, page)
|> assign(:count, socket.assigns.count - 1)}
else
nil -> {:noreply, put_flash(socket, :error, "That status page is gone.")}
{:error, _} -> {:noreply, forbidden(socket)}
end
end
@impl true
def render(assigns) do
~H"""
<Layouts.app flash={@flash} current_scope={@current_scope} active={:uptime}>
<div class="mx-auto max-w-5xl space-y-6">
<.header>
Status pages
<:subtitle>
Show chosen monitors' state to your users, or to your members only.
</:subtitle>
<:actions>
<div class="flex flex-wrap items-center gap-1">
<.link
navigate={~p"/orgs/#{@current_scope.organization.slug}/uptime"}
class="btn btn-ghost btn-sm"
id="status-pages-back"
>
<.icon name="hero-arrow-left-micro" class="size-4" /> Uptime
</.link>
<button
:if={@can_manage? and is_nil(@editing)}
type="button"
id="new-status-page"
class="btn btn-primary btn-sm"
phx-click="new"
>
<.icon name="hero-plus-micro" class="size-4" /> New status page
</button>
</div>
</:actions>
</.header>
<.form
:if={@can_manage? and @form}
for={@form}
id="status-page-form"
phx-change="validate"
phx-submit="save"
class="space-y-1 rounded-box border border-base-300 bg-base-200/60 p-4"
>
<h2 class="mb-2 text-sm font-semibold">
{if @editing == :new, do: "New status page", else: "Edit #{@editing.name}"}
</h2>
<div class="grid gap-x-3 sm:grid-cols-2">
<.input field={@form[:name]} label="Name" placeholder="Acme status" required />
<.input
field={@form[:slug]}
label="Slug (in its address)"
placeholder="made from the name"
autocomplete="off"
/>
</div>
<.input
field={@form[:is_public]}
type="checkbox"
label="Public: anyone with the link can see it (otherwise members only)"
/>
<fieldset class="mt-2" id="status-page-monitors">
<legend class="mb-1 text-sm font-medium">Monitors to show</legend>
<input type="hidden" name="monitor_ids[]" value="" />
<p :if={@monitors == []} class="text-sm text-base-content/60">
No uptime monitors yet: make some first.
</p>
<div class="grid gap-1 sm:grid-cols-2">
<label
:for={m <- @monitors}
class="flex cursor-pointer items-center gap-2 rounded-box px-2 py-1.5 text-sm transition hover:bg-base-300/50"
>
<input
type="checkbox"
name="monitor_ids[]"
value={m.id}
id={"status-page-monitor-#{m.id}"}
checked={MapSet.member?(@selected, m.id)}
class="checkbox checkbox-sm"
/>
<span class="truncate">{m.name}</span>
<span class="ml-auto truncate text-xs text-base-content/50">{m.project.name}</span>
</label>
</div>
</fieldset>
<div class="flex gap-2 pt-3">
<.button class="btn btn-primary btn-sm" phx-disable-with="Saving…" id="save-status-page">
Save
</.button>
<button
type="button"
class="btn btn-ghost btn-sm"
phx-click="cancel"
id="cancel-status-page"
>
Cancel
</button>
</div>
</.form>
<ul id="status-pages" phx-update="stream" class="space-y-2">
<li
id="status-pages-empty"
class="hidden rounded-box border border-dashed border-base-300 bg-base-100 p-8 text-center text-sm text-base-content/60 only:block"
>
<.icon name="hero-signal" class="mx-auto mb-2 block size-8 text-primary/50" />
No status pages yet.
<span :if={@can_manage?}>
Make one with <em>New status page</em>, pick its monitors, and share its link.
</span>
</li>
<li
:for={{dom_id, page} <- @streams.pages}
id={dom_id}
class="flex flex-wrap items-center gap-x-4 gap-y-2 rounded-box border border-base-300 bg-base-100 px-4 py-3 transition hover:border-base-content/20"
>
<div class="min-w-0 flex-1">
<p class="flex flex-wrap items-center gap-2">
<span class="font-semibold">{page.name}</span>
<%= if page.is_public do %>
<span class="badge badge-info badge-soft badge-sm gap-1 status-page-public">
<.icon name="hero-globe-alt-micro" class="size-3.5" /> Public
</span>
<% else %>
<span class="badge badge-ghost badge-sm gap-1 status-page-private">
<.icon name="hero-lock-closed-micro" class="size-3.5" /> Members only
</span>
<% end %>
</p>
<p class="mt-0.5 truncate text-xs text-base-content/60">
<.link
navigate={~p"/status/#{@current_scope.organization.slug}/#{page.slug}"}
class="link link-hover font-mono"
id={"status-page-link-#{page.id}"}
>
/status/{@current_scope.organization.slug}/{page.slug}
</.link>
· {length(page.monitors)} {if length(page.monitors) == 1,
do: "monitor",
else: "monitors"}
</p>
</div>
<div :if={@can_manage?} class="flex flex-wrap items-center gap-1">
<button
type="button"
id={"edit-status-page-#{page.id}"}
class="btn btn-ghost btn-sm"
phx-click="edit"
phx-value-id={page.id}
>
<.icon name="hero-pencil-square-micro" class="size-4" /> Edit
</button>
<button
type="button"
id={"toggle-status-page-#{page.id}"}
class="btn btn-ghost btn-sm"
phx-click="toggle_public"
phx-value-id={page.id}
>
<%= if page.is_public do %>
<.icon name="hero-lock-closed-micro" class="size-4" /> Make private
<% else %>
<.icon name="hero-globe-alt-micro" class="size-4" /> Make public
<% end %>
</button>
<button
type="button"
id={"delete-status-page-#{page.id}"}
class="btn btn-ghost btn-sm text-error"
phx-click="delete"
phx-value-id={page.id}
data-confirm={"Delete the status page #{page.name}?"}
>
<.icon name="hero-trash-micro" class="size-4" /> Delete
</button>
</div>
</li>
</ul>
</div>
</Layouts.app>
"""
end
end
neiam
/vigil
public · Issues · Pulls · Labels · Forks · Compare · Actions queued · Badges · Packages
⭐
Log in to mark this repository.
11.7 KiB · text
History
573fef1