Open source CapCut alternative.
TypeScript
Wire landing pricing to self-serve Vision API checkout
David Le1b9d51c8b372
diff --git a/site/src/app/_components/landing/BillingPortalButton.tsx b/site/src/app/_components/landing/BillingPortalButton.tsxindex 8d3e18b8..d90358a4 100644--- a/site/src/app/_components/landing/BillingPortalButton.tsx+++ b/site/src/app/_components/landing/BillingPortalButton.tsx@@ -3,29 +3,23 @@import { ArrowRight } from "lucide-react";import { useCallback, useState } from "react";-import {- BillingApiError,- createBillingPortalSession,-} from "@/app/api-clients/billingApi";import { PillButton } from "@/app/_components/landing/LandingPrimitives";import { authClient } from "@/lib/auth-client";+import { ApiError } from "@/queries/apiClient";+import { useOpenBillingPortal } from "@/queries/billing";export function BillingPortalButton() {- const [isPending, setIsPending] = useState(false);const [statusMessage, setStatusMessage] = useState<string | null>(null);+ const portal = useOpenBillingPortal();const handleOpenPortal = useCallback(async () => {- setIsPending(true);setStatusMessage(null);try {- const session = await createBillingPortalSession();+ const session = await portal.mutateAsync();window.location.assign(session.url);} catch (error) {- if (- error instanceof BillingApiError &&- error.code === "unauthorized"- ) {+ if (error instanceof ApiError && error.status === 401) {await authClient.signIn.social({callbackURL: "/pricing",provider: "google",@@ -33,20 +27,19 @@ export function BillingPortalButton() {return;}- if (error instanceof BillingApiError && error.code === "not-found") {- setStatusMessage("Billing portal is not available yet.");- } else {- setStatusMessage("Billing portal is not available yet.");- }- } finally {- setIsPending(false);+ setStatusMessage("Billing portal is not available yet.");}- }, []);+ }, [portal]);return (<div>- <PillButton disabled={isPending} onClick={handleOpenPortal} variant="secondary">- {isPending ? "Opening..." : "Manage billing"} <ArrowRight size={14} />+ <PillButton+ disabled={portal.isPending}+ onClick={handleOpenPortal}+ variant="secondary"+ >+ {portal.isPending ? "Opening..." : "Manage billing"}{" "}+ <ArrowRight size={14} /></PillButton>{statusMessage ? (<divdiff --git a/site/src/app/_components/landing/PricingPlanCard.tsx b/site/src/app/_components/landing/PricingPlanCard.tsxindex 21ba65a9..867e0f3c 100644--- a/site/src/app/_components/landing/PricingPlanCard.tsx+++ b/site/src/app/_components/landing/PricingPlanCard.tsx@@ -3,10 +3,6 @@import { ArrowRight, Check } from "lucide-react";import { useCallback, useState } from "react";-import {- BillingApiError,- createCheckoutSession,-} from "@/app/api-clients/billingApi";import {PillButton,TapedCard,@@ -14,6 +10,8 @@ import {import type { PricingPlan } from "@/app/_components/landing/pricingPlans";import { useMediaQuery } from "@/app/_components/landing/useMediaQuery";import { authClient } from "@/lib/auth-client";+import { ApiError } from "@/queries/apiClient";+import { useStartCheckout } from "@/queries/billing";type Props = {plan: PricingPlan;@@ -21,8 +19,8 @@ type Props = {export function PricingPlanCard({ plan }: Props) {const isDesktop = useMediaQuery("(min-width: 768px)");- const [isPending, setIsPending] = useState(false);const [statusMessage, setStatusMessage] = useState<string | null>(null);+ const checkout = useStartCheckout();const action = plan.action;const handleCheckout = useCallback(async () => {@@ -30,17 +28,13 @@ export function PricingPlanCard({ plan }: Props) {return;}- setIsPending(true);setStatusMessage(null);try {- const session = await createCheckoutSession(action.planKey);+ const session = await checkout.mutateAsync(action.planKey);window.location.assign(session.url);} catch (error) {- if (- error instanceof BillingApiError &&- error.code === "unauthorized"- ) {+ if (error instanceof ApiError && error.status === 401) {await authClient.signIn.social({callbackURL: `/pricing?checkout=${action.planKey}`,provider: "google",@@ -48,24 +42,19 @@ export function PricingPlanCard({ plan }: Props) {return;}- if (error instanceof BillingApiError && error.code === "not-found") {- setStatusMessage("Checkout is not available yet.");- } else {- setStatusMessage("Checkout is not available yet. Please try again soon.");- }- } finally {- setIsPending(false);+ setStatusMessage("Checkout is not available yet. Please try again soon.");}- }, [action]);+ }, [action, checkout]);const button =action.kind === "checkout" ? (<PillButton- disabled={isPending}+ disabled={checkout.isPending}onClick={handleCheckout}variant="dark">- {isPending ? "Opening..." : action.label} <ArrowRight size={14} />+ {checkout.isPending ? "Opening..." : action.label}{" "}+ <ArrowRight size={14} /></PillButton>) : (<PillButton href={action.href} variant="dark">diff --git a/site/src/app/_components/landing/pricingPlans.ts b/site/src/app/_components/landing/pricingPlans.tsindex 32bc2608..a4e00de7 100644--- a/site/src/app/_components/landing/pricingPlans.ts+++ b/site/src/app/_components/landing/pricingPlans.ts@@ -1,4 +1,4 @@-import type { BillingPlanKey } from "@/app/api-clients/billingApi";+import type { BillingPlanKey } from "@/queries/billing";import type { CardColor } from "@/app/_components/landing/theme";export type PricingPlanAction =diff --git a/site/src/app/api-clients/billingApi.ts b/site/src/app/api-clients/billingApi.tsdeleted file mode 100644index f74bb192..00000000--- a/site/src/app/api-clients/billingApi.ts+++ /dev/null@@ -1,109 +0,0 @@-export type BillingPlanKey = "pro";--export type BillingCheckoutResponse = {- url: string;-};--export type BillingPortalResponse = {- url: string;-};--export type BillingSubscription = {- cancelAtPeriodEnd: boolean;- currentPeriodEnd: string | null;- planKey: BillingPlanKey;- status: string;-};--export type BillingSubscriptionResponse = {- subscription: BillingSubscription | null;-};--type BillingApiErrorCode =- | "bad-request"- | "not-found"- | "server-error"- | "unauthorized"- | "unknown";--export class BillingApiError extends Error {- code: BillingApiErrorCode;- status: number;-- constructor(message: string, status: number, code: BillingApiErrorCode) {- super(message);- this.name = "BillingApiError";- this.code = code;- this.status = status;- }-}--function errorCodeForStatus(status: number): BillingApiErrorCode {- if (status === 400) {- return "bad-request";- }-- if (status === 401) {- return "unauthorized";- }-- if (status === 404) {- return "not-found";- }-- if (status >= 500) {- return "server-error";- }-- return "unknown";-}--async function readJson<T>(response: Response): Promise<T> {- return (await response.json()) as T;-}--async function requireOk(response: Response, fallbackMessage: string) {- if (response.ok) {- return;- }-- throw new BillingApiError(- fallbackMessage,- response.status,- errorCodeForStatus(response.status),- );-}--export async function createCheckoutSession(planKey: BillingPlanKey) {- const response = await fetch("/api/billing/checkout/", {- body: JSON.stringify({ planKey }),- headers: {- "Content-Type": "application/json",- },- method: "POST",- });-- await requireOk(response, "Unable to start checkout.");-- return readJson<BillingCheckoutResponse>(response);-}--export async function createBillingPortalSession() {- const response = await fetch("/api/billing/portal/", {- method: "POST",- });-- await requireOk(response, "Unable to open billing portal.");-- return readJson<BillingPortalResponse>(response);-}--export async function getBillingSubscription() {- const response = await fetch("/api/billing/subscription/", {- method: "GET",- });-- await requireOk(response, "Unable to load subscription.");-- return readJson<BillingSubscriptionResponse>(response);-}diff --git a/site/src/app/donkeyvision/PricingSection.tsx b/site/src/app/donkeyvision/PricingSection.tsxindex 66f34402..93ee27ff 100644--- a/site/src/app/donkeyvision/PricingSection.tsx+++ b/site/src/app/donkeyvision/PricingSection.tsx@@ -1,7 +1,15 @@-import { Mail } from "lucide-react";+import Link from "next/link";+import { ArrowRight, Mail } from "lucide-react";import { ContactSalesButton } from "@/app/_components/landing/ContactSalesButton";+const planFeatures = [+ "5,000 API calls per month",+ "3 requests per second",+ "Sign in with Google, generate API keys",+ "Element boxes, center points, and labels",+];+export function PricingSection() {return (<section className="mx-auto w-full max-w-[1400px] px-6 py-20 md:px-12" id="contact">@@ -13,16 +21,30 @@ export function PricingSection() {Get Donkey Vision API access.</h2><p className="mt-6 max-w-2xl text-lg leading-8 text-white/75">- Tell us your screenshot volume and latency target. We’ll shape- access, request limits, and support around what you’re- building.+ Self-serve from <span className="font-semibold text-white">$50/month</span>.+ Subscribe, create an API key, and start parsing screenshots in+ minutes. Need higher volume or custom limits? Talk to us.</p>+ <ul className="mt-8 grid max-w-2xl gap-2 text-base text-white/85 sm:grid-cols-2">+ {planFeatures.map((feature) => (+ <li key={feature} className="flex items-center gap-2">+ <span aria-hidden="true" className="text-[#EC7868]">+ ●+ </span>+ {feature}+ </li>+ ))}+ </ul></div>- <div className="flex items-center md:justify-end">- <ContactSalesButton+ <div className="flex flex-col items-stretch justify-center gap-3 md:items-end">+ <Link+ href="/dashboard"className="inline-flex min-h-14 items-center justify-center gap-2 rounded-full bg-[#EC7868] px-7 text-base font-semibold text-[#0F0E0D] transition hover:-translate-y-0.5">- Contact us <Mail size={18} aria-hidden="true" />+ Get started <ArrowRight size={18} aria-hidden="true" />+ </Link>+ <ContactSalesButton className="inline-flex min-h-14 items-center justify-center gap-2 rounded-full border border-white/30 px-7 text-base font-semibold text-white transition hover:-translate-y-0.5">+ Contact sales <Mail size={18} aria-hidden="true" /></ContactSalesButton></div></div>diff --git a/site/src/app/donkeyvision/page.tsx b/site/src/app/donkeyvision/page.tsxindex ac416b60..2bf90b37 100644--- a/site/src/app/donkeyvision/page.tsx+++ b/site/src/app/donkeyvision/page.tsx@@ -20,7 +20,7 @@ export const metadata: Metadata = {export default function DonkeyVisionPage() {return (<main className="min-h-screen w-full overflow-x-hidden bg-[#F5EFE0] font-[-apple-system,BlinkMacSystemFont,'Segoe_UI',sans-serif] text-[#0F0E0D]">- <TopNav ctaHref="#contact" ctaLabel="Contact us" />+ <TopNav ctaHref="/dashboard" ctaLabel="Get started" /><HeroSection /><ProofSection /><ApiSection />