Skip to content

Open source CapCut alternative.

TypeScript

feat(site): connect to the engine from the account menu

David Leb663f0f8f1a5
diff --git a/site/src/cut/components/ConnectGate.tsx b/site/src/cut/components/ConnectGate.tsx
index 48e8b2bd..24af24a7 100644
--- a/site/src/cut/components/ConnectGate.tsx
+++ b/site/src/cut/components/ConnectGate.tsx
@@ -17,13 +17,17 @@ import {
} from "@/components/ui/dialog";
import {
ENGINE_LOST_EVENT,
- engineConnect,
engineGateOpen,
engineProbe,
servedFromEngine,
} from "@/cut/lib/api";
import { announceLocalCompute, setCutMode } from "@/cut/lib/backend";
import { useCutMode } from "@/cut/lib/backend/hooks";
+import {
+ CONNECT_ACK_KEY,
+ ENGINE_CONNECTED_EVENT,
+ connectToEngine,
+} from "@/cut/lib/connect";
import { setNeedsApp, useNeedsApp } from "@/cut/lib/needsApp";
import { engineSeen, markEngineSeen } from "@/cut/lib/residency";
@@ -44,7 +48,6 @@ import { engineSeen, markEngineSeen } from "@/cut/lib/residency";
// steps in a dialog. The gate keeps probing behind it, so starting the Donkey
// app clears the banner and the project loads. Engine loss mid-session
// (api.ts's engineLost) lands in the same state.
-const ACK_KEY = "cut-engine-connect-acked";
/** Dev-only preview of the banner and its dialog, since localhost always
* reaches its engine — open a local project with it, since that is where the
@@ -122,7 +125,7 @@ export function ConnectGate({ children }: { children: ReactNode }) {
const quiet =
servedFromEngine() ||
perm === "granted" ||
- (perm === null && localStorage.getItem(ACK_KEY) === "1");
+ (perm === null && localStorage.getItem(CONNECT_ACK_KEY) === "1");
setNeedsAsk(!quiet && perm !== "denied");
if (quiet) {
try {
@@ -163,14 +166,23 @@ export function ConnectGate({ children }: { children: ReactNode }) {
return () => window.removeEventListener(ENGINE_LOST_EVENT, onLost);
}, [bindCloud]);
+ // A user-triggered connect elsewhere (the account menu's "Run locally on
+ // Mac") reached the engine: fold it into this gate's state so the banner
+ // drops and the poll stops.
+ useEffect(() => {
+ const onConnected = () => bindEngine();
+ window.addEventListener(ENGINE_CONNECTED_EVENT, onConnected);
+ return () => window.removeEventListener(ENGINE_CONNECTED_EVENT, onConnected);
+ }, [bindEngine]);
+
const connect = async () => {
setConnecting(true);
- localStorage.setItem(ACK_KEY, "1");
- const ok = await engineConnect();
+ const ok = await connectToEngine();
setConnecting(false);
if (ok) {
+ // bindEngine ran via ENGINE_CONNECTED_EVENT.
setHowOpen(false);
- return bindEngine();
+ return;
}
// No engine answered, but the browser ask may still have been decided.
const perm = await permissionState();
diff --git a/site/src/cut/components/NavUser.tsx b/site/src/cut/components/NavUser.tsx
index 74508e19..09e723d5 100644
--- a/site/src/cut/components/NavUser.tsx
+++ b/site/src/cut/components/NavUser.tsx
@@ -20,6 +20,7 @@ import {
} from "@/components/ui/dropdown-menu";
import { Skeleton } from "@/components/ui/skeleton";
import { UserAvatar } from "@/cut/components/UserAvatar";
+import { connectToEngine } from "@/cut/lib/connect";
import { cutInstallHref } from "@/cut/lib/install";
import { openOnboarding } from "@/cut/lib/onboarding";
import { useCutBase } from "@/cut/lib/nav";
@@ -54,6 +55,19 @@ export function NavUser() {
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 and
+ // running, and this click is the browser-permission ask landing in
+ // context. Only when nothing answers does the install page open; without
+ // noopener so a popup-blocked open (the permission ask can outlive the
+ // click's activation window) is detectable and falls back to this tab.
+ void connectToEngine().then((ok) => {
+ if (ok) return;
+ const tab = window.open(cutInstallHref(), "_blank");
+ if (!tab) window.location.assign(cutInstallHref());
+ });
+ };
+
const signOut = () => {
// Sign out everywhere: revoke every session for this user (so the Mac app
// signs out too), then clear this browser's session and land on the Cut
@@ -71,7 +85,7 @@ export function NavUser() {
return (
<DropdownMenu>
- <DropdownMenuTrigger className="flex w-full cursor-pointer items-center gap-2.5 rounded-lg px-2 py-1.5 text-left transition-colors hover:bg-muted data-[popup-open]:bg-muted">
+ <DropdownMenuTrigger className="flex w-full items-center gap-2.5 rounded-lg px-2 py-1.5 text-left transition-colors hover:bg-muted data-[popup-open]:bg-muted">
<UserAvatar name={name} image={image} />
<span className="min-w-0 flex-1 truncate text-sm font-medium">{name}</span>
<EllipsisVertical className="size-4 shrink-0 text-muted-foreground" />
@@ -101,11 +115,7 @@ export function NavUser() {
<DropdownMenuItem onClick={openOnboarding}>
<Sparkles /> View onboarding
</DropdownMenuItem>
- {/* The install page lives outside the editor, so it opens in its own
- tab rather than navigating away from an open project. */}
- <DropdownMenuItem
- onClick={() => window.open(cutInstallHref(), "_blank", "noopener")}
- >
+ <DropdownMenuItem onClick={runLocally}>
<MonitorDown /> Run locally on Mac
</DropdownMenuItem>
<DropdownMenuSeparator />
diff --git a/site/src/cut/lib/connect.ts b/site/src/cut/lib/connect.ts
new file mode 100644
index 00000000..35cdd432
--- /dev/null
+++ b/site/src/cut/lib/connect.ts
@@ -0,0 +1,44 @@
+"use client";
+
+// The user-triggered path onto this Mac's engine. The ConnectGate's quiet
+// probe only runs when it costs nothing, so a browser with no memory of the
+// app — a fresh profile, a private window — starts in the cloud with no
+// automatic way back. Any surface can offer that way instead: this connect is
+// run from a click, so the browser's local-network prompt lands in context,
+// and a success binds the whole app to the engine exactly as the gate's own
+// probe would. The gate listens for ENGINE_CONNECTED_EVENT to fold the result
+// into its banner state.
+import { engineConnect, engineGateOpen } from "./api";
+import { announceLocalCompute, setCutMode } from "./backend";
+import { setNeedsApp } from "./needsApp";
+import { markEngineSeen } from "./residency";
+
+// Set on the first user-approved connect; lets browsers without the
+// local-network permission API (where a loopback probe never prompts) probe
+// quietly on later visits.
+export const CONNECT_ACK_KEY = "cut-engine-connect-acked";
+
+/** Fired on window when a user-triggered connect reached the engine. */
+export const ENGINE_CONNECTED_EVENT = "cut-engine-connected";
+
+/**
+ * Connect to the engine from a user gesture. Resolves true once the app is
+ * bound to the engine — mode set, gate latch open, listeners notified — and
+ * false when no engine answered (not installed, not running, or the browser
+ * ask was declined).
+ */
+export async function connectToEngine(): Promise<boolean> {
+ try {
+ localStorage.setItem(CONNECT_ACK_KEY, "1");
+ } catch {
+ // Private mode: the ask repeats next visit.
+ }
+ if (!(await engineConnect())) return false;
+ markEngineSeen();
+ setNeedsApp(false);
+ setCutMode("local");
+ engineGateOpen();
+ announceLocalCompute();
+ window.dispatchEvent(new Event(ENGINE_CONNECTED_EVENT));
+ return true;
+}