Skip to content

Open source CapCut alternative.

TypeScript

feat(cut): the editor knows when ChatGPT hosts it

David Le5893f8a2167f
diff --git a/site/src/cut/components/Editor.tsx b/site/src/cut/components/Editor.tsx
index f3ee59c4..0b66d6e6 100644
--- a/site/src/cut/components/Editor.tsx
+++ b/site/src/cut/components/Editor.tsx
@@ -1,6 +1,8 @@
"use client";
import { chatRuntime } from "@/cut/lib/chatRuntime";
+import { useHostInset } from "@/cut/lib/hostInset";
+import { useEnvironment } from "@/cut/lib/environment";
import { useCallback, useEffect, useRef, useState } from "react";
import Link from "next/link";
import { Clapperboard, Laptop, Loader2, X } from "lucide-react";
@@ -165,6 +167,8 @@ export function Editor({
const aiOpen = useEditor((s) => s.aiOpen);
const timelineOpen = useEditor((s) => s.timelineOpen);
const sharedFeatures = useEditor((s) => s.sharedFeatures);
+ useHostInset();
+ const { chatgpt } = useEnvironment();
// The inspector only earns its column when the selection has a panel to
// show; otherwise (nothing selected, a subtitle cue, a transition bar — the
// Transitions tab is its panel) it is an empty white panel, so collapse it
@@ -1264,7 +1268,7 @@ export function Editor({
</div>
)}
</div>
- {(!viewer || sharedFeatures?.chat) && (
+ {(!viewer || sharedFeatures?.chat) && !chatgpt && (
<AiPanel
key={projectId}
projectId={projectId}
diff --git a/site/src/cut/components/LibraryCard.tsx b/site/src/cut/components/LibraryCard.tsx
index 5519089f..2ff7d076 100644
--- a/site/src/cut/components/LibraryCard.tsx
+++ b/site/src/cut/components/LibraryCard.tsx
@@ -1,6 +1,7 @@
"use client";
import { useState, type ReactNode } from "react";
+import { useEnvironment } from "@/cut/lib/environment";
import { Download, Ellipsis, ExternalLink, Film, Image as ImageIcon, Music, Plus, Share2, Trash2, Type } from "lucide-react";
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger } from "@/components/ui/dropdown-menu";
import { Skeleton } from "@/components/ui/skeleton";
@@ -70,6 +71,7 @@ export function LibraryCard({
onShare?: () => void;
onDragStartExtra?: (e: React.DragEvent) => void;
}) {
+ const { chatgpt } = useEnvironment();
// ⌘C over the card copies its mention token. A card inside the current
// selection carries the whole set, the same rule its drag follows. A font is
// used from the font menu, so it names nothing a prompt can point a tool
@@ -381,7 +383,7 @@ export function LibraryCard({
onClick={(e) => e.stopPropagation()}
>
{onShare && <DropdownMenuItem disabled={offline} onClick={onShare}><Share2 /> Share</DropdownMenuItem>}
- {sharedMedia ? (
+ {chatgpt ? null : sharedMedia ? (
<DropdownMenuItem render={<a href={sharedMedia.downloadHref} />}>
<Download /> Download
</DropdownMenuItem>
diff --git a/site/src/cut/components/Preview.tsx b/site/src/cut/components/Preview.tsx
index 6e13c010..e5846906 100644
--- a/site/src/cut/components/Preview.tsx
+++ b/site/src/cut/components/Preview.tsx
@@ -754,7 +754,8 @@ function ShowTimelineButton() {
title="Show timeline"
aria-label="Show timeline"
onClick={() => useEditor.getState().setTimelineOpen(true)}
- className="absolute right-3 bottom-3 z-20 grid size-8 place-items-center rounded-full border bg-background/95 text-foreground shadow-md backdrop-blur hover:bg-accent"
+ style={{ bottom: "calc(0.75rem + var(--host-inset-bottom, 0px))" }}
+ className="absolute right-3 z-20 grid size-8 place-items-center rounded-full border bg-background/95 text-foreground shadow-md backdrop-blur hover:bg-accent"
>
<PanelTopClose className="size-4" />
</button>
diff --git a/site/src/cut/components/RequireSession.tsx b/site/src/cut/components/RequireSession.tsx
index 3d42e980..d7f27c4d 100644
--- a/site/src/cut/components/RequireSession.tsx
+++ b/site/src/cut/components/RequireSession.tsx
@@ -1,7 +1,8 @@
"use client";
import { bindChatRuntime } from "@/cut/lib/chatRuntime";
-import { useEffect, useMemo, type ReactNode } from "react";
+import { useEffect, type ReactNode } from "react";
+import { useEnvironment } from "@/cut/lib/environment";
import { authHrefFor } from "@/app/_components/landing/useAppEntryHref";
import {
@@ -29,10 +30,7 @@ export function RequireSession({ children }: { children: ReactNode }) {
// Framed by the ChatGPT card, the page carries its own partitioned session;
// a browser that dropped it gets a link out, since sign-in cannot run in a
// frame.
- const embedded = useMemo(
- () => typeof window !== "undefined" && window.self !== window.top && new URLSearchParams(window.location.search).get("embed") === "chatgpt",
- [],
- );
+ const { chatgpt: embedded } = useEnvironment();
useAppLoaded("cut", session?.user);
diff --git a/site/src/cut/components/SidePanel.tsx b/site/src/cut/components/SidePanel.tsx
index c2418f1a..6c879dde 100644
--- a/site/src/cut/components/SidePanel.tsx
+++ b/site/src/cut/components/SidePanel.tsx
@@ -113,6 +113,7 @@ import {
import { useGenerate, type GenerateJob } from "@/cut/lib/generate";
import { CAPTION_LIMIT, normalizeTags } from "@/cut/lib/publish";
import { useEditor } from "@/cut/lib/store";
+import { useEnvironment } from "@/cut/lib/environment";
import { formatTime } from "@/cut/lib/time";
import { useLocalPref } from "@/cut/lib/uiState";
import type { MediaAsset, SidePanelTab } from "@/cut/lib/types";
@@ -181,7 +182,12 @@ export function SidePanel({
// withholds sits locked, and hovering it says why. The Library is the
// viewer's own cross-project shelf and never appears there.
const sharedFeatures = useEditor((s) => s.sharedFeatures);
+ const { chatgpt } = useEnvironment();
+ // Inside ChatGPT the card shows no credits, so the tabs that spend them
+ // stay out of the rail.
+ const spends = (id: Tab) => id === "video" || id === "image" || id === "audio" || id === "subtitles";
const offered = (id: Tab): boolean =>
+ (!chatgpt || !spends(id)) && (
!sharedFeatures ||
(id === "media"
? sharedFeatures.media
@@ -191,7 +197,7 @@ export function SidePanel({
? sharedFeatures.subtitles
: id === "publish"
? sharedFeatures.details
- : false);
+ : false));
const visibleTabs = TABS.filter(({ id }) => offered(id));
// `null` collapses the panel: clicking the active tab unselects it, leaving
// just the icon rail so the video canvas takes the freed width.
@@ -766,6 +772,7 @@ function ProjectFilesPanel({
importing: boolean;
}) {
const caps = useCutCaps();
+ const { chatgpt } = useEnvironment();
// Only user-imported media lives here; anything Cut created (recordings, AI
// generations, voiceovers, freeze frames, stock adds) is tagged with an
// `origin` and stays where it was made.
@@ -1176,7 +1183,7 @@ function ProjectFilesPanel({
>
<FolderOpen />
</Button>
- ) : (
+ ) : chatgpt ? null : (
<Button
variant="ghost"
size="icon-xs"
@@ -1375,6 +1382,7 @@ function AssetCard({
onDragLanded?: () => void;
}) {
const caps = useCutCaps();
+ const { chatgpt } = useEnvironment();
const [saved, setSaved] = useState(false);
// Number of timeline items that would be cascade-deleted; null = no prompt.
const [confirmUses, setConfirmUses] = useState<number | null>(null);
@@ -1535,12 +1543,12 @@ function AssetCard({
<DropdownMenuItem onClick={saveToLibrary} disabled={!!asset.upload}>
<FolderPlus /> Save to library
</DropdownMenuItem>
- <DropdownMenuItem
+ {!chatgpt && <DropdownMenuItem
onClick={() => downloadMedia(projectId, asset)}
disabled={!!asset.upload}
>
<Download /> Download
- </DropdownMenuItem>
+ </DropdownMenuItem>}
{caps.revealInFinder && (
<DropdownMenuItem
onClick={() => void revealMedia(projectId, asset.fileName).catch(() => {})}
diff --git a/site/src/cut/components/Timeline.tsx b/site/src/cut/components/Timeline.tsx
index 777433a6..9a717efc 100644
--- a/site/src/cut/components/Timeline.tsx
+++ b/site/src/cut/components/Timeline.tsx
@@ -2202,8 +2202,8 @@ export function Timeline() {
>
<div
ref={innerRef}
- className="tl-content relative flex-1 pb-2"
- style={{ width: contentW, marginLeft: PAD_SIDE }}
+ className="tl-content relative flex-1"
+ style={{ width: contentW, marginLeft: PAD_SIDE, paddingBottom: "calc(0.5rem + var(--host-inset-bottom, 0px))" }}
onPointerDown={deselectIfSelf}
>
{/* An empty project reads as a stack of resting tracks: the same
diff --git a/site/src/cut/components/TopBar.tsx b/site/src/cut/components/TopBar.tsx
index 21284d4b..e3c5edc7 100644
--- a/site/src/cut/components/TopBar.tsx
+++ b/site/src/cut/components/TopBar.tsx
@@ -34,6 +34,7 @@ import { retryUpload } from "@/cut/lib/importQueue";
import { backTarget, projectHref, useCutBase } from "@/cut/lib/nav";
import { copyProjectAcross } from "@/cut/lib/projectCopy";
import { projectDuration, useEditor } from "@/cut/lib/store";
+import { useEnvironment } from "@/cut/lib/environment";
import { useLocalPref } from "@/cut/lib/uiState";
import { ASPECT_PRESETS, aspectLabel, aspectOrientation, normalizeAspect, parseRatio, type Aspect } from "@/cut/lib/types";
import { cn } from "@/lib/utils";
@@ -81,6 +82,7 @@ export function TopBar({
const [editing, setEditing] = useState(false);
const [draft, setDraft] = useState("");
const [recordMode, setRecordMode] = useState<RecordMode | null>(null);
+ const { chatgpt } = useEnvironment();
// The right-hand actions step down as the bar tightens: full labels →
// icon-only Share/Cloud/Export → everything in a … menu. The credits and
// storage pills never fold. Which step fits is measured against hidden copies of the
@@ -392,7 +394,7 @@ export function TopBar({
<Upload data-icon={compact ? undefined : "inline-start"} />
{!compact && "Export"}
</Button>
- <Button
+ {!chatgpt && <Button
variant={aiOpen ? "default" : "outline"}
size="sm"
className="ai-toggle relative"
@@ -406,7 +408,7 @@ export function TopBar({
>
<Sparkles data-icon="inline-start" /> Chat
{!aiOpen && <ChatStatusBadge status={chatStatus} />}
- </Button>
+ </Button>}
</>
);
@@ -417,7 +419,7 @@ export function TopBar({
// toward whichever side has slack instead of colliding with the rail.
<header ref={headerRef} className="relative flex items-center border-b border-border bg-card">
<div ref={leftRef} className="flex shrink-0 items-center gap-1 pl-2">
- <Button
+ {!chatgpt && <Button
variant="ghost"
size="icon-sm"
aria-label={`Back to ${back.tab}`}
@@ -425,7 +427,7 @@ export function TopBar({
render={<Link href={back.href} />}
>
<ChevronLeft />
- </Button>
+ </Button>}
<span className="grid size-[22px] shrink-0 place-items-center">
<img
src="/donkey-logo.svg"
@@ -603,7 +605,7 @@ export function TopBar({
</DropdownMenu>
);
})()}
- <DropdownMenu>
+ {!chatgpt && <DropdownMenu>
<DropdownMenuTrigger className="record-switch flex items-center gap-1.5 rounded-full border border-border bg-card px-3 py-1.5 text-xs font-medium text-muted-foreground shadow-xs transition-colors hover:text-foreground">
<span className="size-2 rounded-full bg-red-500" aria-hidden />
Record
@@ -617,7 +619,7 @@ export function TopBar({
<Mic /> Record audio
</DropdownMenuItem>
</DropdownMenuContent>
- </DropdownMenu>
+ </DropdownMenu>}
</div>
{recordMode && (
<RecordDialog
@@ -633,8 +635,8 @@ export function TopBar({
button. */}
<div className="flex shrink-0 items-center gap-2 pr-3">
<div ref={pillRef} className="flex items-center gap-2">
- <SubscribeBonusPill />
- <CreditsPill />
+ {!chatgpt && <SubscribeBonusPill />}
+ {!chatgpt && <CreditsPill />}
<StoragePill />
</div>
<div className="relative flex items-center">
@@ -687,7 +689,7 @@ export function TopBar({
>
<Upload /> {cloudUploading ? finishingLabel : "Export"}
</DropdownMenuItem>
- <DropdownMenuItem
+ {!chatgpt && <DropdownMenuItem
onClick={() => {
const s = useEditor.getState();
s.setAiOpen(!s.aiOpen);
@@ -696,7 +698,7 @@ export function TopBar({
<Sparkles />
<span className="relative flex-1">Chat{!aiOpen && <ChatStatusBadge status={chatStatus} />}</span>
{aiOpen && <Check className="size-3.5 text-muted-foreground" />}
- </DropdownMenuItem>
+ </DropdownMenuItem>}
</DropdownMenuContent>
</DropdownMenu>
)}
diff --git a/site/src/cut/lib/environment.ts b/site/src/cut/lib/environment.ts
new file mode 100644
index 00000000..b5c75238
--- /dev/null
+++ b/site/src/cut/lib/environment.ts
@@ -0,0 +1,27 @@
+"use client";
+
+import { useSyncExternalStore } from "react";
+
+/**
+ * Where the editor is running. The web is the editor on donkeycut.com; ChatGPT
+ * is the same page framed inside the ChatGPT card, which arrives with
+ * `?embed=chatgpt` and its own partitioned session. The card is the whole
+ * surface there, so chrome that belongs to the site (navigation, credits,
+ * recording, downloads, the assistant) stays out of the frame.
+ */
+export type EditorHost = "web" | "chatgpt";
+
+export function editorHost(): EditorHost {
+ if (typeof window === "undefined" || window.self === window.top) return "web";
+ return new URLSearchParams(window.location.search).get("embed") === "chatgpt" ? "chatgpt" : "web";
+}
+
+const subscribe = () => () => {};
+const serverHost = (): EditorHost => "web";
+
+/** The host as React state: the server and the first client render agree on
+ * the web, and a framed page settles on ChatGPT right after hydration. */
+export function useEnvironment() {
+ const host = useSyncExternalStore(subscribe, editorHost, serverHost);
+ return { host, chatgpt: host === "chatgpt" };
+}
diff --git a/site/src/cut/lib/hostInset.ts b/site/src/cut/lib/hostInset.ts
new file mode 100644
index 00000000..be907f0e
--- /dev/null
+++ b/site/src/cut/lib/hostInset.ts
@@ -0,0 +1,26 @@
+"use client";
+
+import { useEffect } from "react";
+import { editorHost } from "@/cut/lib/environment";
+
+/**
+ * Framed inside the ChatGPT card, the editor sits under the host's composer
+ * in fullscreen. The card tells the frame how tall that band is, and the
+ * editor keeps its bottom controls and the timeline's last rows above it
+ * through `--host-inset-bottom`. Outside the card the variable stays unset.
+ */
+export function useHostInset() {
+ useEffect(() => {
+ if (editorHost() !== "chatgpt") return;
+ const onMessage = (event: MessageEvent) => {
+ if (event.source !== window.parent) return;
+ const data = event.data as { type?: unknown; insetBottom?: unknown } | null;
+ if (data?.type !== "donkeycut:host-inset" || typeof data.insetBottom !== "number") return;
+ const px = Math.max(0, Math.min(400, Math.round(data.insetBottom)));
+ document.documentElement.style.setProperty("--host-inset-bottom", `${px}px`);
+ };
+ window.addEventListener("message", onMessage);
+ window.parent.postMessage({ type: "donkeycut:host-inset?" }, "*");
+ return () => window.removeEventListener("message", onMessage);
+ }, []);
+}