Clarity.Content behaviour (Clarity v0.4.0)

View Source

Behavior and struct for content providers that display information about vertices.

Content providers decide whether they should be displayed for a given vertex and lens, and can provide either static content (markdown, mermaid, graphviz) or implement a full LiveView for interactive content.

Static Content Providers

Static content providers implement the render_static/2 callback:

defmodule MyApp.CustomContent do
  @behaviour Clarity.Content

  @impl Clarity.Content
  def name, do: "Custom Analysis"

  @impl Clarity.Content
  def description, do: "Provides custom analysis for resources"

  @impl Clarity.Content
  def applies?(%Vertex.Ash.Resource{}, _lens), do: true
  def applies?(_vertex, _lens), do: false

  @impl Clarity.Content
  def render_static(vertex, _lens) do
    {:markdown, "# Analysis for #{inspect(vertex)}"}
  end
end

LiveView Content Providers

Content providers can also be LiveView modules. Simply implement Phoenix.LiveView alongside the Clarity.Content behavior:

defmodule MyApp.InteractiveContent do
  use Phoenix.LiveView
  @behaviour Clarity.Content

  @impl Clarity.Content
  def name, do: "Interactive Dashboard"

  @impl Clarity.Content
  def description, do: "Interactive visualization"

  @impl Clarity.Content
  def applies?(%Vertex.Ash.Resource{}, _lens), do: true
  def applies?(_vertex, _lens), do: false

  @impl Phoenix.LiveView
  def mount(_params, session, socket) do
    vertex = session["vertex"]
    lens = session["lens"]
    {:ok, assign(socket, vertex: vertex, lens: lens)}
  end

  @impl Phoenix.LiveView
  def render(assigns) do
    ~H"""
    <div>Interactive content for {@vertex}</div>
    """
  end
end

Configuration

Content provider configuration is managed by Clarity.Config. See the documentation for Clarity.Config for detailed configuration options and examples.

Summary

Types

A module implementing the Clarity.Content behavior

t()

Content struct representing a content provider instance for a specific vertex and lens.

Callbacks

Determines whether this content should be displayed for the given vertex and lens.

Returns an optional description of this content provider.

Returns the name of this content provider.

Renders static content for the given vertex and lens.

Types

provider()

@type provider() :: module()

A module implementing the Clarity.Content behavior

rendered_static_content()

@type rendered_static_content() ::
  {static_content_type(), (static_content_props() -> iodata())}

static_content()

@type static_content() ::
  {static_content_type(), iodata() | (static_content_props() -> iodata())}

static_content_props()

@type static_content_props() :: %{theme: theme(), zoom_subgraph: Clarity.Graph.t()}

static_content_type()

@type static_content_type() :: :markdown | :mermaid | :viz

t()

@type t() :: %Clarity.Content{
  description: String.t() | nil,
  id: String.t(),
  live_component?: boolean(),
  live_view?: boolean(),
  name: String.t(),
  provider: provider(),
  render_static: rendered_static_content() | nil
}

Content struct representing a content provider instance for a specific vertex and lens.

theme()

@type theme() :: :light | :dark

Callbacks

applies?(vertex, lens)

@callback applies?(vertex :: Clarity.Vertex.t(), lens :: Clarity.Perspective.Lens.t()) ::
  boolean()

Determines whether this content should be displayed for the given vertex and lens.

Return true to show this content, false to hide it.

description()

@callback description() :: String.t() | nil

Returns an optional description of this content provider.

This may be used in tooltips or help text.

name()

@callback name() :: String.t()

Returns the name of this content provider.

This is displayed as the tab name in the UI.

render_static(vertex, lens)

(optional)
@callback render_static(
  vertex :: Clarity.Vertex.t(),
  lens :: Clarity.Perspective.Lens.t()
) ::
  static_content()

Renders static content for the given vertex and lens.

Returns a tuple of {type, content} where:

  • :markdown - Markdown text (iodata or function returning iodata)
  • :mermaid - Mermaid diagram (iodata or function returning iodata)
  • :viz - Graphviz DOT format (iodata or function returning iodata, or function taking theme map)