From ec30de06278300882a898f5fa0b27a599e0dd550 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Tue, 10 Nov 2026 12:37:28 +0000 Subject: [PATCH] fix(studio): messages that count clips or keyframes say 1 clip, not 1 clips (#7496) * fix(studio): messages that count clips or keyframes say 1 clip, not 1 clips * fix(studio): countOf needs no comment --- .../src/hooks/timelineAudioGroupCreate.ts | 3 +- packages/studio/src/hooks/useGestureCommit.ts | 3 +- packages/studio/src/hooks/useRazorSplit.ts | 6 +++- .../src/hooks/useTimelineDeleteOps.test.tsx | 28 +++++++++++++++++++ .../studio/src/hooks/useTimelineDeleteOps.ts | 5 +++- packages/studio/src/utils/countOf.test.ts | 10 +++++++ packages/studio/src/utils/countOf.ts | 3 ++ 7 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 packages/studio/src/utils/countOf.test.ts create mode 100644 packages/studio/src/utils/countOf.ts diff --git a/packages/studio/src/hooks/timelineAudioGroupCreate.ts b/packages/studio/src/hooks/timelineAudioGroupCreate.ts index fd878d3d22..b210c99cd4 100644 --- a/packages/studio/src/hooks/timelineAudioGroupCreate.ts +++ b/packages/studio/src/hooks/timelineAudioGroupCreate.ts @@ -26,6 +26,7 @@ import { type MutableRef, type UseTimelineElementVisibilityEditingInput, } from "./timelineTrackVisibility"; +import { countOf } from "../utils/countOf"; interface AudioGroupCarveInput extends UseTimelineElementVisibilityEditingInput { checkEditable?: (elements: readonly TimelineElement[]) => boolean; @@ -181,7 +182,7 @@ export async function createAudioGroupAndAssignMembers({ // then persists `sources: [groupId]` on success, so a quiet no-op leaves the // carve aimed at a group that does not exist. if (elements.length < 2) { - throw new Error(`Cannot group ${elements.length} clip(s): a group needs at least two`); + throw new Error(`Cannot group ${countOf(elements.length, "clip")}: a group needs at least two`); } if (!GROUP_ID_PATTERN.test(groupId)) { throw new Error(`Invalid audio group id ${JSON.stringify(groupId)}`); diff --git a/packages/studio/src/hooks/useGestureCommit.ts b/packages/studio/src/hooks/useGestureCommit.ts index b7816b7e8b..d614a28865 100644 --- a/packages/studio/src/hooks/useGestureCommit.ts +++ b/packages/studio/src/hooks/useGestureCommit.ts @@ -25,6 +25,7 @@ import { } from "./gsapShared"; import { useStableHandlers } from "./useStableHandlers"; import { progressAtTime, runEaseOf, timeAtProgress, warpsTime } from "../utils/gsapKeyframeEases"; +import { countOf } from "../utils/countOf"; type RecordedKeyframe = { percentage: number; @@ -375,7 +376,7 @@ export function useGestureCommit({ } trackPreviewEditResult("gesture_recording", recordingMethodRef.current, outcome.finish()); } - showToast(`Recorded ${sortedPcts.length} keyframes`, "info"); + showToast(`Recorded ${countOf(sortedPcts.length, "keyframe")}`, "info"); } catch (err) { console.error("[GR:error]", err); showToast(`Gesture commit failed: ${err}`, "error"); diff --git a/packages/studio/src/hooks/useRazorSplit.ts b/packages/studio/src/hooks/useRazorSplit.ts index 1769211825..89d7a3049a 100644 --- a/packages/studio/src/hooks/useRazorSplit.ts +++ b/packages/studio/src/hooks/useRazorSplit.ts @@ -9,6 +9,7 @@ import { isLinkedSelectionOn } from "../utils/linkedClipPreferences"; import { buildAtomicCutIntents, runAtomicCutTransaction } from "../utils/razorSplitTransaction"; import type { RecordEditInput } from "./timelineEditingHelpers"; import { useFreezeFrame } from "./useFreezeFrame"; +import { countOf } from "../utils/countOf"; interface UseRazorSplitOptions { projectId: string | null; @@ -138,7 +139,10 @@ export function useRazorSplit({ const result = await runCut(splittable, splitTime, "all"); if (!result) return; if (result.syncFailed) return; - showToast(`Split ${result.splitCount} clips at ${splitTime.toFixed(2)}s`, "info"); + showToast( + `Split ${countOf(result.splitCount, "clip")} at ${splitTime.toFixed(2)}s`, + "info", + ); } catch (error) { const message = error instanceof Error ? error.message : "Failed to split clips"; showToast(message, "error"); diff --git a/packages/studio/src/hooks/useTimelineDeleteOps.test.tsx b/packages/studio/src/hooks/useTimelineDeleteOps.test.tsx index ed2b71c960..fbc5b8bd71 100644 --- a/packages/studio/src/hooks/useTimelineDeleteOps.test.tsx +++ b/packages/studio/src/hooks/useTimelineDeleteOps.test.tsx @@ -156,6 +156,34 @@ describe("useTimelineDeleteOps: ripple undo label", () => { expect(body.targets).toHaveLength(elements.length); }); + it("a refused delete names its count: 1 clip, 2 clips", async () => { + vi.stubGlobal( + "fetch", + vi.fn(async (input: RequestInfo | URL) => + String(input).includes("/file-mutations/remove-elements/") + ? new Response("{}", { status: 500 }) + : new Response(JSON.stringify({ content: html }), { status: 200 }), + ), + ); + const showToast = vi.fn(); + const { elements, b, getHook } = mountDeleteHarness({ + handleTimelineGroupMove: vi.fn(), + showToast, + }); + + await act(async () => { + await getHook().handleTimelineElementDelete(b); + }); + await act(async () => { + await getHook().handleTimelineElementsDelete(elements.slice(0, 2)); + }); + + expect(showToast.mock.calls.map(([message]) => message)).toEqual([ + "Failed to delete 1 clip from index.html", + "Failed to delete 2 clips from index.html", + ]); + }); + it("shows exactly one toast when a ripple fails to persist after a committed delete", async () => { const handleTimelineGroupMove = vi.fn().mockRejectedValue(new Error("persist failed")); const showToast = vi.fn(); diff --git a/packages/studio/src/hooks/useTimelineDeleteOps.ts b/packages/studio/src/hooks/useTimelineDeleteOps.ts index 69ffed0180..4b51310859 100644 --- a/packages/studio/src/hooks/useTimelineDeleteOps.ts +++ b/packages/studio/src/hooks/useTimelineDeleteOps.ts @@ -25,6 +25,7 @@ import type { TimelineGroupMoveChange, } from "./useTimelineGroupEditing"; import { studioApiFetch } from "../utils/studioApiFetch"; +import { countOf } from "../utils/countOf"; /** Apply already-resolved ripple changes to the surviving elements for the * optimistic store update after a delete. Pure — no IO. */ @@ -139,7 +140,9 @@ export function useTimelineDeleteOps({ }, ); if (!removeResponse.ok) { - throw new Error(`Failed to delete ${sameFile.length} clips from ${targetPath}`); + throw new Error( + `Failed to delete ${countOf(sameFile.length, "clip")} from ${targetPath}`, + ); } const removeData = (await removeResponse.json()) as { content?: string }; const removedContent = diff --git a/packages/studio/src/utils/countOf.test.ts b/packages/studio/src/utils/countOf.test.ts new file mode 100644 index 0000000000..219615a5a8 --- /dev/null +++ b/packages/studio/src/utils/countOf.test.ts @@ -0,0 +1,10 @@ +import { describe, expect, it } from "vitest"; +import { countOf } from "./countOf"; + +describe("countOf", () => { + it("names one thing in the singular and two in the plural", () => { + expect(countOf(1, "clip")).toBe("1 clip"); + expect(countOf(2, "clip")).toBe("2 clips"); + expect(countOf(0, "keyframe")).toBe("0 keyframes"); + }); +}); diff --git a/packages/studio/src/utils/countOf.ts b/packages/studio/src/utils/countOf.ts new file mode 100644 index 0000000000..5f37796f68 --- /dev/null +++ b/packages/studio/src/utils/countOf.ts @@ -0,0 +1,3 @@ +export function countOf(n: number, word: string): string { + return `${n} ${n === 1 ? word : `${word}s`}`; +}