LiveView integration for dashboard tabs with real-time updates.
This module provides hooks and helpers for LiveViews to integrate with the dashboard tab system, including:
- Automatic tab subscription and updates
- Badge value synchronization via PubSub
- Presence tracking for viewer counts
- Group collapse state management
Usage in LiveViews
defmodule MyAppWeb.DashboardLive do
use MyAppWeb, :live_view
use PhoenixKitWeb.Components.Dashboard.LiveTabs
def mount(_params, _session, socket) do
socket =
socket
|> init_dashboard_tabs()
|> track_tab_presence(:my_tab)
{:ok, socket}
end
# Tabs automatically update when badges change
endManual Integration
If you prefer manual control:
def mount(_params, _session, socket) do
if connected?(socket) do
Phoenix.PubSub.subscribe(PhoenixKit.PubSub, PhoenixKit.Dashboard.pubsub_topic())
# Subscribe to badge topics for live updates
for tab <- PhoenixKit.Dashboard.get_tabs() do
if tab.badge && tab.badge.subscribe do
topic = PhoenixKit.Dashboard.Badge.get_topic(tab.badge)
Phoenix.PubSub.subscribe(PhoenixKit.PubSub, topic)
end
end
end
{:ok, assign(socket, :dashboard_tabs, PhoenixKit.Dashboard.get_tabs())}
end
def handle_info({:tab_updated, _tab}, socket) do
{:noreply, assign(socket, :dashboard_tabs, PhoenixKit.Dashboard.get_tabs())}
end
Summary
Functions
Use this module in a LiveView to get dashboard tab helpers.
Clears attention animation from a tab.
Gets the badge value for a tab, checking context-aware values first.
Initializes dashboard tabs in a LiveView socket.
Refreshes the dashboard tabs from the registry.
Reinitializes context-aware badges when context changes.
Sets attention animation on a tab.
Tracks the current user's presence on a specific tab.
Untracks the current user from their tracked tab.
Updates a context-aware badge value in socket assigns.
Updates a specific tab's badge and broadcasts the update.
Functions
Use this module in a LiveView to get dashboard tab helpers.
@spec clear_tab_attention(Phoenix.LiveView.Socket.t(), atom()) :: Phoenix.LiveView.Socket.t()
Clears attention animation from a tab.
Gets the badge value for a tab, checking context-aware values first.
For context-aware badges, returns the value from :context_badge_values.
For regular badges, returns the badge's stored value.
Examples
# In template
<%= get_badge_value(@context_badge_values, tab) %>
@spec init_dashboard_tabs( Phoenix.LiveView.Socket.t(), keyword() ) :: Phoenix.LiveView.Socket.t()
Initializes dashboard tabs in a LiveView socket.
This function:
- Loads tabs from the registry
- Subscribes to tab updates
- Subscribes to badge update topics (with context resolution for context-aware badges)
- Initializes viewer counts
- Loads initial values for context-aware badges
Options
:show_presence- Load and track presence counts (default: true):subscribe_badges- Subscribe to live badge topics (default: true)
Context-Aware Badges
For badges with context_key set, this function:
- Resolves topic placeholders using the current context from
current_contexts_map - Loads initial badge values using the badge's loader function
- Stores context badge values in
:context_badge_valuesassign
Examples
socket = init_dashboard_tabs(socket)
socket = init_dashboard_tabs(socket, show_presence: false)
@spec refresh_dashboard_tabs(Phoenix.LiveView.Socket.t()) :: Phoenix.LiveView.Socket.t()
Refreshes the dashboard tabs from the registry.
Call this when you need to force a refresh of tab data. Context-aware badge values are preserved during refresh.
@spec reinit_context_badges(Phoenix.LiveView.Socket.t()) :: Phoenix.LiveView.Socket.t()
Reinitializes context-aware badges when context changes.
Call this when the user switches context (e.g., selects a different organization). It will:
- Subscribe to new context-specific PubSub topics
- Load new badge values for the new context
- Merge updated values into dashboard tabs
Note: Old PubSub subscriptions are not explicitly removed. They will be cleaned up when the LiveView process terminates. If your context switch doesn't involve navigation, filter incoming PubSub messages by checking the current context.
Examples
def handle_info({:context_changed, :organization, new_org}, socket) do
socket =
socket
|> assign(:current_contexts_map, %{organization: new_org})
|> reinit_context_badges()
{:noreply, socket}
end
@spec set_tab_attention(Phoenix.LiveView.Socket.t(), atom(), atom()) :: Phoenix.LiveView.Socket.t()
Sets attention animation on a tab.
Examples
set_tab_attention(socket, :alerts, :pulse)
@spec track_tab_presence(Phoenix.LiveView.Socket.t(), atom(), keyword()) :: Phoenix.LiveView.Socket.t()
Tracks the current user's presence on a specific tab.
Call this in mount/3 to track which tab the user is viewing.
Examples
socket = track_tab_presence(socket, :orders)
socket = track_tab_presence(socket, :printers, meta: %{printer_id: 123})
@spec untrack_tab_presence(Phoenix.LiveView.Socket.t()) :: Phoenix.LiveView.Socket.t()
Untracks the current user from their tracked tab.
Call this when leaving a tab or on terminate.
@spec update_context_badge(Phoenix.LiveView.Socket.t(), atom(), any()) :: Phoenix.LiveView.Socket.t()
Updates a context-aware badge value in socket assigns.
Use this when handling PubSub messages for context-aware badges. Unlike
update_tab_badge/3 which updates global ETS, this updates the socket assigns
so each user sees their own value.
Examples
def handle_info({:alert_count_update, count}, socket) do
{:noreply, update_context_badge(socket, :alerts, count)}
end
@spec update_tab_badge(Phoenix.LiveView.Socket.t(), atom(), any()) :: Phoenix.LiveView.Socket.t()
Updates a specific tab's badge and broadcasts the update.
This is a convenience wrapper that updates the badge and triggers a broadcast to all connected LiveViews.
Examples
update_tab_badge(socket, :notifications, 5)
update_tab_badge(socket, :alerts, count: 3, color: :error)