mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 03:32:06 +00:00
428 lines
14 KiB
Elixir
428 lines
14 KiB
Elixir
defmodule ChesstrainerWeb.Layouts do
|
|
@moduledoc """
|
|
This module holds layouts and related functionality
|
|
used by your application.
|
|
"""
|
|
use ChesstrainerWeb, :html
|
|
|
|
alias Chesstrainer.Accounts.Policy
|
|
alias Chesstrainer.Accounts.Role
|
|
alias Chesstrainer.Accounts.Scope
|
|
|
|
# Embed all files in layouts/* within this module.
|
|
# The default root.html.heex file contains the HTML
|
|
# skeleton of your application, namely HTML headers
|
|
# and other static content.
|
|
embed_templates "layouts/*"
|
|
|
|
@doc """
|
|
Renders your app layout.
|
|
|
|
This function is typically invoked from every template,
|
|
and it often contains your application menu, sidebar,
|
|
or similar.
|
|
|
|
## Examples
|
|
|
|
<Layouts.app flash={@flash}>
|
|
<h1>Content</h1>
|
|
</Layouts.app>
|
|
|
|
"""
|
|
attr :flash, :map, required: true, doc: "the map of flash messages"
|
|
|
|
attr :current_scope, :map,
|
|
default: nil,
|
|
doc: "the current [scope](https://phoenix.hexdocs.pm/scopes.html)"
|
|
|
|
attr :wide, :boolean,
|
|
default: false,
|
|
doc: "use a wide container instead of the default narrow column"
|
|
|
|
slot :inner_block, required: true
|
|
|
|
def app(assigns) do
|
|
~H"""
|
|
<main id="main-content" tabindex="-1" class="px-4 py-20 sm:px-6 lg:px-8">
|
|
<div class={["mx-auto space-y-4", if(@wide, do: "max-w-7xl", else: "max-w-2xl")]}>
|
|
{render_slot(@inner_block)}
|
|
</div>
|
|
</main>
|
|
<.flash_group flash={@flash} />
|
|
"""
|
|
end
|
|
|
|
@doc """
|
|
Renders the application navbar.
|
|
|
|
The marketing and account links are defined once in `nav_links/1` and rendered
|
|
in both the desktop bar and the mobile menu. Edit that list to change what
|
|
appears in either place.
|
|
"""
|
|
attr :current_scope, :map,
|
|
default: nil,
|
|
doc: "the current [scope](https://phoenix.hexdocs.pm/scopes.html)"
|
|
|
|
def navbar(assigns) do
|
|
user = signed_in_user(assigns.current_scope)
|
|
nav_links = nav_links(assigns.current_scope)
|
|
prelaunch = ChesstrainerWeb.prelaunch?()
|
|
|
|
assigns =
|
|
assigns
|
|
|> assign(:nav_links, nav_links)
|
|
|> assign(:start_href, start_href(assigns.current_scope))
|
|
|> assign(:user, user)
|
|
|> assign(:prelaunch, prelaunch)
|
|
|> assign(:show_nav, not prelaunch or not is_nil(user) or nav_links != [])
|
|
|
|
~H"""
|
|
<header>
|
|
<nav class="nav-wrap" aria-label="Main navigation">
|
|
<a class="brand" href="/" aria-label={"#{Branding.name()} home"}>
|
|
<span class="brand-mark">♞</span>
|
|
<span>{Branding.wordmark()}</span>
|
|
</a>
|
|
|
|
<div class="nav-right">
|
|
<.colour_mode_toggle />
|
|
|
|
<%= if @show_nav do %>
|
|
<button
|
|
class="menu-button"
|
|
type="button"
|
|
aria-expanded="false"
|
|
aria-controls="nav-links"
|
|
aria-label="Toggle menu"
|
|
>
|
|
<span></span>
|
|
<span></span>
|
|
</button>
|
|
|
|
<div class="nav-links" id="nav-links">
|
|
<%= for link <- @nav_links do %>
|
|
<.link href={link.href}>{link.label}</.link>
|
|
<% end %>
|
|
|
|
<%= if @user do %>
|
|
<details class="nav-user-menu">
|
|
<summary>{@user.username}</summary>
|
|
<div class="nav-user-menu-panel">
|
|
<.link navigate={~p"/users/profile"}>Account</.link>
|
|
<.link navigate={~p"/users/profile/settings"}>Settings</.link>
|
|
<.link navigate={~p"/users/profile/data"}>Data</.link>
|
|
<.link href={~p"/users/log-out"} method="delete">Log out</.link>
|
|
</div>
|
|
</details>
|
|
<% else %>
|
|
<%= if !@prelaunch do %>
|
|
<.link href={~p"/users/log-in"}>Log in</.link>
|
|
<.link href={~p"/users/register"}>Register</.link>
|
|
<% end %>
|
|
<% end %>
|
|
|
|
<%= if !@prelaunch do %>
|
|
<.pill_button variant={:nav} href={@start_href}>
|
|
Start training <.icon_arrow />
|
|
</.pill_button>
|
|
<% end %>
|
|
</div>
|
|
<% end %>
|
|
|
|
<.discord_link />
|
|
</div>
|
|
</nav>
|
|
</header>
|
|
"""
|
|
end
|
|
|
|
@doc """
|
|
Renders the shared sub-navigation for admin pages.
|
|
|
|
Items are filtered by capability, so the same component is safe to render on
|
|
every admin screen.
|
|
"""
|
|
attr :current_scope, :map, default: nil
|
|
|
|
def admin_nav(assigns) do
|
|
links =
|
|
for {capability, label, href} <- [
|
|
{:manage_endgames, "Endgames", ~p"/admin/endgames"},
|
|
{:manage_themes, "Themes", ~p"/admin/themes"},
|
|
{:manage_users, "Users", ~p"/admin/users"}
|
|
],
|
|
Policy.can?(assigns.current_scope, capability),
|
|
do: %{label: label, href: href}
|
|
|
|
assigns = assign(assigns, :links, links)
|
|
|
|
~H"""
|
|
<nav
|
|
aria-label="Admin"
|
|
class="mb-6 flex flex-wrap items-center gap-1 border-b border-base-200 pb-3 text-sm font-semibold"
|
|
>
|
|
<.link
|
|
:for={link <- @links}
|
|
navigate={link.href}
|
|
class="rounded-md px-3 py-1.5 text-base-content/70 hover:bg-base-200 hover:text-base-content"
|
|
>
|
|
{link.label}
|
|
</.link>
|
|
</nav>
|
|
"""
|
|
end
|
|
|
|
defp nav_links(scope) do
|
|
marketing =
|
|
if Scope.premium?(scope) do
|
|
[
|
|
%{label: "Puzzle Search", href: "/endgames"}
|
|
]
|
|
else
|
|
[]
|
|
end
|
|
|
|
admin =
|
|
if admin?(scope) do
|
|
[%{label: "Admin", href: ~p"/admin/endgames"}]
|
|
else
|
|
[]
|
|
end
|
|
|
|
marketing ++ admin
|
|
end
|
|
|
|
defp signed_in_user(%{user: %{username: _} = user}), do: user
|
|
defp signed_in_user(_scope), do: nil
|
|
|
|
defp admin?(%{user: %{role: role}}), do: Role.admin?(role)
|
|
defp admin?(_scope), do: false
|
|
|
|
# Signed-in users start a puzzle session; visitors browse the public list.
|
|
defp start_href(%{user: %{}}), do: ~p"/train"
|
|
defp start_href(_scope), do: ~p"/guest-train"
|
|
|
|
@doc """
|
|
Renders the site-wide footer.
|
|
|
|
Rendered once from the root layout so it appears on every page, matching the
|
|
navbar. Links to the landing-page anchors keep working from other pages
|
|
because they point at `/` first.
|
|
"""
|
|
def footer(assigns) do
|
|
assigns = assign(assigns, :year, DateTime.utc_now().year)
|
|
|
|
~H"""
|
|
<footer class="site-footer">
|
|
<div class="footer-brand">
|
|
<a class="brand" href="/" aria-label={"#{Branding.name()} home"}>
|
|
<span class="brand-mark">♞</span>
|
|
<span>{Branding.wordmark()}</span>
|
|
</a>
|
|
<p>Short, focused endgame training.</p>
|
|
</div>
|
|
|
|
<nav class="footer-links" aria-label="Footer">
|
|
<a href="/privacy">Privacy Policy</a>
|
|
<a href="/terms-conditions">Terms & Conditions</a>
|
|
<a href="/mentions-legales">Mentions légales</a>
|
|
<a href="/sitemap.xml">Sitemap</a>
|
|
<.discord_link />
|
|
</nav>
|
|
|
|
<small>© {@year} {Branding.name()}</small>
|
|
</footer>
|
|
"""
|
|
end
|
|
|
|
@doc """
|
|
Renders the Discord invite as an icon-only link.
|
|
|
|
The glyph is decorative; the `aria-label` carries the link's purpose for
|
|
screen readers. Renders nothing when no invite is configured, so environments
|
|
without one never show a dead link.
|
|
"""
|
|
def discord_link(assigns) do
|
|
assigns = assign(assigns, :href, Branding.discord_invite_url())
|
|
|
|
~H"""
|
|
<a
|
|
:if={@href}
|
|
class="discord-link"
|
|
href={@href}
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
aria-label={"Join the #{Branding.name()} Discord"}
|
|
>
|
|
<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false">
|
|
<path
|
|
fill="currentColor"
|
|
d="M20.317 4.3698a19.7913 19.7913 0 0 0-4.8851-1.5152.0741.0741 0 0 0-.0785.0371c-.211.3753-.4447.8648-.6083 1.2495-1.8447-.2762-3.68-.2762-5.4868 0-.1636-.3933-.4058-.8742-.6177-1.2495a.077.077 0 0 0-.0785-.037 19.7363 19.7363 0 0 0-4.8852 1.515.0699.0699 0 0 0-.0321.0277C.5334 9.0458-.319 13.5799.0992 18.0578a.0824.0824 0 0 0 .0312.0561c2.0528 1.5076 4.0413 2.4228 5.9929 3.0294a.0777.0777 0 0 0 .0842-.0276c.4616-.6304.8731-1.2952 1.226-1.9942a.076.076 0 0 0-.0416-.1057c-.6528-.2476-1.2743-.5495-1.8722-.8923a.077.077 0 0 1-.0076-.1277c.1258-.0943.2517-.1923.3718-.2914a.0743.0743 0 0 1 .0776-.0105c3.9278 1.7933 8.18 1.7933 12.0614 0a.0739.0739 0 0 1 .0785.0105c.1202.0991.246.1971.3728.2914a.077.077 0 0 1-.0066.1277c-.5986.3428-1.2201.6447-1.8739.8923a.0766.0766 0 0 0-.0407.1057c.36.699.7715 1.3647 1.225 1.9942a.076.076 0 0 0 .0843.0276c1.9608-.6066 3.9493-1.5218 6.0021-3.0294a.077.077 0 0 0 .0322-.0561c.5008-5.1763-.8398-9.6746-3.5543-13.6607a.061.061 0 0 0-.0312-.0286zM8.02 15.3312c-1.1825 0-2.1569-1.0857-2.1569-2.419 0-1.3332.9555-2.4189 2.157-2.4189 1.2108 0 2.1757 1.0952 2.1568 2.419 0 1.3332-.9555 2.4189-2.1569 2.4189zm7.9748 0c-1.1825 0-2.1569-1.0857-2.1569-2.419 0-1.3332.9554-2.4189 2.1569-2.4189 1.2108 0 2.1757 1.0952 2.1568 2.419 0 1.3332-.946 2.4189-2.1568 2.4189Z"
|
|
/>
|
|
</svg>
|
|
</a>
|
|
"""
|
|
end
|
|
|
|
@doc """
|
|
Shows the flash group with standard titles and content.
|
|
|
|
## Examples
|
|
|
|
<.flash_group flash={@flash} />
|
|
"""
|
|
attr :flash, :map, required: true, doc: "the map of flash messages"
|
|
attr :id, :string, default: "flash-group", doc: "the optional id of flash container"
|
|
|
|
def flash_group(assigns) do
|
|
~H"""
|
|
<div
|
|
id={@id}
|
|
aria-live="polite"
|
|
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
|
|
id="client-error"
|
|
kind={:error}
|
|
title={gettext("We can't find the internet")}
|
|
phx-disconnected={
|
|
show(".phx-client-error #client-error")
|
|
|> JS.remove_attribute("hidden", to: ".phx-client-error #client-error")
|
|
}
|
|
phx-connected={hide("#client-error") |> JS.set_attribute({"hidden", ""})}
|
|
hidden
|
|
>
|
|
{gettext("Attempting to reconnect")}
|
|
<.icon name="hero-arrow-path" class="ml-1 size-3 motion-safe:animate-spin" />
|
|
</.flash>
|
|
|
|
<.flash
|
|
id="server-error"
|
|
kind={:error}
|
|
title={gettext("Something went wrong!")}
|
|
phx-disconnected={
|
|
show(".phx-server-error #server-error")
|
|
|> JS.remove_attribute("hidden", to: ".phx-server-error #server-error")
|
|
}
|
|
phx-connected={hide("#server-error") |> JS.set_attribute({"hidden", ""})}
|
|
hidden
|
|
>
|
|
{gettext("Attempting to reconnect")}
|
|
<.icon name="hero-arrow-path" class="ml-1 size-3 motion-safe:animate-spin" />
|
|
</.flash>
|
|
|
|
<div
|
|
id="connection-actions"
|
|
hidden
|
|
phx-disconnected={show("#connection-actions")}
|
|
phx-connected={hide("#connection-actions")}
|
|
class="pointer-events-auto flex items-center gap-3 rounded-lg border border-base-300 bg-base-100 px-4 py-2 text-sm shadow-lg"
|
|
>
|
|
<a href="" class="font-semibold text-primary-text hover:underline">Reload</a>
|
|
<.link href={~p"/"} class="text-muted hover:underline">Home</.link>
|
|
</div>
|
|
</div>
|
|
"""
|
|
end
|
|
|
|
@doc """
|
|
Provides the light / dark colour-mode switch.
|
|
|
|
With no saved preference the page follows the OS setting; only the explicit
|
|
light and dark modes are selectable here.
|
|
|
|
The click handling and `aria-checked` syncing live in `theme_script.html.heex`,
|
|
which runs before paint and also on pages without LiveView (the landing page
|
|
uses the marketing bundle). Error pages include it explicitly because they do
|
|
not render through the root layout.
|
|
"""
|
|
def colour_mode_toggle(assigns) do
|
|
~H"""
|
|
<div
|
|
class="relative flex flex-row items-center rounded-full border-2 border-base-300 bg-base-300"
|
|
role="radiogroup"
|
|
aria-label="Colour mode"
|
|
>
|
|
<div
|
|
class="absolute w-1/2 h-full rounded-full border border-base-200 bg-base-100 brightness-200 left-0 [[data-theme=dark]_&]:left-1/2 transition-[left]"
|
|
aria-hidden="true"
|
|
>
|
|
</div>
|
|
|
|
<button
|
|
type="button"
|
|
class="flex p-2 cursor-pointer w-1/2 justify-center rounded-full focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary"
|
|
data-phx-theme="light"
|
|
role="radio"
|
|
aria-checked="true"
|
|
aria-label="Light colour mode"
|
|
tabindex="0"
|
|
>
|
|
<.icon name="hero-sun-micro" class="size-4 opacity-75 hover:opacity-100" />
|
|
</button>
|
|
|
|
<button
|
|
type="button"
|
|
class="flex p-2 cursor-pointer w-1/2 justify-center rounded-full focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary"
|
|
data-phx-theme="dark"
|
|
role="radio"
|
|
aria-checked="false"
|
|
aria-label="Dark colour mode"
|
|
tabindex="-1"
|
|
>
|
|
<.icon name="hero-moon-micro" class="size-4 opacity-75 hover:opacity-100" />
|
|
</button>
|
|
</div>
|
|
"""
|
|
end
|
|
|
|
@doc """
|
|
Renders the Matomo configuration for anonymous public pages.
|
|
|
|
The actual tracker is initialised by `assets/js/analytics.js`. Signed-in
|
|
visitors never get the config element, so account and admin pages are not
|
|
tracked.
|
|
"""
|
|
attr :current_scope, :map, default: nil
|
|
|
|
def analytics(assigns) do
|
|
config = Application.get_env(:chesstrainer, :matomo, [])
|
|
url = normalize_matomo_url(config[:url])
|
|
site_id = config[:site_id]
|
|
user = Map.get(assigns, :current_scope) && assigns.current_scope.user
|
|
|
|
assigns =
|
|
assign(assigns,
|
|
matomo_url: url,
|
|
matomo_site_id: site_id && to_string(site_id),
|
|
analytics_enabled:
|
|
is_binary(url) and byte_size(url) > 0 and is_binary(site_id) and byte_size(site_id) > 0 and
|
|
is_nil(user)
|
|
)
|
|
|
|
~H"""
|
|
<div
|
|
:if={@analytics_enabled}
|
|
id="matomo-config"
|
|
data-url={@matomo_url}
|
|
data-site-id={@matomo_site_id}
|
|
hidden
|
|
>
|
|
</div>
|
|
"""
|
|
end
|
|
|
|
defp normalize_matomo_url(url) when is_binary(url) do
|
|
case String.trim(url) do
|
|
"" -> nil
|
|
url -> String.trim_trailing(url, "/") <> "/"
|
|
end
|
|
end
|
|
|
|
defp normalize_matomo_url(_url), do: nil
|
|
end
|