Skip to content

Open source CapCut alternative.

TypeScript

feat(site): show cloud storage use in the Cut sidebar

David Le7671c1dcddda
diff --git a/site/src/components/ui/meter.tsx b/site/src/components/ui/meter.tsx
new file mode 100644
index 00000000..17b81b52
--- /dev/null
+++ b/site/src/components/ui/meter.tsx
@@ -0,0 +1,25 @@
+import { Meter as MeterPrimitive } from "@base-ui/react/meter"
+
+import { cn } from "@/lib/utils"
+
+function Meter({
+ className,
+ indicatorClassName,
+ ...props
+}: MeterPrimitive.Root.Props & { indicatorClassName?: string }) {
+ return (
+ <MeterPrimitive.Root data-slot="meter" className={cn("w-full", className)} {...props}>
+ <MeterPrimitive.Track
+ data-slot="meter-track"
+ className="relative h-1.5 w-full overflow-hidden rounded-full bg-muted"
+ >
+ <MeterPrimitive.Indicator
+ data-slot="meter-indicator"
+ className={cn("bg-primary transition-[width]", indicatorClassName)}
+ />
+ </MeterPrimitive.Track>
+ </MeterPrimitive.Root>
+ )
+}
+
+export { Meter }
diff --git a/site/src/cut/components/AppSidebar.tsx b/site/src/cut/components/AppSidebar.tsx
index 6b224822..6e6cd962 100644
--- a/site/src/cut/components/AppSidebar.tsx
+++ b/site/src/cut/components/AppSidebar.tsx
@@ -18,6 +18,7 @@ import { apiFetch, getBackend } from "@/cut/lib/backend";
import { seedNewProjectDoc } from "@/cut/lib/docCache";
import { patchProjects } from "@/cut/lib/queries";
import { track } from "@/lib/analytics";
+import { NavStorage } from "@/cut/components/NavStorage";
import { NavUser } from "@/cut/components/NavUser";
import { homeHref, projectHref, tabForPath, useCutBase, type CutTab } from "@/cut/lib/nav";
import type { ProjectSummary } from "@/cut/lib/types";
@@ -107,7 +108,10 @@ export function AppSidebar() {
);
})}
</nav>
- <NavUser />
+ <div className="mt-auto flex flex-col">
+ <NavStorage />
+ <NavUser />
+ </div>
<Dialog open={createOpen} onOpenChange={setCreateOpen}>
<DialogContent className="sm:max-w-sm">
<DialogHeader>
diff --git a/site/src/cut/components/NavStorage.tsx b/site/src/cut/components/NavStorage.tsx
new file mode 100644
index 00000000..523aa08d
--- /dev/null
+++ b/site/src/cut/components/NavStorage.tsx
@@ -0,0 +1,74 @@
+"use client";
+
+// The free tier's cloud storage meter, pinned above the account row: how much
+// of the quota this account has used, and the one thing that buys more. The
+// quota belongs to the account, not to the backend the app happens to be
+// running against, so this shows in local mode too — a local editor still has
+// cloud projects, and the number is the same either way. Pro accounts see
+// nothing here — 50 GB is enough that a meter would only take up room.
+import { Loader2 } from "lucide-react";
+import { Button } from "@/components/ui/button";
+import { Meter } from "@/components/ui/meter";
+import { useCloudUsage } from "@/cut/lib/backend/hooks";
+import { useUpgradeToPro } from "@/cut/lib/proUpgrade";
+import { cn } from "@/lib/utils";
+import { useProSubscription } from "@/queries/billing";
+import { formatBytes } from "./desktopFolders";
+import { usageLabel } from "./StoragePill";
+
+export function NavStorage() {
+ const upgrade = useUpgradeToPro();
+ const pro = useProSubscription();
+ // The meter has to move as media lands, so this reader polls. Both faces wait
+ // on billing, so a Pro account never sees the block flash in front of it.
+ const free = pro.data?.isActive === false;
+ const usage = useCloudUsage(free, { poll: true });
+
+ if (!free) return null;
+ const u = usage.data;
+ if (!u || u.quotaBytes === null) return null;
+
+ const frac = u.bytes / u.quotaBytes;
+ return (
+ <div className="flex flex-col gap-2 px-2 py-3">
+ <div className="flex items-baseline justify-between gap-2 text-xs">
+ <span className="font-medium">Cloud storage</span>
+ <span
+ className={cn(
+ "tabular-nums",
+ frac >= 1
+ ? "text-destructive"
+ : frac >= 0.8
+ ? "text-amber-600 dark:text-amber-500"
+ : "text-muted-foreground"
+ )}
+ >
+ {usageLabel(u.bytes, u.quotaBytes)}
+ </span>
+ </div>
+ <Meter
+ value={Math.min(u.bytes, u.quotaBytes)}
+ max={u.quotaBytes}
+ aria-label="Cloud storage used"
+ indicatorClassName={
+ frac >= 1 ? "bg-destructive" : frac >= 0.8 ? "bg-amber-500" : undefined
+ }
+ />
+ <p className="text-xs leading-snug text-muted-foreground">
+ {u.grace
+ ? `Your Pro plan ended. Upgrade or free ${formatBytes(u.grace.overBytes)} before your oldest projects are deleted.`
+ : "Pro gives you 50 GB of cloud storage."}
+ </p>
+ <Button
+ variant="outline"
+ size="sm"
+ className="w-full"
+ disabled={upgrade.isPending}
+ onClick={upgrade.start}
+ >
+ {upgrade.isPending && <Loader2 className="animate-spin" data-icon="inline-start" />}
+ Upgrade to Pro
+ </Button>
+ </div>
+ );
+}
diff --git a/site/src/cut/components/StoragePill.tsx b/site/src/cut/components/StoragePill.tsx
index b87f4179..5b0ab43f 100644
--- a/site/src/cut/components/StoragePill.tsx
+++ b/site/src/cut/components/StoragePill.tsx
@@ -26,7 +26,7 @@ const PILL =
/** "278 / 250 MB" — the pill sits in a crowded top bar, so a unit is spelled
* out only where it carries information: repeat it just when the two numbers
* land on different ones ("1.2 GB / 250 MB"). */
-function usageLabel(bytes: number, quotaBytes: number): string {
+export function usageLabel(bytes: number, quotaBytes: number): string {
const used = formatBytes(bytes);
const cap = formatBytes(quotaBytes);
const unit = cap.slice(cap.lastIndexOf(" ") + 1);
diff --git a/site/src/cut/components/StorageUpgradeDialog.tsx b/site/src/cut/components/StorageUpgradeDialog.tsx
index 12113bd6..48fc331e 100644
--- a/site/src/cut/components/StorageUpgradeDialog.tsx
+++ b/site/src/cut/components/StorageUpgradeDialog.tsx
@@ -13,7 +13,7 @@ import {
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
-import { useCutBase } from "@/cut/lib/nav";
+import { useUpgradeToPro } from "@/cut/lib/proUpgrade";
import {
clearStorageQuotaWall,
onStorageQuota,
@@ -21,7 +21,7 @@ import {
} from "@/cut/lib/storageQuota";
import { daysUntil } from "@/cut/lib/time";
import { track } from "@/lib/analytics";
-import { useProSubscription, useStartCheckout } from "@/queries/billing";
+import { useProSubscription } from "@/queries/billing";
import { formatBytes } from "./desktopFolders";
export function StorageUpgradeDialog() {
@@ -48,8 +48,7 @@ export function StorageUpgradeDialog() {
// Split so the billing query only runs while the dialog is up.
function OpenDialog({ detail, onClose }: { detail: StorageQuotaDetail; onClose: () => void }) {
const pro = useProSubscription();
- const checkout = useStartCheckout();
- const base = useCutBase();
+ const upgrade = useUpgradeToPro();
const isPro = pro.data?.isActive === true;
const used =
@@ -58,19 +57,6 @@ function OpenDialog({ detail, onClose }: { detail: StorageQuotaDetail; onClose:
: null;
const graceDays = detail.grace ? daysUntil(detail.grace.deadline) : null;
- const upgrade = async () => {
- track("pro_checkout_started");
- try {
- const { url } = await checkout.mutateAsync("pro");
- window.location.assign(url);
- } catch {
- // Checkout can't start (an unconfigured plan 404s); the billing page
- // carries the same subscribe action, so send them there rather than
- // leaving the button dead.
- window.location.assign(`${base}/settings`);
- }
- };
-
return (
<Dialog open onOpenChange={(open) => !open && onClose()}>
<DialogContent className="sm:max-w-sm">
@@ -101,8 +87,8 @@ function OpenDialog({ detail, onClose }: { detail: StorageQuotaDetail; onClose:
</Button>
) : (
<div className="flex w-full flex-col gap-2">
- <Button className="w-full" disabled={checkout.isPending} onClick={() => void upgrade()}>
- {checkout.isPending && <Loader2 className="animate-spin" data-icon="inline-start" />}
+ <Button className="w-full" disabled={upgrade.isPending} onClick={upgrade.start}>
+ {upgrade.isPending && <Loader2 className="animate-spin" data-icon="inline-start" />}
Upgrade to Pro
</Button>
<Button variant="ghost" className="w-full" onClick={onClose}>
diff --git a/site/src/cut/lib/proUpgrade.ts b/site/src/cut/lib/proUpgrade.ts
new file mode 100644
index 00000000..ed4e0eeb
--- /dev/null
+++ b/site/src/cut/lib/proUpgrade.ts
@@ -0,0 +1,28 @@
+"use client";
+
+// One way into Pro checkout, shared by every surface that sells storage.
+import { useCutBase } from "@/cut/lib/nav";
+import { track } from "@/lib/analytics";
+import { useStartCheckout } from "@/queries/billing";
+
+export function useUpgradeToPro() {
+ const checkout = useStartCheckout();
+ const base = useCutBase();
+
+ const start = () => {
+ track("pro_checkout_started");
+ void (async () => {
+ try {
+ const { url } = await checkout.mutateAsync("pro");
+ window.location.assign(url);
+ } catch {
+ // Checkout can't start (an unconfigured plan 404s); the billing page
+ // carries the same subscribe action, so send them there rather than
+ // leaving the button dead.
+ window.location.assign(`${base}/settings`);
+ }
+ })();
+ };
+
+ return { start, isPending: checkout.isPending };
+}