Open source CapCut alternative.
TypeScript
feat(site): name the tab after the open project, folder, note or page
David Le3049aa0bc22c
diff --git a/site/src/app/cut/app/(home)/settings/SettingsHeader.tsx b/site/src/app/cut/app/(home)/settings/SettingsHeader.tsxindex 4ea5bcae..e87a5d01 100644--- a/site/src/app/cut/app/(home)/settings/SettingsHeader.tsx+++ b/site/src/app/cut/app/(home)/settings/SettingsHeader.tsx@@ -2,6 +2,7 @@import { usePathname } from "next/navigation";+import { useTabTitle } from "@/cut/hooks/useTabTitle";import { cn } from "@/lib/utils";// Ordered so the more specific path wins the suffix match. Billing pins its@@ -32,6 +33,7 @@ export function SettingsHeader() {// Billing is the settings root, so it's also the fallback title.const section =SECTIONS.find((s) => pathname.endsWith(s.suffix)) ?? SECTIONS.at(-1)!;+ useTabTitle(section.title);return (<divclassName={cn(diff --git a/site/src/app/cut/app/(home)/settings/layout.tsx b/site/src/app/cut/app/(home)/settings/layout.tsxindex 6cb69611..925efcd2 100644--- a/site/src/app/cut/app/(home)/settings/layout.tsx+++ b/site/src/app/cut/app/(home)/settings/layout.tsx@@ -5,7 +5,6 @@ import { SettingsHeader } from "@/app/cut/app/(home)/settings/SettingsHeader";import { SettingsGuard } from "@/cut/components/SettingsGuard";export const metadata: Metadata = {- title: "Settings | Donkey Cut",description: "Manage your Donkey Cut subscription, credits, and usage.",};diff --git a/site/src/cut/components/CameraRollView.tsx b/site/src/cut/components/CameraRollView.tsxindex ee27c57b..ad14d73a 100644--- a/site/src/cut/components/CameraRollView.tsx+++ b/site/src/cut/components/CameraRollView.tsx@@ -13,6 +13,7 @@ import {AlertDialogHeader,AlertDialogTitle,} from "@/components/ui/alert-dialog";+import { useTabTitle } from "@/cut/hooks/useTabTitle";import { useClipTitles } from "@/cut/lib/clipTitle";import { deleteFromLibrary, type LibraryAsset } from "@/cut/lib/library";import { lightboxItemFromLibrary, useLightbox } from "@/cut/lib/lightbox";@@ -42,6 +43,7 @@ export function CameraRollView() {[client]);useClipTitles(clips, patch);+ useTabTitle("Camera Roll");const remove = async () => {if (!deleting) return;diff --git a/site/src/cut/components/Editor.tsx b/site/src/cut/components/Editor.tsxindex db2968b2..daa86b95 100644--- a/site/src/cut/components/Editor.tsx+++ b/site/src/cut/components/Editor.tsx@@ -4,6 +4,7 @@ import { chatRuntime } from "@/cut/lib/chatRuntime";import { useHostBridge, useHostReady } from "@/cut/lib/hostBridge";import { useCutMode } from "@/cut/lib/backend/hooks";import { usePhoneInbox } from "@/cut/hooks/usePhoneInbox";+import { useTabTitle } from "@/cut/hooks/useTabTitle";import { useHostCommands } from "@/cut/lib/hostCommands";import { useEnvironment } from "@/cut/lib/environment";import { useCallback, useEffect, useRef, useState } from "react";@@ -151,16 +152,8 @@ export function Editor({const loaded = useEditor((s) => s.loaded);const loadError = useEditor((s) => s.loadError);// The tab is the open project, so it wears the project's name — through- // renames too. Leaving hands the title back to whatever it was.- const projectName = useEditor((s) => s.projectName);- useEffect(() => {- if (!projectName) return;- const previous = document.title;- document.title = projectName;- return () => {- document.title = previous;- };- }, [projectName]);+ // renames too.+ useTabTitle(useEditor((s) => s.projectName));// Until loadProject runs, the store still holds the previously open project;// rendering the editor against it would leak that project's state (chat,// clips, selection) into this route.diff --git a/site/src/cut/components/LibraryView.tsx b/site/src/cut/components/LibraryView.tsxindex 67d3114f..58c81baf 100644--- a/site/src/cut/components/LibraryView.tsx+++ b/site/src/cut/components/LibraryView.tsx@@ -19,6 +19,7 @@ import {import { Button } from "@/components/ui/button";import { DropdownMenuItem } from "@/components/ui/dropdown-menu";import { useDeleteKey } from "@/cut/hooks/useDeleteKey";+import { useTabTitle } from "@/cut/hooks/useTabTitle";import {Dialog,DialogContent,@@ -197,7 +198,8 @@ export function LibraryView() {const reload = useCallback(() => refetchLibrary(client), [client]);// The open folder lives in the URL (?folder=…) so the browser's back button// steps folder → root and the location survives reloads.- const openFolder = useSearchParams().get("folder");+ const params = useSearchParams();+ const openFolder = params.get("folder");// Media on its way in — an upload or a link — each one a tile in the grid// from the moment it starts, so the library shows the work rather than the// dialog holding it.@@ -570,6 +572,8 @@ export function LibraryView() {);// The way down to the open folder, and what is filed right here.const trail = useMemo(() => folderTrail(folders, openFolder), [folders, openFolder]);+ // The tab names the open folder; an open note names itself.+ useTabTitle(params.has("note") ? null : openFolder ? trail.at(-1)?.name : "Library");const shownFolders = useMemo(() => childrenOf(folders, openFolder), [folders, openFolder]);// What can be picked at this level, in the order a ⇧-range runs: the// shelf's folders, then the grid with templates leading.diff --git a/site/src/cut/components/NotesView.tsx b/site/src/cut/components/NotesView.tsxindex bc283230..67c3213c 100644--- a/site/src/cut/components/NotesView.tsx+++ b/site/src/cut/components/NotesView.tsx@@ -4,6 +4,7 @@ import { useEffect, useImperativeHandle, useRef, useState, type Ref } from "reacimport { useSearchParams } from "next/navigation";import { useQueryClient, type QueryClient } from "@tanstack/react-query";import { FolderPlus, Loader2, Plus, StickyNote } from "lucide-react";+import { useTabTitle } from "@/cut/hooks/useTabTitle";import { noteInLibraryFolder, noteRef, type NoteLocation } from "@/cut/lib/noteReference";import { setRefDragData, clearRefDrag } from "@/cut/lib/assetRef";import type { Residency } from "@/cut/lib/residency";@@ -191,6 +192,17 @@ export function NotesView({ library, ref }: Props = {}) {const stored = openNoteId ? list.find((n) => n.id === openNoteId) : undefined;const draft =editing && editing.id === openNoteId ? editing : stored ? draftOf(stored) : null;+ // The tab names the open note, as it is typed; without one, the open folder.+ // Inside the library, the library names the folder.+ useTabTitle(+ draft+ ? draft.title.trim() || "Untitled"+ : library+ ? null+ : openFolder+ ? trail.at(-1)?.name+ : "Notes",+ );const reload = () => void client.invalidateQueries({ queryKey: notesKey });const notesHref = (folder: string | null, note?: string | null) => {diff --git a/site/src/cut/components/ProjectsHome.tsx b/site/src/cut/components/ProjectsHome.tsxindex 6bf2b209..8b99cbdc 100644--- a/site/src/cut/components/ProjectsHome.tsx+++ b/site/src/cut/components/ProjectsHome.tsx@@ -63,6 +63,7 @@ import {} from "@/cut/lib/queries";import { useDeleteKey } from "@/cut/hooks/useDeleteKey";import { useInView } from "@/cut/hooks/useInView";+import { useTabTitle } from "@/cut/hooks/useTabTitle";import { useNewProjectTarget } from "@/cut/lib/newProject";import { NewProjectButton } from "@/cut/components/NewProjectButton";import { track } from "@/lib/analytics";@@ -663,6 +664,7 @@ export function ProjectsHome() {// The way down to the open folder, for the crumb.const trail = folderOwner ? folderTrail(data[folderOwner].folders, openFolder) : [];+ useTabTitle(openFolder ? trail.at(-1)?.name : "Projects");// A folder pins where a new project lands, but only a shelf that answers can// take one: inside a folder on an unreachable shelf, New project goes where// the picker points instead.diff --git a/site/src/cut/components/TabStatus.tsx b/site/src/cut/components/TabStatus.tsxindex 746b05ea..b85268ca 100644--- a/site/src/cut/components/TabStatus.tsx+++ b/site/src/cut/components/TabStatus.tsx@@ -3,6 +3,7 @@import { useEffect, useState } from "react";import { useExports } from "@/cut/lib/exportStore";import { useGenerate } from "@/cut/lib/generate";+import { stripTabMark as strip } from "@/cut/hooks/useTabTitle";import { useEditor } from "@/cut/lib/store";import { useTabActivity } from "@/cut/lib/tabActivity";@@ -14,8 +15,8 @@ import { useTabActivity } from "@/cut/lib/tabActivity";* The icon stays the donkey and takes a badge in its corner — amber, carrying* how many jobs are under way, then green with a check once the last of them* settles. The check waits for the user: until they look at the tab, or a few- * seconds if they were already looking. Then the tab goes back to being the- * project's name and the plain donkey.+ * seconds if they were already looking. Then the tab goes back to its plain+ * name and the plain donkey.*/const DONE_MARK = "✓ ";@@ -26,9 +27,6 @@ const BASE_ICON = "/favicon.ico";const BUSY_COLOR = "#ff9500";const DONE_COLOR = "#30d158";-/** Take back a mark this component put in front of the title, and nothing else. */-const strip = (title: string) => title.replace(/^(?:[✓●]|\(\d+\+?\)) /, "");-/** What the badge and the title say when several jobs run at once. */const tally = (count: number) => (count > 9 ? "9+" : String(count));@@ -120,9 +118,6 @@ function paint(state: "busy" | "done", count: number): string {}export function TabStatus() {- // The title is repainted when the project is renamed, which takes the mark- // with it, so the name is a dependency of the paint.- const projectName = useEditor((s) => s.projectName);const [count, setCount] = useState(0);const [done, setDone] = useState(false);const busy = count > 0;@@ -201,10 +196,17 @@ export function TabStatus() {return () => {live = false;};- }, [busy, done, count, projectName]);+ }, [busy, done, count]);- // Leaving the editor leaves the tab as it was found.- useEffect(() => dropIconLink, []);+ // Leaving takes the mark and the badge with it, so the next page's name+ // arrives bare.+ useEffect(+ () => () => {+ dropIconLink();+ document.title = strip(document.title);+ },+ [],+ );return null;}diff --git a/site/src/cut/hooks/useTabTitle.ts b/site/src/cut/hooks/useTabTitle.tsnew file mode 100644index 00000000..5a4aaea7--- /dev/null+++ b/site/src/cut/hooks/useTabTitle.ts@@ -0,0 +1,25 @@+"use client";++import { useEffect } from "react";++/** The work mark TabStatus puts in front of the title: a running count or a check. */+const MARK = /^(?:[✓●]|\(\d+\+?\)) /;++/** The title without the work mark. */+export const stripTabMark = (title: string) => title.replace(MARK, "");++/**+ * The tab names what is open: the project, the folder, the note, the page.+ *+ * Each view passes its own name, and null while it defers to a view inside it+ * that names something more specific. The title is set on every show, so a+ * page the router kept mounted takes the tab back when it is visited again.+ * A work mark already on the tab stays in front of the new name.+ */+export function useTabTitle(title: string | null | undefined) {+ useEffect(() => {+ if (!title) return;+ const mark = document.title.match(MARK)?.[0] ?? "";+ document.title = mark + title;+ }, [title]);+}