Open source CapCut alternative.
TypeScript
refactor(site): drive first-open staging from the project doc [rebuild]
David Le215228e5591d
diff --git a/site/public/cut-starter/project.json b/site/public/cut-starter/project.jsonindex e989f799..87ea65d3 100644--- a/site/public/cut-starter/project.json+++ b/site/public/cut-starter/project.json@@ -441,5 +441,10 @@"musicAssetId": "1d1583d4","musicClipId": "1fc154e8","endedAt": 1785304696184+ },+ "firstOpen": {+ "sidePanel": null,+ "chat": "59ba035c-d37a-400e-ac1a-d216c254fa5f",+ "play": true}}diff --git a/site/src/cut/components/AiPanel.tsx b/site/src/cut/components/AiPanel.tsxindex 945b19de..df6075d8 100644--- a/site/src/cut/components/AiPanel.tsx+++ b/site/src/cut/components/AiPanel.tsx@@ -88,7 +88,6 @@ import { useCreditsRecheck, useOutOfCredits } from "@/cut/lib/hosted";import { streamGeminiChat } from "@/cut/lib/geminiChat";import { AI_MODELS } from "@/cut/lib/aiModels";import { saveAssetToLibrary } from "@/cut/lib/library";-import { isStarterProjectId } from "@/cut/lib/starter";import { formatDuration, useGenScene } from "@/cut/lib/genScene";import { lightboxItemFromRef, useLightbox } from "@/cut/lib/lightbox";import { refsFromDroppedFiles } from "@/cut/lib/refMedia";@@ -259,12 +258,6 @@ export function AiPanel({? crypto.randomUUID(): (readActiveChat(projectId) ?? crypto.randomUUID()),);- // Whether this browser held a chat pointer for the project before the write- // below stamps one; captured at mount so the resume in the merge effect can- // still tell a first open apart.- const [hadStoredChat] = useState(- () => typeof window !== "undefined" && readActiveChat(projectId) !== null,- );useEffect(() => {writeActiveChat(projectId, activeChat);}, [activeChat, projectId]);@@ -276,22 +269,12 @@ export function AiPanel({useEffect(() => {let alive = true;void ensureCloudThreads(projectId).then(() => {- if (!alive) return;- // The starter project's first open on this browser resumes its newest- // saved thread — the seeded walkthrough — instead of a blank chat. Set- // before chatsReady so the session mounts once, on the right thread.- if (!hadStoredChat && isStarterProjectId(projectId)) {- setActiveChat((cur) => {- const list = readThreads(projectId);- return list.length > 0 && !list.some((t) => t.id === cur) ? list[0].id : cur;- });- }- setChatsReady(true);+ if (alive) setChatsReady(true);});return () => {alive = false;};- }, [projectId, hadStoredChat]);+ }, [projectId]);// A shared view opens on the owner's newest thread — the viewer has no chat// of their own to resume. Derived, so the fallback holds until the viewerdiff --git a/site/src/cut/components/Editor.tsx b/site/src/cut/components/Editor.tsxindex d9243dca..27cbd80f 100644--- a/site/src/cut/components/Editor.tsx+++ b/site/src/cut/components/Editor.tsx@@ -26,11 +26,12 @@ import { enrichAsset, importFileToProject, prepareImport } from "@/cut/lib/media// persisted run resumes on project load even when the AI panel never mounts.import "@/cut/lib/genScene";import { installDevHooks } from "@/cut/lib/devHooks";+import { ensureCloudThreads } from "@/cut/lib/chatCloud";+import { readProjectThreads, writeActiveChat } from "@/cut/lib/chatThreads";import { backTarget, useCutBase } from "@/cut/lib/nav";import { useOnboardingCover } from "@/cut/lib/onboarding";import { requestSidePanel } from "@/cut/lib/panelRequest";import { resolveProjectPlacement } from "@/cut/lib/residency";-import { starterProjectId } from "@/cut/lib/starter";import {docAudioClips,docClips,@@ -40,7 +41,6 @@ import {useEditor,} from "@/cut/lib/store";import type { MediaAsset } from "@/cut/lib/types";-import { authClient } from "@/lib/auth-client";import { AiPanel } from "./AiPanel";import { ExportDialog } from "./ExportDialog";import { Inspector } from "./Inspector";@@ -171,31 +171,55 @@ export function Editor({};}, [projectId, viewer]);- // The starter project's first open presents the finished cut: the side- // panel folds to its rail, chat opens on the seeded thread, and the video- // plays — held while the welcome slides still cover the editor, so it- // starts the moment they hand over. Once per browser; after this the- // layout is the user's own to keep.- const { data: session } = authClient.useSession();- const userId = session?.user.id;+ // A doc that carries `firstOpen` presents itself on its first open in this+ // browser: the editor reads the block and follows it — which side-panel tab+ // shows (null folds the panel to its rail), whether chat opens on the+ // newest saved thread, whether playback starts. Playback is held while the+ // welcome slides still cover the editor, so it starts the moment they hand+ // over. Applied once per browser; after this the layout is the user's own+ // to keep. The seeded starter template is what ships the block today.useEffect(() => {- if (!loaded || viewer || !userId || projectId !== starterProjectId(userId)) return;- const seenKey = `cut-starter-intro-${projectId}`;+ if (!loaded || viewer) return;+ const intro = useEditor.getState().firstOpen;+ if (!intro) return;+ const seenKey = `cut-first-open-${projectId}`;if (localStorage.getItem(seenKey)) return;localStorage.setItem(seenKey, "1");- requestSidePanel(null);- useEditor.getState().setAiOpen(true);- if (!useOnboardingCover.getState().up) {- useEditor.getState().setPlaying(true);- return;+ let alive = true;+ const cleanups: (() => void)[] = [];+ if (intro.sidePanel !== undefined) requestSidePanel(intro.sidePanel);+ if (intro.chat === true) {+ // No thread named: the panel opens on a fresh empty chat.+ useEditor.getState().setAiOpen(true);+ } else if (intro.chat) {+ // Point the chat at the named thread before the panel mounts, so it+ // opens the seeded conversation rather than a blank one.+ const wantedId = intro.chat;+ void ensureCloudThreads(projectId).then(() => {+ if (!alive) return;+ const threads = readProjectThreads(projectId);+ const wanted = threads.find((t) => t.id === wantedId) ?? threads[0];+ if (wanted) writeActiveChat(projectId, wanted.id);+ useEditor.getState().setAiOpen(true);+ });}- const unsub = useOnboardingCover.subscribe((s) => {- if (s.up) return;- unsub();- useEditor.getState().setPlaying(true);- });- return unsub;- }, [loaded, viewer, userId, projectId]);+ if (intro.play) {+ if (!useOnboardingCover.getState().up) {+ useEditor.getState().setPlaying(true);+ } else {+ const unsub = useOnboardingCover.subscribe((s) => {+ if (s.up) return;+ unsub();+ useEditor.getState().setPlaying(true);+ });+ cleanups.push(unsub);+ }+ }+ return () => {+ alive = false;+ cleanups.forEach((fn) => fn());+ };+ }, [loaded, viewer, projectId]);// Delayed follow of the owner's edits: poll the doc version and reload on// change, keeping the playhead. A 401/403/404 means the share was revoked ordiff --git a/site/src/cut/components/SidePanel.tsx b/site/src/cut/components/SidePanel.tsxindex 7c4f6dbe..5218edce 100644--- a/site/src/cut/components/SidePanel.tsx+++ b/site/src/cut/components/SidePanel.tsx@@ -73,10 +73,10 @@ import { CAPTION_LIMIT, normalizeTags } from "@/cut/lib/publish";import { useEditor } from "@/cut/lib/store";import { formatTime } from "@/cut/lib/time";import { useLocalPref } from "@/cut/lib/uiState";-import type { MediaAsset } from "@/cut/lib/types";+import type { MediaAsset, SidePanelTab } from "@/cut/lib/types";import { cn } from "@/lib/utils";import { useRevealEffect, useRevealFlash } from "@/cut/lib/refReveal";-import { usePanelRequestEffect, type SidePanelTab } from "@/cut/lib/panelRequest";+import { usePanelRequestEffect } from "@/cut/lib/panelRequest";import { CopyNameLabel } from "./AssetRefs";import { AudioCardFace, AudioPanel } from "./AudioPanel";import { FolderCrumb, FolderShelf } from "./desktopFolders";diff --git a/site/src/cut/lib/aiTools.ts b/site/src/cut/lib/aiTools.tsindex f7a29017..5e7ec4c5 100644--- a/site/src/cut/lib/aiTools.ts+++ b/site/src/cut/lib/aiTools.ts@@ -30,7 +30,7 @@ import {makeStillSheetClientSide,renderAudioSpanWav,} from "./media";-import { requestSidePanel, SIDE_PANEL_TABS, type SidePanelTab } from "./panelRequest";+import { requestSidePanel, SIDE_PANEL_TABS } from "./panelRequest";import { blobToInlineAudio, refToInlineAudio, visualRefs, type InlineImage } from "./refMedia";import { characterPrompt, stockTitle } from "./stock";import { STOCK_IMAGES } from "./stockManifest";@@ -68,6 +68,7 @@ import {type FontId,type MediaAsset,type Selection,+ type SidePanelTab,type TransitionStyle,type VideoClip,} from "./types";diff --git a/site/src/cut/lib/panelRequest.ts b/site/src/cut/lib/panelRequest.tsindex 7de54e40..92d4dd56 100644--- a/site/src/cut/lib/panelRequest.ts+++ b/site/src/cut/lib/panelRequest.ts@@ -3,19 +3,11 @@import { useEffect } from "react";import { create } from "zustand";-// The assistant's set_side_panel tool lands here: a one-shot request the-// SidePanel applies — open a tab, or collapse the panel to the icon rail so-// the preview canvas takes the freed width.+// The assistant's set_side_panel tool and the doc's firstOpen block land+// here: a one-shot request the SidePanel applies — open a tab, or collapse+// the panel to the icon rail so the preview canvas takes the freed width.-/** Side-panel tabs; `null` collapses the panel to the icon rail. */-export type SidePanelTab =- | "media"- | "library"- | "video"- | "image"- | "audio"- | "subtitles"- | "publish";+import type { SidePanelTab } from "./types";export const SIDE_PANEL_TABS: SidePanelTab[] = ["media",diff --git a/site/src/cut/lib/starter.ts b/site/src/cut/lib/starter.tsindex 551a7fee..1b984ce4 100644--- a/site/src/cut/lib/starter.ts+++ b/site/src/cut/lib/starter.ts@@ -2,6 +2,3 @@// Its id is derived from the owner, so the seed is idempotent and the client// can address the project straight from the session without a lookup.export const starterProjectId = (userId: string) => `starter-${userId}`;--/** Whether an id addresses an account's starter project. */-export const isStarterProjectId = (id: string) => id.startsWith("starter-");diff --git a/site/src/cut/lib/store.ts b/site/src/cut/lib/store.tsindex a83ac31d..054f2e5c 100644--- a/site/src/cut/lib/store.ts+++ b/site/src/cut/lib/store.ts@@ -177,6 +177,9 @@ export interface EditorState {* so their chat cards render on machines that never ran the job. Outside the* undo history, like assets. */renders: RenderRecord[];+ /** The doc's first-open presentation (ProjectDoc.firstOpen), carried so the+ * editor can apply it and saves keep it. */+ firstOpen?: ProjectDoc["firstOpen"];/** Load a project into the store. `inPlace` is for re-reading a project the* editor is already showing (the viewer's change poll, a conflict reload):@@ -908,6 +911,7 @@ export const useEditor = create<EditorState>((baseSet, get) => {exportOpen: false,genvideo: undefined,renders: [],+ firstOpen: undefined,});hydrating = false;// A background scene run may still be writing this project's doc — drain@@ -986,6 +990,7 @@ export const useEditor = create<EditorState>((baseSet, get) => {subtitleStatus: (doc.subtitles?.cues.length ?? 0) > 0 ? "ready" : "idle",genvideo: doc.genvideo ?? undefined,renders: Array.isArray(doc.renders) ? doc.renders : [],+ firstOpen: doc.firstOpen,loaded: true,loadEpoch: get().loadEpoch + 1,});@@ -3189,6 +3194,7 @@ export function serializeDoc(s: {subtitles: SubtitlesBlock;genvideo?: VideoProject;renders: RenderRecord[];+ firstOpen?: ProjectDoc["firstOpen"];}): Partial<ProjectDoc> {return {name: s.projectName,@@ -3207,6 +3213,7 @@ export function serializeDoc(s: {// to the PUT handler, so a dismissed plan could otherwise never be cleared.genvideo: s.genvideo ?? null,renders: s.renders,+ firstOpen: s.firstOpen,};}diff --git a/site/src/cut/lib/types.ts b/site/src/cut/lib/types.tsindex a5d1c1df..b8c671cf 100644--- a/site/src/cut/lib/types.ts+++ b/site/src/cut/lib/types.ts@@ -749,6 +749,16 @@ export interface ShareFeatures {details: boolean;}+/** The editor's side-panel tabs ("publish" shows as Details). */+export type SidePanelTab =+ | "media"+ | "library"+ | "video"+ | "image"+ | "audio"+ | "subtitles"+ | "publish";+export interface ProjectDoc {version: 1;name: string;@@ -803,6 +813,20 @@ export interface ProjectDoc {genvideo?: VideoProject | null;/** Chat-launched video renders, running and settled — see RenderRecord. */renders?: RenderRecord[];+ /** How the project presents itself the first time a browser opens it: the+ * editor reads this and applies it once, then the layout is the user's own.+ * Seeded template docs (public/cut-starter) carry it; absent means an+ * ordinary open. */+ firstOpen?: {+ /** Side-panel tab to show; null folds the panel to its icon rail. */+ sidePanel?: SidePanelTab | null;+ /** Open the chat panel: `true` on a fresh empty thread, a thread id on+ * that saved thread ("newest", or any id no saved thread carries,+ * resolves to the project's most recent one). */+ chat?: boolean | string;+ /** Start playback (held until the welcome slides hand over). */+ play?: boolean;+ };}/** A chat-launched video render, mirrored into the doc as it runs and settles.