Skip to content

Open source CapCut alternative.

TypeScript
2.6 KB
Raw Blame
"use client";

// The wall an account meets at a paid limit — a request that ran out of
// credits, one project past the free count — or the plans it asks to see.
// Each opens the plan ladder with words for why it is up.
import { useFormatter, useTranslations } from "next-intl";
import { useEffect, useState } from "react";

import { PlansDialog } from "@/cut/components/PlansDialog";
import { onOperationFailure } from "@/cut/lib/operationFailure";
import { onUpgrade, openUpgrade, type UpgradeRequest } from "@/cut/lib/upgrade";
import { PAID_PLANS, PLAN_NAMES } from "@/lib/billing/plans";
import { useProSubscription } from "@/queries/billing";

export function UpgradeDialog() {
  const [request, setRequest] = useState<UpgradeRequest | null>(null);

  useEffect(() => onUpgrade(setRequest), []);

  // A request refused for credits always raises the plans.
  useEffect(
    () =>
      onOperationFailure((failure) => {
        if (failure.code === "insufficient_credits" || failure.code === "insufficient_credits_for_generation") {
          openUpgrade("credits", "credits_refused");
        }
      }),
    [],
  );

  if (!request) {
    return null;
  }
  return <OpenDialog request={request} onClose={() => setRequest(null)} />;
}

// Split so the billing read only runs while the dialog is up.
function OpenDialog({ request, onClose }: { request: UpgradeRequest; onClose: () => void }) {
  const { reason, entry } = request;
  const t = useTranslations("billing");
  const format = useFormatter();
  const subscription = useProSubscription();
  const plan = subscription.data?.plan ?? null;
  const periodEnd = subscription.data?.currentPeriodEnd ?? null;

  // Out of credits: a free account is asked to pick a plan; a paying one is
  // told a bigger plan lands credits today, and when its own refill comes.
  const outOfCredits = () => {
    if (!plan || !periodEnd) {
      return { title: t("plans.freeOutTitle") };
    }
    const words = { plan: PLAN_NAMES[plan], date: format.dateTime(new Date(periodEnd), { day: "numeric", month: "long" }) };
    return plan === PAID_PLANS[PAID_PLANS.length - 1]
      ? { title: t("plans.topOutTitle", words) }
      : { title: t("plans.paidOutTitle"), body: t("plans.paidOutBody", words) };
  };

  const wall: { title: string; body?: string } =
    reason === "credits"
      ? outOfCredits()
      : reason === "projects"
        ? { title: t("upgrade.projectsTitle"), body: t("upgrade.projectsBody") }
        : { title: t("plans.chooseTitle"), body: t("plans.chooseBody") };

  return <PlansDialog source={reason} entry={entry} {...wall} onClose={onClose} />;
}