defmodule LiveDebuggerWeb.Live.Traces.Components.Trace do @moduledoc """ This component is responsible for rendering a single trace. It produces `open-trace` event when clicked that can be handled by hook declared via `init/1`. It also produces `toggle-collapsible` event when clicked that can be handled by hook declared via `init/1`. """ use LiveDebuggerWeb, :hook_component alias LiveDebugger.Structs.Trace alias LiveDebugger.Services.TraceService alias LiveDebugger.Structs.TraceDisplay alias LiveDebugger.Utils.Parsers alias LiveDebuggerWeb.Hooks.Flash alias LiveDebuggerWeb.Live.Traces.Components @required_assigns [:lv_process, :displayed_trace] @doc """ Initializes the trace component by attaching the hook to the socket and checking the required assigns. """ @spec init(Phoenix.LiveView.Socket.t()) :: Phoenix.LiveView.Socket.t() def init(socket) do socket |> check_assigns!(@required_assigns) |> attach_hook(:trace, :handle_event, &handle_event/3) |> register_hook(:trace) end @doc """ Renders the trace component. It produces `open-trace` event when clicked that can be handled by hook declared via `init/1`. It also produces `toggle-collapsible` event when clicked that can be handled by hook declared via `init/1`. """ attr(:id, :string, required: true) attr(:wrapped_trace, :map, required: true, doc: "The Trace to render") slot :label, required: true do attr(:class, :string, doc: "Additional class for label") end def trace(assigns) do assigns = assigns |> assign(:trace, assigns.wrapped_trace.trace) |> assign(:render_body?, assigns.wrapped_trace.render_body?) |> assign(:from_tracing?, assigns.wrapped_trace.from_tracing?) |> assign(:callback_name, Trace.callback_name(assigns.wrapped_trace.trace)) ~H""" <.collapsible id={@id} icon="icon-chevron-right" chevron_class="w-5 h-5 text-accent-icon" class="max-w-full border border-default-border rounded last:mb-4" label_class={[ "font-semibold bg-surface-1-bg p-2 py-3", @trace.exception && "border border-error-icon" ]} phx-click={if(@render_body?, do: nil, else: "toggle-collapsible")} phx-value-trace-id={@trace.id} > <:label>
<%= @content %>
""" end attr(:trace, :map, default: nil) def short_trace_content(assigns) do assigns = assign(assigns, :content, Enum.map_join(assigns.trace.args, " ", &inspect/1)) ~H"""