defmodule PlausibleWeb.Components.Billing do @moduledoc false use Phoenix.Component import PlausibleWeb.Components.Generic require Plausible.Billing.Subscription.Status alias Plausible.Billing.Feature.{Props, StatsAPI} alias PlausibleWeb.Router.Helpers, as: Routes alias Plausible.Billing.{Subscription, Plans, Subscriptions} attr(:billable_user, Plausible.Auth.User, required: true) attr(:current_user, Plausible.Auth.User, required: true) attr(:feature_mod, :atom, required: true, values: Plausible.Billing.Feature.list()) attr(:grandfathered?, :boolean, default: false) attr(:size, :atom, default: :sm) attr(:rest, :global) # credo:disable-for-next-line Credo.Check.Refactor.CyclomaticComplexity def premium_feature_notice(assigns) do billable_user = Plausible.Users.with_subscription(assigns.billable_user) plan = Plans.get_regular_plan(billable_user.subscription, only_non_expired: true) business? = plan && plan.kind == :business legacy_feature_access? = Timex.before?(assigns.billable_user.inserted_at, Plans.business_tier_launch()) && assigns.feature_mod in [StatsAPI, Props] private_preview? = FunWithFlags.enabled?(:premium_features_private_preview) has_access? = assigns.feature_mod.check_availability(assigns.billable_user) == :ok cond do legacy_feature_access? -> ~H"" Plausible.Billing.on_trial?(assigns.billable_user) -> ~H""" <.notice class="rounded-t-md rounded-b-none" size={@size} {@rest}> <%= @feature_mod.display_name() %> is part of the Plausible Business plan. You can access it during your trial, but you'll need to subscribe to the Business plan to retain access after the trial ends." """ private_preview? && !business? -> ~H""" <.notice class="rounded-t-md rounded-b-none" size={@size} {@rest}> Business plans are now live! The private preview of <%= @feature_mod.display_name() %> ends <%= private_preview_days_remaining() %>. If you wish to continue using this feature, <.upgrade_call_to_action current_user={@current_user} billable_user={@billable_user} />. """ not has_access? -> ~H""" <.notice class="rounded-t-md rounded-b-none" size={@size} {@rest}> <%= account_label(@current_user, @billable_user) %> does not have access to <%= assigns.feature_mod.display_name() %>. To get access to this feature, <.upgrade_call_to_action current_user={@current_user} billable_user={@billable_user} />. """ true -> ~H"" end end defp private_preview_days_remaining do private_preview_ends_at = Timex.shift(Plausible.Billing.Plans.business_tier_launch(), days: 8) days_remaining = Timex.diff(private_preview_ends_at, NaiveDateTime.utc_now(), :day) cond do days_remaining <= 0 -> "today" days_remaining == 1 -> "tomorrow" true -> "in #{days_remaining} days" end end attr(:billable_user, Plausible.Auth.User, required: true) attr(:current_user, Plausible.Auth.User, required: true) attr(:limit, :integer, required: true) attr(:resource, :string, required: true) attr(:rest, :global) # credo:disable-for-next-line Credo.Check.Refactor.CyclomaticComplexity def limit_exceeded_notice(assigns) do ~H""" <.notice {@rest}> <%= account_label(@current_user, @billable_user) %> is limited to <%= @limit %> <%= @resource %>. To increase this limit, <.upgrade_call_to_action current_user={@current_user} billable_user={@billable_user} />. """ end attr(:current_user, :map) attr(:billable_user, :map) defp upgrade_call_to_action(assigns) do billable_user = Plausible.Users.with_subscription(assigns.billable_user) plan = Plausible.Billing.Plans.get_regular_plan(billable_user.subscription, only_non_expired: true) trial? = Plausible.Billing.on_trial?(assigns.billable_user) growth? = plan && plan.kind == :growth cond do assigns.billable_user.id !== assigns.current_user.id -> ~H"please reach out to the site owner to upgrade their subscription" growth? || trial? -> ~H""" please <.link class="underline inline-block" href={Plausible.Billing.upgrade_route_for(@current_user)}> upgrade your subscription """ true -> ~H"please contact support@plausible.io about the Enterprise plan" end end defp account_label(current_user, billable_user) do if current_user.id == billable_user.id do "Your account" else "The owner of this site" end end slot(:inner_block, required: true) attr(:rest, :global) def usage_and_limits_table(assigns) do ~H"""