neiam
/vigil
public · Issues · Pulls · Labels · Forks · Compare · Actions queued · Badges · Packages
⭐
Log in to mark this repository.
Merge branch 'worktree-agent-acb5e258cb09ecbeb'
a2cdf65 · gmorell · 2026-12-01 22:34
Files changed
added
lib/vigil_web/components/issue_list_components.ex
+241
−0
@@ -0,0 +1,241 @@
| 1 | +defmodule VigilWeb.IssueListComponents do | |
| 2 | + @moduledoc """ | |
| 3 | + The small pieces of the issue list (`VigilWeb.IssueLive.Index`): a | |
| 4 | + level marker, an hourly sparkline, relative times, compact counts, the | |
| 5 | + status badge, the assignee's initial, and the bulk-action toolbar. | |
| 6 | + """ | |
| 7 | + use VigilWeb, :html | |
| 8 | + | |
| 9 | + @doc "A coloured bar at a row's edge for the issue's level (`fatal` … `debug`)." | |
| 10 | + attr :level, :string, required: true | |
| 11 | + | |
| 12 | + def level_marker(assigns) do | |
| 13 | + ~H""" | |
| 14 | + <span | |
| 15 | + class={["block h-9 w-1 shrink-0 rounded-full", level_class(@level)]} | |
| 16 | + title={"Level: #{@level}"} | |
| 17 | + aria-label={"Level: #{@level}"} | |
| 18 | + role="img" | |
| 19 | + /> | |
| 20 | + """ | |
| 21 | + end | |
| 22 | + | |
| 23 | + defp level_class("fatal"), do: "bg-error ring-2 ring-error/30" | |
| 24 | + defp level_class("error"), do: "bg-error" | |
| 25 | + defp level_class("warning"), do: "bg-warning" | |
| 26 | + defp level_class("info"), do: "bg-info" | |
| 27 | + defp level_class(_), do: "bg-base-content/30" | |
| 28 | + | |
| 29 | + @doc """ | |
| 30 | + Events per hour as bars, oldest first, in inline SVG. Zero hours keep a | |
| 31 | + faint baseline so the shape of a quiet day still reads. | |
| 32 | + """ | |
| 33 | + attr :id, :string, required: true | |
| 34 | + attr :counts, :list, required: true | |
| 35 | + attr :class, :string, default: nil | |
| 36 | + | |
| 37 | + def sparkline(assigns) do | |
| 38 | + counts = assigns.counts | |
| 39 | + max = Enum.max(counts, fn -> 0 end) | |
| 40 | + | |
| 41 | + bars = | |
| 42 | + counts | |
| 43 | + |> Enum.with_index() | |
| 44 | + |> Enum.map(fn {n, i} -> | |
| 45 | + h = if n > 0, do: max(2, round(n / max * 22)), else: 1 | |
| 46 | + %{x: i * 4, y: 24 - h, h: h, zero?: n == 0} | |
| 47 | + end) | |
| 48 | + | |
| 49 | + assigns = | |
| 50 | + assign(assigns, | |
| 51 | + bars: bars, | |
| 52 | + total: Enum.sum(counts), | |
| 53 | + width: max(length(counts) * 4 - 1, 1) | |
| 54 | + ) | |
| 55 | + | |
| 56 | + ~H""" | |
| 57 | + <svg | |
| 58 | + id={@id} | |
| 59 | + viewBox={"0 0 #{@width} 24"} | |
| 60 | + width={@width} | |
| 61 | + height="24" | |
| 62 | + role="img" | |
| 63 | + aria-label={"#{@total} #{plural(@total, "event")} in the last #{length(@counts)} hours"} | |
| 64 | + class={["overflow-visible", @class]} | |
| 65 | + > | |
| 66 | + <title>{@total} {plural(@total, "event")} in the last {length(@counts)} hours</title> | |
| 67 | + <rect | |
| 68 | + :for={bar <- @bars} | |
| 69 | + x={bar.x} | |
| 70 | + y={bar.y} | |
| 71 | + width="3" | |
| 72 | + height={bar.h} | |
| 73 | + rx="0.5" | |
| 74 | + class={if(bar.zero?, do: "fill-base-content/15", else: "fill-primary/70")} | |
| 75 | + /> | |
| 76 | + </svg> | |
| 77 | + """ | |
| 78 | + end | |
| 79 | + | |
| 80 | + @doc "A `<time>` reading like `5m ago`, with the full timestamp on hover." | |
| 81 | + attr :at, :any, required: true | |
| 82 | + attr :id, :string, default: nil | |
| 83 | + attr :class, :string, default: nil | |
| 84 | + | |
| 85 | + def relative_time(assigns) do | |
| 86 | + ~H""" | |
| 87 | + <time | |
| 88 | + id={@id} | |
| 89 | + datetime={@at && DateTime.to_iso8601(@at)} | |
| 90 | + title={@at && Calendar.strftime(@at, "%Y-%m-%d %H:%M:%S UTC")} | |
| 91 | + class={@class} | |
| 92 | + > | |
| 93 | + {ago(@at)} | |
| 94 | + </time> | |
| 95 | + """ | |
| 96 | + end | |
| 97 | + | |
| 98 | + @doc "How long ago `at` was, briefly: `just now`, `12m ago`, `3h ago`, `5d ago`, or a date." | |
| 99 | + def ago(nil), do: "never" | |
| 100 | + | |
| 101 | + def ago(at, now \\ DateTime.utc_now()) do | |
| 102 | + diff = DateTime.diff(now, at) | |
| 103 | + | |
| 104 | + cond do | |
| 105 | + diff < 45 -> "just now" | |
| 106 | + diff < 3600 -> "#{max(div(diff, 60), 1)}m ago" | |
| 107 | + diff < 86_400 -> "#{div(diff, 3600)}h ago" | |
| 108 | + diff < 30 * 86_400 -> "#{div(diff, 86_400)}d ago" | |
| 109 | + true -> Calendar.strftime(at, "%b %-d, %Y") | |
| 110 | + end | |
| 111 | + end | |
| 112 | + | |
| 113 | + @doc "A count shortened for a narrow column: `999`, `1.2k`, `34k`, `1.5M`." | |
| 114 | + def compact(n) when n < 1000, do: Integer.to_string(n) | |
| 115 | + def compact(n) when n < 10_000, do: "#{Float.round(n / 1000, 1)}k" | |
| 116 | + def compact(n) when n < 1_000_000, do: "#{div(n, 1000)}k" | |
| 117 | + def compact(n), do: "#{Float.round(n / 1_000_000, 1)}M" | |
| 118 | + | |
| 119 | + @doc "`word` with an `s` unless `n` is one." | |
| 120 | + def plural(1, word), do: word | |
| 121 | + def plural(_, word), do: word <> "s" | |
| 122 | + | |
| 123 | + @doc "A badge for resolved and ignored issues; nothing for unresolved ones." | |
| 124 | + attr :status, :atom, required: true | |
| 125 | + | |
| 126 | + def status_badge(%{status: :unresolved} = assigns), do: ~H"" | |
| 127 | + | |
| 128 | + def status_badge(assigns) do | |
| 129 | + ~H""" | |
| 130 | + <span class={[ | |
| 131 | + "badge badge-sm gap-1", | |
| 132 | + if(@status == :resolved, do: "badge-success badge-soft", else: "badge-ghost") | |
| 133 | + ]}> | |
| 134 | + <.icon | |
| 135 | + name={if(@status == :resolved, do: "hero-check-micro", else: "hero-eye-slash-micro")} | |
| 136 | + class="size-3" | |
| 137 | + /> | |
| 138 | + {@status} | |
| 139 | + </span> | |
| 140 | + """ | |
| 141 | + end | |
| 142 | + | |
| 143 | + @doc "The assignee's initial in a circle, or a dashed placeholder." | |
| 144 | + attr :user, :any, required: true | |
| 145 | + | |
| 146 | + def assignee(%{user: nil} = assigns) do | |
| 147 | + ~H""" | |
| 148 | + <span | |
| 149 | + class="inline-flex size-6 items-center justify-center rounded-full border border-dashed border-base-content/25 text-base-content/30" | |
| 150 | + title="Unassigned" | |
| 151 | + > | |
| 152 | + <.icon name="hero-user-micro" class="size-3" /> | |
| 153 | + </span> | |
| 154 | + """ | |
| 155 | + end | |
| 156 | + | |
| 157 | + def assignee(assigns) do | |
| 158 | + ~H""" | |
| 159 | + <span | |
| 160 | + class="inline-flex size-6 items-center justify-center rounded-full bg-primary/15 text-xs font-semibold uppercase text-primary" | |
| 161 | + title={"Assigned to #{@user.email}"} | |
| 162 | + > | |
| 163 | + {String.first(@user.email)} | |
| 164 | + </span> | |
| 165 | + """ | |
| 166 | + end | |
| 167 | + | |
| 168 | + @doc """ | |
| 169 | + What can be done to the selected issues. Delete shows only to callers | |
| 170 | + holding `event:admin`, and asks first. | |
| 171 | + """ | |
| 172 | + attr :count, :integer, required: true | |
| 173 | + attr :can_delete, :boolean, default: false | |
| 174 | + | |
| 175 | + def bulk_actions(assigns) do | |
| 176 | + ~H""" | |
| 177 | + <div | |
| 178 | + id="bulk-actions" | |
| 179 | + class="flex flex-wrap items-center gap-1" | |
| 180 | + role="toolbar" | |
| 181 | + aria-label="Selected issues" | |
| 182 | + > | |
| 183 | + <span id="selected-count" class="mr-2 text-sm font-medium"> | |
| 184 | + {@count} selected | |
| 185 | + </span> | |
| 186 | + <.bulk_button id="bulk-resolve" action="resolve" icon="hero-check-micro"> | |
| 187 | + Resolve | |
| 188 | + </.bulk_button> | |
| 189 | + <.bulk_button id="bulk-ignore" action="ignore" icon="hero-eye-slash-micro"> | |
| 190 | + Ignore | |
| 191 | + </.bulk_button> | |
| 192 | + <.bulk_button id="bulk-unresolve" action="unresolve" icon="hero-arrow-uturn-left-micro"> | |
| 193 | + Unresolve | |
| 194 | + </.bulk_button> | |
| 195 | + <.bulk_button id="bulk-assign-me" action="assign_me" icon="hero-user-micro"> | |
| 196 | + Assign to me | |
| 197 | + </.bulk_button> | |
| 198 | + <button | |
| 199 | + :if={@can_delete} | |
| 200 | + type="button" | |
| 201 | + id="bulk-delete" | |
| 202 | + class="btn btn-ghost btn-xs text-error phx-click-loading:opacity-50" | |
| 203 | + phx-click="bulk" | |
| 204 | + phx-value-action="delete" | |
| 205 | + phx-disable-with="Deleting…" | |
| 206 | + data-confirm={"Delete #{@count} #{plural(@count, "issue")}? Their events go too."} | |
| 207 | + > | |
| 208 | + <.icon name="hero-trash-micro" class="size-3.5" /> Delete | |
| 209 | + </button> | |
| 210 | + <button | |
| 211 | + type="button" | |
| 212 | + id="bulk-clear" | |
| 213 | + class="btn btn-ghost btn-xs text-base-content/60" | |
| 214 | + phx-click="clear_selection" | |
| 215 | + > | |
| 216 | + Clear | |
| 217 | + </button> | |
| 218 | + </div> | |
| 219 | + """ | |
| 220 | + end | |
| 221 | + | |
| 222 | + attr :id, :string, required: true | |
| 223 | + attr :action, :string, required: true | |
| 224 | + attr :icon, :string, required: true | |
| 225 | + slot :inner_block, required: true | |
| 226 | + | |
| 227 | + defp bulk_button(assigns) do | |
| 228 | + ~H""" | |
| 229 | + <button | |
| 230 | + type="button" | |
| 231 | + id={@id} | |
| 232 | + class="btn btn-ghost btn-xs transition hover:bg-base-200 phx-click-loading:opacity-50" | |
| 233 | + phx-click="bulk" | |
| 234 | + phx-value-action={@action} | |
| 235 | + phx-disable-with="Working…" | |
| 236 | + > | |
| 237 | + <.icon name={@icon} class="size-3.5" /> {render_slot(@inner_block)} | |
| 238 | + </button> | |
| 239 | + """ | |
| 240 | + end | |
| 241 | +end |
modified
lib/vigil_web/live/issue_live/index.ex
+696
−4
@@ -1,16 +1,708 @@
| 1 | 1 | defmodule VigilWeb.IssueLive.Index do |
| 2 | − @moduledoc "Placeholder; replaced in slice 3." | |
| 2 | + @moduledoc """ | |
| 3 | + A project's issues: the page a project link lands on. | |
| 4 | + | |
| 5 | + Everything the list shows is in the URL, so a view can be shared and | |
| 6 | + back/forward walks through it: `q` (the search box, parsed with | |
| 7 | + `Vigil.Issues.parse_query/1`), and `status`, `env`, `period`, `sort` and | |
| 8 | + `page`, which win over what `q` says. Every control patches the URL; | |
| 9 | + `handle_params/3` reads it and searches. | |
| 10 | + | |
| 11 | + Rows can be selected (one by one, or the whole page) and resolved, | |
| 12 | + ignored, reopened, assigned or deleted together. New events and changes | |
| 13 | + arrive on the project's PubSub topic; they refresh the page at most once | |
| 14 | + a second, keeping the selection where the rows remain. A keyboard hook | |
| 15 | + moves a focus ring with `j`/`k` and acts on the focused row. | |
| 16 | + | |
| 17 | + Until the project has seen an event, the page is its setup guide. | |
| 18 | + """ | |
| 3 | 19 | use VigilWeb, :live_view |
| 4 | 20 | |
| 21 | + import VigilWeb.IssueListComponents | |
| 22 | + | |
| 23 | + alias Vigil.Accounts.Scope | |
| 24 | + alias Vigil.{Issues, Projects, Releases} | |
| 25 | + alias Vigil.Projects.Platforms | |
| 26 | + | |
| 27 | + @per_page 50 | |
| 28 | + @refresh_ms 1_000 | |
| 29 | + | |
| 30 | + @statuses [ | |
| 31 | + {"unresolved", "Unresolved", :unresolved}, | |
| 32 | + {"resolved", "Resolved", :resolved}, | |
| 33 | + {"ignored", "Ignored", :ignored}, | |
| 34 | + {"all", "All", :all} | |
| 35 | + ] | |
| 36 | + @sorts [{"Last seen", "last_seen"}, {"First seen", "first_seen"}, {"Events", "count"}] | |
| 37 | + @past %{ | |
| 38 | + "resolve" => "resolved", | |
| 39 | + "ignore" => "ignored", | |
| 40 | + "unresolve" => "reopened", | |
| 41 | + "assign_me" => "assigned to you", | |
| 42 | + "delete" => "deleted" | |
| 43 | + } | |
| 44 | + | |
| 5 | 45 | @impl true |
| 6 | − def mount(_params, _session, socket), do: {:ok, socket} | |
| 46 | + def mount(_params, _session, socket) do | |
| 47 | + %{current_scope: scope, project: project} = socket.assigns | |
| 48 | + | |
| 49 | + if connected?(socket), | |
| 50 | + do: Phoenix.PubSub.subscribe(Vigil.PubSub, "project:#{project.id}:issues") | |
| 51 | + | |
| 52 | + key = project |> Projects.list_keys() |> Enum.find(& &1.active) | |
| 53 | + dsn = key && Projects.dsn(key, project) | |
| 54 | + | |
| 55 | + {:ok, | |
| 56 | + socket | |
| 57 | + |> assign(:page_title, project.name) | |
| 58 | + |> assign(:dsn, dsn) | |
| 59 | + |> assign(:snippet, dsn && Platforms.snippet(project.platform, dsn)) | |
| 60 | + |> assign(:can_write, Scope.can?(scope, "event:write")) | |
| 61 | + |> assign(:can_delete, Scope.can?(scope, "event:admin")) | |
| 62 | + |> assign(:ever?, project.first_event_at != nil) | |
| 63 | + |> assign(:selected, MapSet.new()) | |
| 64 | + |> assign(:page_ids, []) | |
| 65 | + |> assign(:refresh_pending?, false) | |
| 66 | + |> stream(:issues, [])} | |
| 67 | + end | |
| 68 | + | |
| 69 | + @impl true | |
| 70 | + def handle_params(params, _uri, socket) do | |
| 71 | + q = params |> Map.get("q", "") |> to_string() |> String.trim() | |
| 72 | + parsed = if q == "", do: %{}, else: Issues.parse_query(q) | |
| 73 | + status = status_param(params["status"]) | |
| 74 | + env = blank(params["env"]) | |
| 75 | + period = if params["period"] in Enum.map(Issues.periods(), &elem(&1, 0)), do: params["period"] | |
| 76 | + sort = if params["sort"] in Enum.map(@sorts, &elem(&1, 1)), do: params["sort"] | |
| 77 | + page = page_param(params["page"]) | |
| 78 | + | |
| 79 | + filters = | |
| 80 | + Map.merge(parsed, %{ | |
| 81 | + status: status || parsed[:status] || :unresolved, | |
| 82 | + environment: env || parsed[:environment], | |
| 83 | + period: period, | |
| 84 | + sort: String.to_existing_atom(sort || "last_seen"), | |
| 85 | + limit: @per_page, | |
| 86 | + offset: (page - 1) * @per_page | |
| 87 | + }) | |
| 88 | + | |
| 89 | + query = | |
| 90 | + %{ | |
| 91 | + "q" => q, | |
| 92 | + "status" => status && Atom.to_string(status), | |
| 93 | + "env" => env, | |
| 94 | + "period" => period, | |
| 95 | + "sort" => sort | |
| 96 | + } | |
| 97 | + |> reject_blank() | |
| 98 | + | |
| 99 | + {:noreply, | |
| 100 | + socket | |
| 101 | + |> assign(:query, query) | |
| 102 | + |> assign(:page, page) | |
| 103 | + |> assign(:filters, filters) | |
| 104 | + |> assign(:search_form, to_form(%{"q" => q}, as: :search)) | |
| 105 | + |> assign( | |
| 106 | + :filter_form, | |
| 107 | + to_form(%{"env" => env || "", "period" => period || "", "sort" => sort || "last_seen"}, | |
| 108 | + as: :filters | |
| 109 | + ) | |
| 110 | + ) | |
| 111 | + |> load()} | |
| 112 | + end | |
| 113 | + | |
| 114 | + defp status_param(s) do | |
| 115 | + Enum.find_value(@statuses, fn {key, _, atom} -> key == s && atom end) | |
| 116 | + end | |
| 117 | + | |
| 118 | + defp page_param(p) do | |
| 119 | + case Integer.parse(to_string(p)) do | |
| 120 | + {n, ""} when n > 0 -> n | |
| 121 | + _ -> 1 | |
| 122 | + end | |
| 123 | + end | |
| 124 | + | |
| 125 | + defp blank(v) when v in [nil, ""], do: nil | |
| 126 | + defp blank(v), do: v | |
| 127 | + | |
| 128 | + defp reject_blank(map), do: map |> Enum.reject(fn {_, v} -> v in [nil, ""] end) |> Map.new() | |
| 129 | + | |
| 130 | + # The current page of issues, with sparklines, streamed afresh. | |
| 131 | + defp load(socket) do | |
| 132 | + %{current_scope: scope, project: project, filters: filters} = socket.assigns | |
| 133 | + %{issues: issues, more?: more?} = Issues.search(scope, project.id, filters) | |
| 134 | + ids = Enum.map(issues, & &1.id) | |
| 135 | + sparks = Issues.sparklines(ids) | |
| 136 | + rows = Enum.map(issues, &%{id: &1.id, issue: &1, spark: Map.get(sparks, &1.id, [])}) | |
| 137 | + | |
| 138 | + ever? = | |
| 139 | + socket.assigns.ever? or issues != [] or | |
| 140 | + Issues.list_issues(project.id, status: :all, limit: 1) != [] | |
| 141 | + | |
| 142 | + socket | |
| 143 | + |> assign(:ever?, ever?) | |
| 144 | + |> assign(:more?, more?) | |
| 145 | + |> assign(:page_ids, ids) | |
| 146 | + |> assign(:selected, MapSet.intersection(socket.assigns.selected, MapSet.new(ids))) | |
| 147 | + |> assign(:environments, environments(project.id, filters[:environment])) | |
| 148 | + |> stream(:issues, rows, reset: true) | |
| 149 | + end | |
| 150 | + | |
| 151 | + defp environments(project_id, current) do | |
| 152 | + envs = Releases.list_environments(project_id) | |
| 153 | + if current && current not in envs, do: envs ++ [current], else: envs | |
| 154 | + end | |
| 155 | + | |
| 156 | + # A link to this page with `changes` applied to its query; changing a | |
| 157 | + # filter goes back to the first page. | |
| 158 | + defp list_path(assigns, changes) do | |
| 159 | + query = | |
| 160 | + assigns.query | |
| 161 | + |> Map.merge(changes) | |
| 162 | + |> Map.put("page", Map.get(changes, "page")) | |
| 163 | + |> then(&if(&1["page"] in [1, "1"], do: Map.delete(&1, "page"), else: &1)) | |
| 164 | + |> reject_blank() | |
| 165 | + | |
| 166 | + base = ~p"/orgs/#{assigns.current_scope.organization.slug}/projects/#{assigns.project.slug}" | |
| 167 | + if query == %{}, do: base, else: base <> "?" <> URI.encode_query(query) | |
| 168 | + end | |
| 169 | + | |
| 170 | + defp issue_path(scope, project, issue), | |
| 171 | + do: ~p"/orgs/#{scope.organization.slug}/projects/#{project.slug}/issues/#{issue.short_id}" | |
| 172 | + | |
| 173 | + defp short_id(project, issue), do: "#{String.upcase(project.slug)}-#{issue.short_id}" | |
| 7 | 174 | |
| 8 | 175 | @impl true |
| 9 | 176 | def render(assigns) do |
| 177 | + assigns = | |
| 178 | + assign(assigns, | |
| 179 | + statuses: @statuses, | |
| 180 | + sorts: @sorts, | |
| 181 | + periods: Issues.periods(), | |
| 182 | + all_selected?: | |
| 183 | + assigns.page_ids != [] and MapSet.size(assigns.selected) == length(assigns.page_ids), | |
| 184 | + status_paths: | |
| 185 | + Map.new(@statuses, fn {k, _, _} -> {k, list_path(assigns, %{"status" => k})} end), | |
| 186 | + prev_path: list_path(assigns, %{"page" => assigns.page - 1}), | |
| 187 | + next_path: list_path(assigns, %{"page" => assigns.page + 1}), | |
| 188 | + setup_path: | |
| 189 | + ~p"/orgs/#{assigns.current_scope.organization.slug}/projects/#{assigns.project.slug}/setup" | |
| 190 | + ) | |
| 191 | + | |
| 10 | 192 | ~H""" |
| 11 | − <Layouts.app flash={@flash} current_scope={@current_scope}> | |
| 12 | − <p id="placeholder">Coming in slice 3.</p> | |
| 193 | + <Layouts.app flash={@flash} current_scope={@current_scope} active={:projects}> | |
| 194 | + <.header> | |
| 195 | + <span class="flex items-center gap-2"> | |
| 196 | + {@project.name} | |
| 197 | + <span | |
| 198 | + :if={@ever?} | |
| 199 | + id="live-indicator" | |
| 200 | + class="inline-flex items-center gap-1 rounded-full bg-success/10 px-2 py-0.5 text-xs font-medium text-success" | |
| 201 | + title="This list updates as events arrive" | |
| 202 | + > | |
| 203 | + <span class="size-1.5 rounded-full bg-success motion-safe:animate-pulse" /> Live | |
| 204 | + </span> | |
| 205 | + </span> | |
| 206 | + <:subtitle> | |
| 207 | + {Platforms.name(@project.platform)} · <span class="font-mono">{@project.slug}</span> | |
| 208 | + </:subtitle> | |
| 209 | + <:actions> | |
| 210 | + <div class="flex items-center gap-1"> | |
| 211 | + <.link | |
| 212 | + navigate={@setup_path} | |
| 213 | + class="btn btn-ghost btn-sm" | |
| 214 | + id="project-setup" | |
| 215 | + > | |
| 216 | + <.icon name="hero-code-bracket-micro" class="size-4" /> Setup | |
| 217 | + </.link> | |
| 218 | + <.link | |
| 219 | + :if={Scope.can?(@current_scope, "project:write")} | |
| 220 | + navigate={ | |
| 221 | + ~p"/orgs/#{@current_scope.organization.slug}/projects/#{@project.slug}/settings" | |
| 222 | + } | |
| 223 | + class="btn btn-ghost btn-sm" | |
| 224 | + id="project-settings" | |
| 225 | + > | |
| 226 | + <.icon name="hero-cog-6-tooth-micro" class="size-4" /> Settings | |
| 227 | + </.link> | |
| 228 | + </div> | |
| 229 | + </:actions> | |
| 230 | + </.header> | |
| 231 | + | |
| 232 | + <%= if @ever? do %> | |
| 233 | + <.filter_bar | |
| 234 | + search_form={@search_form} | |
| 235 | + filter_form={@filter_form} | |
| 236 | + environments={@environments} | |
| 237 | + periods={@periods} | |
| 238 | + sorts={@sorts} | |
| 239 | + statuses={@statuses} | |
| 240 | + status={@filters.status} | |
| 241 | + paths={@status_paths} | |
| 242 | + /> | |
| 243 | + | |
| 244 | + <div | |
| 245 | + id="issue-list" | |
| 246 | + phx-hook=".IssueKeys" | |
| 247 | + class="overflow-hidden rounded-box border border-base-300 bg-base-100" | |
| 248 | + > | |
| 249 | + <div class="flex min-h-11 items-center gap-3 border-b border-base-300 bg-base-200/50 px-3 py-1.5"> | |
| 250 | + <input | |
| 251 | + :if={@can_write} | |
| 252 | + type="checkbox" | |
| 253 | + id="select-all" | |
| 254 | + class="checkbox checkbox-sm" | |
| 255 | + checked={@all_selected?} | |
| 256 | + disabled={@page_ids == []} | |
| 257 | + phx-click="select_all" | |
| 258 | + aria-label="Select all issues on this page" | |
| 259 | + /> | |
| 260 | + <%= if MapSet.size(@selected) > 0 do %> | |
| 261 | + <.bulk_actions count={MapSet.size(@selected)} can_delete={@can_delete} /> | |
| 262 | + <% else %> | |
| 263 | + <div class="flex flex-1 items-center justify-between gap-3 text-xs text-base-content/60"> | |
| 264 | + <span>Issue</span> | |
| 265 | + <span class="hidden items-center gap-6 sm:flex"> | |
| 266 | + <span class="w-24 text-center">24h</span> | |
| 267 | + <span class="w-12 text-right">Events</span> | |
| 268 | + <span class="w-20 text-right">Seen</span> | |
| 269 | + <span class="w-6"></span> | |
| 270 | + </span> | |
| 271 | + </div> | |
| 272 | + <% end %> | |
| 273 | + </div> | |
| 274 | + | |
| 275 | + <ul id="issues" phx-update="stream" class="divide-y divide-base-300"> | |
| 276 | + <li | |
| 277 | + id="issues-empty" | |
| 278 | + class="hidden px-4 py-12 text-center text-sm text-base-content/60 only:block" | |
| 279 | + > | |
| 280 | + <.icon name="hero-sparkles" class="mx-auto mb-2 size-6 text-base-content/30" /> | |
| 281 | + No issues match. | |
| 282 | + </li> | |
| 283 | + <li | |
| 284 | + :for={{dom_id, row} <- @streams.issues} | |
| 285 | + id={dom_id} | |
| 286 | + data-issue-id={row.id} | |
| 287 | + phx-mounted={JS.ignore_attributes(["data-focused"])} | |
| 288 | + class="group flex items-center gap-3 px-3 py-2.5 transition-colors hover:bg-base-200/60 has-checked:bg-primary/5 data-focused:bg-base-200/80 data-focused:shadow-[inset_3px_0_0_var(--color-primary)]" | |
| 289 | + > | |
| 290 | + <input | |
| 291 | + :if={@can_write} | |
| 292 | + type="checkbox" | |
| 293 | + id={"select-#{dom_id}"} | |
| 294 | + class="checkbox checkbox-sm" | |
| 295 | + checked={MapSet.member?(@selected, row.id)} | |
| 296 | + phx-click="toggle" | |
| 297 | + phx-value-id={row.id} | |
| 298 | + aria-label={"Select #{short_id(@project, row.issue)}"} | |
| 299 | + /> | |
| 300 | + <.level_marker level={row.issue.level} /> | |
| 301 | + <div class="min-w-0 flex-1"> | |
| 302 | + <.link | |
| 303 | + navigate={issue_path(@current_scope, @project, row.issue)} | |
| 304 | + id={"link-#{dom_id}"} | |
| 305 | + data-issue-link | |
| 306 | + class="block truncate font-medium transition-colors group-hover:text-primary" | |
| 307 | + > | |
| 308 | + {row.issue.title} | |
| 309 | + </.link> | |
| 310 | + <p | |
| 311 | + :if={row.issue.culprit not in [nil, ""]} | |
| 312 | + class="truncate font-mono text-xs text-base-content/60" | |
| 313 | + > | |
| 314 | + {row.issue.culprit} | |
| 315 | + </p> | |
| 316 | + <div class="mt-0.5 flex flex-wrap items-center gap-x-2 gap-y-0.5 text-xs text-base-content/50"> | |
| 317 | + <span class="font-mono">{short_id(@project, row.issue)}</span> | |
| 318 | + <.status_badge status={row.issue.status} /> | |
| 319 | + <span class="hidden sm:inline"> | |
| 320 | + first seen <.relative_time at={row.issue.first_seen} /> | |
| 321 | + </span> | |
| 322 | + <span class="sm:hidden"> | |
| 323 | + <.relative_time at={row.issue.last_seen} /> · {compact(row.issue.count)} events | |
| 324 | + </span> | |
| 325 | + </div> | |
| 326 | + </div> | |
| 327 | + <div class="hidden shrink-0 items-center gap-6 sm:flex"> | |
| 328 | + <.sparkline id={"spark-#{dom_id}"} counts={row.spark} class="w-24" /> | |
| 329 | + <span | |
| 330 | + class="w-12 text-right font-mono text-sm tabular-nums" | |
| 331 | + title={"#{row.issue.count} #{plural(row.issue.count, "event")}"} | |
| 332 | + > | |
| 333 | + {compact(row.issue.count)} | |
| 334 | + </span> | |
| 335 | + <.relative_time | |
| 336 | + at={row.issue.last_seen} | |
| 337 | + class="w-20 text-right text-xs text-base-content/60" | |
| 338 | + /> | |
| 339 | + </div> | |
| 340 | + <.assignee user={row.issue.assignee} /> | |
| 341 | + </li> | |
| 342 | + </ul> | |
| 343 | + | |
| 344 | + <div class="flex flex-wrap items-center justify-between gap-2 border-t border-base-300 bg-base-200/30 px-3 py-2 text-xs text-base-content/50"> | |
| 345 | + <p id="keyboard-hint" class="hidden md:block"> | |
| 346 | + Keyboard: <kbd class="kbd kbd-xs">j</kbd>/<kbd class="kbd kbd-xs">k</kbd> | |
| 347 | + move · <kbd class="kbd kbd-xs">o</kbd> | |
| 348 | + open · <kbd class="kbd kbd-xs">x</kbd> | |
| 349 | + select · <kbd class="kbd kbd-xs">r</kbd> | |
| 350 | + resolve · <kbd class="kbd kbd-xs">i</kbd> | |
| 351 | + ignore · <kbd class="kbd kbd-xs">/</kbd> | |
| 352 | + search | |
| 353 | + </p> | |
| 354 | + <nav id="pagination" class="ml-auto flex items-center gap-1" aria-label="Pages"> | |
| 355 | + <span class="mr-1">Page {@page}</span> | |
| 356 | + <.link | |
| 357 | + :if={@page > 1} | |
| 358 | + id="page-prev" | |
| 359 | + patch={@prev_path} | |
| 360 | + class="btn btn-ghost btn-xs" | |
| 361 | + > | |
| 362 | + <.icon name="hero-chevron-left-micro" class="size-3.5" /> Prev | |
| 363 | + </.link> | |
| 364 | + <.link | |
| 365 | + :if={@more?} | |
| 366 | + id="page-next" | |
| 367 | + patch={@next_path} | |
| 368 | + class="btn btn-ghost btn-xs" | |
| 369 | + > | |
| 370 | + Next <.icon name="hero-chevron-right-micro" class="size-3.5" /> | |
| 371 | + </.link> | |
| 372 | + </nav> | |
| 373 | + </div> | |
| 374 | + </div> | |
| 375 | + | |
| 376 | + <script :type={Phoenix.LiveView.ColocatedHook} name=".IssueKeys"> | |
| 377 | + export default { | |
| 378 | + mounted() { | |
| 379 | + this.focusId = null | |
| 380 | + this.focusIndex = -1 | |
| 381 | + this.onKey = e => { | |
| 382 | + if (e.metaKey || e.ctrlKey || e.altKey) return | |
| 383 | + const t = e.target | |
| 384 | + if (t.closest && t.closest("input, textarea, select, [contenteditable]")) { | |
| 385 | + if (e.key === "Escape") t.blur() | |
| 386 | + return | |
| 387 | + } | |
| 388 | + if (e.key === "/") { | |
| 389 | + e.preventDefault() | |
| 390 | + document.getElementById("issue-search-q")?.focus() | |
| 391 | + return | |
| 392 | + } | |
| 393 | + if (e.key === "j" || e.key === "k") { | |
| 394 | + e.preventDefault() | |
| 395 | + this.move(e.key === "j" ? 1 : -1) | |
| 396 | + return | |
| 397 | + } | |
| 398 | + const row = this.focused() | |
| 399 | + if (!row) return | |
| 400 | + const id = row.dataset.issueId | |
| 401 | + switch (e.key) { | |
| 402 | + case "Enter": | |
| 403 | + case "o": | |
| 404 | + row.querySelector("[data-issue-link]")?.click() | |
| 405 | + break | |
| 406 | + case "x": | |
| 407 | + row.querySelector("input[type=checkbox]")?.click() | |
| 408 | + break | |
| 409 | + case "r": | |
| 410 | + this.pushEvent("resolve_issue", {id}) | |
| 411 | + break | |
| 412 | + case "i": | |
| 413 | + this.pushEvent("ignore_issue", {id}) | |
| 414 | + break | |
| 415 | + default: | |
| 416 | + return | |
| 417 | + } | |
| 418 | + e.preventDefault() | |
| 419 | + } | |
| 420 | + window.addEventListener("keydown", this.onKey) | |
| 421 | + }, | |
| 422 | + destroyed() { | |
| 423 | + window.removeEventListener("keydown", this.onKey) | |
| 424 | + }, | |
| 425 | + rows() { | |
| 426 | + return Array.from(this.el.querySelectorAll("li[data-issue-id]")) | |
| 427 | + }, | |
| 428 | + // The focused row; if it left the list (resolved, say), the one | |
| 429 | + // that took its place. | |
| 430 | + focused() { | |
| 431 | + const rows = this.rows() | |
| 432 | + if (rows.length === 0) return null | |
| 433 | + let row = rows.find(r => r.dataset.issueId === this.focusId) | |
| 434 | + if (!row && this.focusIndex >= 0) { | |
| 435 | + row = rows[Math.min(this.focusIndex, rows.length - 1)] | |
| 436 | + this.focusId = row.dataset.issueId | |
| 437 | + this.paint() | |
| 438 | + } | |
| 439 | + return row || null | |
| 440 | + }, | |
| 441 | + move(delta) { | |
| 442 | + const rows = this.rows() | |
| 443 | + if (rows.length === 0) return | |
| 444 | + const current = this.focused() | |
| 445 | + const at = current ? rows.indexOf(current) : -1 | |
| 446 | + const next = Math.max(0, Math.min(rows.length - 1, at < 0 ? 0 : at + delta)) | |
| 447 | + this.focusIndex = next | |
| 448 | + this.focusId = rows[next].dataset.issueId | |
| 449 | + this.paint() | |
| 450 | + rows[next].scrollIntoView({block: "nearest"}) | |
| 451 | + }, | |
| 452 | + paint() { | |
| 453 | + this.rows().forEach(r => r.toggleAttribute("data-focused", r.dataset.issueId === this.focusId)) | |
| 454 | + } | |
| 455 | + } | |
| 456 | + </script> | |
| 457 | + <% else %> | |
| 458 | + <.setup_panel dsn={@dsn} snippet={@snippet} setup_path={@setup_path} /> | |
| 459 | + <% end %> | |
| 13 | 460 | </Layouts.app> |
| 14 | 461 | """ |
| 15 | 462 | end |
| 463 | + | |
| 464 | + attr :search_form, :any, required: true | |
| 465 | + attr :filter_form, :any, required: true | |
| 466 | + attr :environments, :list, required: true | |
| 467 | + attr :periods, :list, required: true | |
| 468 | + attr :sorts, :list, required: true | |
| 469 | + attr :statuses, :list, required: true | |
| 470 | + attr :status, :atom, required: true | |
| 471 | + attr :paths, :map, required: true | |
| 472 | + | |
| 473 | + defp filter_bar(assigns) do | |
| 474 | + ~H""" | |
| 475 | + <div class="mb-4 space-y-3"> | |
| 476 | + <.form | |
| 477 | + for={@search_form} | |
| 478 | + id="issue-search" | |
| 479 | + phx-submit="search" | |
| 480 | + class="flex items-center gap-2" | |
| 481 | + > | |
| 482 | + <label class="input input-sm flex-1 transition focus-within:border-primary"> | |
| 483 | + <.icon name="hero-magnifying-glass-micro" class="size-4 text-base-content/50" /> | |
| 484 | + <input | |
| 485 | + type="search" | |
| 486 | + id="issue-search-q" | |
| 487 | + name={@search_form[:q].name} | |
| 488 | + value={@search_form[:q].value} | |
| 489 | + placeholder="Search: is:resolved level:error env:production browser.name:Chrome timeout" | |
| 490 | + class="grow" | |
| 491 | + autocomplete="off" | |
| 492 | + phx-debounce="blur" | |
| 493 | + aria-label="Search issues" | |
| 494 | + /> | |
| 495 | + <kbd class="kbd kbd-xs hidden sm:inline-flex">/</kbd> | |
| 496 | + </label> | |
| 497 | + <button type="submit" class="btn btn-sm btn-primary" phx-disable-with="…">Search</button> | |
| 498 | + </.form> | |
| 499 | + | |
| 500 | + <div class="flex flex-wrap items-center justify-between gap-2"> | |
| 501 | + <nav id="status-tabs" role="tablist" class="tabs tabs-box tabs-sm"> | |
| 502 | + <.link | |
| 503 | + :for={{key, label, atom} <- @statuses} | |
| 504 | + id={"status-#{key}"} | |
| 505 | + patch={@paths[key]} | |
| 506 | + role="tab" | |
| 507 | + aria-selected={to_string(@status == atom)} | |
| 508 | + class={["tab transition", @status == atom && "tab-active"]} | |
| 509 | + > | |
| 510 | + {label} | |
| 511 | + </.link> | |
| 512 | + </nav> | |
| 513 | + | |
| 514 | + <.form | |
| 515 | + for={@filter_form} | |
| 516 | + id="issue-filters" | |
| 517 | + phx-change="filter" | |
| 518 | + class="flex flex-wrap items-center gap-2" | |
| 519 | + > | |
| 520 | + <.input | |
| 521 | + field={@filter_form[:env]} | |
| 522 | + type="select" | |
| 523 | + id="filter-env" | |
| 524 | + class="select select-sm w-auto" | |
| 525 | + prompt="All environments" | |
| 526 | + options={@environments} | |
| 527 | + aria-label="Environment" | |
| 528 | + /> | |
| 529 | + <.input | |
| 530 | + field={@filter_form[:period]} | |
| 531 | + type="select" | |
| 532 | + id="filter-period" | |
| 533 | + class="select select-sm w-auto" | |
| 534 | + prompt="Any time" | |
| 535 | + options={Enum.map(@periods, fn {k, label} -> {label, k} end)} | |
| 536 | + aria-label="Time range" | |
| 537 | + /> | |
| 538 | + <.input | |
| 539 | + field={@filter_form[:sort]} | |
| 540 | + type="select" | |
| 541 | + id="filter-sort" | |
| 542 | + class="select select-sm w-auto" | |
| 543 | + options={@sorts} | |
| 544 | + aria-label="Sort by" | |
| 545 | + /> | |
| 546 | + </.form> | |
| 547 | + </div> | |
| 548 | + </div> | |
| 549 | + """ | |
| 550 | + end | |
| 551 | + | |
| 552 | + attr :dsn, :string, default: nil | |
| 553 | + attr :snippet, :string, default: nil | |
| 554 | + attr :setup_path, :string, required: true | |
| 555 | + | |
| 556 | + defp setup_panel(assigns) do | |
| 557 | + ~H""" | |
| 558 | + <section | |
| 559 | + id="setup" | |
| 560 | + class="space-y-4 rounded-box border border-dashed border-base-300 bg-base-100 p-5" | |
| 561 | + > | |
| 562 | + <div class="flex items-center gap-2"> | |
| 563 | + <span class="size-2.5 rounded-full bg-warning motion-safe:animate-pulse" /> | |
| 564 | + <h2 class="font-semibold">Waiting for the first event</h2> | |
| 565 | + </div> | |
| 566 | + <p class="text-sm text-base-content/70"> | |
| 567 | + Point an SDK at this DSN and send an error; it shows up here as it happens. | |
| 568 | + </p> | |
| 569 | + | |
| 570 | + <%= if @dsn do %> | |
| 571 | + <div class="join w-full"> | |
| 572 | + <input | |
| 573 | + id="dsn" | |
| 574 | + type="text" | |
| 575 | + readonly | |
| 576 | + value={@dsn} | |
| 577 | + class="input input-sm join-item min-w-0 flex-1 font-mono text-xs" | |
| 578 | + aria-label="DSN" | |
| 579 | + /> | |
| 580 | + <button | |
| 581 | + id="copy-dsn" | |
| 582 | + type="button" | |
| 583 | + class="btn btn-sm btn-primary join-item" | |
| 584 | + phx-click={JS.dispatch("phx:copy", to: "#dsn")} | |
| 585 | + > | |
| 586 | + <.icon name="hero-clipboard-document-micro" class="size-4" /> Copy | |
| 587 | + </button> | |
| 588 | + </div> | |
| 589 | + <pre | |
| 590 | + id="snippet" | |
| 591 | + class="overflow-x-auto rounded-box bg-base-200 p-4 font-mono text-xs leading-relaxed" | |
| 592 | + ><code>{@snippet}</code></pre> | |
| 593 | + <% else %> | |
| 594 | + <p class="text-sm text-warning">This project has no active key. Make one in its settings.</p> | |
| 595 | + <% end %> | |
| 596 | + | |
| 597 | + <.link navigate={@setup_path} class="link link-primary text-sm" id="setup-guide"> | |
| 598 | + The full setup guide <.icon name="hero-arrow-right-micro" class="size-3.5" /> | |
| 599 | + </.link> | |
| 600 | + </section> | |
| 601 | + """ | |
| 602 | + end | |
| 603 | + | |
| 604 | + ## Filters | |
| 605 | + | |
| 606 | + @impl true | |
| 607 | + def handle_event("search", %{"search" => %{"q" => q}}, socket), | |
| 608 | + do: {:noreply, push_patch(socket, to: list_path(socket.assigns, %{"q" => String.trim(q)}))} | |
| 609 | + | |
| 610 | + def handle_event("filter", %{"filters" => f}, socket) do | |
| 611 | + changes = %{ | |
| 612 | + "env" => f["env"], | |
| 613 | + "period" => f["period"], | |
| 614 | + "sort" => if(f["sort"] == "last_seen", do: nil, else: f["sort"]) | |
| 615 | + } | |
| 616 | + | |
| 617 | + {:noreply, push_patch(socket, to: list_path(socket.assigns, changes))} | |
| 618 | + end | |
| 619 | + | |
| 620 | + ## Selection | |
| 621 | + | |
| 622 | + # The checkbox has already toggled in the browser, so the row needn't | |
| 623 | + # re-render; only the toolbar follows. | |
| 624 | + def handle_event("toggle", %{"id" => id}, socket) do | |
| 625 | + selected = socket.assigns.selected | |
| 626 | + | |
| 627 | + selected = | |
| 628 | + cond do | |
| 629 | + id not in socket.assigns.page_ids -> selected | |
| 630 | + MapSet.member?(selected, id) -> MapSet.delete(selected, id) | |
| 631 | + true -> MapSet.put(selected, id) | |
| 632 | + end | |
| 633 | + | |
| 634 | + {:noreply, assign(socket, :selected, selected)} | |
| 635 | + end | |
| 636 | + | |
| 637 | + def handle_event("select_all", _params, socket) do | |
| 638 | + %{selected: selected, page_ids: ids} = socket.assigns | |
| 639 | + all? = ids != [] and MapSet.size(selected) == length(ids) | |
| 640 | + selected = if all?, do: MapSet.new(), else: MapSet.new(ids) | |
| 641 | + {:noreply, socket |> assign(:selected, selected) |> load()} | |
| 642 | + end | |
| 643 | + | |
| 644 | + def handle_event("clear_selection", _params, socket), | |
| 645 | + do: {:noreply, socket |> assign(:selected, MapSet.new()) |> load()} | |
| 646 | + | |
| 647 | + ## Actions | |
| 648 | + | |
| 649 | + def handle_event("bulk", %{"action" => action}, socket) when is_map_key(@past, action) do | |
| 650 | + %{current_scope: scope, project: project, selected: selected} = socket.assigns | |
| 651 | + | |
| 652 | + cond do | |
| 653 | + action == "delete" and not socket.assigns.can_delete -> | |
| 654 | + {:noreply, put_flash(socket, :error, "You can't delete issues here.")} | |
| 655 | + | |
| 656 | + MapSet.size(selected) == 0 -> | |
| 657 | + {:noreply, socket} | |
| 658 | + | |
| 659 | + true -> | |
| 660 | + op = if action == "assign_me", do: {"assign", scope.user.id}, else: action | |
| 661 | + n = Issues.bulk(scope, project.id, MapSet.to_list(selected), op) | |
| 662 | + | |
| 663 | + {:noreply, | |
| 664 | + socket | |
| 665 | + |> put_flash(:info, "#{n} #{plural(n, "issue")} #{@past[action]}.") | |
| 666 | + |> assign(:selected, MapSet.new()) | |
| 667 | + |> load()} | |
| 668 | + end | |
| 669 | + end | |
| 670 | + | |
| 671 | + def handle_event("resolve_issue", %{"id" => id}, socket), | |
| 672 | + do: one(socket, id, &Issues.resolve(&1, &2), "resolved") | |
| 673 | + | |
| 674 | + def handle_event("ignore_issue", %{"id" => id}, socket), | |
| 675 | + do: one(socket, id, &Issues.ignore/2, "ignored") | |
| 676 | + | |
| 677 | + defp one(socket, id, fun, done) do | |
| 678 | + %{current_scope: scope, project: project} = socket.assigns | |
| 679 | + | |
| 680 | + with %{} = issue <- Issues.get_issue(project.id, id), | |
| 681 | + {:ok, issue} <- fun.(scope, issue) do | |
| 682 | + {:noreply, | |
| 683 | + socket | |
| 684 | + |> put_flash(:info, "#{short_id(project, issue)} #{done}.") | |
| 685 | + |> load()} | |
| 686 | + else | |
| 687 | + _ -> {:noreply, put_flash(socket, :error, "Couldn't change that issue.")} | |
| 688 | + end | |
| 689 | + end | |
| 690 | + | |
| 691 | + ## Live updates | |
| 692 | + | |
| 693 | + @impl true | |
| 694 | + def handle_info({event, _}, socket) when event in [:issue_event, :issue_updated] do | |
| 695 | + if socket.assigns.refresh_pending? do | |
| 696 | + {:noreply, socket} | |
| 697 | + else | |
| 698 | + Process.send_after(self(), :refresh, @refresh_ms) | |
| 699 | + {:noreply, assign(socket, :refresh_pending?, true)} | |
| 700 | + end | |
| 701 | + end | |
| 702 | + | |
| 703 | + # Comments don't change anything the list shows. | |
| 704 | + def handle_info({:issue_comment, _}, socket), do: {:noreply, socket} | |
| 705 | + | |
| 706 | + def handle_info(:refresh, socket), | |
| 707 | + do: {:noreply, socket |> assign(:refresh_pending?, false) |> load()} | |
| 16 | 708 | end |
added
test/vigil_web/live/issue_live/index_test.exs
+292
−0
@@ -0,0 +1,292 @@
| 1 | +defmodule VigilWeb.IssueLive.IndexTest do | |
| 2 | + use VigilWeb.ConnCase, async: true | |
| 3 | + | |
| 4 | + import Phoenix.LiveViewTest | |
| 5 | + import Vigil.AccountsFixtures | |
| 6 | + import Vigil.OrganizationsFixtures | |
| 7 | + | |
| 8 | + alias Vigil.Ingest | |
| 9 | + alias Vigil.Ingest.{Drain, Envelope} | |
| 10 | + alias Vigil.Issues | |
| 11 | + alias VigilWeb.IssueListComponents | |
| 12 | + | |
| 13 | + setup :register_and_log_in_user | |
| 14 | + | |
| 15 | + setup %{user: user} do | |
| 16 | + org = organization_fixture(user) | |
| 17 | + scope = org_scope(user, org) | |
| 18 | + project = project_fixture(scope) | |
| 19 | + | |
| 20 | + target = %Ingest.Target{ | |
| 21 | + project_id: project.id, | |
| 22 | + dsn_id: project.dsn_id, | |
| 23 | + organization_id: org.id, | |
| 24 | + key_id: Ecto.UUID.generate() | |
| 25 | + } | |
| 26 | + | |
| 27 | + path = ~p"/orgs/#{org.slug}/projects/#{project.slug}" | |
| 28 | + %{org: org, scope: scope, project: project, target: target, path: path} | |
| 29 | + end | |
| 30 | + | |
| 31 | + defp send_event(target, type, extra \\ %{}) do | |
| 32 | + event = | |
| 33 | + Map.merge( | |
| 34 | + %{ | |
| 35 | + "event_id" => Vigil.Schema.uuid7() |> String.replace("-", ""), | |
| 36 | + "exception" => [%{"type" => type, "value" => "v"}], | |
| 37 | + "platform" => "python" | |
| 38 | + }, | |
| 39 | + extra | |
| 40 | + ) | |
| 41 | + | |
| 42 | + item = %Envelope.Item{type: "event", headers: %{}, payload: Jason.encode!(event)} | |
| 43 | + Ingest.accept(target, %{headers: %{}, items: [item]}, %{}) | |
| 44 | + Drain.drain(target.project_id) | |
| 45 | + end | |
| 46 | + | |
| 47 | + defp issue(scope, project, title) do | |
| 48 | + %{issues: issues} = Issues.search(scope, project.id, %{status: :all, text: title}) | |
| 49 | + Enum.find(issues, &(&1.title == title)) | |
| 50 | + end | |
| 51 | + | |
| 52 | + defp row(issue), do: "#issues-#{issue.id}" | |
| 53 | + | |
| 54 | + test "a project without events shows its DSN and setup", %{conn: conn, path: path} do | |
| 55 | + {:ok, view, _html} = live(conn, path) | |
| 56 | + assert has_element?(view, "#setup #dsn") | |
| 57 | + assert has_element?(view, "#copy-dsn") | |
| 58 | + assert has_element?(view, "#snippet") | |
| 59 | + assert has_element?(view, "#setup-guide") | |
| 60 | + refute has_element?(view, "#issues") | |
| 61 | + end | |
| 62 | + | |
| 63 | + test "issues are listed once events arrive", ctx do | |
| 64 | + %{conn: conn, path: path, target: target, scope: scope, project: project} = ctx | |
| 65 | + send_event(target, "KeyError") | |
| 66 | + send_event(target, "ValueError", %{"level" => "warning"}) | |
| 67 | + | |
| 68 | + {:ok, view, _html} = live(conn, path) | |
| 69 | + refute has_element?(view, "#setup") | |
| 70 | + | |
| 71 | + key_error = issue(scope, project, "KeyError: v") | |
| 72 | + assert has_element?(view, row(key_error)) | |
| 73 | + assert has_element?(view, "#link-issues-#{key_error.id}", "KeyError: v") | |
| 74 | + assert has_element?(view, "#spark-issues-#{key_error.id}[role=img]") | |
| 75 | + assert has_element?(view, row(key_error), "#{String.upcase(project.slug)}-") | |
| 76 | + assert has_element?(view, "#keyboard-hint") | |
| 77 | + assert has_element?(view, "#issue-list[phx-hook]") | |
| 78 | + end | |
| 79 | + | |
| 80 | + test "status tabs, search and environment change the list by patch", ctx do | |
| 81 | + %{conn: conn, path: path, target: target, scope: scope, project: project} = ctx | |
| 82 | + send_event(target, "KeyError", %{"environment" => "production"}) | |
| 83 | + send_event(target, "ValueError", %{"environment" => "staging"}) | |
| 84 | + key_error = issue(scope, project, "KeyError: v") | |
| 85 | + value_error = issue(scope, project, "ValueError: v") | |
| 86 | + {:ok, _} = Issues.resolve(scope, value_error) | |
| 87 | + | |
| 88 | + {:ok, view, _html} = live(conn, path) | |
| 89 | + assert has_element?(view, row(key_error)) | |
| 90 | + refute has_element?(view, row(value_error)) | |
| 91 | + | |
| 92 | + view |> element("#status-resolved") |> render_click() | |
| 93 | + assert_patch(view, path <> "?status=resolved") | |
| 94 | + assert has_element?(view, row(value_error)) | |
| 95 | + refute has_element?(view, row(key_error)) | |
| 96 | + assert has_element?(view, "#status-resolved.tab-active") | |
| 97 | + | |
| 98 | + view |> element("#status-all") |> render_click() | |
| 99 | + assert_patch(view, path <> "?status=all") | |
| 100 | + assert has_element?(view, row(key_error)) | |
| 101 | + assert has_element?(view, row(value_error)) | |
| 102 | + | |
| 103 | + view |> form("#issue-search", search: %{q: "KeyError"}) |> render_submit() | |
| 104 | + assert_patch(view, path <> "?q=KeyError&status=all") | |
| 105 | + assert has_element?(view, row(key_error)) | |
| 106 | + refute has_element?(view, row(value_error)) | |
| 107 | + | |
| 108 | + view |> form("#issue-search", search: %{q: ""}) |> render_submit() | |
| 109 | + assert_patch(view, path <> "?status=all") | |
| 110 | + | |
| 111 | + view |> form("#issue-filters", filters: %{env: "staging"}) |> render_change() | |
| 112 | + assert_patch(view, path <> "?env=staging&status=all") | |
| 113 | + assert has_element?(view, row(value_error)) | |
| 114 | + refute has_element?(view, row(key_error)) | |
| 115 | + | |
| 116 | + {:ok, view, _html} = live(conn, path <> "?env=nowhere") | |
| 117 | + assert has_element?(view, "#filter-env option[value=nowhere][selected]") | |
| 118 | + assert has_element?(view, "#issues-empty") | |
| 119 | + end | |
| 120 | + | |
| 121 | + test "the search box's is: works, and an explicit status wins", ctx do | |
| 122 | + %{conn: conn, path: path, target: target, scope: scope, project: project} = ctx | |
| 123 | + send_event(target, "KeyError") | |
| 124 | + key_error = issue(scope, project, "KeyError: v") | |
| 125 | + {:ok, _} = Issues.ignore(scope, key_error) | |
| 126 | + | |
| 127 | + {:ok, view, _html} = live(conn, path <> "?q=is:ignored") | |
| 128 | + assert has_element?(view, row(key_error)) | |
| 129 | + assert has_element?(view, "#status-ignored.tab-active") | |
| 130 | + | |
| 131 | + {:ok, view, _html} = live(conn, path <> "?q=is:ignored&status=unresolved") | |
| 132 | + refute has_element?(view, row(key_error)) | |
| 133 | + end | |
| 134 | + | |
| 135 | + test "sort and period are kept in the URL", %{conn: conn, path: path, target: target} do | |
| 136 | + send_event(target, "KeyError") | |
| 137 | + {:ok, view, _html} = live(conn, path) | |
| 138 | + | |
| 139 | + view |> form("#issue-filters", filters: %{sort: "count", period: "24h"}) |> render_change() | |
| 140 | + assert_patch(view, path <> "?period=24h&sort=count") | |
| 141 | + assert has_element?(view, "#filter-sort option[value=count][selected]") | |
| 142 | + end | |
| 143 | + | |
| 144 | + test "bulk resolve takes the selected rows out of the unresolved view", ctx do | |
| 145 | + %{conn: conn, path: path, target: target, scope: scope, project: project} = ctx | |
| 146 | + send_event(target, "KeyError") | |
| 147 | + send_event(target, "ValueError") | |
| 148 | + key_error = issue(scope, project, "KeyError: v") | |
| 149 | + value_error = issue(scope, project, "ValueError: v") | |
| 150 | + | |
| 151 | + {:ok, view, _html} = live(conn, path) | |
| 152 | + refute has_element?(view, "#bulk-actions") | |
| 153 | + | |
| 154 | + view |> element("#select-issues-#{key_error.id}") |> render_click() | |
| 155 | + assert has_element?(view, "#bulk-actions") | |
| 156 | + assert has_element?(view, "#selected-count", "1 selected") | |
| 157 | + assert has_element?(view, "#bulk-delete[data-confirm]") | |
| 158 | + | |
| 159 | + view |> element("#bulk-resolve") |> render_click() | |
| 160 | + refute has_element?(view, row(key_error)) | |
| 161 | + assert has_element?(view, row(value_error)) | |
| 162 | + refute has_element?(view, "#bulk-actions") | |
| 163 | + assert render(view) =~ "1 issue resolved." | |
| 164 | + assert issue(scope, project, "KeyError: v").status == :resolved | |
| 165 | + end | |
| 166 | + | |
| 167 | + test "select all, then assign to me and delete", ctx do | |
| 168 | + %{conn: conn, path: path, target: target, scope: scope, project: project, user: user} = ctx | |
| 169 | + send_event(target, "KeyError") | |
| 170 | + send_event(target, "ValueError") | |
| 171 | + | |
| 172 | + {:ok, view, _html} = live(conn, path) | |
| 173 | + view |> element("#select-all") |> render_click() | |
| 174 | + assert has_element?(view, "#selected-count", "2 selected") | |
| 175 | + assert has_element?(view, "#select-all[checked]") | |
| 176 | + | |
| 177 | + view |> element("#bulk-assign-me") |> render_click() | |
| 178 | + assert issue(scope, project, "KeyError: v").assignee_id == user.id | |
| 179 | + | |
| 180 | + view |> element("#select-all") |> render_click() | |
| 181 | + view |> element("#bulk-delete") |> render_click() | |
| 182 | + assert has_element?(view, "#issues-empty") | |
| 183 | + assert Issues.search(scope, project.id, %{status: :all}).issues == [] | |
| 184 | + end | |
| 185 | + | |
| 186 | + test "Delete shows only to callers with event:admin" do | |
| 187 | + html = render_component(&IssueListComponents.bulk_actions/1, count: 2, can_delete: false) | |
| 188 | + doc = LazyHTML.from_fragment(html) | |
| 189 | + assert LazyHTML.query(doc, "#bulk-resolve") |> Enum.count() == 1 | |
| 190 | + assert LazyHTML.query(doc, "#bulk-delete") |> Enum.empty?() | |
| 191 | + | |
| 192 | + html = render_component(&IssueListComponents.bulk_actions/1, count: 2, can_delete: true) | |
| 193 | + assert LazyHTML.from_fragment(html) |> LazyHTML.query("#bulk-delete") |> Enum.count() == 1 | |
| 194 | + end | |
| 195 | + | |
| 196 | + test "another member of the organization can triage, Delete included", ctx do | |
| 197 | + %{org: org, target: target, path: path} = ctx | |
| 198 | + manager = user_fixture() | |
| 199 | + member_fixture(org, manager, :manager) | |
| 200 | + send_event(target, "KeyError") | |
| 201 | + | |
| 202 | + {:ok, view, _html} = live(log_in_user(build_conn(), manager), path) | |
| 203 | + view |> element("#select-all") |> render_click() | |
| 204 | + assert has_element?(view, "#bulk-resolve") | |
| 205 | + assert has_element?(view, "#bulk-delete") | |
| 206 | + end | |
| 207 | + | |
| 208 | + test "a new event's issue appears live", ctx do | |
| 209 | + %{conn: conn, path: path, target: target, scope: scope, project: project} = ctx | |
| 210 | + send_event(target, "KeyError") | |
| 211 | + {:ok, view, _html} = live(conn, path) | |
| 212 | + | |
| 213 | + Phoenix.PubSub.subscribe(Vigil.PubSub, "project:#{project.id}:issues") | |
| 214 | + send_event(target, "ValueError") | |
| 215 | + assert_receive {:issue_event, %{new: true}} | |
| 216 | + # The view got the same broadcast and waits out its coalescing tick; | |
| 217 | + # skip the wait. | |
| 218 | + _ = :sys.get_state(view.pid) | |
| 219 | + send(view.pid, :refresh) | |
| 220 | + | |
| 221 | + assert has_element?(view, row(issue(scope, project, "ValueError: v"))) | |
| 222 | + end | |
| 223 | + | |
| 224 | + test "the setup panel gives way when the first event arrives", ctx do | |
| 225 | + %{conn: conn, path: path, target: target, scope: scope, project: project} = ctx | |
| 226 | + {:ok, view, _html} = live(conn, path) | |
| 227 | + assert has_element?(view, "#dsn") | |
| 228 | + | |
| 229 | + send_event(target, "KeyError") | |
| 230 | + send(view.pid, {:issue_event, %{issue_id: nil, event_id: nil, new: true}}) | |
| 231 | + send(view.pid, :refresh) | |
| 232 | + | |
| 233 | + refute has_element?(view, "#dsn") | |
| 234 | + assert has_element?(view, row(issue(scope, project, "KeyError: v"))) | |
| 235 | + end | |
| 236 | + | |
| 237 | + test "pages of 50", %{conn: conn, path: path, target: target} do | |
| 238 | + for n <- 1..51, do: send_event(target, "Error#{n}") | |
| 239 | + | |
| 240 | + {:ok, view, _html} = live(conn, path) | |
| 241 | + refute has_element?(view, "#page-prev") | |
| 242 | + assert has_element?(view, "#page-next") | |
| 243 | + | |
| 244 | + view |> element("#page-next") |> render_click() | |
| 245 | + assert_patch(view, path <> "?page=2") | |
| 246 | + assert has_element?(view, "#page-prev") | |
| 247 | + refute has_element?(view, "#page-next") | |
| 248 | + | |
| 249 | + html = render(view) | |
| 250 | + rows = html |> LazyHTML.from_fragment() |> LazyHTML.query("#issues li[data-issue-id]") | |
| 251 | + assert Enum.count(rows) == 1 | |
| 252 | + | |
| 253 | + view |> element("#page-prev") |> render_click() | |
| 254 | + assert_patch(view, path) | |
| 255 | + end | |
| 256 | + | |
| 257 | + test "keyboard: r resolves and i ignores the focused issue", ctx do | |
| 258 | + %{conn: conn, path: path, target: target, scope: scope, project: project} = ctx | |
| 259 | + send_event(target, "KeyError") | |
| 260 | + send_event(target, "ValueError") | |
| 261 | + key_error = issue(scope, project, "KeyError: v") | |
| 262 | + value_error = issue(scope, project, "ValueError: v") | |
| 263 | + | |
| 264 | + {:ok, view, _html} = live(conn, path) | |
| 265 | + | |
| 266 | + view |> element("#issue-list") |> render_hook("resolve_issue", %{"id" => key_error.id}) | |
| 267 | + refute has_element?(view, row(key_error)) | |
| 268 | + assert issue(scope, project, "KeyError: v").status == :resolved | |
| 269 | + | |
| 270 | + view |> element("#issue-list") |> render_hook("ignore_issue", %{"id" => value_error.id}) | |
| 271 | + refute has_element?(view, row(value_error)) | |
| 272 | + assert issue(scope, project, "ValueError: v").status == :ignored | |
| 273 | + | |
| 274 | + view |> element("#issue-list") |> render_hook("ignore_issue", %{"id" => "nope"}) | |
| 275 | + assert render(view) =~ "Couldn't change that issue." | |
| 276 | + end | |
| 277 | + | |
| 278 | + describe "helpers" do | |
| 279 | + test "ago and compact" do | |
| 280 | + now = ~U[2026-01-10 12:00:00Z] | |
| 281 | + assert IssueListComponents.ago(~U[2026-01-10 11:59:50Z], now) == "just now" | |
| 282 | + assert IssueListComponents.ago(~U[2026-01-10 11:55:00Z], now) == "5m ago" | |
| 283 | + assert IssueListComponents.ago(~U[2026-01-10 09:00:00Z], now) == "3h ago" | |
| 284 | + assert IssueListComponents.ago(~U[2026-01-08 12:00:00Z], now) == "2d ago" | |
| 285 | + assert IssueListComponents.ago(~U[2025-06-01 12:00:00Z], now) == "Jun 1, 2025" | |
| 286 | + assert IssueListComponents.compact(999) == "999" | |
| 287 | + assert IssueListComponents.compact(1234) == "1.2k" | |
| 288 | + assert IssueListComponents.compact(45_000) == "45k" | |
| 289 | + assert IssueListComponents.compact(1_500_000) == "1.5M" | |
| 290 | + end | |
| 291 | + end | |
| 292 | +end |