defmodule PlausibleWeb.Components.Site.Feature do
@moduledoc """
Phoenix Component for rendering a user-facing feature toggle
capable of flipping booleans in `Plausible.Site` via the `toggle_feature` controller action.
"""
use PlausibleWeb, :view
attr :site, Plausible.Site, required: true
attr :setting, :atom, required: true
attr :label, :string, required: true
attr :conn, Plug.Conn, required: true
slot :inner_block
def toggle(assigns) do
~H"""
<%= if Map.fetch!(@site, @setting) do %>
<.button_active to={target(@site, @setting, @conn, false)} />
<% else %>
<.button_inactive to={target(@site, @setting, @conn, true)} />
<% end %>
<%= @label %>
<%= render_slot(@inner_block) %>
"""
end
def target(site, setting, conn, set_to) when is_boolean(set_to) do
r = conn.request_path
Routes.site_path(conn, :update_feature_visibility, site.domain, setting, r: r, set: set_to)
end
def button_active(assigns) do
~H"""
<%= button(to: @to, method: :put, class: "bg-indigo-600 relative inline-flex flex-shrink-0 h-6 w-11 border-2 border-transparent rounded-full cursor-pointer transition-colors ease-in-out duration-200 focus:outline-none focus:ring") do %>
<% end %>
"""
end
def button_inactive(assigns) do
~H"""
<%= button(to: @to, method: :put, class: "bg-gray-200 dark:bg-gray-700 relative inline-flex flex-shrink-0 h-6 w-11 border-2 border-transparent rounded-full cursor-pointer transition-colors ease-in-out duration-200 focus:outline-none focus:ring") do %>
<% end %>
"""
end
end