The VS Code of video editing, built for agents.
TypeScript
Add purchase success poster and enhance success dialog
konstantin-paulus77eee77313f6
diff --git a/apps/web/src/assets/images/purchase-success-poster.webp b/apps/web/src/assets/images/purchase-success-poster.webpnew file mode 100644index 00000000..46a72a58Binary files /dev/null and b/apps/web/src/assets/images/purchase-success-poster.webp differdiff --git a/apps/web/src/components/purchase-success.css b/apps/web/src/components/purchase-success.cssnew file mode 100644index 00000000..c45af232--- /dev/null+++ b/apps/web/src/components/purchase-success.css@@ -0,0 +1,148 @@+/* This Source Code Form is subject to the terms of the Mozilla Public+ * License, v. 2.0. If a copy of the MPL was not distributed with this+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */++/* The moment right after checkout. The poster settles into place and the+ copy rises in behind it, one line after another. Afterwards the poster+ leans toward the pointer. Anyone who asked the system for less motion+ sees the settled state, and a poster that holds still. */++.ps-dialog {+ --ps-ease-out: cubic-bezier(0.22, 1, 0.36, 1);+}++.ps-poster {+ animation: ps-settle 1.2s var(--ps-ease-out) both;+}++/* Tilt: the frame clips, the image inside leans. The zoom keeps the receding+ edges from pulling clear of the frame. Tracking eases a little so the+ motion reads smooth; leaving eases back slower. */++.ps-poster {+ perspective: 800px;+}++.ps-poster-image {+ transform: rotateX(var(--ps-tilt-x, 0deg)) rotateY(var(--ps-tilt-y, 0deg))+ scale(1);+ transition: transform 0.6s var(--ps-ease-out);+}++.ps-poster[data-tilting] .ps-poster-image {+ transform: rotateX(var(--ps-tilt-x, 0deg)) rotateY(var(--ps-tilt-y, 0deg))+ scale(1.08);+ transition-duration: 0.15s;+}++.ps-poster-glare {+ background: radial-gradient(+ circle at var(--ps-glare-x, 50%) var(--ps-glare-y, 50%),+ rgb(255 255 255 / 0.35),+ transparent 55%+ );+ mix-blend-mode: soft-light;+ opacity: 0;+ transition: opacity 0.4s ease-out;+}++.ps-poster[data-tilting] .ps-poster-glare {+ opacity: 1;+}++@keyframes ps-settle {+ from {+ opacity: 0;+ transform: scale(1.08);+ }+ to {+ opacity: 1;+ transform: scale(1);+ }+}++.ps-rise {+ animation: ps-rise 0.5s var(--ps-ease-out) both;+ animation-delay: var(--ps-delay, 0s);+}++@keyframes ps-rise {+ from {+ opacity: 0;+ transform: translateY(6px);+ }+ to {+ opacity: 1;+ transform: translateY(0);+ }+}++/* Button: a band of white light crosses it once it has risen in, and again+ on every hover, echoing the light streak in the poster. Hover needs its+ own keyframes, since only a new animation name restarts one. Hover lifts+ the blue rather than darkening it. */++.ps-cta {+ transition:+ transform 0.15s var(--ps-ease-out),+ filter 0.15s;+}++.ps-cta:hover {+ background-color: var(--primary);+ filter: brightness(1.08);+}++.ps-cta:active {+ transform: scale(0.98);+}++.ps-cta-sheen {+ width: 45%;+ background: linear-gradient(+ 105deg,+ transparent 0%,+ rgb(255 255 255 / 0.4) 50%,+ transparent 100%+ );+ transform: translateX(-100%);+ animation: ps-sheen 1s cubic-bezier(0.45, 0, 0.25, 1) 0.8s;+}++.ps-cta:hover .ps-cta-sheen {+ animation: ps-sheen-hover 0.8s cubic-bezier(0.45, 0, 0.25, 1);+}++@keyframes ps-sheen {+ from {+ transform: translateX(-100%);+ }+ to {+ transform: translateX(325%);+ }+}++@keyframes ps-sheen-hover {+ from {+ transform: translateX(-100%);+ }+ to {+ transform: translateX(325%);+ }+}++@media (prefers-reduced-motion: reduce) {+ .ps-poster,+ .ps-rise {+ animation: none;+ }++ .ps-poster-glare,+ .ps-cta-sheen {+ display: none;+ }++ .ps-cta:active {+ transform: none;+ }+}diff --git a/apps/web/src/components/purchase-success.tsx b/apps/web/src/components/purchase-success.tsxindex 1d6bb4da..fba1d5d4 100644--- a/apps/web/src/components/purchase-success.tsx+++ b/apps/web/src/components/purchase-success.tsx@@ -3,23 +3,166 @@* file, You can obtain one at http://mozilla.org/MPL/2.0/. */import { useSearchParams } from "@solidjs/router";-import { Show, createEffect, createSignal, onCleanup, onMount } from "solid-js";+import { Dialog as DialogPrimitive } from "@kobalte/core/dialog";+import { Match, Switch, createEffect, createSignal, onCleanup, onMount, untrack } from "solid-js";+import type { Accessor } from "solid-js";+import type { PaidPlan } from "@diffusionstudio/api-contract";-import { Icon } from "@/components/ui/icon";+import { Button } from "@/components/ui/button";+import { Dialog, DialogContent, DialogPortal } from "@/components/ui/dialog";import { useAuth } from "@/context/auth";+import { track } from "@/lib/analytics";+import { PLANS } from "@/lib/checkout";import { mainBridge } from "@/lib/ipc";import { MAIN_CHANNELS } from "@desktop/main-channels";+import "./purchase-success.css";++/** What checkout sold, read back from the params `successAndCancelUrls` put on the success URL. */+type Purchase =+ | { kind: "plan"; plan: PaidPlan }+ | { kind: "credits"; credits: number }+ | { kind: "unknown" };++function readPurchase(get: (key: string) => string | null | undefined): Purchase {+ const plan = get("plan");+ if (plan && plan in PLANS) return { kind: "plan", plan: plan as PaidPlan };++ const credits = Number(get("credits"));+ if (Number.isInteger(credits) && credits > 0) return { kind: "credits", credits };++ return { kind: "unknown" };+}++/** Counts from 0 up to `target` once the copy has risen in, easing out. */+function createCountUp(target: Accessor<number>, active: Accessor<boolean>): Accessor<number> {+ const [value, setValue] = createSignal(0);++ createEffect(() => {+ const to = target();+ if (!active()) return;++ if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) {+ setValue(to);+ return;+ }++ const delay = 300;+ const duration = 900;+ const start = performance.now();+ let frame = requestAnimationFrame(function tick(now) {+ const progress = Math.min(Math.max((now - start - delay) / duration, 0), 1);+ setValue(Math.round(to * (1 - (1 - progress) ** 3)));+ if (progress < 1) frame = requestAnimationFrame(tick);+ });++ onCleanup(() => cancelAnimationFrame(frame));+ });++ return value;+}++const POSTER = new URL("@/assets/images/purchase-success-poster.webp", import.meta.url).href;++/** Each plan's headline: a play on its name. */+const PLAN_TITLES: Record<PaidPlan, string> = {+ pro: "You've gone Pro",+ plus: "All Plus, no minus",+ max: "Turned up to Max",+};++/** Most the poster leans toward the pointer, in degrees on each axis. */+const MAX_TILT = 7;++/**+ * The header image. Under a mouse it leans toward the pointer, the point under+ * it pressed in, with a highlight following along, and settles flat on leave.+ */+function Poster() {+ let frame!: HTMLDivElement;++ const settle = () => {+ delete frame.dataset.tilting;+ frame.style.removeProperty("--ps-tilt-x");+ frame.style.removeProperty("--ps-tilt-y");+ };++ // Tracked on the dialog rather than the poster: the close button sits over+ // the poster's corner and would otherwise read as the pointer leaving.+ const tilt = (e: PointerEvent) => {+ if (e.pointerType === "touch") return;+ if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) return;++ const rect = frame.getBoundingClientRect();+ const x = (e.clientX - rect.left) / rect.width;+ const y = (e.clientY - rect.top) / rect.height;+ if (x < 0 || x > 1 || y < 0 || y > 1) {+ settle();+ return;+ }++ frame.dataset.tilting = "";+ frame.style.setProperty("--ps-tilt-x", `${(0.5 - y) * 2 * MAX_TILT}deg`);+ frame.style.setProperty("--ps-tilt-y", `${(x - 0.5) * 2 * MAX_TILT}deg`);+ frame.style.setProperty("--ps-glare-x", `${x * 100}%`);+ frame.style.setProperty("--ps-glare-y", `${y * 100}%`);+ };++ onMount(() => {+ const dialog = frame.parentElement;+ if (!dialog) return;+ dialog.addEventListener("pointermove", tilt);+ dialog.addEventListener("pointerleave", settle);++ onCleanup(() => {+ dialog.removeEventListener("pointermove", tilt);+ dialog.removeEventListener("pointerleave", settle);+ });+ });++ return (+ <div ref={frame} class="ps-poster relative aspect-video w-full overflow-hidden">+ <img src={POSTER} alt="" class="ps-poster-image size-full object-cover" draggable={false} />+ <div class="ps-poster-glare pointer-events-none absolute inset-0" />+ </div>+ );+}++function formatCredits(value: number): string {+ return value.toLocaleString();+}++/** Holds the width of the final number so centered copy does not shift while it counts. */+function CountUp(props: { value: number; target: number }) {+ return (+ <span class="inline-grid tabular-nums">+ <span class="invisible col-start-1 row-start-1">{formatCredits(props.target)}</span>+ <span class="col-start-1 row-start-1 text-right">{formatCredits(props.value)}</span>+ </span>+ );+}+export function PurchaseSuccess() {const auth = useAuth();const [params, setParams] = useSearchParams();- const [deepLinkSuccess, setDeepLinkSuccess] = createSignal(false);+ const [open, setOpen] = createSignal(false);+ // Kept after closing so the copy does not change while the dialog fades out.+ const [purchase, setPurchase] = createSignal<Purchase>({ kind: "unknown" });- const succeeded = () => params.checkout === "success" || deepLinkSuccess();+ const show = (next: Purchase) => {+ setPurchase(next);+ setOpen(true);+ auth.refreshAccount();+ track("purchase_success_viewed", {+ kind: next.kind,+ ...(next.kind === "plan" && { plan: next.plan }),+ ...(next.kind === "credits" && { credits: next.credits }),+ });+ };createEffect(() => {- if (succeeded()) {- auth.refreshAccount();+ if (params.checkout === "success" && !untrack(open)) {+ show(readPurchase((key) => params[key] as string | undefined));}});@@ -31,8 +174,9 @@ export function PurchaseSuccess() {const handleCallbackUrl = (url: string | null) => {if (!url) return;try {- if (new URL(url).searchParams.get("status") === "success") {- setDeepLinkSuccess(true);+ const search = new URL(url).searchParams;+ if (search.get("status") === "success") {+ show(readPurchase((key) => search.get(key)));}} catch {// Malformed deep link — nothing to show.@@ -50,25 +194,77 @@ export function PurchaseSuccess() {onCleanup(unsubscribe);});- const close = (e: MouseEvent) => {- e.stopPropagation();- e.preventDefault();- setDeepLinkSuccess(false);- setParams({ checkout: undefined }, { replace: true });+ const close = () => {+ setOpen(false);+ if (params.checkout) {+ setParams({ checkout: undefined, plan: undefined, credits: undefined }, { replace: true });+ }};+ const plan = () => {+ const current = purchase();+ return current.kind === "plan" ? current.plan : undefined;+ };++ const credits = () => {+ const current = purchase();+ if (current.kind === "plan") return PLANS[current.plan].monthlyCredits;+ if (current.kind === "credits") return current.credits;+ return 0;+ };+ const counted = createCountUp(credits, open);+return (- <Show when={succeeded()}>- <div- class="pointer-events-auto fixed inset-0 z-[999] flex flex-col items-center justify-center gap-2 bg-overlay-strong"- onClick={close}- >- <Icon name="confirm-check" class="size-12 text-foreground" />- <h4 class="text-2xl">Thank you for your purchase!</h4>- <span class="text-[13px] text-foreground/70">- You can click anywhere to close this window and continue using the app.- </span>- </div>- </Show>+ <Dialog open={open()} onOpenChange={(next) => !next && close()}>+ <DialogPortal>+ <DialogContent+ class="ps-dialog z-[1000] gap-0 overflow-hidden rounded-xl p-0 sm:max-w-[360px] [&>button[aria-label=Close]]:text-white"+ overlayClass="z-[1000]"+ >+ <Poster />++ <div class="flex flex-col items-center gap-2 px-6 pt-6 text-center">+ <DialogPrimitive.Title+ class="ps-rise text-xl leading-7 font-semibold tracking-tight text-foreground"+ style={{ "--ps-delay": "0.15s" }}+ >+ <Switch fallback="Purchase complete">+ <Match when={plan()}>+ {(paid) => PLAN_TITLES[paid()]}+ </Match>+ <Match when={purchase().kind === "credits"}>+ <CountUp value={counted()} target={credits()} /> credits added+ </Match>+ </Switch>+ </DialogPrimitive.Title>++ <DialogPrimitive.Description+ class="ps-rise text-[13px] leading-5 text-balance text-muted-foreground"+ style={{ "--ps-delay": "0.25s" }}+ >+ <Switch fallback="Thank you for supporting Diffusion Studio.">+ <Match when={plan()}>+ Your{" "}+ <span class="text-foreground">+ <CountUp value={counted()} target={credits()} /> monthly credits+ </span>{" "}+ are ready to use.+ </Match>+ <Match when={purchase().kind === "credits"}>+ They're in your balance and ready to use.+ </Match>+ </Switch>+ </DialogPrimitive.Description>+ </div>++ <div class="ps-rise px-6 pt-6 pb-6" style={{ "--ps-delay": "0.35s" }}>+ <Button class="ps-cta relative h-8 w-full overflow-hidden" onClick={close}>+ {purchase().kind === "plan" ? "Start creating" : "Continue"}+ <span class="ps-cta-sheen pointer-events-none absolute inset-y-0 left-0" aria-hidden="true" />+ </Button>+ </div>+ </DialogContent>+ </DialogPortal>+ </Dialog>);}diff --git a/apps/web/src/lib/checkout.ts b/apps/web/src/lib/checkout.tsindex 9df05cd7..afaf36d9 100644--- a/apps/web/src/lib/checkout.ts+++ b/apps/web/src/lib/checkout.ts@@ -79,10 +79,19 @@ export function getTopupPrice(tier: TopupCredits): number {const ELECTRON_CHECKOUT_REDIRECT ="https://app.diffusion.studio/checkout/electron-callback.html";-function successAndCancelUrls(): { successUrl: string; cancelUrl: string } {+/**+ * `purchase` rides along on the success URL so the success dialog can name what+ * was bought before the Stripe webhook has updated the account.+ */+function successAndCancelUrls(+ purchase: { plan: PaidPlan } | { credits: number },+): { successUrl: string; cancelUrl: string } {+ const purchaseParams = Object.entries(purchase).map(([key, value]) => [key, String(value)]);+if (window.desktop) {+ const success = new URLSearchParams([["status", "success"], ...purchaseParams]);return {- successUrl: `${ELECTRON_CHECKOUT_REDIRECT}?status=success`,+ successUrl: `${ELECTRON_CHECKOUT_REDIRECT}?${success}`,cancelUrl: `${ELECTRON_CHECKOUT_REDIRECT}?status=cancel`,};}@@ -90,6 +99,7 @@ function successAndCancelUrls(): { successUrl: string; cancelUrl: string } {const cancelUrl = window.location.href;const success = new URL(window.location.href);success.searchParams.set("checkout", "success");+ for (const [key, value] of purchaseParams) success.searchParams.set(key, value);return { successUrl: success.toString(), cancelUrl };}@@ -110,7 +120,7 @@ export async function startSubscriptionCheckout(input: {try {const { url } = await api.billing.subscribe.mutate({...input,- ...successAndCancelUrls(),+ ...successAndCancelUrls({ plan: input.plan }),});await openCheckoutUrl(url);} catch (err) {@@ -124,7 +134,7 @@ export async function startTopupCheckout(try {const { url } = await api.billing.topup.mutate({creditQuantity,- ...successAndCancelUrls(),+ ...successAndCancelUrls({ credits: topupCredits(creditQuantity) }),});await openCheckoutUrl(url);} catch (err) {