defmodule PlausibleWeb.Components.Billing do @moduledoc false use Phoenix.Component import PlausibleWeb.Components.Generic require Plausible.Billing.Subscription.Status alias PlausibleWeb.Router.Helpers, as: Routes alias Plausible.Billing.{Subscription, Subscriptions} def render_monthly_pageview_usage(%{usage: usage} = assigns) when is_map_key(usage, :last_30_days) do ~H""" <.monthly_pageview_usage_table usage={@usage.last_30_days} limit={@limit} period={:last_30_days} /> """ end def render_monthly_pageview_usage(assigns) do ~H"""

Monthly pageviews usage

    <.billing_cycle_tab name="Ongoing cycle" tab={:current_cycle} date_range={@usage.current_cycle.date_range} with_separator={true} /> <.billing_cycle_tab name="Last cycle" tab={:last_cycle} date_range={@usage.last_cycle.date_range} disabled={@usage.last_cycle.total == 0 && @usage.penultimate_cycle.total == 0} with_separator={true} /> <.billing_cycle_tab name="Penultimate cycle" tab={:penultimate_cycle} date_range={@usage.penultimate_cycle.date_range} disabled={@usage.penultimate_cycle.total == 0} />
<.monthly_pageview_usage_table usage={@usage.current_cycle} limit={@limit} period={:current_cycle} />
<.monthly_pageview_usage_table usage={@usage.last_cycle} limit={@limit} period={:last_cycle} />
<.monthly_pageview_usage_table usage={@usage.penultimate_cycle} limit={@limit} period={:penultimate_cycle} />
""" end attr(:usage, :map, required: true) attr(:limit, :any, required: true) attr(:period, :atom, required: true) defp monthly_pageview_usage_table(assigns) do ~H""" <.usage_and_limits_table> <.usage_and_limits_row id={"total_pageviews_#{@period}"} title={"Total billable pageviews#{if @period == :last_30_days, do: " (last 30 days)"}"} usage={@usage.total} limit={@limit} /> <.usage_and_limits_row id={"pageviews_#{@period}"} pad title="Pageviews" usage={@usage.pageviews} class="font-normal text-gray-500 dark:text-gray-400" /> <.usage_and_limits_row id={"custom_events_#{@period}"} pad title="Custom events" usage={@usage.custom_events} class="font-normal text-gray-500 dark:text-gray-400" /> """ end attr(:name, :string, required: true) attr(:date_range, :any, required: true) attr(:tab, :atom, required: true) attr(:disabled, :boolean, default: false) attr(:with_separator, :boolean, default: false) defp billing_cycle_tab(assigns) do ~H"""
  • """ end slot(:inner_block, required: true) attr(:rest, :global) def usage_and_limits_table(assigns) do ~H""" <%= render_slot(@inner_block) %>
    """ end attr(:title, :string, required: true) attr(:usage, :integer, required: true) attr(:limit, :integer, default: nil) attr(:pad, :boolean, default: false) attr(:rest, :global) def usage_and_limits_row(assigns) do ~H""" <%= @title %> <%= Cldr.Number.to_string!(@usage) %> <%= if is_number(@limit), do: "/ #{Cldr.Number.to_string!(@limit)}" %> """ end def monthly_quota_box(assigns) do ~H"""

    Monthly quota

    <%= PlausibleWeb.AuthView.subscription_quota(@subscription, format: :long) %>
    <.styled_link :if={ not (Plausible.Auth.enterprise_configured?(@user) && Subscriptions.halted?(@subscription)) } id="#upgrade-or-change-plan-link" href={Routes.billing_path(PlausibleWeb.Endpoint, :choose_plan)} > <%= change_plan_or_upgrade_text(@subscription) %>
    """ end def present_enterprise_plan(assigns) do ~H""" """ end defp present_limit(enterprise_plan, key) do enterprise_plan |> Map.fetch!(key) |> PlausibleWeb.StatsView.large_number_format() end attr :id, :string, required: true attr :paddle_product_id, :string, required: true attr :checkout_disabled, :boolean, default: false attr :user, :map, required: true attr :confirm_message, :any, default: nil slot :inner_block, required: true def paddle_button(assigns) do confirmed = if assigns.confirm_message, do: "confirm(\"#{assigns.confirm_message}\")", else: "true" assigns = assign(assigns, :confirmed, confirmed) ~H""" """ end def paddle_script(assigns) do ~H""" """ end def upgrade_link(assigns) do ~H""" Upgrade """ end defp change_plan_or_upgrade_text(nil), do: "Upgrade" defp change_plan_or_upgrade_text(%Subscription{status: Subscription.Status.deleted()}), do: "Upgrade" defp change_plan_or_upgrade_text(_subscription), do: "Change plan" end