defmodule BerrypodWeb.AdminLayoutHook do @moduledoc """ LiveView on_mount hook that assigns the current path for admin sidebar navigation and loads theme settings for the admin layout. """ import Phoenix.Component alias Berrypod.Settings alias Berrypod.Theme.{CSSCache, CSSGenerator} def on_mount(:assign_current_path, _params, _session, socket) do theme_settings = Settings.get_theme_settings() generated_css = case CSSCache.get() do {:ok, css} -> css :miss -> css = CSSGenerator.generate(theme_settings, &BerrypodWeb.Endpoint.static_path/1) CSSCache.put(css) css end socket = socket |> assign(:current_path, "") |> assign(:site_live, Settings.site_live?()) |> assign(:email_configured, Berrypod.Mailer.email_configured?()) |> assign(:theme_settings, theme_settings) |> assign(:generated_css, generated_css) |> Phoenix.LiveView.attach_hook(:set_current_path, :handle_params, fn _params, uri, socket -> {:cont, socket |> assign(:current_path, URI.parse(uri).path) |> assign(:site_live, Settings.site_live?())} end) {:cont, socket} end end