Skip to content

Open source CapCut alternative.

TypeScript

feat(site): add Other referral option with a free-text answer

David Leaa25ad1b3601
diff --git a/site/prisma/Onboarding.prisma b/site/prisma/Onboarding.prisma
index b2d2307a..5811e63b 100644
--- a/site/prisma/Onboarding.prisma
+++ b/site/prisma/Onboarding.prisma
@@ -1,7 +1,7 @@
// What an account has been through on the way in: the welcome sequence, and
// the answers it collected. One row per user, written the first time the
// sequence opens. Referral source ids live in code
-// (src/lib/onboarding/referral-sources.ts); the route accepts only those.
+// (src/lib/onboarding/sequence.ts); the route accepts only those.
model UserOnboarding {
userId String @id
@@ -14,6 +14,8 @@ model UserOnboarding {
skipped Boolean @default(false)
// Every source the account picked; the question takes more than one answer.
referralSources String[] @default([])
+ // The free text that comes with picking "other".
+ referralOther String?
referralAnsweredAt DateTime?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
diff --git a/site/src/app/api/account/onboarding/route.ts b/site/src/app/api/account/onboarding/route.ts
index eab177dc..b18ec379 100644
--- a/site/src/app/api/account/onboarding/route.ts
+++ b/site/src/app/api/account/onboarding/route.ts
@@ -7,6 +7,7 @@ import {
} from "@/lib/donkey-api-auth";
import {
ONBOARDING_VERSION,
+ REFERRAL_OTHER_MAX_LENGTH,
REFERRAL_SOURCES,
isKnownReferralSource,
} from "@/lib/onboarding/sequence";
@@ -19,6 +20,7 @@ type OnboardingState = {
completedAt: string | null;
skipped: boolean;
referralSources: string[];
+ referralOther: string | null;
};
// An account that has never opened the sequence has no row; it reads as an
@@ -28,6 +30,7 @@ const UNSTARTED: OnboardingState = {
completedAt: null,
skipped: false,
referralSources: [],
+ referralOther: null,
};
export const GET = withDonkeyAuth(async (request: DonkeyAuthenticatedRequest) => {
@@ -42,6 +45,7 @@ const updateSchema = z
referralSources: z
.array(z.string().refine(isKnownReferralSource))
.max(REFERRAL_SOURCES.length),
+ referralOther: z.string().trim().max(REFERRAL_OTHER_MAX_LENGTH).optional(),
})
.or(z.object({ completed: z.literal(true), skipped: z.boolean() }));
@@ -58,6 +62,12 @@ export const PUT = withDonkeyAuth(async (request: DonkeyAuthenticatedRequest) =>
"referralSources" in parsed.data
? {
referralSources: [...new Set(parsed.data.referralSources)],
+ // The text only means anything as the "other" answer; without that
+ // pick it stores nothing.
+ referralOther:
+ (parsed.data.referralSources.includes("other") &&
+ parsed.data.referralOther) ||
+ null,
referralAnsweredAt: new Date(),
}
: {
@@ -79,11 +89,13 @@ function toState(row: {
completedAt: Date | null;
skipped: boolean;
referralSources: string[];
+ referralOther: string | null;
}): OnboardingState {
return {
version: row.version,
completedAt: row.completedAt?.toISOString() ?? null,
skipped: row.skipped,
referralSources: row.referralSources,
+ referralOther: row.referralOther,
};
}
diff --git a/site/src/cut/components/onboarding/CutOnboarding.tsx b/site/src/cut/components/onboarding/CutOnboarding.tsx
index 2e54699e..77478b8e 100644
--- a/site/src/cut/components/onboarding/CutOnboarding.tsx
+++ b/site/src/cut/components/onboarding/CutOnboarding.tsx
@@ -62,6 +62,7 @@ export function CutOnboarding() {
const save = useSaveOnboarding();
const [rawStep, setStep] = useState(0);
const [referrals, setReferrals] = useState<ReferralSource[]>([]);
+ const [referralOther, setReferralOther] = useState("");
const [dismissed, setDismissed] = useState(false);
const [replaying, setReplaying] = useState(false);
// Read once per mount: whether this browser can skip the wait below.
@@ -151,6 +152,7 @@ export function CutOnboarding() {
onOpenOnboarding(() => {
setStep(0);
setReferrals([]);
+ setReferralOther("");
setReplaying(true);
}),
[],
@@ -159,12 +161,16 @@ export function CutOnboarding() {
// The answers go up when the sequence leaves the question, so picking several
// is one write rather than one per tap. Every way out passes through here.
const commitReferrals = useCallback(() => {
- const signature = [...referrals].sort().join(",");
+ const other = referrals.includes("other") ? referralOther.trim() : "";
+ const signature = `${[...referrals].sort().join(",")}|${other}`;
if (!referrals.length || signature === savedReferrals.current) return;
savedReferrals.current = signature;
- track("onboarding_referral_selected", { referralSources: referrals });
- save.mutate({ referralSources: referrals });
- }, [referrals, save]);
+ track("onboarding_referral_selected", {
+ referralSources: referrals,
+ ...(other && { referralOther: other }),
+ });
+ save.mutate({ referralSources: referrals, ...(other && { referralOther: other }) });
+ }, [referrals, referralOther, save]);
const finish = useCallback(
(skipped: boolean) => {
@@ -298,7 +304,12 @@ export function CutOnboarding() {
<div className="mx-auto w-full max-w-[1100px]">
{step === 0 && <WelcomeSlide />}
{step === 1 && (
- <ReferralSlide selected={referrals} onToggle={toggleReferral} />
+ <ReferralSlide
+ selected={referrals}
+ onToggle={toggleReferral}
+ otherText={referralOther}
+ onOtherTextChange={setReferralOther}
+ />
)}
{step === 2 && <ModesSlide />}
{step === 3 && <CreditsSlide />}
diff --git a/site/src/cut/components/onboarding/slides/Referral.tsx b/site/src/cut/components/onboarding/slides/Referral.tsx
index b28b2ff7..516e0d79 100644
--- a/site/src/cut/components/onboarding/slides/Referral.tsx
+++ b/site/src/cut/components/onboarding/slides/Referral.tsx
@@ -1,9 +1,13 @@
"use client";
-import type { ReactNode } from "react";
-import { Check, Search, Users } from "lucide-react";
+import { useEffect, useRef, type ReactNode } from "react";
+import { Check, PenLine, Search, Users } from "lucide-react";
-import { REFERRAL_SOURCES, type ReferralSource } from "@/lib/onboarding/sequence";
+import {
+ REFERRAL_OTHER_MAX_LENGTH,
+ REFERRAL_SOURCES,
+ type ReferralSource,
+} from "@/lib/onboarding/sequence";
import { cn } from "@/lib/utils";
// The brand marks are files (public/cut/onboarding), the generic answers are
@@ -15,14 +19,29 @@ const MARKS: Record<ReferralSource, ReactNode> = {
youtube: <BrandMark src="/cut/onboarding/youtube.svg" />,
search: <Search className="size-[18px]" />,
friend: <Users className="size-[18px]" />,
+ other: <PenLine className="size-[18px]" />,
};
type Props = {
selected: ReferralSource[];
onToggle: (source: ReferralSource) => void;
+ otherText: string;
+ onOtherTextChange: (text: string) => void;
};
-export function ReferralSlide({ selected, onToggle }: Props) {
+export function ReferralSlide({
+ selected,
+ onToggle,
+ otherText,
+ onOtherTextChange,
+}: Props) {
+ const otherActive = selected.includes("other");
+ // The field stays mounted so it can unfurl, which rules out autoFocus.
+ const otherInput = useRef<HTMLInputElement>(null);
+ useEffect(() => {
+ if (otherActive) otherInput.current?.focus();
+ }, [otherActive]);
+
return (
<div className="mx-auto w-full max-w-[440px]">
<h2 className="text-center text-[clamp(24px,3vw,32px)] leading-[1.1] font-semibold tracking-[-0.02em]">
@@ -35,26 +54,54 @@ export function ReferralSlide({ selected, onToggle }: Props) {
{REFERRAL_SOURCES.map((source) => {
const active = selected.includes(source.id);
return (
- <button
- key={source.id}
- type="button"
- onClick={() => onToggle(source.id)}
- aria-pressed={active}
- className={cn(
- // Selection is a border and a check, not a filled row: the
- // brand marks keep their own colors and stay legible.
- "flex items-center gap-3 rounded-xl border bg-white px-4 py-3.5 text-left text-[15px] font-medium transition-colors",
- active
- ? "border-ink ring-1 ring-ink"
- : "border-ink/15 hover:border-ink/40",
+ <div key={source.id} className="relative">
+ <button
+ type="button"
+ onClick={() => onToggle(source.id)}
+ aria-pressed={active}
+ className={cn(
+ // Selection is a border and a check, not a filled row: the
+ // brand marks keep their own colors and stay legible.
+ "relative z-[1] flex w-full items-center gap-3 rounded-xl border bg-white px-4 py-3.5 text-left text-[15px] font-medium transition-colors",
+ active
+ ? "border-ink ring-1 ring-ink"
+ : "border-ink/15 hover:border-ink/40",
+ )}
+ >
+ <span className="grid size-5 shrink-0 place-items-center">
+ {MARKS[source.id]}
+ </span>
+ <span className="flex-1">{source.label}</span>
+ <Check className={cn("size-4", active ? "opacity-100" : "opacity-0")} />
+ </button>
+ {/* The tray hangs off the row absolutely so opening it moves
+ nothing else, and it unfurls (the 0fr→1fr grid trick) from
+ behind the row: its top 12px start under the button, so its
+ borders emerge right where the row's corner curves end. */}
+ {source.id === "other" && (
+ <div
+ className={cn(
+ "absolute inset-x-0 top-[calc(100%-12px)] grid transition-[grid-template-rows] duration-300 ease-in-out",
+ active ? "grid-rows-[1fr]" : "grid-rows-[0fr]",
+ )}
+ >
+ {/* Bottom-aligned in the shrinking row, so closing slides the
+ field up behind the button instead of squashing it. */}
+ <div className="flex flex-col justify-end overflow-hidden pl-4">
+ <input
+ ref={otherInput}
+ type="text"
+ value={otherText}
+ onChange={(event) => onOtherTextChange(event.target.value)}
+ placeholder="Tell us where"
+ maxLength={REFERRAL_OTHER_MAX_LENGTH}
+ tabIndex={active ? 0 : -1}
+ className="w-full rounded-b-xl border border-t-0 border-ink bg-white px-4 pt-6 pb-3 text-[15px] outline-none placeholder:text-ink/40"
+ />
+ </div>
+ </div>
)}
- >
- <span className="grid size-5 shrink-0 place-items-center">
- {MARKS[source.id]}
- </span>
- <span className="flex-1">{source.label}</span>
- <Check className={cn("size-4", active ? "opacity-100" : "opacity-0")} />
- </button>
+ </div>
);
})}
</div>
diff --git a/site/src/lib/analytics.ts b/site/src/lib/analytics.ts
index a31d933f..2f67f55b 100644
--- a/site/src/lib/analytics.ts
+++ b/site/src/lib/analytics.ts
@@ -16,7 +16,10 @@ export type AnalyticsEvents = {
app_install_clicked: { source: "connect_gate_button" | "connect_gate_link" };
// The welcome sequence a new account sees, and its replay from settings.
onboarding_started: { source: OnboardingRun };
- onboarding_referral_selected: { referralSources: ReferralSource[] };
+ onboarding_referral_selected: {
+ referralSources: ReferralSource[];
+ referralOther?: string;
+ };
onboarding_completed: { source: OnboardingRun; skipped: boolean; step: number };
// Cut projects home.
project_created: { source: "projects_home" | "sidebar" | "file_import" };
diff --git a/site/src/lib/onboarding/sequence.ts b/site/src/lib/onboarding/sequence.ts
index b3f33201..41ee26e7 100644
--- a/site/src/lib/onboarding/sequence.ts
+++ b/site/src/lib/onboarding/sequence.ts
@@ -14,10 +14,15 @@ export const REFERRAL_SOURCES = [
{ id: "youtube", label: "YouTube" },
{ id: "search", label: "Search" },
{ id: "friend", label: "A friend" },
+ { id: "other", label: "Other" },
] as const;
export type ReferralSource = (typeof REFERRAL_SOURCES)[number]["id"];
+/** Picking "other" opens a free-text field; the slide and the route cap it to
+ * the same length. */
+export const REFERRAL_OTHER_MAX_LENGTH = 120;
+
export const isKnownReferralSource = (id: string): id is ReferralSource =>
REFERRAL_SOURCES.some((s) => s.id === id);
diff --git a/site/src/queries/onboarding.ts b/site/src/queries/onboarding.ts
index c623e819..7438a74f 100644
--- a/site/src/queries/onboarding.ts
+++ b/site/src/queries/onboarding.ts
@@ -12,10 +12,11 @@ export type OnboardingState = {
completedAt: string | null;
skipped: boolean;
referralSources: string[];
+ referralOther: string | null;
};
type OnboardingUpdate =
- | { referralSources: ReferralSource[] }
+ | { referralSources: ReferralSource[]; referralOther?: string }
| { completed: true; skipped: boolean };
/** Whether this account still owes us a run of the current sequence. */