Open source CapCut alternative.
TypeScript
fix(site): the sidebar holds still when the window comes back
David Le132bd1a8bdcc
diff --git a/site/src/cut/components/NavUser.tsx b/site/src/cut/components/NavUser.tsxindex de7112c2..cc0cf1c7 100644--- a/site/src/cut/components/NavUser.tsx+++ b/site/src/cut/components/NavUser.tsx@@ -21,6 +21,7 @@ import {import { Skeleton } from "@/components/ui/skeleton";import { UserAvatar } from "@/cut/components/UserAvatar";import { forgetRememberedEngineUser } from "@/cut/lib/api";+import { useEngineUser } from "@/cut/lib/backend/hooks";import { connectToEngine } from "@/cut/lib/connect";import { cutInstallHref } from "@/cut/lib/install";import { openOnboarding } from "@/cut/lib/onboarding";@@ -30,15 +31,23 @@ import { useAccountProfile, visibleName } from "@/queries/accountProfile";// Signed-in user row pinned to the sidebar bottom; the whole row opens the// account menu. Hidden while signed out — the editor itself needs no account,-// so the row only surfaces once a session exists.+// so the row only surfaces once an account is bound.+//+// Whether to draw the row comes from that binding, not from the session+// object: the auth client re-reads the session on every window focus and+// reports null while the request is in the air, which took the row off the+// sidebar and put it back a moment later. The session is read here only for+// the name and picture the provider gave us, and only until the profile —+// which holds both, and is cached — answers.export function NavUser() {const router = useRouter();const base = useCutBase();+ const user = useEngineUser();const { data: session } = authClient.useSession();- // Mounted above the session check so the hook order is stable; it stays idle- // until there's a session to read a profile for.- const { data: profile, isPending } = useAccountProfile({ enabled: Boolean(session) });- if (!session) return null;+ // Mounted above the account check so the hook order is stable; it stays idle+ // until there's an account to read a profile for.+ const { data: profile, isPending } = useAccountProfile({ enabled: Boolean(user) });+ if (!user) return null;// The name and picture the user chose live in the profile, not the session.// The row waits for them behind a skeleton of its own shape rather than@@ -53,8 +62,8 @@ export function NavUser() {}// The name the user chose wins over the one the provider gave us.- const name = visibleName(profile, session.user.name);- const image = profile?.image ?? session.user.image;+ const name = visibleName(profile, session?.user.name ?? "");+ const image = profile?.image ?? session?.user.image;const runLocally = () => {// Reach for the engine first — the app may already be installed anddiff --git a/site/src/cut/components/SettingsGuard.tsx b/site/src/cut/components/SettingsGuard.tsxindex 8a4d8149..90c724ca 100644--- a/site/src/cut/components/SettingsGuard.tsx+++ b/site/src/cut/components/SettingsGuard.tsx@@ -1,25 +1,22 @@"use client";-import { useEffect, type ReactNode } from "react";+import type { ReactNode } from "react";import { Skeleton } from "@/components/ui/skeleton";-import { signInUrl } from "@/cut/lib/generate";-import { authClient } from "@/lib/auth-client";+import { useEngineUser } from "@/cut/lib/backend/hooks";-// Session guard for Cut's billing and usage pages. Signed-out visitors go-// through the same host-aware sign-in flow as the editor's generation-// surfaces (signInUrl): the same-host sign-in page (Google's redirect_uri is-// pinned to the auth-owning host). Reading window.location is safe from-// hydration mismatch: the redirect runs only after the client session resolves.+// Account guard for Cut's billing and usage pages: they read the account, so+// they wait for it.+//+// It waits on the app's account binding rather than on the session object.+// The auth client re-reads the session on every window focus and answers null+// while that request is in the air, and a guard reading it directly sent a+// signed-in user to sign-in mid-visit. RequireSession, above this whole+// subtree, is what redirects a visitor who really is signed out.export function SettingsGuard({ children }: { children: ReactNode }) {- const { data: session, isPending } = authClient.useSession();+ const user = useEngineUser();- useEffect(() => {- if (isPending || session) return;- window.location.assign(signInUrl());- }, [isPending, session]);-- if (isPending || !session) {+ if (!user) {return (<div className="mx-auto max-w-3xl px-8 py-10"><Skeleton className="h-8 w-48" />diff --git a/site/src/cut/components/onboarding/CutOnboarding.tsx b/site/src/cut/components/onboarding/CutOnboarding.tsxindex 3e1a8ddf..795a9993 100644--- a/site/src/cut/components/onboarding/CutOnboarding.tsx+++ b/site/src/cut/components/onboarding/CutOnboarding.tsx@@ -10,10 +10,10 @@ import { ModesSlide } from "@/cut/components/onboarding/slides/Modes";import { PlansSlide } from "@/cut/components/onboarding/slides/Plans";import { ReferralSlide } from "@/cut/components/onboarding/slides/Referral";import { WelcomeSlide } from "@/cut/components/onboarding/slides/Welcome";+import { useEngineUser } from "@/cut/lib/backend/hooks";import { useCutBase } from "@/cut/lib/nav";import { onOpenOnboarding, setOnboardingCover } from "@/cut/lib/onboarding";import { track } from "@/lib/analytics";-import { authClient } from "@/lib/auth-client";import {ONBOARDING_VERSION,isKnownReferralSource,@@ -61,9 +61,11 @@ const doneHere = (userId: string): boolean =>// thing it writes is what it asks for: where the account heard about us.export function CutOnboarding() {const base = useCutBase();- // Mounted inside a SessionGate, so the session is already resolved.- const { data: session } = authClient.useSession();- const userId = session?.user.id ?? "";+ // Mounted inside a SessionGate, so the account is already bound. Read from+ // the binding rather than the session object, which answers null while the+ // auth client re-reads it and would key this browser's "seen it" mark to+ // no account at all.+ const userId = useEngineUser() ?? "";const { data: state, isPending } = useOnboardingState();const { data: pro } = useProSubscription();const save = useSaveOnboarding();diff --git a/site/src/cut/lib/newProject.ts b/site/src/cut/lib/newProject.tsindex 1cb2ed63..4957b21b 100644--- a/site/src/cut/lib/newProject.ts+++ b/site/src/cut/lib/newProject.ts@@ -12,9 +12,8 @@// and picking in one has to move the other.import { useSyncExternalStore } from "react";-import { authClient } from "@/lib/auth-client";import { supportsBrowserStore } from "./backend/browser/opfs";-import { useCloudUsage, useLocalCompute } from "./backend/hooks";+import { useCloudUsage, useEngineUser, useLocalCompute } from "./backend/hooks";import type { Residency } from "./residency";const KEY = "cut-new-project-residency";@@ -62,7 +61,12 @@ export type NewProjectTarget = {};export function useNewProjectTarget(): NewProjectTarget {- const { data: session } = authClient.useSession();+ // The account the app is bound to, rather than the session object. The auth+ // client re-reads the session on every window focus and reports null while+ // that request is in the air, so a control shaped from it changed shape+ // every time the user came back to the tab. The binding changes when the+ // account changes and at no other time.+ const signedIn = useEngineUser() !== null;// The engine is never probed here — this reads the answer the ConnectGate// already has, and redraws when it lands.const engineUp = useLocalCompute();@@ -70,7 +74,7 @@ export function useNewProjectTarget(): NewProjectTarget {// "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.const store = supportsBrowserStore();- const choices: Residency[] = !session+ const choices: Residency[] = !signedIn? ["local"]: engineUp? ["cloud", "local"]@@ -82,7 +86,7 @@ export function useNewProjectTarget(): NewProjectTarget {// 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 usage = useCloudUsage(signedIn && !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];