Skip to content

Open source CapCut alternative.

TypeScript

fix(site): clear the phone paywall once a plan lands

David Le996dba8992f3
diff --git a/site/src/cut/components/onboarding/CutOnboarding.tsx b/site/src/cut/components/onboarding/CutOnboarding.tsx
index 5d21afa3..f64503ed 100644
--- a/site/src/cut/components/onboarding/CutOnboarding.tsx
+++ b/site/src/cut/components/onboarding/CutOnboarding.tsx
@@ -13,7 +13,7 @@ import { PlansSlide, type PlansExit } from "@/cut/components/onboarding/slides/P
import { ReferralSlide } from "@/cut/components/onboarding/slides/Referral";
import { WelcomeSlide } from "@/cut/components/onboarding/slides/Welcome";
import { useEngineUser } from "@/cut/lib/backend/hooks";
-import { useFormFactor } from "@/cut/lib/environment";
+import { useEnvironment, useFormFactor } from "@/cut/lib/environment";
import { useCutBase } from "@/cut/lib/nav";
import { onOpenOnboarding, setOnboardingCover } from "@/cut/lib/onboarding";
import { track } from "@/lib/analytics";
@@ -38,6 +38,10 @@ const SLIDES = ["welcome", "referral", "modes", "credits", "ai-chat", "plans"] a
const referralSignature = (sources: ReferralSource[], other: string) =>
`${[...sources].sort().join(",")}|${other}`;
+// How often the phone paywall rereads the plan. A checkout comes back before
+// its webhook lands, and the paywall has to clear once the plan does.
+const PAYWALL_POLL_MS = 5_000;
+
// The sequence version this browser last saw this account finish. It exists so
// a returning load paints the app immediately instead of covering it while the
// account read is in flight; the account row stays the source of truth, and a
@@ -70,11 +74,12 @@ export function CutOnboarding() {
// no account at all.
const userId = useEngineUser() ?? "";
const { data: state, isPending } = useOnboardingState();
- const { data: pro } = useProSubscription();
const save = useSaveOnboarding();
// A phone ends on the plans with no free way out: the editor needs a
- // computer, so the phone's job is the account and the plan.
- const exit: PlansExit = useFormFactor() === "phone" ? "paywall" : "free";
+ // computer, so the phone's job is the account and the plan. The ChatGPT card
+ // keeps the editor on a phone and sells no plans, so it keeps the free exit.
+ const { chatgpt } = useEnvironment();
+ const exit: PlansExit = useFormFactor() === "phone" && !chatgpt ? "paywall" : "free";
const [rawStep, setStep] = useState(0);
const saved = useMemo(() => (state?.referralSources ?? []).filter(isKnownReferralSource), [state]);
const savedOther = saved.includes("other") ? (state?.referralOther ?? "") : "";
@@ -113,6 +118,7 @@ export function CutOnboarding() {
? "first_run"
: "replay"
: null;
+ const { data: pro } = useProSubscription({ pollMs: run && exit === "paywall" ? PAYWALL_POLL_MS : undefined });
// Navigation and progress use the slides this account can see.
const isPro = pro?.isActive ?? false;
diff --git a/site/src/cut/lib/proUpgrade.ts b/site/src/cut/lib/proUpgrade.ts
index 619d5664..329ab0f4 100644
--- a/site/src/cut/lib/proUpgrade.ts
+++ b/site/src/cut/lib/proUpgrade.ts
@@ -3,12 +3,13 @@
// One way into plan checkout, shared by every surface that sells a plan. A
// free account leaves for Stripe; an account already on a plan moves up in
// place and lands back on settings.
+import { useQueryClient } from "@tanstack/react-query";
import { useState } from "react";
import type { PaidPlan } from "@/lib/billing/plans";
import { track, type CheckoutOrigin } from "@/lib/analytics";
import { ApiError } from "@/queries/apiClient";
-import { useStartCheckout } from "@/queries/billing";
+import { proSubscriptionQueryKey, useStartCheckout } from "@/queries/billing";
/** A checkout's failure as analytics names it: the API's error code, the
* HTTP status when the route sent none, or "network". */
@@ -21,6 +22,7 @@ export function checkoutErrorCode(error: unknown): string {
export function usePlanCheckout(origin: CheckoutOrigin) {
const checkout = useStartCheckout();
+ const queryClient = useQueryClient();
const [pendingPlan, setPendingPlan] = useState<PaidPlan | null>(null);
const start = (plan: PaidPlan) => {
@@ -32,6 +34,9 @@ export function usePlanCheckout(origin: CheckoutOrigin) {
onSuccess: ({ url }) => window.location.assign(url),
onError: (error) => {
setPendingPlan(null);
+ // A refusal can mean the account already holds the plan, synced by
+ // the route just now, so the surface rereads it.
+ void queryClient.invalidateQueries({ queryKey: proSubscriptionQueryKey });
track("pro_checkout_failed", { plan, error: checkoutErrorCode(error), ...origin });
},
},
diff --git a/site/src/queries/billing.ts b/site/src/queries/billing.ts
index 396f8b6c..31455c16 100644
--- a/site/src/queries/billing.ts
+++ b/site/src/queries/billing.ts
@@ -25,10 +25,13 @@ export type ProSubscription = {
export const fetchProSubscription = () => apiFetch<ProSubscription>("/api/billing/pro");
-export function useProSubscription() {
+// A surface waiting on a plan to land, such as the phone paywall after a
+// checkout, rereads it every pollMs.
+export function useProSubscription({ pollMs }: { pollMs?: number } = {}) {
return useQuery({
queryFn: fetchProSubscription,
queryKey: proSubscriptionQueryKey,
+ refetchInterval: (query) => (pollMs && !query.state.data?.isActive ? pollMs : false),
});
}