defmodule AiDebugWeb.DebugDemoLive do @moduledoc """ A demonstration LiveView showcasing all debugging features. This LiveView includes: - State inspection and manipulation - Event tracking and replay - Performance monitoring - Debug annotations - Interactive debugging panel """ use AiDebugWeb, :live_view alias AiDebug.LiveViewDebugger @impl true def mount(_params, _session, socket) do if connected?(socket) do :timer.send_interval(1000, self(), :tick) end {:ok, socket |> assign(:counter, 0) |> assign(:messages, []) |> assign(:form_data, %{}) |> assign(:items, generate_items(10)) |> assign(:selected_item, nil) |> assign(:show_details, false) |> assign(:last_event, nil) |> assign(:tick_count, 0)} end @impl true def handle_event("increment", _params, socket) do {:noreply, socket |> update(:counter, &(&1 + 1)) |> assign(:last_event, "increment")} end @impl true def handle_event("decrement", _params, socket) do {:noreply, socket |> update(:counter, &(&1 - 1)) |> assign(:last_event, "decrement")} end @impl true def handle_event("add_message", %{"message" => message}, socket) do new_message = %{ id: System.unique_integer([:positive]), text: message, timestamp: DateTime.utc_now() } {:noreply, socket |> update(:messages, &([new_message | &1])) |> assign(:last_event, "add_message")} end @impl true def handle_event("select_item", %{"id" => id}, socket) do item = Enum.find(socket.assigns.items, &(&1.id == String.to_integer(id))) {:noreply, socket |> assign(:selected_item, item) |> assign(:show_details, true) |> assign(:last_event, "select_item")} end @impl true def handle_event("close_details", _params, socket) do {:noreply, socket |> assign(:show_details, false) |> assign(:last_event, "close_details")} end @impl true def handle_event("trigger_error", _params, socket) do # Intentionally trigger an error for debugging _ = 1 / 0 {:noreply, socket} rescue error -> {:noreply, socket |> put_flash(:error, "Error caught: #{inspect(error)}") |> assign(:last_event, "trigger_error")} end @impl true def handle_event("heavy_computation", _params, socket) do # Simulate heavy computation result = Enum.reduce(1..1_000_000, 0, fn i, acc -> :math.sin(i) + acc end) {:noreply, socket |> put_flash(:info, "Computation complete: #{Float.round(result, 2)}") |> assign(:last_event, "heavy_computation")} end @impl true def handle_info(:tick, socket) do {:noreply, update(socket, :tick_count, &(&1 + 1))} end @impl true def render(assigns) do ~H"""
Press Ctrl+Shift+D to toggle the debug panel
<%= message.text %>
<%= Calendar.strftime(message.timestamp, "%H:%M:%S") %>
<%= item.description %>
Last Event: <%= @last_event || "none" %>
Tick Count: <%= @tick_count %>
<%= @selected_item.description %>