defmodule Observer.Web.SettingsComponent do
@moduledoc """
This module provides User component
"""
use Observer.Web, :live_component
alias Observer.Web.Components.Icons
@impl Phoenix.LiveComponent
def render(assigns) do
~H"""
Version mismatch across nodes
This node runs v{@version.local}.
Highlighted nodes run a different version.
{node}
v{version}
<.option
:for={theme <- ~w(light dark system)}
myself={@myself}
theme={@theme}
value={theme}
/>
"""
end
# Nodes running a version other than the local one come first, so the
# divergent nodes are visible without scrolling.
defp sorted_versions(%{nodes: nodes, local: local}) do
Enum.sort_by(nodes, fn {node, version} -> {version == local, to_string(node)} end)
end
attr :myself, :any, required: true
attr :theme, :string, required: true
attr :value, :string, required: true
defp option(assigns) do
class =
if assigns.theme == assigns.value do
"text-blue-500 dark:text-blue-400"
else
"text-gray-500 dark:text-gray-400 "
end
assigns = assign(assigns, :class, class)
~H"""
<.theme_icon theme={@value} />
{@value}
"""
end
attr :theme, :string, required: true
defp theme_icon(assigns) do
~H"""
<%= case @theme do %>
<% "light" -> %>
<% "dark" -> %>
<% "system" -> %>
<% end %>
"""
end
@impl Phoenix.LiveComponent
def handle_event("update-theme", %{"theme" => theme}, socket) do
send(self(), {:update_theme, theme})
{:noreply, socket}
end
def handle_event("cycle-theme", _params, socket) do
theme =
case socket.assigns.theme do
"light" -> "dark"
"dark" -> "system"
"system" -> "light"
end
send(self(), {:update_theme, theme})
{:noreply, socket}
end
end