Skip to content

feat(clarity): Clarity content provider for state diagrams#146

Merged
zachdaniel merged 1 commit into
ash-project:mainfrom
joshprice:clarity-integration
Apr 22, 2026
Merged

feat(clarity): Clarity content provider for state diagrams#146
zachdaniel merged 1 commit into
ash-project:mainfrom
joshprice:clarity-integration

Conversation

@joshprice
Copy link
Copy Markdown
Contributor

Summary

Adds a Clarity content provider that surfaces a live Mermaid state diagram on any Ash resource vertex whose module uses AshStateMachine. Same Mermaid as mix ash_state_machine.generate_flow_charts, but rendered in the Clarity UI directly from the DSL — no need to regenerate .mmd files.

Once merged, a consumer with both ash_state_machine and clarity in their deps automatically gets a State Machine tab alongside Resource Overview, Policies, Actions, etc. — zero config.

How it works

  1. lib/clarity/state_machine_diagram.ex defines AshStateMachine.Clarity.StateMachineDiagram, guarded by with {:module, _} <- Code.ensure_loaded(Clarity.Content) so the module is only compiled when Clarity is present.
  2. mix.exs registers it via application/0:
    ```elixir
    env: [clarity_content_providers: [AshStateMachine.Clarity.StateMachineDiagram]]
    ```
    Clarity's Clarity.Config.list_content_providers/0 walks every loaded app's env and picks it up automatically.
  3. {:clarity, \"~> 0.3\", optional: true} added to deps — follows the same convention ash_diagram uses.

Diagram rendering delegates to AshStateMachine.Charts.mermaid_state_diagram/1, so the visual is guaranteed to match the DSL.

Rationale

Cross-cutting Ash diagrams (ER, class, C4) belong in ash_diagram. Extension-specific diagrams like state-machine flow are owned most naturally by the extension itself — the DSL and the diagram are one code change away from each other, versions move together, and no central-library coordination is required as the Ash ecosystem grows. Module name uses AshStateMachine.Clarity.* (rather than ClarityContent.*) to leave room for future Clarity additions (vertex types, introspectors) under the same namespace.

Test plan

  • mix compile — passes
  • mix test — 19 existing tests still green
  • Verified locally in a demo project: when {:clarity, ...} is added, /clarity shows a "State Machine" tab on any resource that uses AshStateMachine, rendering the correct Mermaid diagram
  • Verified the provider's applies?/2 correctly skips resources that don't use AshStateMachine
  • CI

Notes

  • Clarity 0.4.0's published igniter constraint (~> 0.6.25) excludes the igniter 0.7.x currently in mix.lock. mix deps.get after deleting the lock resolved cleanly; CI should confirm. If it doesn't, happy to pin clarity to ~> 0.4 or adjust the igniter constraint — whichever you prefer.
  • Documentation: added a brief "Clarity integration" section to documentation/topics/charts.md.

Adds a Clarity content provider that renders a live Mermaid state
diagram on any Ash resource vertex whose module uses AshStateMachine.

The provider is only compiled when `clarity` is a dependency of the
host project (guarded via `Code.ensure_loaded?(Clarity.Content)`) and
is registered via this library's `application/0` environment under
`:clarity_content_providers` — Clarity discovers it automatically.

Diagram output comes from `AshStateMachine.Charts.mermaid_state_diagram/1`,
the same function behind `mix ash_state_machine.generate_flow_charts`,
so the diagram stays in lock-step with the DSL.

Adds `{:clarity, "~> 0.3", optional: true}` to deps.
@zachdaniel zachdaniel merged commit 687de84 into ash-project:main Apr 22, 2026
23 checks passed
@zachdaniel
Copy link
Copy Markdown
Contributor

🚀 Thank you for your contribution! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants