defmodule CMSWeb.Globals do
  @moduledoc false
  use CMSWeb, :live_view

  def assign_globals(%Plug.Conn{} = conn, _opts) do
    Plug.Conn.assign(conn, :current_path, conn.request_path)
  end

  def on_mount(:default, _params, _session, socket) do
    socket =
      attach_hook(socket, :assign_handle_params_globals, :handle_params, fn _params, uri, socket ->
        %URI{path: current_path} = URI.parse(uri)
        {:cont, assign(socket, :current_path, current_path)}
      end)

    {:cont, socket}
  end
end