Files
chess-endgame-trainer/lib/chesstrainer_web/components/theme_picker.ex
T
owenrees 69b89f68ef Add a public health probe and monitoring runbook (#166)
* move theme picker position in add endgames form

* Add branded favicons and web manifest

- Replace the placeholder favicon with the knight mark: .ico (16/32/48),
  SVG, and 16/32 PNGs.
- Add opaque apple-touch and Android launcher icons plus a site
  webmanifest naming the app and using the brand palette.
- Link the icon set, manifest, and theme-color from the root layout and
  the 404/500 pages.
- Whitelist the new static files in static_paths/0 and the prelaunch
  allowlist so they are served in production and behind the gate.
- Sync the coming-soon site's favicon.ico and drop the unused Phoenix
  logo.

* Add a public health probe and monitoring runbook

Probe https://<PHX_HOST>/health through Cloudflare every two minutes and
report to a healthchecks.io check. A healthy run posts the raw health JSON;
a tablebase mismatch is a green check-in with the mismatch recorded; a
non-200, non-JSON, down database or network failure posts one fail ping with
the response body and a loopback cross-check as evidence.

--dry-run evaluates the success, degraded, fail, non-JSON and unreachable
fixtures without reading config or touching the network. The runbook covers
the healthchecks.io check, install, what /health proves, Beszel's blind spot
and the drills.

* Fix theme picker search after moving it into the endgame form

The picker moved inside the endgame form, which nested the component's
own <form>. Browsers, and the LiveView test DOM, drop a nested form along
with its phx-change and phx-submit bindings, so the search box did nothing
and three form tests lost their #theme-form selector.

The component now renders no form: phx-change sits on the search input,
which works both inside the endgame form and standalone on the bulk
importer. The unreachable create_theme_from_search handlers go away; theme
creation uses the existing button. Tests search through the input and
click that button.
2026-11-13 13:24:35 +01:00

137 lines
3.8 KiB
Elixir
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
defmodule ChesstrainerWeb.ThemePicker do
@moduledoc """
Search-and-select control shared by the endgame form and bulk importer.
The parent LiveView owns the assignments and handles the events:
`search_themes`, `add_theme`, `create_theme` and `remove_theme`.
The control renders no `<form>` of its own. It is embedded in the endgame
form, where a nested form would be dropped by the HTML parser and take its
bindings with it, and it is used standalone on the bulk importer. Binding
`phx-change` on the search input keeps both call sites working.
"""
use ChesstrainerWeb, :html
alias Chesstrainer.Themes
attr :selected_themes, :list, required: true
attr :theme_query, :string, required: true
attr :theme_results, :list, required: true
attr :can_create, :boolean, required: true
def theme_picker(assigns) do
~H"""
<div class="mb-6">
<span class="mb-1 block text-sm font-medium text-base-content">Themes</span>
<div class="flex flex-wrap gap-2 mb-2">
<.badge :for={theme <- @selected_themes} color="primary">
{theme.name}
<button
type="button"
phx-click="remove_theme"
phx-value-id={theme.id}
aria-label={"Remove #{theme.name}"}
>
×
</button>
</.badge>
</div>
<div id="theme-picker">
<.input
name="theme_search"
value={@theme_query}
phx-change="search_themes"
phx-debounce="200"
autocomplete="off"
placeholder="Search or create a theme…"
/>
<ul>
<li :for={theme <- @theme_results}>
<button
type="button"
phx-click="add_theme"
phx-value-id={theme.id}
class="block w-full rounded px-3 py-2 text-left text-sm hover:bg-base-200"
>
{theme.name}
</button>
</li>
<li :if={@can_create}>
<button
type="button"
phx-click="create_theme"
phx-value-name={@theme_query}
class="block w-full rounded px-3 py-2 text-left text-sm hover:bg-base-200"
>
Create theme "{String.trim(@theme_query)}"
</button>
</li>
</ul>
</div>
</div>
"""
end
@doc """
Updates the search query, matching endgame themes and whether the query can be
created as a new theme.
"""
def assign_search(socket, query) do
trimmed = String.trim(query)
results = Themes.search(query, "endgame")
can_create =
trimmed != "" and
not Enum.any?(results, &(String.downcase(&1.name) == String.downcase(trimmed)))
socket
|> assign(:theme_query, query)
|> assign(:theme_results, results)
|> assign(:can_create, can_create)
end
@doc """
Creates a theme from the search box (or selects the existing match) and adds
it to the selected themes.
"""
def create_theme(socket, name) do
if String.trim(name) == "" do
socket
else
case Themes.get_or_create(name, "endgame") do
{:ok, theme} ->
socket |> add_selected_theme(theme) |> clear_search()
{:error, _reason} ->
Phoenix.LiveView.put_flash(socket, :error, "Could not create that theme.")
end
end
end
@doc """
Clears the theme search results.
"""
def clear_search(socket) do
socket
|> assign(:theme_query, "")
|> assign(:theme_results, [])
|> assign(:can_create, false)
end
@doc """
Adds a theme to the selected themes, ignoring duplicates.
"""
def add_selected_theme(socket, theme) do
if Enum.any?(socket.assigns.selected_themes, &(&1.id == theme.id)) do
socket
else
assign(socket, :selected_themes, socket.assigns.selected_themes ++ [theme])
end
end
end