Open source CapCut alternative.
TypeScript
feat(site): open the upgrade dialog from Generate
David Leb8bf23234256
diff --git a/site/src/cut/components/AudioPanel.tsx b/site/src/cut/components/AudioPanel.tsxindex a7f7a9c8..718122a6 100644--- a/site/src/cut/components/AudioPanel.tsx+++ b/site/src/cut/components/AudioPanel.tsx@@ -48,7 +48,7 @@ import { STOCK_MUSIC } from "@/cut/lib/stockMusicManifest";import { stockAssetInDoc } from "@/cut/lib/genvideo/docWriter";import { signInUrl, useSignedIn } from "@/cut/lib/generate";import { HostedErrorText } from "./hostedError";-import { CostTag, generateButton, NOT_ENOUGH_CREDITS, useGenerateCost } from "./GenerateCost";+import { generateButton, useGenerateCost } from "./GenerateCost";import { musicMicros } from "@/lib/credits/price-sheet";import { genPulseOverlay, useActiveWork, useGenNotify } from "@/cut/lib/genNotify";import { enrichAsset } from "@/cut/lib/media";@@ -312,13 +312,12 @@ function MusicGenerator({ projectId }: { projectId: string }) {</div><ButtonclassName="music-generate w-full"- disabled={!prompt.trim() || signedOut || button.blocked}+ disabled={!prompt.trim() || signedOut}title={!prompt.trim() ? "Describe the music above to generate" : undefined}onClick={button.onClick}><Music data-icon="inline-start" />- {cost.short ? NOT_ENOUGH_CREDITS : "Generate music"}- <CostTag cost={cost} />+ Generate music</Button>{pending > 0 && (<div className="flex items-center gap-2 text-[11px] text-muted-foreground">diff --git a/site/src/cut/components/GenerateCost.tsx b/site/src/cut/components/GenerateCost.tsxindex ffa03ebb..377e9e4d 100644--- a/site/src/cut/components/GenerateCost.tsx+++ b/site/src/cut/components/GenerateCost.tsx@@ -1,42 +1,31 @@"use client";-// The price of the next generation, shown on its Generate button before it-// spends anything. A price above the balance disables the button, or, while-// the out-of-credits upsell is on, turns it into the way to the upgrade wall;-// the 402 the server would answer is the same check, in the same micros.-import { Zap } from "lucide-react";+// Whether the balance covers the next generation, checked before it spends+// anything. A Generate button the balance can't cover opens the upgrade wall+// when pressed; the 402 the server would answer is the same check, in the+// same micros.import { useEnvironment } from "@/cut/lib/environment";import { useSignedIn } from "@/cut/lib/generate";import { openUpgrade } from "@/cut/lib/upgrade";-import { creditMicrosToString, creditStringToMicros } from "@/lib/credits/amounts";-import { creditsFromDollars, formatCreditCount } from "@/lib/credits/format-credits";+import { creditStringToMicros } from "@/lib/credits/amounts";import type { CreditPriceSheet } from "@/lib/credits/price-sheet";-import { useAccountConfig } from "@/queries/accountConfig";import { useCreditBalance, useCreditPriceSheet } from "@/queries/credits";import { useLiveCreditBalance } from "./CreditsPill";-/** The button's label when the balance can't cover the price. */-export const NOT_ENOUGH_CREDITS = "Not enough credits";-export type GenerateCost = {- /** Credits the generation costs, rounded up; null until the prices are read. */- credits: number | null;/** The balance is read and falls short of the price. */short: boolean;- /** Short, and the button opens the upgrade wall when pressed. */- walled: boolean;};-const UNPRICED: GenerateCost = { credits: null, short: false, walled: false };+const UNPRICED: GenerateCost = { short: false };/** Prices one generation from the sheet, in micros; null leaves it unpriced.- * The ChatGPT card carries no credit readings, so the price stays off there. */+ * The ChatGPT card carries no credit readings, so the check stays off there. */export function useGenerateCost(price: (sheet: CreditPriceSheet) => bigint | null): GenerateCost {const { chatgpt } = useEnvironment();const enabled = useSignedIn() === true && !chatgpt;const sheet = useCreditPriceSheet({ enabled });const balance = useCreditBalance({ enabled, freshness: "reuse" });- const config = useAccountConfig({ enabled });useLiveCreditBalance();if (!enabled || !sheet.data) {return UNPRICED;@@ -46,30 +35,12 @@ export function useGenerateCost(price: (sheet: CreditPriceSheet) => bigint | nulif (micros === null) {return UNPRICED;}- const credits = creditsFromDollars(creditMicrosToString(micros), "up");const short = balance.data ? creditStringToMicros(balance.data.balance) < micros : false;- const walled = short && config.data?.settings.outOfCreditsUpsell.mode === "dialog";- return { credits, short, walled };-}--/** The Generate button's state: disabled when short with no wall to show,- * and pressing it opens the wall in place of generating when there is one. */-export function generateButton(cost: GenerateCost, go: () => void): { blocked: boolean; onClick: () => void } {- return {- blocked: cost.short && !cost.walled,- onClick: cost.walled ? () => void openUpgrade("credits") : go,- };+ return { short };}-/** The price beside the button label: "⚡ 1,318". */-export function CostTag({ cost }: { cost: GenerateCost }) {- if (cost.credits === null) {- return null;- }- return (- <span className="ml-1 inline-flex items-center gap-0.5 tabular-nums opacity-80">- <Zap className="size-3" />- {formatCreditCount(cost.credits)}- </span>- );+/** The Generate button's press: the upgrade wall when the balance falls+ * short, the generation otherwise. */+export function generateButton(cost: GenerateCost, go: () => void): { onClick: () => void } {+ return { onClick: cost.short ? () => void openUpgrade("credits") : go };}diff --git a/site/src/cut/components/GeneratePanel.tsx b/site/src/cut/components/GeneratePanel.tsxindex 21f94ce3..5d182ac3 100644--- a/site/src/cut/components/GeneratePanel.tsx+++ b/site/src/cut/components/GeneratePanel.tsx@@ -40,7 +40,7 @@ import { CopyHandlePill, MentionTextarea, RefChips } from "./AssetRefs";import { DictationControl } from "./MicDictation";import { GeneratedAssetMenu } from "./GeneratedAssetMenu";import { HostedErrorText } from "./hostedError";-import { CostTag, generateButton, NOT_ENOUGH_CREDITS, useGenerateCost } from "./GenerateCost";+import { generateButton, useGenerateCost } from "./GenerateCost";import { cardIconButton } from "./iconButton";import { PillSelect } from "./PillSelect";import { ScrollArea } from "@/components/ui/scroll-area";@@ -272,12 +272,11 @@ export function GenerateVideoPanel({ projectId }: { projectId: string }) {<ButtonclassName="gen-go w-full shrink-0"- disabled={!prompt.trim() || signedIn === false || button.blocked}+ disabled={!prompt.trim() || signedIn === false}onClick={button.onClick}><Sparkles data-icon="inline-start" />- {cost.short ? NOT_ENOUGH_CREDITS : "Generate video"}- <CostTag cost={cost} />+ Generate video</Button>{signedIn === false ? (diff --git a/site/src/cut/components/ImageGenPanel.tsx b/site/src/cut/components/ImageGenPanel.tsxindex 10f9234b..edb47dd9 100644--- a/site/src/cut/components/ImageGenPanel.tsx+++ b/site/src/cut/components/ImageGenPanel.tsx@@ -31,7 +31,7 @@ import { cardIconButton } from "./iconButton";import { PillSelect } from "./PillSelect";import { GeneratedAssetMenu } from "./GeneratedAssetMenu";import { HostedErrorText } from "./hostedError";-import { CostTag, generateButton, NOT_ENOUGH_CREDITS, useGenerateCost } from "./GenerateCost";+import { generateButton, useGenerateCost } from "./GenerateCost";import { ScrollArea } from "@/components/ui/scroll-area";// The generate-image panel: an always-on column in the Image tab, sitting left@@ -185,12 +185,11 @@ export function ImageGenPanel({ projectId }: { projectId: string }) {<ButtonclassName="w-full shrink-0"- disabled={!prompt.trim() || signedIn === false || button.blocked}+ disabled={!prompt.trim() || signedIn === false}onClick={button.onClick}><Sparkles data-icon="inline-start" />- {cost.short ? NOT_ENOUGH_CREDITS : "Generate image"}- <CostTag cost={cost} />+ Generate image</Button>{signedIn === false && (diff --git a/site/src/lib/config/registry.ts b/site/src/lib/config/registry.tsindex 73a3e8f6..b28a9902 100644--- a/site/src/lib/config/registry.ts+++ b/site/src/lib/config/registry.ts@@ -230,7 +230,7 @@ export const SETTINGS = defineSettings({public: true,title: "Out of credits upsell",description:- "What happens when an AI request runs out of credits. Off leaves the inline notice; dialog also opens the upgrade dialog, which sells Pro to a free account and credits to a Pro one.",+ "What happens when the server refuses a request for credits. Off leaves the inline notice; dialog also opens the upgrade dialog, which sells Pro to a free account and credits to a Pro one. A Generate button the balance can't cover opens the dialog either way.",},});