Skip to content

Open source CapCut alternative.

TypeScript

feat(site): the tab says what is still running

David Le0c0d3bd44d6a
diff --git a/site/src/cut/components/AiPanel.tsx b/site/src/cut/components/AiPanel.tsx
index ecac337f..3a524452 100644
--- a/site/src/cut/components/AiPanel.tsx
+++ b/site/src/cut/components/AiPanel.tsx
@@ -98,6 +98,7 @@ import { lightboxItemFromRef, useLightbox } from "@/cut/lib/lightbox";
import { refsFromDroppedFiles } from "@/cut/lib/refMedia";
import { revealRef } from "@/cut/lib/refReveal";
import { useEditor } from "@/cut/lib/store";
+import { reportActivity } from "@/cut/lib/tabActivity";
import { cn } from "@/lib/utils";
import { cardIconButton } from "@/cut/components/iconButton";
import { MentionTextarea, RefChips, RefThumb, RefTokenChip } from "./AssetRefs";
@@ -869,6 +870,11 @@ function ChatSession({
);
const busy = status === "submitted" || status === "streaming";
+ // The tab's own icon carries the turn, for the user reading another tab.
+ useEffect(() => {
+ reportActivity("chat", busy);
+ return () => reportActivity("chat", false);
+ }, [busy]);
// While this thread is open its tools tag created assets with it, so
// deleting the thread later can clean them up.
diff --git a/site/src/cut/components/Editor.tsx b/site/src/cut/components/Editor.tsx
index d1aa9eca..4036cb89 100644
--- a/site/src/cut/components/Editor.tsx
+++ b/site/src/cut/components/Editor.tsx
@@ -31,6 +31,8 @@ import "@/cut/lib/genScene";
import "@/cut/lib/googleFonts";
import { syncFontAssets } from "@/cut/lib/fontAssets";
import { syncLinkedLibrary } from "@/cut/lib/linkedLibrary";
+import { reportActivity } from "@/cut/lib/tabActivity";
+import { TabStatus } from "./TabStatus";
import { installDevHooks } from "@/cut/lib/devHooks";
import { ensureCloudThreads } from "@/cut/lib/chatCloud";
import { readProjectThreads, writeActiveChat } from "@/cut/lib/chatThreads";
@@ -138,6 +140,11 @@ export function Editor({
const sending = useEditor((s) =>
s.assets.reduce((n, a) => (a.upload && !a.upload.error ? n + 1 : n), 0)
);
+ // Bytes moving into the project are work the tab should show.
+ useEffect(() => {
+ reportActivity("import", importing + sending > 0);
+ }, [importing, sending]);
+
// Uploaded fonts become live FontFaces the moment their assets exist (and
// drop out when deleted), so titles set in them never measure a fallback.
const assetsForFonts = useEditor((s) => s.assets);
@@ -935,6 +942,8 @@ export function Editor({
// the panels give up width, the top bar collapses to a menu, and the
// timeline scrolls inside itself as it always has.
<div className="flex h-full min-w-0 overflow-hidden">
+ {/* The tab wears whatever is still running here. */}
+ <TabStatus />
<div className="grid min-w-0 flex-1 grid-rows-[46px_minmax(0,1fr)_auto]">
{viewer ? (
<ViewerTopBar />
diff --git a/site/src/cut/components/LibraryView.tsx b/site/src/cut/components/LibraryView.tsx
index 3e0ee6ed..0470c7ba 100644
--- a/site/src/cut/components/LibraryView.tsx
+++ b/site/src/cut/components/LibraryView.tsx
@@ -88,6 +88,7 @@ import {
} from "@/cut/lib/library";
import { normalizeLink } from "@/cut/lib/link";
import { useLightbox } from "@/cut/lib/lightbox";
+import { reportActivity } from "@/cut/lib/tabActivity";
import { useNewProjectTarget } from "@/cut/lib/newProject";
import { useListedResidencies, useLocalCompute } from "@/cut/lib/backend/hooks";
import { setNeedsApp } from "@/cut/lib/needsApp";
@@ -97,6 +98,7 @@ import {
type Residency,
} from "@/cut/lib/residency";
import { Lightbox } from "./Lightbox";
+import { TabStatus } from "./TabStatus";
import { TemplateCard } from "./TemplateCard";
import { homeHref, useCutBase } from "@/cut/lib/nav";
import {
@@ -418,6 +420,10 @@ export function LibraryView() {
// Track one arrival from its first moment to its last: the tile goes up
// before any bytes move, follows the work, and comes down when the asset
// itself takes its place. A failure leaves the tile up with the reason.
+ // Files land here long after the drop, so the tab carries the arrival.
+ useEffect(() => {
+ reportActivity("import", pending.some((p) => !p.error));
+ }, [pending]);
const addPending = (p: Pending) => setPending((q) => [...q, p]);
const setStage = (id: string, stage: PendingStage) =>
setPending((q) => q.map((p) => (p.id === id ? { ...p, stage } : p)));
@@ -1006,6 +1012,7 @@ export function LibraryView() {
</AlertDialog>
<Lightbox />
+ <TabStatus />
</div>
</div>
);
diff --git a/site/src/cut/components/TabStatus.tsx b/site/src/cut/components/TabStatus.tsx
new file mode 100644
index 00000000..ba705ac3
--- /dev/null
+++ b/site/src/cut/components/TabStatus.tsx
@@ -0,0 +1,156 @@
+"use client";
+
+import { useEffect, useState } from "react";
+import { useExports } from "@/cut/lib/exportStore";
+import { useGenerate } from "@/cut/lib/generate";
+import { useEditor } from "@/cut/lib/store";
+import { busyIn, useTabActivity } from "@/cut/lib/tabActivity";
+
+/**
+ * The tab says what it is doing.
+ *
+ * Work here runs long and the user goes elsewhere while it does, so the two
+ * things visible from another tab carry the state: the favicon and the title.
+ * A ring turns while anything is running, and when the last of it settles a
+ * green check takes its place and waits — until the user looks 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 donkey.
+ */
+
+const BUSY_MARK = "● ";
+const DONE_MARK = "✓ ";
+/** How long the finished mark holds when the user is already on the tab. */
+const DONE_MS = 6000;
+const SPIN_MS = 90;
+
+const strip = (title: string) =>
+ title.startsWith(BUSY_MARK) || title.startsWith(DONE_MARK) ? title.slice(2) : title;
+
+function iconLink(): HTMLLinkElement {
+ const held = document.querySelector<HTMLLinkElement>("link[data-tab-status]");
+ if (held) return held;
+ const link = document.createElement("link");
+ link.rel = "icon";
+ link.dataset.tabStatus = "";
+ // The page's own icon, kept so the tab can be handed back as it was.
+ const first = document.querySelector<HTMLLinkElement>("link[rel~='icon']");
+ link.dataset.rest = first?.href ?? "/favicon.ico";
+ first?.remove();
+ document.head.appendChild(link);
+ return link;
+}
+
+/** The status icon: a turning ring, or a check on a green disc. */
+function paint(state: "busy" | "done", turn: number): string {
+ const canvas = document.createElement("canvas");
+ canvas.width = canvas.height = 32;
+ const ctx = canvas.getContext("2d");
+ if (!ctx) return "";
+ ctx.lineCap = "round";
+ if (state === "busy") {
+ ctx.strokeStyle = "rgba(10,132,255,0.2)";
+ ctx.lineWidth = 5;
+ ctx.beginPath();
+ ctx.arc(16, 16, 12, 0, Math.PI * 2);
+ ctx.stroke();
+ ctx.strokeStyle = "#0a84ff";
+ ctx.beginPath();
+ ctx.arc(16, 16, 12, turn, turn + Math.PI * 1.3);
+ ctx.stroke();
+ } else {
+ ctx.fillStyle = "#30d158";
+ ctx.beginPath();
+ ctx.arc(16, 16, 15, 0, Math.PI * 2);
+ ctx.fill();
+ ctx.strokeStyle = "#ffffff";
+ ctx.lineWidth = 4;
+ ctx.lineJoin = "round";
+ ctx.beginPath();
+ ctx.moveTo(9, 16.5);
+ ctx.lineTo(14, 21.5);
+ ctx.lineTo(23, 11);
+ ctx.stroke();
+ }
+ return canvas.toDataURL("image/png");
+}
+
+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 [busy, setBusy] = useState(false);
+ const [done, setDone] = useState(false);
+
+ // Four stores answer for the work, so the state is read off them together
+ // whenever any one moves: what matters is the moment the last of it stops.
+ useEffect(() => {
+ let was = false;
+ const read = () =>
+ busyIn(useTabActivity.getState().running) ||
+ useGenerate.getState().jobs.some((j) => j.status === "running") ||
+ useEditor.getState().renders.some((r) => r.status === "running") ||
+ useEditor.getState().subtitleStatus === "running" ||
+ useExports.getState().local.length > 0 ||
+ useExports.getState().jobs.some((j) => j.status === "queued" || j.status === "running");
+ const apply = () => {
+ const now = read();
+ if (now === was) return;
+ was = now;
+ setBusy(now);
+ // Starting clears the last finish; stopping is the finish.
+ setDone(!now);
+ };
+ const off = [
+ useTabActivity.subscribe(apply),
+ useGenerate.subscribe(apply),
+ useEditor.subscribe(apply),
+ useExports.subscribe(apply),
+ ];
+ apply();
+ return () => off.forEach((f) => f());
+ }, []);
+
+ // The finished mark is for someone who was elsewhere, so it waits for them;
+ // on a tab they are already looking at it says its piece and goes.
+ useEffect(() => {
+ if (!done) return;
+ if (!document.hidden) {
+ const t = setTimeout(() => setDone(false), DONE_MS);
+ return () => clearTimeout(t);
+ }
+ const seen = () => {
+ if (!document.hidden) setDone(false);
+ };
+ document.addEventListener("visibilitychange", seen);
+ window.addEventListener("focus", seen);
+ return () => {
+ document.removeEventListener("visibilitychange", seen);
+ window.removeEventListener("focus", seen);
+ };
+ }, [done]);
+
+ useEffect(() => {
+ const link = iconLink();
+ const base = strip(document.title);
+ if (!busy && !done) {
+ document.title = base;
+ link.href = link.dataset.rest || "/favicon.ico";
+ return;
+ }
+ document.title = (busy ? BUSY_MARK : DONE_MARK) + base;
+ if (!busy) {
+ link.href = paint("done", 0);
+ return;
+ }
+ let turn = 0;
+ const step = () => {
+ link.href = paint("busy", turn);
+ turn += Math.PI / 5;
+ };
+ step();
+ const timer = setInterval(step, SPIN_MS);
+ return () => clearInterval(timer);
+ }, [busy, done, projectName]);
+
+ return null;
+}
diff --git a/site/src/cut/lib/tabActivity.ts b/site/src/cut/lib/tabActivity.ts
new file mode 100644
index 00000000..9ef286bf
--- /dev/null
+++ b/site/src/cut/lib/tabActivity.ts
@@ -0,0 +1,32 @@
+"use client";
+
+import { create } from "zustand";
+
+/**
+ * What the tab is busy with.
+ *
+ * Work in Cut outlives the click that started it — a chat turn streams, a
+ * render runs, an import lands — and the user is often reading another tab
+ * while it happens. Anything that takes a while says so here, and the tab's
+ * own icon and title carry it (`components/TabStatus.tsx`).
+ */
+export type ActivityKind = "chat" | "generate" | "render" | "export" | "transcribe" | "import";
+
+interface TabActivityState {
+ running: Partial<Record<ActivityKind, boolean>>;
+ /** Say whether this kind of work is under way right now. */
+ setRunning: (kind: ActivityKind, on: boolean) => void;
+}
+
+export const useTabActivity = create<TabActivityState>((set) => ({
+ running: {},
+ setRunning: (kind, on) =>
+ set((s) => (!!s.running[kind] === on ? s : { running: { ...s.running, [kind]: on } })),
+}));
+
+export const busyIn = (running: Partial<Record<ActivityKind, boolean>>): boolean =>
+ Object.values(running).some(Boolean);
+
+/** Report a piece of work for as long as `on` holds. */
+export const reportActivity = (kind: ActivityKind, on: boolean): void =>
+ useTabActivity.getState().setRunning(kind, on);