Skip to content

Open source CapCut alternative.

TypeScript

feat(site): the open editor claims clips from the phone inbox

David Le73137e6a944c
diff --git a/site/src/app/cut/app/(home)/settings/profile/PhoneLinkSection.tsx b/site/src/app/cut/app/(home)/settings/profile/PhoneLinkSection.tsx
new file mode 100644
index 00000000..26f5a974
--- /dev/null
+++ b/site/src/app/cut/app/(home)/settings/profile/PhoneLinkSection.tsx
@@ -0,0 +1,150 @@
+"use client";
+
+import { Laptop, Smartphone } from "lucide-react";
+
+import { Button } from "@/components/ui/button";
+import {
+ Tooltip,
+ TooltipContent,
+ TooltipProvider,
+ TooltipTrigger,
+} from "@/components/ui/tooltip";
+import { useLocalCompute } from "@/cut/lib/backend/hooks";
+import { useAccount } from "@/queries/credits";
+import {
+ useCancelPhonePairing,
+ usePhoneLink,
+ useStartPhonePairing,
+ useUnpairPhone,
+} from "@/queries/phoneLink";
+
+// Pairing a phone with this Mac, so a shoot in the field reaches the editor.
+//
+// The clips ride peer-to-peer Wi-Fi from the phone to the Mac app, which hands
+// them to the engine; the editor takes them from there into whichever project is
+// open. Nothing about it needs a network, which is the whole point — the phone
+// and the Mac find each other on the radio and talk directly.
+//
+// Super user only, and only on a Mac running the app: there is no hosted twin of
+// a Bonjour listener, so with no engine here there is nothing to pair with.
+export function PhoneLinkSection() {
+ const superUser = useAccount().data?.superUser === true;
+ const hasEngine = useLocalCompute();
+ const link = usePhoneLink(superUser && hasEngine);
+ const start = useStartPhonePairing();
+ const cancel = useCancelPhonePairing();
+ const unpair = useUnpairPhone();
+
+ if (!superUser || !hasEngine) return null;
+
+ const devices = link.data?.devices ?? [];
+ const pairing = link.data?.pairing ?? null;
+ const refused = link.data?.closed === "refused";
+
+ return (
+ <div className="rounded-xl border bg-card p-5">
+ <div className="flex items-center gap-2">
+ <Laptop className="size-4 text-muted-foreground" />
+ <div className="text-sm font-medium">Phone link</div>
+ </div>
+ <p className="mt-1 text-sm text-muted-foreground">
+ Send video from the Donkey Cut iPhone app straight to this Mac over Wi-Fi Direct, with
+ or without a network. Clips land in the project you have open.
+ </p>
+
+ <div className="mt-4 border-t pt-4">
+ {pairing ? (
+ <div>
+ <p className="text-sm">
+ On the phone, open the avatar menu → Mac Link, pick this Mac, and type:
+ </p>
+ <div className="mt-3 font-mono text-3xl tracking-[0.2em] tabular-nums">
+ {pairing.code}
+ </div>
+ <div className="mt-3 flex items-center gap-2">
+ <Button variant="outline" size="sm" onClick={() => cancel.mutate()}>
+ Cancel
+ </Button>
+ <span className="text-sm text-muted-foreground">
+ The code is good for five minutes.
+ </span>
+ </div>
+ </div>
+ ) : (
+ <div className="flex items-center gap-3">
+ <Button size="sm" onClick={() => start.mutate()} disabled={start.isPending}>
+ Pair a phone
+ </Button>
+ {refused && (
+ <span className="text-sm text-red-600">
+ The last code was answered wrong three times and closed. Start a new one.
+ </span>
+ )}
+ </div>
+ )}
+
+ {devices.length > 0 && (
+ <ul className="mt-4 space-y-3 border-t pt-4">
+ {devices.map((device) => (
+ <li key={device.id} className="flex items-center justify-between gap-6">
+ <span className="flex min-w-0 items-center gap-2">
+ <Smartphone className="size-4 shrink-0 text-muted-foreground" />
+ <span className="min-w-0">
+ <span className="block truncate text-sm font-medium">{device.name}</span>
+ <span className="mt-0.5 block text-sm text-muted-foreground">
+ Last seen {relative(device.lastSeenAt)}
+ </span>
+ </span>
+ </span>
+ <TooltipProvider>
+ <Tooltip>
+ <TooltipTrigger
+ render={
+ <Button
+ variant="ghost"
+ size="sm"
+ onClick={() => unpair.mutate(device.id)}
+ disabled={unpair.isPending}
+ >
+ Unpair
+ </Button>
+ }
+ />
+ <TooltipContent>This phone stops being able to send here</TooltipContent>
+ </Tooltip>
+ </TooltipProvider>
+ </li>
+ ))}
+ </ul>
+ )}
+
+ {(start.isError || unpair.isError) && (
+ <p className="mt-3 text-sm text-red-600">
+ Couldn&apos;t reach the app on this Mac — check that it&apos;s running.
+ </p>
+ )}
+ </div>
+ </div>
+ );
+}
+
+/** "3 minutes ago", down to "just now". */
+function relative(stamp: number): string {
+ const seconds = Math.round((Date.now() - stamp) / 1000);
+ if (seconds < 60) return "just now";
+ const units: [Intl.RelativeTimeFormatUnit, number][] = [
+ ["minute", 60],
+ ["hour", 3600],
+ ["day", 86400],
+ ];
+ const format = new Intl.RelativeTimeFormat(undefined, { numeric: "auto" });
+ let unit: Intl.RelativeTimeFormatUnit = "minute";
+ let size = 60;
+ for (const [candidate, candidateSize] of units) {
+ if (seconds >= candidateSize) {
+ unit = candidate;
+ size = candidateSize;
+ }
+ }
+ return format.format(-Math.round(seconds / size), unit);
+}
diff --git a/site/src/app/cut/app/(home)/settings/profile/page.tsx b/site/src/app/cut/app/(home)/settings/profile/page.tsx
index bd3df18d..e9b5649c 100644
--- a/site/src/app/cut/app/(home)/settings/profile/page.tsx
+++ b/site/src/app/cut/app/(home)/settings/profile/page.tsx
@@ -7,6 +7,7 @@ import { AvatarDialog } from "@/app/cut/app/(home)/settings/profile/AvatarDialog
import { EditorSection } from "@/app/cut/app/(home)/settings/profile/EditorSection";
import { EmailSection } from "@/app/cut/app/(home)/settings/profile/EmailSection";
import { FeatureFlagsSection } from "@/app/cut/app/(home)/settings/profile/FeatureFlagsSection";
+import { PhoneLinkSection } from "@/app/cut/app/(home)/settings/profile/PhoneLinkSection";
import { SuperUserSection } from "@/app/cut/app/(home)/settings/profile/SuperUserSection";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
@@ -132,6 +133,8 @@ export default function CutProfilePage() {
<SuperUserSection />
+ <PhoneLinkSection />
+
<AvatarDialog
open={editingAvatar}
onOpenChange={setEditingAvatar}
diff --git a/site/src/cut/components/Editor.tsx b/site/src/cut/components/Editor.tsx
index 96692d5d..8304e60e 100644
--- a/site/src/cut/components/Editor.tsx
+++ b/site/src/cut/components/Editor.tsx
@@ -3,6 +3,7 @@
import { chatRuntime } from "@/cut/lib/chatRuntime";
import { useHostBridge } from "@/cut/lib/hostBridge";
import { useCutMode } from "@/cut/lib/backend/hooks";
+import { usePhoneInbox } from "@/cut/hooks/usePhoneInbox";
import { useHostCommands } from "@/cut/lib/hostCommands";
import { useEnvironment } from "@/cut/lib/environment";
import { useCallback, useEffect, useRef, useState } from "react";
@@ -768,6 +769,12 @@ export function Editor({
/** Media folder the import files into — the one open in the Media
* panel when the upload started. */
folderId?: string;
+ /** Called once per file with what became of it. A caller that holds the
+ * only other copy of the bytes — the phone link, whose clip is staged on
+ * this Mac until an editor has it — reads this to know whether it may
+ * let that copy go. Failure here is quiet by design for a drag and drop;
+ * it is not for something waiting on the answer. */
+ onOutcome?: (result: { file: File; assetId: string } | { file: File; failed: true }) => void;
}
) => {
const list = Array.from(files);
@@ -793,7 +800,10 @@ export function Editor({
// first, and the asset is built from what comes back.
const pending = await prepareImport(projectId, file);
const asset = pending?.asset ?? (await importFileToProject(projectId, file));
- if (!asset) continue;
+ if (!asset) {
+ opts?.onOutcome?.({ file, failed: true });
+ continue;
+ }
// Recordings are created media: tag them so they land on the timeline
// but never in the Media panel (reserved for user imports).
if (opts?.origin) asset.origin = opts.origin;
@@ -805,7 +815,10 @@ export function Editor({
// bytes live in that project's storage, so filing it into whatever
// document is open now would point the new project at the old one's
// media.
- if (s.projectId !== projectId) continue;
+ if (s.projectId !== projectId) {
+ opts?.onOutcome?.({ file, failed: true });
+ continue;
+ }
s.addAsset(asset);
// mediaOnly stocks the Media panel and leaves the timeline alone
// (drops that land outside the timeline); placement is up to the user.
@@ -852,8 +865,10 @@ export function Editor({
// drawn from those rather than downloaded back from storage.
void enrichAsset(asset, pending?.localUrl);
if (pending) startUpload(projectId, pending);
+ opts?.onOutcome?.({ file, assetId: asset.id });
} catch (err) {
reportSwallowed(`[cut] import failed for ${file.name}`, err);
+ opts?.onOutcome?.({ file, failed: true });
} finally {
setImporting((n) => n - 1);
}
@@ -1188,6 +1203,9 @@ export function Editor({
return () => window.removeEventListener("keydown", onKey);
}, [importFiles]);
+ // Takes shot on a paired phone land in this project's media as they arrive.
+ usePhoneInbox(importFiles);
+
// The project is here, on this Mac — it just can't be opened without the app
// that holds it. The gate's banner sits above this with the recovery steps,
// and the moment the app answers this screen loads the project itself.
diff --git a/site/src/cut/hooks/usePhoneInbox.ts b/site/src/cut/hooks/usePhoneInbox.ts
new file mode 100644
index 00000000..4421f514
--- /dev/null
+++ b/site/src/cut/hooks/usePhoneInbox.ts
@@ -0,0 +1,202 @@
+"use client";
+
+import { useEffect, useRef } from "react";
+
+import { apiFetch } from "@/cut/lib/api";
+import { getBackend } from "@/cut/lib/backend";
+import { assetFromProjectFile } from "@/cut/lib/media";
+import { useLocalCompute } from "@/cut/lib/backend/hooks";
+import { uploadInFlight } from "@/cut/lib/importQueue";
+import { useEditor } from "@/cut/lib/store";
+
+/** A clip waiting on this Mac, shot on a paired phone. */
+type PhoneClip = {
+ id: string;
+ fileName: string;
+ bytes: number;
+ capturedAt: number;
+ deviceName: string;
+};
+
+/** How often the editor looks for clips a phone has handed over. A readdir on
+ * loopback, and only while a phone is actually paired. */
+const POLL_MS = 4000;
+/** How often it re-checks whether a phone is paired at all, which is what turns
+ * the clip poll on. */
+const PAIRED_POLL_MS = 60_000;
+/** How long the hook waits for an asset's bytes to finish reaching storage
+ * before it leaves the clip in the inbox for another pass. Generous: a field
+ * take on a slow uplink is minutes. */
+const LANDED_TIMEOUT_MS = 20 * 60 * 1000;
+
+/**
+ * Field clips reaching the open project.
+ *
+ * A phone shooting with no network hands its takes to the Mac app over
+ * peer-to-peer Wi-Fi, and the app leaves them in the engine's inbox. This is the
+ * other end: the open editor takes each one and imports it the way any file
+ * import lands, so it works the same whether the project lives in this page's
+ * OPFS, on this Mac, or in the cloud.
+ *
+ * Two rules make it safe to let the Mac's copy go. A clip is claimed before it
+ * is read, so two open editors cannot both take it — the engine hands the claim
+ * to exactly one of them. And it is deleted only once its bytes are actually in
+ * the project's storage, which for a cloud project is after the upload has
+ * finished, not after the asset appears on screen. Anything short of that puts
+ * the clip back, because the phone has already stopped offering it.
+ */
+export function usePhoneInbox(
+ importFiles: (
+ files: File[],
+ opts?: {
+ mediaOnly?: boolean;
+ onOutcome?: (r: { file: File; assetId: string } | { file: File; failed: true }) => void;
+ }
+ ) => Promise<unknown>
+) {
+ const hasEngine = useLocalCompute();
+ // The import function is rebuilt on every editor render; the loop reads the
+ // latest through a ref so it is never restarted by one.
+ const importRef = useRef(importFiles);
+ useEffect(() => {
+ importRef.current = importFiles;
+ }, [importFiles]);
+
+ useEffect(() => {
+ if (!hasEngine) return;
+ let stopped = false;
+ let working = false;
+ let paired = false;
+ let lastPairedCheck = 0;
+
+ /** Take the clip into the open project, and say whether its bytes got there. */
+ const land = async (clip: PhoneClip): Promise<boolean> => {
+ const backend = getBackend();
+ const projectId = useEditor.getState().projectId;
+ if (!projectId) return false;
+
+ // A project on this Mac is the case where the page has no business
+ // carrying the bytes: the clip and the project are both on this disk, so
+ // the engine moves the file and the editor only registers what landed.
+ if (backend.kind === "local") {
+ const res = await apiFetch(`/api/cut/phone/clips/${clip.id}/into-project`, {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({ projectId }),
+ });
+ if (!res.ok) return false;
+ const { fileName } = (await res.json()) as { fileName: string };
+ await registerMoved(projectId, fileName);
+ return true;
+ }
+
+ const res = await apiFetch(`/api/cut/phone/clips/${clip.id}/file`);
+ if (!res.ok) return false;
+ const blob = await res.blob();
+ const file = new File([blob], clip.fileName, {
+ type: blob.type || "video/quicktime",
+ lastModified: clip.capturedAt,
+ });
+
+ let landedId: string | null = null;
+ await importRef.current([file], {
+ mediaOnly: true,
+ onOutcome: (r) => {
+ if ("assetId" in r) landedId = r.assetId;
+ },
+ });
+ if (!landedId) return false;
+ if (await stored(landedId)) return true;
+ // The bytes never reached storage. The asset would sit in the project
+ // pointing at nothing, and the clip is going back in the inbox for
+ // another pass, so take the half-import out of the way of that.
+ useEditor.getState().removeAsset(landedId);
+ return false;
+ };
+
+ /** Wait for the asset's bytes to reach project storage. The import queue
+ * holds a job for as long as one is in flight or retryable, so the clip is
+ * safe to drop once that job is gone and the asset is still here. */
+ const stored = async (assetId: string): Promise<boolean> => {
+ const deadline = Date.now() + LANDED_TIMEOUT_MS;
+ while (Date.now() < deadline) {
+ if (stopped) return false;
+ const present = useEditor.getState().assets.some((a) => a.id === assetId);
+ if (!present) return false;
+ if (!uploadInFlight(assetId)) return true;
+ await new Promise((resolve) => setTimeout(resolve, 500));
+ }
+ return false;
+ };
+
+ /** Register a clip the engine moved into the project's media folder. The
+ * bytes are already where they belong, so this only probes the file for its
+ * duration and size and puts the asset in the Media panel — nothing about
+ * the clip passes through this tab.
+ *
+ * The move already happened, so the clip is landed either way: a probe that
+ * fails leaves the file in the project without an asset pointing at it,
+ * which is a file to find rather than a take to lose. */
+ const registerMoved = async (projectId: string, fileName: string): Promise<void> => {
+ try {
+ const asset = await assetFromProjectFile(projectId, fileName, fileName);
+ const store = useEditor.getState();
+ // The probe outlives a project switch; the file is in the other
+ // project's folder, so it must not be filed into whatever is open now.
+ if (store.projectId === projectId) store.addAsset(asset);
+ } catch {
+ // The bytes are in the project; the next open of it lists them.
+ }
+ };
+
+ const take = async (clip: PhoneClip) => {
+ const claim = await apiFetch(`/api/cut/phone/clips/${clip.id}/claim`, { method: "POST" });
+ // 409: another editor has it. Nothing to do and nothing to put back.
+ if (!claim.ok) return;
+ let landed = false;
+ try {
+ landed = await land(clip);
+ } catch {
+ landed = false;
+ }
+ await apiFetch(
+ landed ? `/api/cut/phone/clips/${clip.id}` : `/api/cut/phone/clips/${clip.id}/release`,
+ { method: landed ? "DELETE" : "POST" }
+ );
+ };
+
+ const tick = async () => {
+ if (working) return;
+ working = true;
+ try {
+ const now = Date.now();
+ if (now - lastPairedCheck > PAIRED_POLL_MS) {
+ lastPairedCheck = now;
+ const res = await apiFetch("/api/cut/phone");
+ const body = res.ok ? ((await res.json()) as { devices?: unknown[] }) : null;
+ paired = (body?.devices?.length ?? 0) > 0;
+ }
+ if (!paired) return;
+ const res = await apiFetch("/api/cut/phone/clips");
+ if (!res.ok) return;
+ const { clips = [] } = (await res.json()) as { clips?: PhoneClip[] };
+ for (const clip of clips) {
+ if (stopped) return;
+ await take(clip);
+ }
+ } catch {
+ // The app quit, or the engine is restarting after an update. The next
+ // tick finds it again.
+ } finally {
+ working = false;
+ }
+ };
+
+ void tick();
+ const timer = setInterval(() => void tick(), POLL_MS);
+ return () => {
+ stopped = true;
+ clearInterval(timer);
+ };
+ }, [hasEngine]);
+}
diff --git a/site/src/queries/phoneLink.ts b/site/src/queries/phoneLink.ts
new file mode 100644
index 00000000..985cc886
--- /dev/null
+++ b/site/src/queries/phoneLink.ts
@@ -0,0 +1,74 @@
+"use client";
+
+import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
+
+import { apiFetch } from "@/cut/lib/api";
+
+// The phone link lives on this Mac's engine, so these go through the engine
+// transport rather than the site's own API. Everything here is off when the
+// app is not running: there is no hosted twin of a Bonjour listener.
+
+export const phoneLinkQueryKey = ["cut", "phone-link"] as const;
+
+export type PhoneDevice = {
+ id: string;
+ name: string;
+ pairedAt: number;
+ lastSeenAt: number;
+};
+
+export type PhonePairingWindow = {
+ code: string;
+ expiresAt: number;
+};
+
+export type PhoneLinkStatus = {
+ devices: PhoneDevice[];
+ pairing: PhonePairingWindow | null;
+ /** How the last window ended, once one is no longer open. */
+ closed: "paired" | "refused" | null;
+};
+
+async function engineJson<T>(path: string, init?: RequestInit): Promise<T> {
+ const response = await apiFetch(path, init);
+ if (!response.ok) throw new Error(`phone link ${response.status}`);
+ return (await response.json()) as T;
+}
+
+/** The paired phones and the open pairing code. Polls while a code is up, so
+ * the card can close itself the moment a phone answers. */
+export function usePhoneLink(enabled: boolean) {
+ return useQuery<PhoneLinkStatus>({
+ enabled,
+ queryKey: phoneLinkQueryKey,
+ queryFn: () => engineJson<PhoneLinkStatus>("/api/cut/phone"),
+ refetchInterval: (query) => (query.state.data?.pairing ? 2000 : false),
+ });
+}
+
+export function useStartPhonePairing() {
+ const queryClient = useQueryClient();
+ return useMutation({
+ mutationFn: () => engineJson<PhonePairingWindow>("/api/cut/phone/pair", { method: "POST" }),
+ onSuccess: () => queryClient.invalidateQueries({ queryKey: phoneLinkQueryKey }),
+ });
+}
+
+export function useCancelPhonePairing() {
+ const queryClient = useQueryClient();
+ return useMutation({
+ mutationFn: () => engineJson<{ ok: true }>("/api/cut/phone/pair", { method: "DELETE" }),
+ onSuccess: () => queryClient.invalidateQueries({ queryKey: phoneLinkQueryKey }),
+ });
+}
+
+export function useUnpairPhone() {
+ const queryClient = useQueryClient();
+ return useMutation({
+ mutationFn: (id: string) =>
+ engineJson<{ ok: true }>(`/api/cut/phone/devices/${encodeURIComponent(id)}`, {
+ method: "DELETE",
+ }),
+ onSuccess: () => queryClient.invalidateQueries({ queryKey: phoneLinkQueryKey }),
+ });
+}