Open source CapCut alternative.
TypeScript
feat(site): one offer card with a banner, countdown and shared copy
David Lecc3668e9deea
diff --git a/site/public/cut/credits-banner.webp b/site/public/cut/credits-banner.webpnew file mode 100644index 00000000..23227e94Binary files /dev/null and b/site/public/cut/credits-banner.webp differdiff --git a/site/public/cut/offer-banner.webp b/site/public/cut/offer-banner.webpnew file mode 100644index 00000000..a0d2d57aBinary files /dev/null and b/site/public/cut/offer-banner.webp differdiff --git a/site/src/app/cut/app/(home)/settings/_components/ProCard.tsx b/site/src/app/cut/app/(home)/settings/_components/ProCard.tsxindex 3ef4a12c..d7c1dcdb 100644--- a/site/src/app/cut/app/(home)/settings/_components/ProCard.tsx+++ b/site/src/app/cut/app/(home)/settings/_components/ProCard.tsx@@ -123,7 +123,7 @@ export function ProCard() {) : offer && offerOpen ? (<CardContent className="text-sm text-foreground">Subscribe for {CUT_PRO.price} by {formatDeadline(offer.closesAt)} and get a one-time{" "}- {formatUsd(offer.dollars)} in credits{creditLifeClause(offer)}.+ {formatUsd(offer.dollars)} in credits.{creditLifeClause(offer)}</CardContent>) : null}<CardFooter className="gap-3">diff --git a/site/src/cut/components/ClaimCreditsDialog.tsx b/site/src/cut/components/ClaimCreditsDialog.tsxindex 107a5d3a..46d8909e 100644--- a/site/src/cut/components/ClaimCreditsDialog.tsx+++ b/site/src/cut/components/ClaimCreditsDialog.tsx@@ -11,18 +11,9 @@// An offer landed by subscribing has a Subscribe button in place of Claim:// the press starts the Pro checkout, which carries the offer to the webhook.import { useSearchParams } from "next/navigation";-import { Loader2, Sparkle } from "lucide-react";+import { Loader2 } from "lucide-react";import { useEffect, useState } from "react";-import { Button } from "@/components/ui/button";-import {- Dialog,- DialogContent,- DialogDescription,- DialogFooter,- DialogHeader,- DialogTitle,-} from "@/components/ui/dialog";-import { CUT_PRO } from "@/app/cut/_components/landing/cutPricingPlans";+import { claimOfferCopy, OfferButton, OfferDialog, OfferDismiss, subscribeOfferCopy } from "@/cut/components/OfferDialog";import { useEngineUser } from "@/cut/lib/backend/hooks";import { track } from "@/lib/analytics";import { formatCreditExpiry } from "@/lib/credits/top-up";@@ -76,13 +67,15 @@ function OpenDialog({ token, onClose }: { token: string; onClose: () => void })});checkout.mutate({ offerToken: token }, { onSuccess: (result) => window.location.assign(result.url) });};- // The dialog opens at once, checking the link, at the size it keeps: the- // body reserves two lines and every state ends in one row of buttons.+ // The card opens at once, checking the link, at the size it keeps: the+ // body reserves two lines and every state ends in one button.const done = claim.data ?? (offer.data?.claimed ? { expiresAt: offer.data.expiresAt } : null);- let title = "Claim your credits";+ let title: React.ReactNode = "Claim your credits";let body: React.ReactNode;- let footer: React.ReactNode;+ let terms: React.ReactNode = null;+ let cta: React.ReactNode;+ let secondary: React.ReactNode = <OfferDismiss onClick={onClose}>Not now</OfferDismiss>;if (offer.isPending) {body = (<span className="flex items-center gap-2">@@ -90,14 +83,7 @@ function OpenDialog({ token, onClose }: { token: string; onClose: () => void })Checking your offer…</span>);- footer = (- <>- <Button variant="ghost" onClick={onClose}>- Not now- </Button>- <Button disabled>Claim</Button>- </>- );+ cta = <OfferButton disabled>Claim</OfferButton>;} else if (offer.isError) {const status = offer.error instanceof ApiError ? offer.error.status : null;title =@@ -110,73 +96,65 @@ function OpenDialog({ token, onClose }: { token: string; onClose: () => void }): status === 409? "This offer is for accounts without a Pro subscription.": "The offer it pointed to is gone.";- footer = <Button onClick={onClose}>OK</Button>;+ cta = <OfferButton onClick={onClose}>OK</OfferButton>;+ secondary = null;} else if (done) {- body = `${offer.data.credits} in AI credits is on your account.${+ title = `${offer.data.credits} is on your account`;+ body = `${offer.data.credits} in AI credits is ready to spend.${done.expiresAt ? ` It expires ${formatCreditExpiry(new Date(done.expiresAt))}.` : ""}`;- footer = <Button onClick={onClose}>Start editing</Button>;+ cta = <OfferButton onClick={onClose}>Start editing</OfferButton>;+ secondary = null;} else if (offer.data.claim === "subscribe") {title = `Get ${offer.data.credits} in credits with Pro`;- body = `Subscribe to Pro for ${CUT_PRO.price}${- offer.data.closesAt ? ` by ${formatCreditExpiry(new Date(offer.data.closesAt))}` : ""- } and a one-time ${offer.data.credits} in AI credits lands in your account${- offer.data.lifetime ? `, good for ${offer.data.lifetime}` : ""- }.`;- footer = (- <>- <Button variant="ghost" onClick={onClose}>- Not now- </Button>- <Button disabled={checkout.isPending} onClick={subscribe}>- {checkout.isPending && <Loader2 className="animate-spin" data-icon="inline-start" />}- Subscribe to Pro- </Button>- </>+ ({ body, terms } = subscribeOfferCopy({+ credits: offer.data.credits,+ expiresAt: null,+ lifetimeDays: offer.data.lifetimeDays,+ }));+ cta = (+ <OfferButton disabled={checkout.isPending} onClick={subscribe}>+ {checkout.isPending && <Loader2 className="animate-spin" data-icon="inline-start" />}+ Subscribe to Pro+ </OfferButton>);} else {- body = `You have ${offer.data.credits} in AI credits. They’re waiting for you.${- offer.data.lifetime ? ` Once claimed, it’s good for ${offer.data.lifetime}.` : ""- }${offer.data.closesAt ? ` Claim by ${formatCreditExpiry(new Date(offer.data.closesAt))}.` : ""}`;- footer = (- <>- <Button variant="ghost" onClick={onClose}>- Not now- </Button>- <Button disabled={claim.isPending} onClick={() => claim.mutate(token)}>- {claim.isPending && <Loader2 className="animate-spin" data-icon="inline-start" />}- Claim {offer.data.credits}- </Button>- </>+ title = `${offer.data.credits} in credits is waiting`;+ ({ body, terms } = claimOfferCopy({+ credits: offer.data.credits,+ closesAt: offer.data.closesAt,+ lifetimeDays: offer.data.lifetimeDays,+ }));+ cta = (+ <OfferButton disabled={claim.isPending} onClick={() => claim.mutate(token)}>+ {claim.isPending && <Loader2 className="animate-spin" data-icon="inline-start" />}+ Claim {offer.data.credits}+ </OfferButton>);}+ const error = checkout.isError+ ? "Billing is unavailable right now."+ : claim.isError+ ? claim.error instanceof ApiError && claim.error.status === 403+ ? "This offer belongs to a different account. Sign in with the address the email was sent to."+ : claim.error instanceof ApiError && claim.error.status === 410+ ? "The claim window for this offer has closed."+ : "That didn’t go through. Try again."+ : null;+return (- <Dialog open onOpenChange={(next) => !next && onClose()}>- <DialogContent className="top-[18%] translate-y-0 px-6 sm:max-w-md">- <DialogHeader>- <DialogTitle className="flex items-center gap-2 text-lg">- <Sparkle className="size-5 shrink-0 fill-violet-500/25 text-violet-500" />- {title}- </DialogTitle>- <DialogDescription className="min-h-12 text-base">{body}</DialogDescription>- </DialogHeader>- <DialogFooter className="mx-0 mb-0 items-center border-0 bg-transparent p-0">- {checkout.isError && (- <p className="text-sm text-destructive sm:mr-auto">Billing is unavailable right now.</p>- )}- {claim.isError && (- <p className="text-sm text-destructive sm:mr-auto">- {claim.error instanceof ApiError && claim.error.status === 403- ? "This offer belongs to a different account. Sign in with the address the email was sent to."- : claim.error instanceof ApiError && claim.error.status === 410- ? "The claim window for this offer has closed."- : "That didn’t go through. Try again."}- </p>- )}- {footer}- </DialogFooter>- </DialogContent>- </Dialog>+ <OfferDialog+ open+ onOpenChange={(next) => !next && onClose()}+ banner={offer.data?.claim === "subscribe" ? "subscribe" : "credits"}+ closesAt={offer.data && !done ? offer.data.closesAt : null}+ title={title}+ body={body}+ terms={terms}+ cta={cta}+ secondary={secondary}+ error={error}+ />);}diff --git a/site/src/cut/components/OfferDialog.tsx b/site/src/cut/components/OfferDialog.tsxnew file mode 100644index 00000000..42f8c4c4--- /dev/null+++ b/site/src/cut/components/OfferDialog.tsx@@ -0,0 +1,163 @@+"use client";++// The face of a credit offer, whichever way it reached the person: a picture+// across the top, a title, a line of body, and one full-width button. The+// email's claim link and the offer in the top bar both open this, so an+// offer looks the same from anywhere.+import Image from "next/image";+import { useEffect, useState } from "react";+import { CUT_PRO } from "@/app/cut/_components/landing/cutPricingPlans";+import { Button } from "@/components/ui/button";+import { formatCreditExpiry } from "@/lib/credits/top-up";+import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "@/components/ui/dialog";++// One picture per kind of offer: a gift for credit that comes with Pro, coins+// for credit that is simply waiting to be claimed.+export const OFFER_BANNERS = {+ subscribe: "/cut/offer-banner.webp",+ credits: "/cut/credits-banner.webp",+} as const;+export type OfferBanner = keyof typeof OFFER_BANNERS;++/** A day as the card names it: "September 30". */+export function offerDay(iso: string): string {+ return new Date(iso).toLocaleDateString("en-US", { day: "numeric", month: "long" });+}++/** An offer's words: the message, and the terms line under it. */+export type OfferCopy = { body: string; terms: string | null };++/** How long the credit lasts once it lands, as the card says it; null when+ * it keeps forever. */+export function creditValidity(offer: { expiresAt: string | null; lifetimeDays: number | null }): string | null {+ if (offer.expiresAt) return `Extra credits are valid through ${offerDay(offer.expiresAt)}.`;+ if (offer.lifetimeDays !== null) return `Extra credits are valid for ${offer.lifetimeDays} days.`;+ return null;+}++/** The claim's words: the credit that is waiting and the last day to take it,+ * then how long it lasts once taken. */+export function claimOfferCopy(offer: { credits: string; closesAt: string | null; lifetimeDays: number | null }): OfferCopy {+ const by = offer.closesAt ? ` Claim them by ${formatCreditExpiry(new Date(offer.closesAt))}.` : "";+ return {+ body: `You have ${offer.credits} in AI credits.${by}`,+ terms: offer.lifetimeDays !== null ? `Once claimed, they’re valid for ${offer.lifetimeDays} days.` : null,+ };+}++/** The Pro offer's words. The deadline is the countdown's, so the message+ * leaves it out. */+export function subscribeOfferCopy(offer: {+ credits: string;+ expiresAt: string | null;+ lifetimeDays: number | null;+}): OfferCopy {+ return {+ body: `Subscribe to Pro for ${CUT_PRO.price} and get ${offer.credits} in extra AI credits.`,+ terms: creditValidity(offer),+ };+}++/** What is left until a moment, as the countdown reads it: hours, minutes+ * and seconds, the hours running past a day. */+export function countdownLabel(closesAt: string, now: number): string {+ const left = Math.max(0, Math.floor((Date.parse(closesAt) - now) / 1000));+ const hours = Math.floor(left / 3600);+ const minutes = Math.floor((left % 3600) / 60);+ const seconds = left % 60;+ const two = (n: number) => String(n).padStart(2, "0");+ return `${hours}h ${two(minutes)}m ${two(seconds)}s`;+}++/** The clock the countdown reads; one tick a second while the card is open. */+function useNow(): number {+ const [now, setNow] = useState(() => Date.now());+ useEffect(() => {+ const id = setInterval(() => setNow(Date.now()), 1000);+ return () => clearInterval(id);+ }, []);+ return now;+}++function Countdown({ closesAt }: { closesAt: string }) {+ const now = useNow();+ return (+ <span className="absolute top-3 left-3 rounded-md bg-foreground/85 px-3 py-1.5 text-sm font-medium tabular-nums text-background">+ Offer expires in {countdownLabel(closesAt, now)}+ </span>+ );+}++export function OfferDialog({+ open,+ onOpenChange,+ banner,+ closesAt,+ title,+ body,+ terms,+ cta,+ secondary,+ error,+}: {+ open: boolean;+ onOpenChange: (open: boolean) => void;+ banner: OfferBanner;+ // The last moment the offer can be taken up; a countdown sits over the+ // picture while there is one.+ closesAt?: string | null;+ title: React.ReactNode;+ body: React.ReactNode;+ // The fine print under the message, on its own line.+ terms?: React.ReactNode;+ // The one thing to do, full width.+ cta: React.ReactNode;+ // A quieter way out beneath it, when there is one.+ secondary?: React.ReactNode;+ error?: React.ReactNode;+}) {+ return (+ <Dialog open={open} onOpenChange={onOpenChange}>+ <DialogContent+ showCloseButton={false}+ className="top-[18%] translate-y-0 gap-0 overflow-hidden rounded-lg bg-background p-0 sm:max-w-sm"+ >+ <div className="relative">+ <Image+ src={OFFER_BANNERS[banner]}+ alt=""+ className="aspect-[8/3] w-full object-cover"+ width={1584}+ height={672}+ priority+ />+ {closesAt ? <Countdown closesAt={closesAt} /> : null}+ </div>+ <div className="flex flex-col gap-5 p-6">+ <DialogHeader className="gap-2">+ <DialogTitle className="text-2xl leading-tight font-medium tracking-tight">{title}</DialogTitle>+ <DialogDescription className="min-h-12 text-base leading-relaxed text-muted-foreground">+ {body}+ </DialogDescription>+ {terms ? <p className="text-base leading-relaxed text-muted-foreground">{terms}</p> : null}+ </DialogHeader>+ {error ? <p className="text-sm text-destructive">{error}</p> : null}+ <div className="mt-5 flex flex-col gap-2">+ {cta}+ {secondary}+ </div>+ </div>+ </DialogContent>+ </Dialog>+ );+}++/** The full-width button the card ends in. */+export function OfferButton(props: React.ComponentProps<typeof Button>) {+ return <Button size="lg" className="h-12 w-full rounded-md text-base font-semibold" {...props} />;+}++/** The quieter way out under the button. */+export function OfferDismiss(props: React.ComponentProps<typeof Button>) {+ return <Button variant="ghost" className="w-full text-muted-foreground" {...props} />;+}diff --git a/site/src/cut/components/SubscribeBonusPill.tsx b/site/src/cut/components/SubscribeBonusPill.tsxindex b6aed54a..b40ac44e 100644--- a/site/src/cut/components/SubscribeBonusPill.tsx+++ b/site/src/cut/components/SubscribeBonusPill.tsx@@ -8,21 +8,11 @@import { useEffect, useState } from "react";import { useQueryClient } from "@tanstack/react-query";import { Gift } from "lucide-react";-import { CUT_PRO } from "@/app/cut/_components/landing/cutPricingPlans";-import { Button } from "@/components/ui/button";-import {- Dialog,- DialogContent,- DialogDescription,- DialogFooter,- DialogHeader,- DialogTitle,-} from "@/components/ui/dialog";+import { creditValidity, OfferButton, OfferDialog, OfferDismiss, subscribeOfferCopy } from "@/cut/components/OfferDialog";import { useEngineUser } from "@/cut/lib/backend/hooks";import { useInvalidateOnSettle } from "@/cut/lib/hosted";import { track } from "@/lib/analytics";import { formatUsd } from "@/lib/credits/format-usd";-import { formatCreditExpiry } from "@/lib/credits/top-up";import { useStartCheckout } from "@/queries/billing";import { subscribeBonusQueryKey, useSubscribeBonus, type SubscribeBonus } from "@/queries/credits";@@ -43,11 +33,10 @@ export function timeLeftLabel(minutes: number): string {return `${minutes}m left`;}-/** How long the landed credit lasts, as a clause after the amount. */-export function creditLifeClause(offer: { creditsExpireAt: string | null; creditsLifetime: string | null }): string {- if (offer.creditsExpireAt) return `, spendable through ${formatCreditExpiry(new Date(offer.creditsExpireAt))}`;- if (offer.creditsLifetime) return `, good for ${offer.creditsLifetime}`;- return "";+/** How long the landed credit lasts, as the sentence after the offer. */+export function creditLifeClause(offer: { creditsExpireAt: string | null; creditsLifetimeDays: number | null }): string {+ const valid = creditValidity({ expiresAt: offer.creditsExpireAt, lifetimeDays: offer.creditsLifetimeDays });+ return valid ? ` ${valid}` : "";}/** The deadline as people read it in their own clock: "Sep 8, 3:14 PM". */@@ -139,29 +128,25 @@ function OpenOffer({ offer }: { offer: SubscribeBonus }) {<Gift className="size-3.5" />{dollars} with Pro · {timeLeftLabel(minutes)}</button>- <Dialog open={open} onOpenChange={setOpen}>- <DialogContent className="sm:max-w-md">- <DialogHeader>- <DialogTitle>Get {dollars} in credits with Pro</DialogTitle>- <DialogDescription>- {offer.origin === "app" ? "You have used half of your signup credits. " : ""}- Subscribe to Pro for {CUT_PRO.price} by {formatDeadline(offer.closesAt)} and a one-time {dollars} in- credits lands in your account{creditLifeClause(offer)}.- </DialogDescription>- </DialogHeader>- <DialogFooter>- <Button variant="ghost" onClick={() => setOpen(false)}>- Not now- </Button>- <Button disabled={checkout.isPending} onClick={subscribe}>- {checkout.isPending ? "Starting…" : "Subscribe to Pro"}- </Button>- </DialogFooter>- {checkout.isError ? (- <p className="text-sm text-destructive">Billing is unavailable right now.</p>- ) : null}- </DialogContent>- </Dialog>+ <OfferDialog+ open={open}+ onOpenChange={setOpen}+ banner="subscribe"+ title={`Get ${dollars} in credits with Pro`}+ closesAt={offer.closesAt}+ {...subscribeOfferCopy({+ credits: dollars,+ expiresAt: offer.creditsExpireAt,+ lifetimeDays: offer.creditsLifetimeDays,+ })}+ cta={+ <OfferButton disabled={checkout.isPending} onClick={subscribe}>+ {checkout.isPending ? "Starting…" : "Subscribe to Pro"}+ </OfferButton>+ }+ secondary={<OfferDismiss onClick={() => setOpen(false)}>Not now</OfferDismiss>}+ error={checkout.isError ? "Billing is unavailable right now." : null}+ /></>);}