PhiaUi.Components.Collab.CollabHelpers (phia_ui v0.1.17)

Copy Markdown View Source

Shared helper functions for collaboration components.

Provides utilities for time formatting, user display, color assignment, mention rendering, and text truncation used across the Collab Suite.

Summary

Functions

Deterministic OKLCH color from a user_id hash for consistent color assignment.

Formats an @mention as a display string.

Formats a DateTime as a relative time string.

Truncates text to max_length characters with ellipsis appended.

Returns initials from a user name (up to 2 characters).

Functions

collab_color(user_id)

@spec collab_color(String.t() | any()) :: String.t()

Deterministic OKLCH color from a user_id hash for consistent color assignment.

Uses :erlang.phash2/2 to generate a hue value (0-359) from the user_id string, producing the same color for the same user across sessions.

Examples

iex> collab_color("user-1")
"oklch(0.7 0.15 42)"  # deterministic hue from hash

format_mention(name)

@spec format_mention(String.t()) :: String.t()

Formats an @mention as a display string.

Examples

iex> format_mention("Alice")
"@Alice"

format_relative_time(datetime)

@spec format_relative_time(DateTime.t() | nil) :: String.t()

Formats a DateTime as a relative time string.

Returns human-readable strings like "just now", "2 minutes ago", "Yesterday", "3 days ago", or a formatted date for older timestamps.

Examples

iex> format_relative_time(nil)
""

iex> format_relative_time(~U[2026-03-19 12:00:00Z])
"just now"  # if called within 5 seconds

truncate_preview(text, max)

@spec truncate_preview(String.t() | nil, non_neg_integer()) :: String.t()

Truncates text to max_length characters with ellipsis appended.

Returns the original text if it is shorter than or equal to max_length.

Examples

iex> truncate_preview("Hello world", 5)
"Hello..."

iex> truncate_preview("Hi", 10)
"Hi"

iex> truncate_preview(nil, 10)
""

user_initials(name)

@spec user_initials(String.t() | nil) :: String.t()

Returns initials from a user name (up to 2 characters).

Splits the name on whitespace, takes the first letter of the first two words, and uppercases them.

Examples

iex> user_initials("Jane Doe")
"JD"

iex> user_initials("alice")
"A"

iex> user_initials(nil)
"?"