LiveView on_mount hook for loading dashboard contexts.
This module provides an on_mount hook that loads available contexts for the current user and sets socket assigns for the context selector.
Usage
Add to your live_session:
live_session :dashboard,
on_mount: [
{PhoenixKitWeb.Users.Auth, :phoenix_kit_ensure_authenticated_scope},
{PhoenixKitWeb.Dashboard.ContextProvider, :default}
]Assigns Set (Single Selector - Legacy)
@dashboard_contexts- List of all contexts available to the user@current_context- The currently selected context (or nil)@show_context_selector- Boolean, true only if user has 2+ contexts@context_selector_config- The ContextSelector config struct@current_contexts_map- Map with single entry%{key => current_context}for badge compatibility@dashboard_contexts_map- Map with single entry%{key => contexts}for consistency@show_context_selectors_map- Map with single entry%{key => show_selector}@dashboard_tabs- (Optional) List of Tab structs whentab_loaderis configured
Note: The key used in maps is config.key if set, otherwise :default.
This ensures context-aware badges work correctly with legacy single-selector configs.
Assigns Set (Multiple Selectors)
@dashboard_contexts_map- Map of key => list of contexts@current_contexts_map- Map of key => current context item@show_context_selectors_map- Map of key => boolean@context_selector_configs- List of all ContextSelector configs
Note: Legacy assigns are also set for backward compatibility when using multiple selectors. The first selector's data populates the legacy assigns.
Accessing in LiveViews
def mount(_params, _session, socket) do
# Single selector (legacy)
context = socket.assigns.current_context
# Multiple selectors
org = socket.assigns.current_contexts_map[:organization]
project = socket.assigns.current_contexts_map[:project]
if context do
items = MyApp.Items.list_for_context(context.id)
{:ok, assign(socket, items: items)}
else
{:ok, assign(socket, items: [])}
end
end
Summary
Functions
On mount hook that loads contexts and sets assigns.