From 1abe0fa507be3c6a309c95b2346c678da7443b95 Mon Sep 17 00:00:00 2001 From: x Date: Tue, 10 Nov 2026 18:46:22 -0500 Subject: [PATCH 001/334] fix(web): keep a cap card's title when it is cleared and trim saved titles --- .../unit/shared-cap-card-delete.test.ts | 79 +++++++++++++++++++ .../unit/video-settings-action.test.ts | 29 +++++++ apps/web/actions/videos/edit-title.ts | 5 +- .../components/CapCard/CapCardContent.tsx | 13 ++- 4 files changed, 120 insertions(+), 6 deletions(-) diff --git a/apps/web/__tests__/unit/shared-cap-card-delete.test.ts b/apps/web/__tests__/unit/shared-cap-card-delete.test.ts index 814bd216622..2ec8f33d2d5 100644 --- a/apps/web/__tests__/unit/shared-cap-card-delete.test.ts +++ b/apps/web/__tests__/unit/shared-cap-card-delete.test.ts @@ -11,6 +11,7 @@ const mocks = vi.hoisted(() => ({ | { mutationFn: (id: string) => unknown } | undefined, capCardProps: undefined as { onDelete?: () => void } | undefined, + editTitle: vi.fn(), })); vi.mock("@fortawesome/free-solid-svg-icons", () => ({ @@ -38,6 +39,13 @@ vi.mock("@/app/(org)/dashboard/caps/components/CapCard/CapCard", () => ({ }, })); +vi.mock("@/actions/videos/edit-title", () => ({ editTitle: mocks.editTitle })); +vi.mock("@/actions/videos/edit-date", () => ({ editDate: vi.fn() })); +vi.mock("@/components/Tooltip", () => ({ + Tooltip: ({ children }: { children: unknown }) => children, +})); + +import { CapCardContent } from "@/app/(org)/dashboard/caps/components/CapCard/CapCardContent"; import { SharedCapCard } from "@/app/(org)/dashboard/spaces/[spaceId]/components/SharedCapCard"; const cap = { @@ -111,3 +119,74 @@ describe("SharedCapCard delete", () => { expect(mocks.capCardProps?.onDelete).toBeUndefined(); }); }); + +describe("CapCardContent title edit", () => { + let container: HTMLDivElement; + let root: Root; + + beforeEach(() => { + ( + globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean } + ).IS_REACT_ACT_ENVIRONMENT = true; + container = document.createElement("div"); + document.body.append(container); + root = createRoot(container); + mocks.editTitle.mockReset(); + mocks.editTitle.mockResolvedValue({ success: true }); + }); + + afterEach(() => { + act(() => root.unmount()); + container.remove(); + }); + + const title = () => container.querySelector("p")?.textContent; + + const editTo = async (value: string) => { + act(() => { + root.render( + createElement(CapCardContent, { + cap: cap as unknown as Parameters[0]["cap"], + userId: "owner-1", + isOwner: true, + hideSharedStatus: true, + setIsSharingDialogOpen: () => {}, + }), + ); + }); + act(() => { + container.querySelector("p")?.click(); + }); + const textarea = container.querySelector("textarea"); + if (!textarea) throw new Error("title editor did not open"); + const setValue = Object.getOwnPropertyDescriptor( + HTMLTextAreaElement.prototype, + "value", + )?.set; + await act(async () => { + setValue?.call(textarea, value); + textarea.dispatchEvent(new Event("input", { bubbles: true })); + }); + await act(async () => { + textarea.focus(); + textarea.blur(); + }); + }; + + it.each(["", " "])( + "keeps the current title when cleared to %j", + async (value) => { + await editTo(value); + + expect(mocks.editTitle).not.toHaveBeenCalled(); + expect(title()).toBe("Demo"); + }, + ); + + it("saves the trimmed title", async () => { + await editTo(" Launch demo "); + + expect(mocks.editTitle).toHaveBeenCalledWith("video-1", "Launch demo"); + expect(title()).toBe("Launch demo"); + }); +}); diff --git a/apps/web/__tests__/unit/video-settings-action.test.ts b/apps/web/__tests__/unit/video-settings-action.test.ts index 343fd43c84a..7bf9310c151 100644 --- a/apps/web/__tests__/unit/video-settings-action.test.ts +++ b/apps/web/__tests__/unit/video-settings-action.test.ts @@ -34,7 +34,14 @@ vi.mock("@cap/database", () => ({ }), })); +vi.mock("next/cache", () => ({ revalidatePath: vi.fn() })); + +vi.mock("@/lib/sync-video-storage-names", () => ({ + enqueueVideoStorageNameSync: vi.fn(), +})); + import type { Video } from "@cap/web-domain"; +import { editTitle } from "@/actions/videos/edit-title"; import { updateVideoSettings } from "@/actions/videos/settings"; const patchOf = (value: unknown): Record => { @@ -124,3 +131,25 @@ describe("updateVideoSettings", () => { }); }); }); + +describe("editTitle", () => { + beforeEach(() => { + mocks.updates = []; + }); + + it.each(["", " ", "\t\n"])( + "refuses a blank title %j without writing", + async (title) => { + await expect(editTitle("video" as Video.VideoId, title)).rejects.toThrow( + "Missing required data", + ); + expect(mocks.updates).toEqual([]); + }, + ); + + it("saves the trimmed title", async () => { + await editTitle("video" as Video.VideoId, " Launch demo "); + + expect((mocks.updates[0] as { name: unknown }).name).toBe("Launch demo"); + }); +}); diff --git a/apps/web/actions/videos/edit-title.ts b/apps/web/actions/videos/edit-title.ts index 37937c043d7..70e8d64147e 100644 --- a/apps/web/actions/videos/edit-title.ts +++ b/apps/web/actions/videos/edit-title.ts @@ -10,8 +10,9 @@ import { enqueueVideoStorageNameSync } from "@/lib/sync-video-storage-names"; export async function editTitle(videoId: Video.VideoId, title: string) { const user = await getCurrentUser(); + const name = title?.trim(); - if (!user || !title || !videoId) { + if (!user || !name || !videoId) { throw new Error("Missing required data for updating video title"); } @@ -35,7 +36,7 @@ export async function editTitle(videoId: Video.VideoId, title: string) { await db() .update(videos) .set({ - name: title, + name, metadata: sql`JSON_SET(COALESCE(${videos.metadata}, JSON_OBJECT()), '$.titleManuallyEdited', true)`, }) .where(eq(videos.id, videoId)); diff --git a/apps/web/app/(org)/dashboard/caps/components/CapCard/CapCardContent.tsx b/apps/web/app/(org)/dashboard/caps/components/CapCard/CapCardContent.tsx index 7347dc9e996..98910476f96 100644 --- a/apps/web/app/(org)/dashboard/caps/components/CapCard/CapCardContent.tsx +++ b/apps/web/app/(org)/dashboard/caps/components/CapCard/CapCardContent.tsx @@ -40,16 +40,21 @@ export const CapCardContent: React.FC = ({ const [isDateEditing, setIsDateEditing] = useState(false); const [showFullDate, setShowFullDate] = useState(false); const [title, setTitle] = useState(cap.name); + const [savedTitle, setSavedTitle] = useState(cap.name); const [isEditing, setIsEditing] = useState(false); - const handleTitleBlur = async (capName: string) => { - if (!title || capName === title) { + const handleTitleBlur = async () => { + const next = title.trim(); + if (!next || next === savedTitle) { + setTitle(savedTitle); setIsEditing(false); return; } try { - await editTitle(cap.id, title); + await editTitle(cap.id, next); + setSavedTitle(next); + setTitle(next); toast.success("Video title updated"); setIsEditing(false); router.refresh(); @@ -187,7 +192,7 @@ export const CapCardContent: React.FC = ({ rows={1} value={title} onChange={(e) => setTitle(e.target.value)} - onBlur={() => handleTitleBlur(cap.name)} + onBlur={handleTitleBlur} onKeyDown={(e) => handleTitleKeyDown(e)} className="text-md resize-none bg-transparent truncate w-full border-0 outline-0 text-gray-12 font-medium p-0 m-0 h-[1.25rem] overflow-hidden leading-[1.25rem] tracking-normal font-[inherit]" /> From 90fd95552eee7549bb42daf8c211c6c9f236c85c Mon Sep 17 00:00:00 2001 From: x Date: Tue, 10 Nov 2026 19:03:06 -0500 Subject: [PATCH 002/334] fix(web): keep a title typed while the card's save is in flight --- .../unit/shared-cap-card-delete.test.ts | 53 +++++++++++++++++-- .../components/CapCard/CapCardContent.tsx | 20 +++++-- 2 files changed, 63 insertions(+), 10 deletions(-) diff --git a/apps/web/__tests__/unit/shared-cap-card-delete.test.ts b/apps/web/__tests__/unit/shared-cap-card-delete.test.ts index 2ec8f33d2d5..925614209cd 100644 --- a/apps/web/__tests__/unit/shared-cap-card-delete.test.ts +++ b/apps/web/__tests__/unit/shared-cap-card-delete.test.ts @@ -141,8 +141,9 @@ describe("CapCardContent title edit", () => { }); const title = () => container.querySelector("p")?.textContent; + const editor = () => container.querySelector("textarea"); - const editTo = async (value: string) => { + const mount = () => act(() => { root.render( createElement(CapCardContent, { @@ -154,10 +155,14 @@ describe("CapCardContent title edit", () => { }), ); }); - act(() => { - container.querySelector("p")?.click(); - }); - const textarea = container.querySelector("textarea"); + + const type = async (value: string) => { + if (!editor()) { + act(() => { + container.querySelector("p")?.click(); + }); + } + const textarea = editor(); if (!textarea) throw new Error("title editor did not open"); const setValue = Object.getOwnPropertyDescriptor( HTMLTextAreaElement.prototype, @@ -167,6 +172,11 @@ describe("CapCardContent title edit", () => { setValue?.call(textarea, value); textarea.dispatchEvent(new Event("input", { bubbles: true })); }); + return textarea; + }; + + const editTo = async (value: string) => { + const textarea = await type(value); await act(async () => { textarea.focus(); textarea.blur(); @@ -176,6 +186,7 @@ describe("CapCardContent title edit", () => { it.each(["", " "])( "keeps the current title when cleared to %j", async (value) => { + mount(); await editTo(value); expect(mocks.editTitle).not.toHaveBeenCalled(); @@ -184,9 +195,41 @@ describe("CapCardContent title edit", () => { ); it("saves the trimmed title", async () => { + mount(); await editTo(" Launch demo "); expect(mocks.editTitle).toHaveBeenCalledWith("video-1", "Launch demo"); expect(title()).toBe("Launch demo"); }); + + it("compares and restores against the last saved title", async () => { + mount(); + await editTo("Launch demo"); + await editTo(""); + + expect(title()).toBe("Launch demo"); + + await editTo("Demo"); + + expect(mocks.editTitle.mock.calls).toEqual([ + ["video-1", "Launch demo"], + ["video-1", "Demo"], + ]); + expect(title()).toBe("Demo"); + }); + + it("keeps text typed while a save is in flight", async () => { + let finish = () => {}; + mocks.editTitle.mockReturnValueOnce( + new Promise((resolve) => { + finish = () => resolve({ success: true }); + }), + ); + mount(); + await editTo("Launch demo"); + await type("Launch demo v2"); + await act(async () => finish()); + + expect(editor()?.value).toBe("Launch demo v2"); + }); }); diff --git a/apps/web/app/(org)/dashboard/caps/components/CapCard/CapCardContent.tsx b/apps/web/app/(org)/dashboard/caps/components/CapCard/CapCardContent.tsx index 98910476f96..9e9d154fcaf 100644 --- a/apps/web/app/(org)/dashboard/caps/components/CapCard/CapCardContent.tsx +++ b/apps/web/app/(org)/dashboard/caps/components/CapCard/CapCardContent.tsx @@ -1,7 +1,7 @@ import { faChevronDown } from "@fortawesome/free-solid-svg-icons/faChevronDown"; import clsx from "clsx"; import { useRouter } from "next/navigation"; -import { useState } from "react"; +import { useRef, useState } from "react"; import { toast } from "sonner"; import { editDate } from "@/actions/videos/edit-date"; import { editTitle } from "@/actions/videos/edit-title"; @@ -42,11 +42,19 @@ export const CapCardContent: React.FC = ({ const [title, setTitle] = useState(cap.name); const [savedTitle, setSavedTitle] = useState(cap.name); const [isEditing, setIsEditing] = useState(false); + // The field stays editable while a save is in flight, so the save reads + // the latest draft to avoid replacing text typed after the blur. + const draftRef = useRef(cap.name); + + const setDraft = (value: string) => { + draftRef.current = value; + setTitle(value); + }; const handleTitleBlur = async () => { const next = title.trim(); if (!next || next === savedTitle) { - setTitle(savedTitle); + setDraft(savedTitle); setIsEditing(false); return; } @@ -54,9 +62,11 @@ export const CapCardContent: React.FC = ({ try { await editTitle(cap.id, next); setSavedTitle(next); - setTitle(next); toast.success("Video title updated"); - setIsEditing(false); + if (draftRef.current.trim() === next) { + setDraft(next); + setIsEditing(false); + } router.refresh(); } catch (error) { if (error instanceof Error) { @@ -191,7 +201,7 @@ export const CapCardContent: React.FC = ({