existential-birds/beagle / plugins/beagle-elixir/skills/liveview-code-review
liveview-code-review
VerifiedReviews Phoenix LiveView code for lifecycle patterns, assigns/streams usage, components, and security. Use when reviewing LiveView modules, .heex templates, or LiveComponents.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 4.2 KB |
| references/assigns-streams.md | markdown | 3.2 KB |
| references/components.md | markdown | 3.3 KB |
| references/lifecycle.md | markdown | 3.1 KB |
| references/security.md | markdown | 3.6 KB |
references/lifecycle.md
View on GitHub# LiveView Lifecycle
## Mount
### connected?/1 for Subscriptions
```elixir
def mount(_params, _session, socket) do
# Only subscribe when actually connected (not during static render)
if connected?(socket) do
Phoenix.PubSub.subscribe(MyApp.PubSub, "updates")
end
{:ok, assign(socket, items: [])}
end
```
### Expensive Operations
```elixir
# BAD - blocks initial render
def mount(_params, _session, socket) do
items = Repo.all(Item) # Blocks!
{:ok, assign(socket, items: items)}
end
# GOOD - defer with assign_async
def mount(_params, _session, socket) do
{:ok,
socket
|> assign(:page_title, "Items")
|> assign_async(:items, fn -> {:ok, %{items: Repo.all(Item)}} end)}
end
```
## handle_params
### URL-Based State
```elixir
def handle_params(%{"page" => page}, _uri, socket) do
page =
case Integer.parse(page) do
{n, ""} when n > 0 -> n
_ -> 1
end
{:noreply, assign(socket, page: page, items: load_page(page))}
end
def handle_params(_params, _uri, socket) do
{:noreply, assign(socket, page: 1, items: load_page(1))}
end
```
### Live Navigation
```elixir
# Triggers handle_params, not full remount
<.link patch={~p"/items?page=2"}>Page 2</.link>
# Full remount (different LiveView)
<.link navigate={~p"/other"}>Other Page</.link>
```
## handle_event
### Pattern Match Events
```elixir
def handle_event("save", %{"form" => form_params}, socket) do
# Handle save
end
def handle_event("delete", %{"id" => id}, socket) do
# Handle delete
end
def handle_event("toggle", %{"value" => value}, socket) do
# Handle toggle
end
```
### Form Events
```elixir
def handle_event("validate", %{"user" => params}, socket) do
changeset =
socket.assigns.user
|> User.changeset(params)
|> Map.put(:action, :validate)
{:noreply, assign(socket, changeset: changeset)}
end
def handle_event("save", %{"user" => params}, socket) do
case Accounts.update_user(socket.assigns.user, params) do
{:ok, user} ->
{:noreply,
socket
|> put_flash(:info, "Saved!")
|> push_navigate(to: ~p"/users/#{user}")}
{:error, changeset} ->
{:noreply, assign(socket, changeset: changeset)}
end
end
```
## handle_async
### With assign_async
```elixir
def mount(_params, _session, socket) do
{:ok,
socket
|> assign_async(:user, fn -> {:ok, %{user: load_user()}} end)}
end
# In template - handle all states
<.async_result :let={user} assign={@user}>
<:loading>Loading user...</:loading>
<:failed :let={reason}>Error: <%= inspect(reason) %></:failed>
<%= user.name %>
</.async_result>
```
### With start_async
```elixir
def handle_event("refresh", _, socket) do
{:noreply, start_async(socket, :refresh, fn -> fetch_data() end)}
end
def handle_async(:refresh, {:ok, data}, socket) do
{:noreply, assign(socket, data: data)}
end
def handle_async(:refresh, {:exit, reason}, socket) do
{:noreply, put_flash(socket, :error, "Refresh failed")}
end
```
## Review Questions
1. Are PubSub subscriptions wrapped in connected?(socket)?
2. Is handle_params used for URL-based state changes?
3. Do async operations handle both success and failure?
4. Is expensive loading deferred with assign_async?
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit. | Stars: 470k | Updated | Claude CodeCodex | Verified | Install |
| rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure. | Stars: 470k | Updated | Claude CodeCodex | Verified | Install |
| rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics. | Stars: 470k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |