Open source CapCut alternative.
TypeScript
refactor(site): the app waits behind one loading overlay
David Led9cb3df2fa36
diff --git a/site/src/app/cut/app/(home)/page.tsx b/site/src/app/cut/app/(home)/page.tsxindex e218d807..96abc49c 100644--- a/site/src/app/cut/app/(home)/page.tsx+++ b/site/src/app/cut/app/(home)/page.tsx@@ -5,9 +5,9 @@ import { SessionGate } from "@/cut/components/SessionGate";export const unstable_instant = { prefetch: "static" };// Suspense: the view reads the open folder from ?folder=…, and useSearchParams-// needs a boundary in a statically prerendered shell. SessionGate is what makes-// that shell worth having: the sidebar and this pane's skeleton are the first-// paint, and the shelf fills in when the account id lands.+// needs a boundary in a statically prerendered shell. SessionGate holds the+// shelf until the account id lands; the shell's loading overlay is what covers+// that wait.export default function Home() {return (<Suspense>diff --git a/site/src/app/cut/app/layout.tsx b/site/src/app/cut/app/layout.tsxindex fba9c32c..2c13db7d 100644--- a/site/src/app/cut/app/layout.tsx+++ b/site/src/app/cut/app/layout.tsx@@ -1,28 +1,29 @@import { Suspense, type ReactNode } from "react";-import { AppSurfaceBackground } from "@/components/AppSurfaceBackground";import { NoSessionReplay } from "@/app/_components/NoSessionReplay";import { ConnectGate } from "@/cut/components/ConnectGate";import { ExportsDock } from "@/cut/components/ExportsDock";import { CutOnboarding } from "@/cut/components/onboarding/CutOnboarding";import { RequireSession } from "@/cut/components/RequireSession";-import { SessionGate, SessionSkeleton } from "@/cut/components/SessionGate";+import { AppLoading, AppLoadingOverlay, SessionGate } from "@/cut/components/SessionGate";-// The Cut app (projects home, library, editor) renders on the same white-// product surface as Donkey's /app, not the cream marketing background of the-// landing page that lives one segment up. AppSurfaceBackground paints the root-// html white so the cream does not show through the overscroll area, and-// font-system matches the /app system font stack. RequireSession binds the-// account and redirects signed-out visitors to sign-in with their target URL as-// the callback. ConnectGate picks the backend the app runs on — the engine on-// this Mac when it answers without raising the browser's local-network prompt,-// the cloud otherwise — and owns the banner that reports an engine this browser-// can no longer reach. CutOnboarding is the welcome sequence a new account sees-// before any of it.+// The Cut app (projects home, library, editor) renders on the white product+// surface. The `app-surface` class on this wrapper is what repoints the+// --background token (see globals.css) for the whole document, so the cream+// landing background is gone from the first painted frame — including the+// browser's overscroll area — with no script to run. font-system matches the+// /app system font stack. RequireSession binds the account and redirects+// signed-out visitors to sign-in with their target URL as the callback.+// ConnectGate picks the backend the app runs on — the engine on this Mac when+// it answers without raising the browser's local-network prompt, the cloud+// otherwise — and owns the banner that reports an engine this browser can no+// longer reach. CutOnboarding is the welcome sequence a new account sees before+// any of it.//-// The surface, the sidebar under it, and the banner are the shell: they render-// with no session and no account, which is what a cold load paints. Everything-// that reads the engine waits behind a SessionGate for the account id.+// Everything the app draws reads the engine, and every engine URL carries the+// account id, so the shell has nothing true to show before the session lands:+// a cold load is one centered spinner, and the sidebar and content arrive+// together behind it.export default function CutAppLayout({ children }: { children: ReactNode }) {return (@@ -34,33 +35,34 @@ export default function CutAppLayout({ children }: { children: ReactNode }) {// around a canvas, so the page keeps its own words and stays standing.<divtranslate="no"- className="min-h-screen bg-white font-system text-foreground antialiased"+ className="app-surface min-h-screen bg-white font-system text-foreground antialiased">- <AppSurfaceBackground /><NoSessionReplay />{/* A route whose address carries data — the editor's project id — can't bein the prerendered shell, and the framework resolves that above the- client shell below. The boundary is here so it has somewhere to land:- the home routes suspend on nothing and prerender whole, and the editor- arrives as this skeleton on the app's own surface. */}- <Suspense fallback={<SessionSkeleton />}>+ client shell below. The boundary is here so it has somewhere to land. */}+ <Suspense fallback={<AppLoading />}><RequireSession><ConnectGate>{children}{/* App-wide: exports keep showing as you move between projects. Bothoverlays read the account, so they wait for it; neither has ashape worth drawing before then. */}- <SessionGate fallback={null}>+ <SessionGate><ExportsDock /></SessionGate></ConnectGate>{/* Outside the gate so a first run covers the whole window, gate andall, and hands over to it when the last slide closes. */}- <SessionGate fallback={null}>+ <SessionGate><CutOnboarding /></SessionGate></RequireSession></Suspense>+ {/* The cold-load cover, over the shell and the banner both: the app is one+ centered spinner until the account id lands. It sits outside the+ boundary above so it also covers what that boundary is waiting on. */}+ <AppLoadingOverlay /></div>);}diff --git a/site/src/app/cut/app/p/[id]/page.tsx b/site/src/app/cut/app/p/[id]/page.tsxindex 2ee53783..9d844c67 100644--- a/site/src/app/cut/app/p/[id]/page.tsx+++ b/site/src/app/cut/app/p/[id]/page.tsx@@ -1,15 +1,15 @@import { Suspense } from "react";-import { SessionSkeleton } from "@/cut/components/SessionGate";+import { AppLoading } from "@/cut/components/SessionGate";import { ProjectEditor } from "./ProjectEditor";// The project id is in the URL, so it is request data and the editor can never// be part of the prerendered shell. Resolving the param here, inside this-// boundary, is what keeps the rest of the app — surface, banner, sidebar — in-// that shell: the page arrives painted with a skeleton where the editor lands.+// boundary, keeps the surface and the banner in that shell; the wait shows the+// app's loading spinner.export default function ProjectPage({ params }: { params: Promise<{ id: string }> }) {return (- <Suspense fallback={<SessionSkeleton />}>+ <Suspense fallback={<AppLoading />}>{params.then(({ id }) => (<ProjectEditor projectId={id} />))}diff --git a/site/src/app/globals.css b/site/src/app/globals.cssindex e5d72105..07622860 100644--- a/site/src/app/globals.css+++ b/site/src/app/globals.css@@ -147,14 +147,17 @@--sidebar-ring: oklch(0.556 0 0);}-/* App theme: the signed-in /app surface renders on white instead of the cream- landing background. Defined unlayered (like :root, the landing theme) so it- wins the cascade — an @layer base rule would lose to the unlayered :root no- matter its specificity. Repointing this one token flows everywhere keyed to- bg-background: the html/overscroll area, body, and every shadcn component- (e.g. the outline button variant). CSS variables inherit from html, so- portaled dialogs pick it up too. */-html.app-surface {+/* App theme: the signed-in /app surface renders on white; the cream is the+ landing theme. The app layout marks its wrapper `.app-surface`, and this rule+ repoints the token on the document as soon as that element parses — no script,+ so the first painted frame is already the product surface and a session replay+ shows the same colors the visitor saw. Defined unlayered (like :root, the+ landing theme) so it wins the cascade — an @layer base rule would lose to the+ unlayered :root no matter its specificity. This one token flows everywhere+ keyed to bg-background: the html/overscroll area, body, and every shadcn+ component (e.g. the outline button variant). CSS variables inherit from html,+ so portaled dialogs pick it up too. */+html:has(.app-surface) {--background: oklch(1 0 0);}diff --git a/site/src/app/layout.tsx b/site/src/app/layout.tsxindex dbdffaa6..a6c895d8 100644--- a/site/src/app/layout.tsx+++ b/site/src/app/layout.tsx@@ -39,14 +39,9 @@ export default function RootLayout({children: React.ReactNode;}>) {return (- // The /app subtree adds its surface class to html from an inline script so- // the first painted frame is already white (AppSurfaceBackground). The- // served markup and the DOM React hydrates differ by that class on- // purpose, and this is the element it lands on.<htmllang="en"className={`${geistSans.variable} ${geistMono.variable} h-full antialiased`}- suppressHydrationWarning><body className="min-h-full flex flex-col"><PostHogProvider>diff --git a/site/src/components/AppSurfaceBackground.tsx b/site/src/components/AppSurfaceBackground.tsxdeleted file mode 100644index 51b5210d..00000000--- a/site/src/components/AppSurfaceBackground.tsx+++ /dev/null@@ -1,23 +0,0 @@-"use client";--import { useEffect } from "react";--const SURFACE = "app-surface";--// The signed-in /app surface renders on white, but the root html/body default to-// the cream landing background — which shows through in the browser overscroll-// area above the content, and in every element keyed to bg-background until the-// app has hydrated. The flag goes on the html element inline, so the first-// painted frame is already the product surface, and comes back off when the app-// unmounts and the landing pages take the root back.-const paint = `document.documentElement.classList.add(${JSON.stringify(SURFACE)})`;--export function AppSurfaceBackground() {- useEffect(() => {- const root = document.documentElement;- root.classList.add(SURFACE);- return () => root.classList.remove(SURFACE);- }, []);-- return <script dangerouslySetInnerHTML={{ __html: paint }} />;-}diff --git a/site/src/cut/components/RequireSession.tsx b/site/src/cut/components/RequireSession.tsxindex ec4ff75e..0a769f4c 100644--- a/site/src/cut/components/RequireSession.tsx+++ b/site/src/cut/components/RequireSession.tsx@@ -16,11 +16,8 @@ import { authClient } from "@/lib/auth-client";// navigation to an app URL the same way, sending the visitor to sign-in with// the URL they wanted as the post-auth callback.//-// It binds the account id and never holds the tree: the chrome around the-// content — the surface, the sidebar, the connect banner — is worth painting-// before a session exists, and it is what a cold load shows. What must wait is-// anything that reads the engine, because every engine URL carries the id, and-// SessionGate is where each surface waits for it.+// It binds the account id; SessionGate, one level in, is what holds the app+// until the id lands, because every engine URL carries it.export function RequireSession({ children }: { children: ReactNode }) {const { data: session, isPending } = authClient.useSession();diff --git a/site/src/cut/components/SessionGate.tsx b/site/src/cut/components/SessionGate.tsxindex 239b64fc..25312e16 100644--- a/site/src/cut/components/SessionGate.tsx+++ b/site/src/cut/components/SessionGate.tsx@@ -1,37 +1,44 @@"use client";import type { ReactNode } from "react";+import { Loader2 } from "lucide-react";-import { Skeleton } from "@/components/ui/skeleton";import { useEngineUser } from "@/cut/lib/backend/hooks";-// The line between the app's chrome and its contents. The sidebar, the surface,-// and the banner render from the first byte; everything that reads the engine-// waits behind this, because every engine URL carries the account id (the `u`-// param) and the id only exists once the browser has read the session cookie.+// The line between the app's chrome and its contents. Every engine URL carries+// the account id (the `u` param), and the id only exists once the browser has+// read the session cookie, so everything that reads the engine waits here.//-// Holding the surface here — rather than above the sidebar — is what lets the-// app paint before the session resolves: the shell a visitor sees on a cold-// load is the real navigation with a skeleton where the content will land.+// Nothing behind it is worth showing half-drawn, so the wait is not a skeleton+// per pane: AppLoadingOverlay covers the whole window until the id lands, and+// the shell — sidebar, banner, content — arrives whole underneath it.export function SessionGate({children,- fallback,+ fallback = null,}: {children: ReactNode;- /** Shown while the session resolves. Defaults to a page-sized skeleton. */+ /** Shown while the session resolves. Nothing, by default: the overlay is+ * what a visitor sees during the wait. */fallback?: ReactNode;}) {const user = useEngineUser();- if (user) return <>{children}</>;- return <>{fallback ?? <SessionSkeleton />}</>;+ return <>{user ? children : fallback}</>;}-/** The shape of a loading app surface: the title line alone, so the wait reads- * as a pane about to fill rather than a slab of gray. */-export function SessionSkeleton() {+/** What a loading app looks like: a spinner centered on an opaque surface.+ * Fixed, so it centers on the viewport wherever it is mounted. */+export function AppLoading() {return (- <div className="h-full w-full p-10">- <Skeleton className="h-8 w-48" />+ <div className="fixed inset-0 z-50 grid place-items-center bg-background">+ <Loader2 className="size-6 animate-spin text-muted-foreground" /></div>);}++/** The cold-load cover, mounted once in the app shell: the app is a spinner in+ * the middle of the window until the account id lands. */+export function AppLoadingOverlay() {+ const user = useEngineUser();+ if (user) return null;+ return <AppLoading />;+}