Open source CapCut alternative.
TypeScript
feat(site): new projects default to cloud until storage fills
David Le418f61c098a4
diff --git a/site/src/cut/lib/newProject.ts b/site/src/cut/lib/newProject.tsindex 426599e3..1cb2ed63 100644--- a/site/src/cut/lib/newProject.ts+++ b/site/src/cut/lib/newProject.ts@@ -14,7 +14,7 @@ import { useSyncExternalStore } from "react";import { authClient } from "@/lib/auth-client";import { supportsBrowserStore } from "./backend/browser/opfs";-import { useLocalCompute } from "./backend/hooks";+import { useCloudUsage, useLocalCompute } from "./backend/hooks";import type { Residency } from "./residency";const KEY = "cut-new-project-residency";@@ -22,13 +22,14 @@ const KEY = "cut-new-project-residency";let choice: Residency | null = null;const listeners = new Set<() => void>();-function current(): Residency {+/** The user's own pick, or null when they have never picked. */+function current(): Residency | null {if (choice) return choice;try {const raw = localStorage.getItem(KEY);- choice = raw === "cloud" || raw === "local" || raw === "browser" ? raw : "local";+ choice = raw === "cloud" || raw === "local" || raw === "browser" ? raw : null;} catch {- choice = "local";+ choice = null;}return choice;}@@ -65,21 +66,28 @@ export function useNewProjectTarget(): NewProjectTarget {// The engine is never probed here — this reads the answer the ConnectGate// already has, and redraws when it lands.const engineUp = useLocalCompute();- const stored = useSyncExternalStore(subscribe, current, () => "local" as const);+ const stored = useSyncExternalStore(subscribe, current, () => null);// "Local" is one place to the user, so one local entry ever lists: the Mac- // engine when it is up, else the browser's own store. First entry is the- // fallback default, so a signed-in user with no engine lands in the browser- // store: instant imports, no upload, no quota.+ // engine when it is up, else the browser's own store.const store = supportsBrowserStore();const choices: Residency[] = !session? ["local"]: engineUp- ? ["local", "cloud"]+ ? ["cloud", "local"]: store- ? ["browser", "cloud"]+ ? ["cloud", "browser"]: ["cloud"];+ const picked = stored && choices.includes(stored) ? stored : null;+ // A new account starts on the cloud shelf, and stays there until its free+ // storage fills; from then on new projects land on the device. The read only+ // happens while the default is still deciding — a user who has picked is+ // done being defaulted.+ const usage = useCloudUsage(Boolean(session) && !picked, { poll: false });+ const u = usage.data;+ const cloudFull = u != null && u.quotaBytes !== null && u.bytes >= u.quotaBytes;+ const fallback = (cloudFull && choices.find((c) => c !== "cloud")) || choices[0];return {- target: choices.includes(stored) ? stored : choices[0],+ target: picked ?? fallback,choices,pick: setNewProjectResidency,};