Skip to content

Open source CapCut alternative.

TypeScript

feat(site): gate Next on a referral answer and prefill saved picks

David Le91beb049e9fa
diff --git a/site/src/cut/components/onboarding/CutOnboarding.tsx b/site/src/cut/components/onboarding/CutOnboarding.tsx
index 77478b8e..982caa46 100644
--- a/site/src/cut/components/onboarding/CutOnboarding.tsx
+++ b/site/src/cut/components/onboarding/CutOnboarding.tsx
@@ -16,6 +16,7 @@ import { track } from "@/lib/analytics";
import { authClient } from "@/lib/auth-client";
import {
ONBOARDING_VERSION,
+ isKnownReferralSource,
type OnboardingRun,
type ReferralSource,
} from "@/lib/onboarding/sequence";
@@ -28,6 +29,12 @@ import {
} from "@/queries/onboarding";
const SLIDE_COUNT = 6;
+const REFERRAL_STEP = 1;
+
+// One string for "what the question's answer is right now", so the seeded
+// answer, the last write, and the current picks compare cheaply.
+const referralSignature = (sources: ReferralSource[], other: string) =>
+ `${[...sources].sort().join(",")}|${other}`;
// 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
@@ -78,6 +85,21 @@ export function CutOnboarding() {
// anything writes nothing.
const savedReferrals = useRef("");
+ // An answer given in the past shows again: once the account read lands, the
+ // question opens already filled in, and it reads as saved rather than as a
+ // pending change.
+ const seeded = useRef(false);
+ useEffect(() => {
+ if (!state || seeded.current) return;
+ seeded.current = true;
+ const saved = state.referralSources.filter(isKnownReferralSource);
+ if (!saved.length) return;
+ const other = saved.includes("other") ? (state.referralOther ?? "") : "";
+ setReferrals(saved);
+ setReferralOther(other);
+ savedReferrals.current = referralSignature(saved, other);
+ }, [state]);
+
// Which run is on screen, derived rather than set: the account read landing
// is what opens a first run, and nothing has to push that into state. On the
// sequence's address the run opens before that read answers; it is a first
@@ -151,8 +173,6 @@ export function CutOnboarding() {
() =>
onOpenOnboarding(() => {
setStep(0);
- setReferrals([]);
- setReferralOther("");
setReplaying(true);
}),
[],
@@ -162,7 +182,7 @@ export function CutOnboarding() {
// is one write rather than one per tap. Every way out passes through here.
const commitReferrals = useCallback(() => {
const other = referrals.includes("other") ? referralOther.trim() : "";
- const signature = `${[...referrals].sort().join(",")}|${other}`;
+ const signature = referralSignature(referrals, other);
if (!referrals.length || signature === savedReferrals.current) return;
savedReferrals.current = signature;
track("onboarding_referral_selected", {
@@ -190,6 +210,9 @@ export function CutOnboarding() {
setStep(Math.max(0, step - 1));
}, [commitReferrals, step]);
const advance = useCallback(() => {
+ // The question holds the sequence until it has an answer. Guarded here as
+ // well as on the button, or the arrow keys would walk right past it.
+ if (step === REFERRAL_STEP && !referrals.length) return;
commitReferrals();
if (step >= slideCount - 1) {
// The plans slide ends the sequence through its own two answers — Get
@@ -200,7 +223,7 @@ export function CutOnboarding() {
return;
}
setStep(step + 1);
- }, [commitReferrals, finish, isPro, slideCount, step]);
+ }, [commitReferrals, finish, isPro, referrals, slideCount, step]);
useEffect(() => {
if (!run) return;
@@ -346,6 +369,7 @@ export function CutOnboarding() {
takes them out. */}
<Button
onClick={advance}
+ disabled={step === REFERRAL_STEP && !referrals.length}
className={cn(
"gap-1.5 rounded-full bg-ink px-5 text-white hover:bg-ink/90",
last && !done && "invisible",