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

Content

""" 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"""
{render_slot(@inner_block)}
<.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"""
""" 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""" """ 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""" """ 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""" """ 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"""
<.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 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" />
""" 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"""
""" 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""" """ 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