mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
* Show rating changes in flashes and session UI (#146) - Flash the new rating and delta on every rated completion, with a dedicated success flash kind for solved puzzles - Move flash notifications to the top-left - Add a colour-coded rating-change pill and show the current rating with the session's net change in the train session bar - Show the user's rating and change beside the puzzle rating after an attempt, and show the finishing rating in the last-session summary * fix header component and start reusing it * Hide the sign-up link on the prelaunch admin login The header refactor dropped the @prelaunch guard, so the gated admin login linked to the public registration page again.
This commit is contained in:
@@ -65,7 +65,7 @@ defmodule ChesstrainerWeb.CoreComponents do
|
||||
attr :id, :string, doc: "the optional id of flash container"
|
||||
attr :flash, :map, default: %{}, doc: "the map of flash messages to display"
|
||||
attr :title, :string, default: nil
|
||||
attr :kind, :atom, values: [:info, :error], doc: "used for styling and flash lookup"
|
||||
attr :kind, :atom, values: [:info, :success, :error], doc: "used for styling and flash lookup"
|
||||
attr :rest, :global, doc: "the arbitrary HTML attributes to add to the flash container"
|
||||
|
||||
slot :inner_block, doc: "the optional inner block that renders the flash message"
|
||||
@@ -86,9 +86,11 @@ defmodule ChesstrainerWeb.CoreComponents do
|
||||
<div class={[
|
||||
"flex w-full items-start gap-3 rounded-lg border px-4 py-3 text-sm text-wrap shadow-lg",
|
||||
@kind == :info && "border-info bg-info text-info-content",
|
||||
@kind == :success && "border-accent bg-accent text-accent-content",
|
||||
@kind == :error && "border-error bg-error text-error-content"
|
||||
]}>
|
||||
<.icon :if={@kind == :info} name="hero-information-circle" class="size-5 shrink-0" />
|
||||
<.icon :if={@kind == :success} name="hero-check-circle" class="size-5 shrink-0" />
|
||||
<.icon :if={@kind == :error} name="hero-exclamation-circle" class="size-5 shrink-0" />
|
||||
<div>
|
||||
<p :if={@title} class="font-semibold">{@title}</p>
|
||||
@@ -356,10 +358,10 @@ defmodule ChesstrainerWeb.CoreComponents do
|
||||
~H"""
|
||||
<header class={[@actions != [] && "flex items-center justify-between gap-6", "pb-4"]}>
|
||||
<div>
|
||||
<h1 class="text-lg font-semibold leading-8">
|
||||
<h1 class="settings-header">
|
||||
{render_slot(@inner_block)}
|
||||
</h1>
|
||||
<p :if={@subtitle != []} class="text-sm text-base-content/70 mt-4">
|
||||
<p :if={@subtitle != []} class="settings-subtitle">
|
||||
{render_slot(@subtitle)}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
@@ -247,9 +247,10 @@ defmodule ChesstrainerWeb.Layouts do
|
||||
<div
|
||||
id={@id}
|
||||
aria-live="polite"
|
||||
class="pointer-events-none fixed right-4 top-4 z-50 flex w-80 max-w-80 flex-col gap-2 sm:w-96 sm:max-w-96"
|
||||
class="pointer-events-none fixed left-4 top-4 z-50 flex w-80 max-w-80 flex-col gap-2 sm:w-96 sm:max-w-96"
|
||||
>
|
||||
<.flash kind={:info} flash={@flash} />
|
||||
<.flash kind={:success} flash={@flash} />
|
||||
<.flash kind={:error} flash={@flash} />
|
||||
|
||||
<.flash
|
||||
|
||||
@@ -30,6 +30,8 @@ defmodule ChesstrainerWeb.PlayComponents do
|
||||
attr :move_input_error, :string, default: nil
|
||||
attr :last_check, :boolean, default: false
|
||||
attr :last_checkmate, :boolean, default: false
|
||||
attr :rating, :any, default: nil
|
||||
attr :rating_delta, :any, default: nil
|
||||
|
||||
def play_surface(assigns) do
|
||||
assigns =
|
||||
@@ -250,9 +252,17 @@ defmodule ChesstrainerWeb.PlayComponents do
|
||||
<p class="text-muted font-bold">Make a move.</p>
|
||||
<p class="text-muted">You play {colour_name(@endgame.color)}</p>
|
||||
<% end %>
|
||||
<p :if={@status in [:solved, :failed]} class="mt-1 text-xs text-muted">
|
||||
Puzzle rating: {round(@endgame.rating)}
|
||||
</p>
|
||||
<div
|
||||
:if={@status in [:solved, :failed]}
|
||||
class="mt-1 flex flex-wrap items-center gap-x-3 gap-y-1 text-xs"
|
||||
>
|
||||
<span class="text-muted">Puzzle rating: {round(@endgame.rating)}</span>
|
||||
<span :if={@rating} id="your-rating" class="flex items-center gap-1.5 text-muted">
|
||||
Your rating
|
||||
<span class="font-mono font-semibold text-base-content">{round(@rating)}</span>
|
||||
<.rating_change :if={@rating_delta} id="rating-change" delta={@rating_delta} />
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div
|
||||
|
||||
@@ -10,6 +10,8 @@ defmodule ChesstrainerWeb.RatingComponents do
|
||||
|
||||
use Phoenix.Component
|
||||
|
||||
import ChesstrainerWeb.CoreComponents, only: [icon: 1]
|
||||
|
||||
# Glicko ratings are conventionally shown on a 0–3000 scale. The default
|
||||
# rating is 1500 and the deviation is capped at 350, so 500–3000 keeps the
|
||||
# useful range readable without letting a stray value push the marker off.
|
||||
@@ -78,6 +80,56 @@ defmodule ChesstrainerWeb.RatingComponents do
|
||||
"""
|
||||
end
|
||||
|
||||
@doc """
|
||||
Renders a signed rating change as a compact, colour-coded pill.
|
||||
|
||||
A gain is green with an upward trend, a loss is red with a downward trend,
|
||||
and no change is neutral.
|
||||
"""
|
||||
attr :delta, :any, required: true
|
||||
attr :id, :string, default: nil
|
||||
attr :class, :any, default: nil
|
||||
|
||||
def rating_change(assigns) do
|
||||
~H"""
|
||||
<span
|
||||
id={@id}
|
||||
class={[
|
||||
"inline-flex items-center gap-0.5 rounded-full px-1.5 py-0.5 font-mono text-xs font-semibold",
|
||||
change_class(@delta),
|
||||
@class
|
||||
]}
|
||||
>
|
||||
<.icon name={change_icon(@delta)} class="size-3.5" />
|
||||
{format_delta(@delta)}
|
||||
</span>
|
||||
"""
|
||||
end
|
||||
|
||||
@doc "Formats a rating change with an explicit sign, e.g. `+12`, `-8` or `0`."
|
||||
def format_delta(nil), do: "—"
|
||||
|
||||
def format_delta(delta) when is_number(delta) do
|
||||
rounded = round(delta)
|
||||
|
||||
cond do
|
||||
rounded > 0 -> "+#{rounded}"
|
||||
true -> Integer.to_string(rounded)
|
||||
end
|
||||
end
|
||||
|
||||
defp change_class(delta) when is_number(delta) and delta > 0,
|
||||
do: "bg-success/15 text-success-text"
|
||||
|
||||
defp change_class(delta) when is_number(delta) and delta < 0,
|
||||
do: "bg-error/15 text-error-text"
|
||||
|
||||
defp change_class(_delta), do: "bg-base-200 text-muted"
|
||||
|
||||
defp change_icon(delta) when is_number(delta) and delta > 0, do: "hero-arrow-trending-up"
|
||||
defp change_icon(delta) when is_number(delta) and delta < 0, do: "hero-arrow-trending-down"
|
||||
defp change_icon(_delta), do: "hero-minus"
|
||||
|
||||
# Position on the gauge as a percentage, clamped to the drawn scale.
|
||||
defp pct(value) do
|
||||
clamped = value |> max(@scale_min) |> min(@scale_max)
|
||||
|
||||
Reference in New Issue
Block a user