Files
chess-endgame-trainer/lib/chesstrainer_web/live/admin/theme_live/index.ex
T
owenrees 80cb033077 Add theme slugs, kinds and link provenance (#192)
* add function for upgrading a user's plan in interactive Elixir

* Add theme slugs, kinds and link provenance

Themes gain a stable slug, an optional description and a kind so the app can
tell material, motif and technique labels apart. Slugs are generated from the
name on first save and keep their value when the theme is renamed, so links to
a theme stay valid.

Endgame-to-theme links now record their source (derived, curated or community)
and creation time. Existing links are backfilled as curated. A derived pass can
therefore replace only its own links, leaving human work untouched.

The admin index shows each theme's kind and its coverage across the library.
Creating a theme name requires the manage_themes capability, and members get an
unauthorized error instead, ahead of the suggestion flow.

Closes #185
2026-11-17 20:46:43 +01:00

168 lines
4.9 KiB
Elixir

defmodule ChesstrainerWeb.Admin.ThemeLive.Index do
use ChesstrainerWeb, :live_view
alias Chesstrainer.Accounts.Policy
alias Chesstrainer.Endgames
alias Chesstrainer.Themes
@category_options [
{"All categories", ""},
{"Endgame", "endgame"},
{"Opening", "opening"},
{"Tactic", "tactic"}
]
@impl true
def render(assigns) do
~H"""
<Layouts.app flash={@flash} wide>
<Layouts.admin_nav current_scope={@current_scope} />
<.header>
Themes
<:subtitle>
Themes group endgames for filtering and puzzle selection. Coverage shows how
many endgames carry a theme out of the whole library. Deleting a theme detaches
it from every endgame and leaves the endgames intact.
</:subtitle>
<:actions>
<.button variant="primary" navigate={~p"/admin/themes/new"}>
<.icon name="hero-plus" /> New theme
</.button>
</:actions>
</.header>
<.form
for={@filter_form}
id="theme-admin-filter-form"
phx-change="filter"
class="mb-6 flex flex-col gap-3 sm:flex-row sm:items-end"
>
<div class="flex-1 [&>div]:mb-0">
<.input
field={@filter_form[:search]}
type="text"
label="Search by name"
autocomplete="off"
phx-debounce="200"
placeholder="Theme name…"
/>
</div>
<div class="[&>div]:mb-0 sm:w-56">
<.input
field={@filter_form[:category]}
type="select"
label="Category"
options={@category_options}
/>
</div>
</.form>
<p :if={@themes == []} class="text-base-content/70">
<%= if @has_filters do %>
No themes match these filters.
<% else %>
No themes yet. Themes power endgame filtering and puzzle selection, so start by
creating one and attaching it to an endgame.
<% end %>
</p>
<table :if={@themes != []} class={table_class()}>
<thead>
<tr>
<th>Name</th>
<th>Kind</th>
<th>Category</th>
<th>Coverage</th>
<th><span class="sr-only">Actions</span></th>
</tr>
</thead>
<tbody id="themes">
<tr :for={{theme, usage_count} <- @themes} id={"theme-#{theme.id}"}>
<td>{theme.name}</td>
<td>{theme.kind}</td>
<td>{theme.category}</td>
<td>
<span id={"theme-usage-#{theme.id}"}>
{coverage_label(usage_count, @total_endgames)}
</span>
</td>
<td class="w-0 font-semibold">
<div class="flex gap-4">
<.link navigate={~p"/admin/themes/#{theme.id}/edit"}>Edit</.link>
<.link
phx-click="delete"
phx-value-id={theme.id}
data-confirm={
"Delete #{theme.name}? Used by #{coverage_label(usage_count, @total_endgames)}. " <>
"The endgames will be kept and simply detached."
}
>
Delete
</.link>
</div>
</td>
</tr>
</tbody>
</table>
</Layouts.app>
"""
end
@impl true
def mount(_params, _session, socket) do
{:ok,
socket
|> assign(:page_title, "Themes")
|> assign(:search, "")
|> assign(:category, "")
|> assign(:has_filters, false)
|> assign(:category_options, @category_options)
|> assign(:filter_form, to_form(%{"search" => "", "category" => ""}))
|> load_themes()}
end
@impl true
def handle_event("filter", params, socket) do
search = Map.get(params, "search", socket.assigns.search)
category = Map.get(params, "category", socket.assigns.category)
{:noreply,
socket
|> assign(:search, search)
|> assign(:category, category)
|> assign(:has_filters, String.trim(search) != "" or category != "")
|> load_themes()}
end
def handle_event("delete", %{"id" => id}, socket) do
if Policy.can?(socket.assigns.current_scope, :manage_themes) do
theme = Themes.get_theme!(id)
{:ok, _theme} = Themes.delete_theme(theme)
socket =
socket
|> put_flash(:info, "Theme deleted. Attached endgames were kept.")
|> load_themes()
{:noreply, socket}
else
{:noreply, put_flash(socket, :error, "You are not authorized to manage themes.")}
end
end
defp load_themes(socket) do
themes =
Themes.list_themes_for_admin(
search: socket.assigns.search,
category: socket.assigns.category
)
assign(socket, themes: themes, total_endgames: Endgames.count_endgames())
end
defp coverage_label(count, total) do
"#{count} of #{total} #{if total == 1, do: "endgame", else: "endgames"}"
end
end