Skip to content

Open source CapCut alternative.

TypeScript

feat(cut): make engine presence subscribable [rebuild]

David Leae1d882c290b
diff --git a/site/src/cut/components/ConnectGate.tsx b/site/src/cut/components/ConnectGate.tsx
index 2ffaa227..e825fa7f 100644
--- a/site/src/cut/components/ConnectGate.tsx
+++ b/site/src/cut/components/ConnectGate.tsx
@@ -22,7 +22,7 @@ import {
engineProbe,
servedFromEngine,
} from "@/cut/lib/api";
-import { setCutMode } from "@/cut/lib/backend";
+import { announceLocalCompute, setCutMode } from "@/cut/lib/backend";
// Which backend the app starts on, decided once per load. Chrome gates a
// public https page's first fetch to 127.0.0.1 behind its Local Network Access
@@ -89,11 +89,13 @@ export function ConnectGate({ children }: { children: ReactNode }) {
setBlocked(false);
setCutMode("local");
engineGateOpen();
+ announceLocalCompute();
setReached(true);
}, []);
const bindCloud = useCallback(() => {
setCutMode("cloud");
+ announceLocalCompute();
setReached(false);
}, []);
diff --git a/site/src/cut/lib/backend/hooks.ts b/site/src/cut/lib/backend/hooks.ts
index 2a54d98e..4440258b 100644
--- a/site/src/cut/lib/backend/hooks.ts
+++ b/site/src/cut/lib/backend/hooks.ts
@@ -3,13 +3,20 @@
import { useQuery } from "@tanstack/react-query";
import { useSyncExternalStore } from "react";
-import { cutMode, getBackend, subscribeCutMode } from "./index";
+import { cutMode, getBackend, hasLocalCompute, subscribeCutMode } from "./index";
import type { CutCaps, CutMode } from "./types";
export function useCutMode(): CutMode {
return useSyncExternalStore(subscribeCutMode, cutMode, () => "local" as const);
}
+/** Whether this Mac's engine is reachable, as a subscription: the gate resolves
+ * it after the first paint, so a screen that offers local work has to redraw
+ * when the answer lands. */
+export function useLocalCompute(): boolean {
+ return useSyncExternalStore(subscribeCutMode, hasLocalCompute, () => false);
+}
+
export function useCutCaps(): CutCaps {
useCutMode();
return getBackend().caps;
diff --git a/site/src/cut/lib/backend/index.ts b/site/src/cut/lib/backend/index.ts
index ae76ab1f..4ec92b2c 100644
--- a/site/src/cut/lib/backend/index.ts
+++ b/site/src/cut/lib/backend/index.ts
@@ -83,6 +83,19 @@ export function hasLocalCompute(): boolean {
return servedFromEngine() || engineOrigin() !== "";
}
+/**
+ * Announce that the gate settled whether this Mac has an engine.
+ *
+ * The answer lives in api.ts's memoized origin, where React can't subscribe to
+ * it, and binding the mode doesn't stand in for it: an engine that answers
+ * leaves the ambient mode exactly where it started, so nothing would fire.
+ * Screens that offer local work — creating a project on this Mac — appear on
+ * this signal.
+ */
+export function announceLocalCompute() {
+ listeners.forEach((l) => l());
+}
+
/** fetch() against the active backend. */
export const apiFetch = (path: string, init?: RequestInit) => getBackend().fetch(path, init);