Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

App layout refactor: Phase 2 (#6599)

RobertJoonas72d04f46ae48
diff --git a/lib/plausible_web/components/layouts.ex b/lib/plausible_web/components/layouts.ex
index a429347f8a..e7b46c6c3c 100644
--- a/lib/plausible_web/components/layouts.ex
+++ b/lib/plausible_web/components/layouts.ex
@@ -1,44 +1,110 @@
defmodule PlausibleWeb.Layouts do
@moduledoc false
- use Phoenix.Component
+ use PlausibleWeb, :component
+ use Plausible
- alias PlausibleWeb.Router.Helpers, as: Routes
+ require Plausible.Billing
- def legacy(assigns) do
+ alias PlausibleWeb.Components.Billing.Notice
+ alias PlausibleWeb.Components.Layout
+
+ embed_templates "layouts/*.html"
+
+ attr :header?, :boolean, default: true
+ attr :footer?, :boolean, default: true
+ attr :global_notices?, :boolean, default: true
+ attr :trial_badge?, :boolean, default: true
+ attr :embedded?, :boolean, default: false
+ attr :load_dashboard_js?, :boolean, default: false
+ attr :flash, :map, default: %{}
+ attr :current_user, :any, default: nil
+ attr :current_team, :any, default: nil
+ attr :current_team_role, :any, default: nil
+ attr :teams, :list, default: []
+ attr :my_team, :any, default: nil
+ attr :site, :any, default: nil
+ slot :inner_block, required: true
+
+ def app(assigns) do
~H"""
- <div class={["flex flex-col", if(!assigns[:embedded], do: "h-full")]}>
- <%= if !assigns[:embedded] && assigns[:flash] do %>
- {PlausibleWeb.LayoutView.render("_flash.html", assigns)}
- <% end %>
+ <div class={["flex flex-col", if(!@embedded?, do: "h-full")]}>
+ <.flash :if={not @embedded?} flash={@flash} />
- <%= if !assigns[:embedded] && !assigns[:hide_header?] do %>
- {PlausibleWeb.LayoutView.render("_header.html", assigns)}
+ <%= if !@embedded? && @header? do %>
+ <.header
+ current_user={@current_user}
+ site={@site}
+ current_team={@current_team}
+ current_team_role={@current_team_role}
+ trial_badge?={@trial_badge?}
+ teams={@teams}
+ my_team={@my_team}
+ />
- <%= if !assigns[:disable_global_notices?] do %>
- {PlausibleWeb.LayoutView.render("_notice.html", assigns)}
- <% end %>
+ <.team_notices :if={@global_notices?} current_team={@current_team} />
<% end %>
<main class="flex-1 flex flex-col">
- {Map.get(assigns, :inner_layout) || @inner_content}
+ {render_slot(@inner_block)}
</main>
- <%= if assigns[:embedded] do %>
+ <%= if @embedded? do %>
<div data-iframe-height></div>
- <script type="text/javascript" src={Routes.static_path(@conn, "/js/embed.content.js")}>
+ <script
+ type="text/javascript"
+ src={Routes.static_path(PlausibleWeb.Endpoint, "/js/embed.content.js")}
+ >
</script>
<% end %>
- <%= if !assigns[:hide_footer?] do %>
- {PlausibleWeb.LayoutView.render("_footer.html", assigns)}
- <% end %>
- <script type="text/javascript" src={Routes.static_path(@conn, "/js/app.js")}>
+ <.footer :if={@footer?} />
+ <script type="text/javascript" src={Routes.static_path(PlausibleWeb.Endpoint, "/js/app.js")}>
</script>
- <%= if assigns[:load_dashboard_js] do %>
- <script type="text/javascript" src={Routes.static_path(@conn, "/js/dashboard.js")}>
+ <%= if @load_dashboard_js? do %>
+ <script
+ type="text/javascript"
+ src={Routes.static_path(PlausibleWeb.Endpoint, "/js/dashboard.js")}
+ >
</script>
<% end %>
</div>
"""
end
+
+ attr :hide_header?, :boolean, default: false
+ attr :hide_footer?, :boolean, default: false
+ attr :disable_global_notices?, :boolean, default: false
+ attr :hide_trial_badge?, :boolean, default: false
+ attr :embedded, :boolean, default: false
+ attr :load_dashboard_js, :boolean, default: false
+ attr :flash, :map, default: %{}
+ attr :current_user, :any, default: nil
+ attr :current_team, :any, default: nil
+ attr :current_team_role, :any, default: nil
+ attr :teams, :list, default: []
+ attr :my_team, :any, default: nil
+ attr :site, :any, default: nil
+ slot :inner_block, required: true
+
+ def legacy(assigns) do
+ ~H"""
+ <.app
+ header?={not @hide_header?}
+ footer?={not @hide_footer?}
+ global_notices?={not @disable_global_notices?}
+ trial_badge?={not @hide_trial_badge?}
+ embedded?={@embedded}
+ load_dashboard_js?={@load_dashboard_js}
+ flash={@flash}
+ current_user={@current_user}
+ current_team={@current_team}
+ current_team_role={@current_team_role}
+ teams={@teams}
+ my_team={@my_team}
+ site={@site}
+ >
+ {render_slot(@inner_block)}
+ </.app>
+ """
+ end
end
diff --git a/lib/plausible_web/templates/layout/_flash.html.heex b/lib/plausible_web/components/layouts/flash.html.heex
similarity index 100%
rename from lib/plausible_web/templates/layout/_flash.html.heex
rename to lib/plausible_web/components/layouts/flash.html.heex
diff --git a/lib/plausible_web/templates/layout/_footer.html.heex b/lib/plausible_web/components/layouts/footer.html.heex
similarity index 99%
rename from lib/plausible_web/templates/layout/_footer.html.heex
rename to lib/plausible_web/components/layouts/footer.html.heex
index 69ecb59b64..e81121eaf6 100644
--- a/lib/plausible_web/templates/layout/_footer.html.heex
+++ b/lib/plausible_web/components/layouts/footer.html.heex
@@ -6,8 +6,8 @@
<img
src={
PlausibleWeb.Router.Helpers.static_path(
- @conn,
- logo_path("logo_dark.svg")
+ PlausibleWeb.Endpoint,
+ PlausibleWeb.LayoutView.logo_path("logo_dark.svg")
)
}
class="inline-block w-32 mr-1"
diff --git a/lib/plausible_web/templates/layout/_header.html.heex b/lib/plausible_web/components/layouts/header.html.heex
similarity index 81%
rename from lib/plausible_web/templates/layout/_header.html.heex
rename to lib/plausible_web/components/layouts/header.html.heex
index d1699049ab..d4c2fa00bf 100644
--- a/lib/plausible_web/templates/layout/_header.html.heex
+++ b/lib/plausible_web/components/layouts/header.html.heex
@@ -2,21 +2,21 @@
<div class="container print:max-w-full">
<nav class="relative flex items-center justify-between sm:h-10 md:justify-center">
<div class="flex items-center flex-1 md:absolute md:inset-y-0 md:left-0">
- <a href={home_dest(@conn)}>
+ <a href={PlausibleWeb.LayoutView.home_dest(@current_user)}>
<Layout.logo />
</a>
</div>
<div class="absolute inset-y-0 right-0 flex items-center justify-end">
<%= cond do %>
- <% @conn.assigns[:current_user] -> %>
+ <% @current_user -> %>
<ul class="flex items-center gap-2 w-full sm:w-auto">
<li :if={
- ee?() && @conn.assigns[:site] &&
- Plausible.Auth.super_admin?(@conn.assigns[:current_user])
+ ee?() && @site &&
+ Plausible.Auth.super_admin?(@current_user)
}>
<.styled_link
class="text-sm font-medium"
- href={"/cs/sites/site/#{@conn.assigns.site.id}"}
+ href={"/cs/sites/site/#{@site.id}"}
new_tab={true}
>
CS
@@ -24,21 +24,21 @@
</li>
<li
:if={
- ee?() and Plausible.Teams.on_trial?(@conn.assigns[:current_team]) and
- (not Plausible.Teams.setup?(@conn.assigns[:current_team]) or
- @conn.assigns[:current_team_role] in Plausible.Billing.allowed_roles()) and
- !assigns[:hide_trial_badge?]
+ ee?() and Plausible.Teams.on_trial?(@current_team) and
+ (not Plausible.Teams.setup?(@current_team) or
+ @current_team_role in Plausible.Billing.allowed_roles()) and
+ @trial_badge?
}
class="hidden sm:block"
>
<div class="flex items-center gap-x-2 px-3.5 py-2 rounded-lg bg-gradient-to-r from-indigo-100 to-purple-100 dark:from-indigo-600/35 dark:to-purple-600/35 overflow-hidden">
<span class="text-sm font-medium text-gray-900 dark:text-gray-100">
- {trial_notification(@conn.assigns[:current_team])}
+ {PlausibleWeb.LayoutView.trial_notification(@current_team)}
</span>
<div class="w-px h-4 bg-indigo-500/30 dark:bg-indigo-200/30" aria-hidden="true">
</div>
<.styled_link
- href={Routes.settings_path(@conn, :subscription)}
+ href={Routes.settings_path(PlausibleWeb.Endpoint, :subscription)}
class="plausible-event-name=Trial+Notification+Upgrade+Click text-sm font-medium dark:!text-indigo-400 dark:hover:!text-indigo-300"
>
Choose a plan
@@ -49,10 +49,10 @@
<.dropdown>
<:button class="flex items-center gap-2 pl-3 pr-1.5 py-2 rounded-md transition-colors duration-150 hover:bg-gray-100 dark:hover:bg-gray-800">
<span class="hidden md:block text-sm font-medium truncate dark:text-gray-100">
- {@conn.assigns[:current_user].name}
+ {@current_user.name}
</span>
<img
- src={Plausible.Auth.User.profile_img_url(@conn.assigns[:current_user])}
+ src={Plausible.Auth.User.profile_img_url(@current_user)}
class="-my-0.5 w-7 rounded-full"
/>
</:button>
@@ -60,22 +60,24 @@
<.dropdown_item>
<div class="text-xs text-gray-500 dark:text-gray-400">Signed in as</div>
<p class="truncate font-medium text-gray-900 dark:text-gray-100" role="none">
- {@conn.assigns[:current_user].email}
+ {@current_user.email}
</p>
</.dropdown_item>
- <.team_switcher
- conn={@conn}
+ <PlausibleWeb.LayoutView.team_switcher
teams={@teams}
my_team={@my_team}
current_team={@current_team}
/>
<.dropdown_divider />
- <.dropdown_item href={Routes.settings_path(@conn, :index)}>
+ <.dropdown_item href={Routes.settings_path(PlausibleWeb.Endpoint, :index)}>
Account settings
</.dropdown_item>
<div :if={@my_team && @my_team.id == @current_team.id}>
- <.dropdown_item class="flex" href={Routes.team_setup_path(@conn, :setup)}>
+ <.dropdown_item
+ class="flex"
+ href={Routes.team_setup_path(PlausibleWeb.Endpoint, :setup)}
+ >
<span data-test="create-a-team-cta" class="flex-1">
Create a team
</span>
@@ -86,7 +88,7 @@
<div :if={Plausible.Teams.setup?(@current_team)}>
<.dropdown_item
class="flex"
- href={Routes.settings_path(@conn, :team_general)}
+ href={Routes.settings_path(PlausibleWeb.Endpoint, :team_general)}
>
<span class="flex-1">
Team settings
@@ -113,7 +115,7 @@
:if={ee?()}
class="!flex justify-between gap-x-12"
new_tab
- href={feedback_link(@conn.assigns[:current_user])}
+ href={PlausibleWeb.LayoutView.feedback_link(@current_user)}
>
Feature requests
</.dropdown_item>
@@ -129,9 +131,7 @@
</:menu>
</.dropdown>
</li>
- <%= if @conn.assigns[:current_user] && ee?() do %>
- <li id="changelog-notification" class="relative py-2"></li>
- <% end %>
+ <li :if={ee?()} id="changelog-notification" class="relative py-2"></li>
</ul>
<% Keyword.fetch!(Application.get_env(:plausible, :selfhost), :disable_registration) != false -> %>
<ul class="flex" x-show="!document.cookie.includes('logged_in=true')">
diff --git a/lib/plausible_web/templates/layout/_notice.html.heex b/lib/plausible_web/components/layouts/team_notices.html.heex
similarity index 100%
rename from lib/plausible_web/templates/layout/_notice.html.heex
rename to lib/plausible_web/components/layouts/team_notices.html.heex
diff --git a/lib/plausible_web/templates/error/404_error.html.heex b/lib/plausible_web/templates/error/404_error.html.heex
index de173b9583..1c8ba3795a 100644
--- a/lib/plausible_web/templates/error/404_error.html.heex
+++ b/lib/plausible_web/templates/error/404_error.html.heex
@@ -13,7 +13,10 @@
>
Log in
</.button_link>
- <.button_link theme="secondary" href={PlausibleWeb.LayoutView.home_dest(@conn)}>
+ <.button_link
+ theme="secondary"
+ href={PlausibleWeb.LayoutView.home_dest(@conn.assigns[:current_user])}
+ >
Go to homepage
</.button_link>
</div>
diff --git a/lib/plausible_web/templates/error/generic_error.html.heex b/lib/plausible_web/templates/error/generic_error.html.heex
index c51ff40f63..c76ef53f49 100644
--- a/lib/plausible_web/templates/error/generic_error.html.heex
+++ b/lib/plausible_web/templates/error/generic_error.html.heex
@@ -1,5 +1,7 @@
<div class="container text-center mt-24">
<h1 class="text-5xl font-black dark:text-gray-100">{@status}</h1>
<div class="my-4 text-xl dark:text-gray-100">{@message}</div>
- <.button_link href={PlausibleWeb.LayoutView.home_dest(@conn)}>Go to homepage</.button_link>
+ <.button_link href={PlausibleWeb.LayoutView.home_dest(@conn.assigns[:current_user])}>
+ Go to homepage
+ </.button_link>
</div>
diff --git a/lib/plausible_web/templates/error/server_error_report_thanks.html.heex b/lib/plausible_web/templates/error/server_error_report_thanks.html.heex
index 8280119cec..42c3517d4d 100644
--- a/lib/plausible_web/templates/error/server_error_report_thanks.html.heex
+++ b/lib/plausible_web/templates/error/server_error_report_thanks.html.heex
@@ -11,7 +11,7 @@
</div>
<div class="mb-6 mt-12">
<div class="flex gap-x-5 text-sm justify-center">
- <.styled_link href={PlausibleWeb.LayoutView.home_dest(@conn)}>
+ <.styled_link href={PlausibleWeb.LayoutView.home_dest(@conn.assigns[:current_user])}>
Home page
</.styled_link>
<.styled_link href="https://status.plausible.io">
diff --git a/lib/plausible_web/templates/layout/app.html.heex b/lib/plausible_web/templates/layout/app.html.heex
index 83e7658469..1447e4af45 100644
--- a/lib/plausible_web/templates/layout/app.html.heex
+++ b/lib/plausible_web/templates/layout/app.html.heex
@@ -32,6 +32,8 @@
class={assigns[:bg_class] || "bg-gray-50 dark:bg-gray-950"}
style={if assigns[:background], do: "background-color: #{assigns[:background]}"}
>
- <Layouts.legacy {assigns} />
+ <Layouts.legacy {assigns}>
+ {@inner_content}
+ </Layouts.legacy>
</body>
</html>
diff --git a/lib/plausible_web/templates/layout/base_error.html.heex b/lib/plausible_web/templates/layout/base_error.html.heex
index f2648d2f14..27311b6dcc 100644
--- a/lib/plausible_web/templates/layout/base_error.html.heex
+++ b/lib/plausible_web/templates/layout/base_error.html.heex
@@ -10,7 +10,7 @@
</head>
<body class="flex flex-col h-full bg-gray-100 dark:bg-gray-900">
<div class="w-full my-8 text-center">
- <a href={home_dest(@conn)}>
+ <a href={home_dest(@conn.assigns[:current_user])}>
<Layout.logo />
</a>
</div>
diff --git a/lib/plausible_web/views/layout_view.ex b/lib/plausible_web/views/layout_view.ex
index f82555a175..247a1eadc7 100644
--- a/lib/plausible_web/views/layout_view.ex
+++ b/lib/plausible_web/views/layout_view.ex
@@ -3,7 +3,6 @@ defmodule PlausibleWeb.LayoutView do
use Plausible
alias Plausible.Teams
- alias PlausibleWeb.Components.Billing.Notice
alias PlausibleWeb.Components.Layout
alias PlausibleWeb.Layouts
@@ -38,12 +37,8 @@ defmodule PlausibleWeb.LayoutView do
end
end
- def home_dest(conn) do
- if conn.assigns[:current_user] do
- "/sites"
- else
- "/"
- end
+ def home_dest(current_user) do
+ if current_user, do: "/sites", else: "/"
end
def logo_path(filename) do
@@ -163,7 +158,6 @@ defmodule PlausibleWeb.LayoutView do
end
end
- attr :conn, :map, required: true
attr :teams, :list, required: true
attr :my_team, :any, default: nil
attr :current_team, :any, default: nil
@@ -205,7 +199,7 @@ defmodule PlausibleWeb.LayoutView do
</.dropdown_item>
<.dropdown_item
:if={@pinned_team}
- href={Routes.site_path(@conn, :index, __team: @pinned_team.identifier)}
+ href={Routes.site_path(PlausibleWeb.Endpoint, :index, __team: @pinned_team.identifier)}
>
<div class="flex items-center justify-between gap-2" role="none">
<p class="font-semibold truncate min-w-0 text-gray-900 dark:text-gray-100">
@@ -217,7 +211,7 @@ defmodule PlausibleWeb.LayoutView do
<div class="max-h-[200px] overflow-y-auto">
<.dropdown_item
:for={team <- @other_teams}
- href={Routes.site_path(@conn, :index, __team: team.identifier)}
+ href={Routes.site_path(PlausibleWeb.Endpoint, :index, __team: team.identifier)}
>
<p
class="font-medium truncate text-gray-900 dark:text-gray-100 pr-4"
@@ -246,11 +240,6 @@ defmodule PlausibleWeb.LayoutView do
end
end
- @doc "http://blog.plataformatec.com.br/2018/05/nested-layouts-with-phoenix/"
- def render_layout(layout, assigns, do: content) do
- render(layout, Map.put(assigns, :inner_layout, content))
- end
-
def current_tab?(_, nil) do
false
end