fix(studio): stop feedback bar layout shift and add caption-mode exit

This commit is contained in:
Miguel Angel Simon Sierra
2026-07-15 19:23:10 -04:00
parent ce60b85738
commit 7f0ed63abd
7 changed files with 358 additions and 8 deletions
+2 -1
View File
@@ -324,7 +324,7 @@ export function StudioApp() {
domEditSession.domEditGroupSelections,
);
useCaptionDetection({
const exitCaptionMode = useCaptionDetection({
projectId,
activeCompPath,
compIdToSrc,
@@ -499,6 +499,7 @@ export function StudioApp() {
)}
<EditorShell
hidden={viewModeValue.viewMode === "storyboard"}
onExitCaptionMode={exitCaptionMode}
left={
<StudioLeftSidebar
leftSidebarRef={leftSidebarRef}
@@ -0,0 +1,124 @@
// @vitest-environment happy-dom
import React, { act, useState } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { EditorShell } from "./EditorShell";
let captionEditMode = true;
vi.mock("../contexts/StudioContext", () => ({
useStudioPlaybackContext: () => ({
captionEditMode,
refreshKey: 0,
refreshPreviewDocumentVersion: vi.fn(),
}),
useStudioShellContext: () => ({
projectId: "project-1",
activeCompPath: "index.html",
setActiveCompPath: vi.fn(),
handlePreviewIframeRef: vi.fn(),
}),
}));
vi.mock("../contexts/DomEditContext", () => ({
useDomEditActionsContext: () => ({ handleTimelineElementSelect: vi.fn() }),
}));
vi.mock("./nle/NLEContext", () => ({
NLEProvider: ({ children }: { children: React.ReactNode }) => children,
useNLEContext: () => ({
compositionStack: [],
updateCompositionStack: vi.fn(),
containerRef: { current: null },
}),
}));
vi.mock("./nle/useTimelineEditCallbacks", () => ({
useTimelineEditCallbacks: () => ({}),
}));
vi.mock("./nle/PreviewPane", () => ({ PreviewPane: () => null }));
vi.mock("./nle/PreviewOverlays", () => ({ PreviewOverlays: () => null }));
vi.mock("./nle/TimelinePane", () => ({
TimelinePane: ({ timelineFooter }: { timelineFooter?: React.ReactNode }) =>
timelineFooter ?? null,
}));
vi.mock("../captions/components/CaptionTimeline", () => ({
CaptionTimeline: () => "Caption timeline",
}));
vi.mock("./StudioFeedbackBar", () => ({ StudioFeedbackBar: () => null }));
Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });
let root: Root | null = null;
beforeEach(() => {
captionEditMode = true;
});
afterEach(() => {
act(() => root?.unmount());
root = null;
document.body.innerHTML = "";
});
function renderShell(): void {
const host = document.createElement("div");
document.body.append(host);
root = createRoot(host);
function Harness() {
const [active, setActive] = useState(true);
captionEditMode = active;
return (
<EditorShell
left={null}
right={<div>{active ? "Caption inspector" : "Normal inspector panel"}</div>}
timelineToolbar={null}
renderClipContent={() => null}
handleTimelineElementDelete={vi.fn()}
handleTimelineAssetDrop={vi.fn()}
handleTimelineFileDrop={vi.fn()}
handleTimelineElementMove={vi.fn()}
handleTimelineElementsMove={vi.fn()}
handleTimelineElementResize={vi.fn()}
handleTimelineGroupResize={vi.fn()}
handleToggleTrackHidden={vi.fn()}
handleBlockedTimelineEdit={vi.fn()}
handleTimelineElementSplit={vi.fn()}
handleRazorSplit={vi.fn()}
handleRazorSplitAll={vi.fn()}
setCompIdToSrc={vi.fn()}
setCompositionLoading={vi.fn()}
shouldShowSelectedDomBounds={false}
onExitCaptionMode={() => setActive(false)}
/>
);
}
act(() => root?.render(<Harness />));
}
describe("EditorShell caption mode", () => {
it("shows a visible exit control on the caption rail", () => {
renderShell();
expect(document.body.textContent).toContain("Caption timeline");
const exitButton = document.querySelector('button[aria-label="Exit caption mode"]');
expect(exitButton).toBeInstanceOf(HTMLButtonElement);
});
it("restores the normal inspector when the caption mode exit is clicked", () => {
renderShell();
const exitButton = document.querySelector('button[aria-label="Exit caption mode"]');
if (!(exitButton instanceof HTMLButtonElement)) {
throw new Error("caption mode exit button not rendered");
}
act(() => exitButton.click());
expect(document.body.textContent).toContain("Normal inspector panel");
expect(document.body.textContent).not.toContain("Caption timeline");
});
});
+14 -1
View File
@@ -55,6 +55,7 @@ export interface EditorShellProps extends TimelineEditCallbackDeps {
blockPreview?: BlockPreviewInfo | null;
isGestureRecording?: boolean;
gestureOverlay?: ReactNode;
onExitCaptionMode: () => void;
}
// The CapCut-style shell: [left | preview | right] in a top row, with a
@@ -86,6 +87,7 @@ export function EditorShell({
isGestureRecording,
blockPreview,
gestureOverlay,
onExitCaptionMode,
}: EditorShellProps) {
const { projectId, activeCompPath, setActiveCompPath, handlePreviewIframeRef } =
useStudioShellContext();
@@ -128,6 +130,7 @@ export function EditorShell({
left={left}
right={right}
captionEditMode={captionEditMode}
onExitCaptionMode={onExitCaptionMode}
onSelectTimelineElement={handleTimelineElementSelect}
onPreviewBlockDrop={handlePreviewBlockDrop}
timelineToolbar={timelineToolbar}
@@ -156,6 +159,7 @@ interface EditorShellBodyProps {
left: ReactNode;
right: ReactNode;
captionEditMode: boolean;
onExitCaptionMode: () => void;
previewOverlay: ReactNode;
onSelectTimelineElement: (element: TimelineElement | null) => void;
onPreviewBlockDrop?: (
@@ -174,6 +178,7 @@ function EditorShellBody({
left,
right,
captionEditMode,
onExitCaptionMode,
previewOverlay,
onSelectTimelineElement,
onPreviewBlockDrop,
@@ -231,10 +236,18 @@ function EditorShellBody({
timelineFooter={
captionEditMode ? (
<div className="border-t border-neutral-800/30 flex-shrink-0" style={{ height: 60 }}>
<div className="flex items-center gap-1.5 px-2 py-0.5">
<div className="flex items-center justify-between gap-1.5 px-2 py-0.5">
<span className="text-[9px] font-medium text-neutral-500 uppercase tracking-wider">
Captions
</span>
<button
type="button"
onClick={onExitCaptionMode}
aria-label="Exit caption mode"
className="text-[9px] font-medium text-neutral-500 transition-colors hover:text-neutral-200"
>
Exit caption mode
</button>
</div>
<CaptionTimeline pixelsPerSecond={100} />
</div>
@@ -0,0 +1,85 @@
// @vitest-environment happy-dom
import React, { act } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { StudioFeedbackBar } from "./StudioFeedbackBar";
Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });
let root: Root | null = null;
beforeEach(() => {
vi.useFakeTimers();
localStorage.clear();
sessionStorage.clear();
});
afterEach(() => {
act(() => root?.unmount());
root = null;
document.body.innerHTML = "";
vi.useRealTimers();
});
describe("StudioFeedbackBar", () => {
it("mounts and unmounts outside the editor layout flow", () => {
localStorage.setItem("hyperframes-studio:feedbackSessionCount", "9");
localStorage.setItem("hyperframes-studio:feedbackLastPromptedAt", "0");
const host = document.createElement("div");
document.body.append(host);
root = createRoot(host);
act(() => {
root?.render(
<div className="flex h-[600px] flex-col">
<div data-testid="editing-surface" className="min-h-0 flex-1" />
<StudioFeedbackBar />
</div>,
);
});
const editingSurface = document.querySelector('[data-testid="editing-surface"]');
if (!(editingSurface instanceof HTMLElement)) {
throw new Error("editing surface not rendered");
}
const before = {
className: editingSurface.className,
style: editingSurface.getAttribute("style"),
width: editingSurface.getBoundingClientRect().width,
height: editingSurface.getBoundingClientRect().height,
};
act(() => vi.advanceTimersByTime(3000));
const prompt = [...document.querySelectorAll("span")].find(
(element) => element.textContent === "How's the Studio experience?",
);
const feedbackBar = prompt?.parentElement;
if (!feedbackBar) throw new Error("feedback bar not rendered");
expect(feedbackBar.classList.contains("fixed")).toBe(true);
expect(feedbackBar.classList.contains("bottom-0")).toBe(true);
expect(feedbackBar.classList.contains("inset-x-0")).toBe(true);
expect({
className: editingSurface.className,
style: editingSurface.getAttribute("style"),
width: editingSurface.getBoundingClientRect().width,
height: editingSurface.getBoundingClientRect().height,
}).toEqual(before);
const dismissButton = document.querySelector('button[aria-label="Dismiss"]');
if (!(dismissButton instanceof HTMLButtonElement)) {
throw new Error("dismiss button not rendered");
}
act(() => dismissButton.click());
act(() => vi.advanceTimersByTime(300));
expect(document.body.textContent).not.toContain("How's the Studio experience?");
expect({
className: editingSurface.className,
style: editingSurface.getAttribute("style"),
width: editingSurface.getBoundingClientRect().width,
height: editingSurface.getBoundingClientRect().height,
}).toEqual(before);
});
});
@@ -89,8 +89,7 @@ export const StudioFeedbackBar = memo(function StudioFeedbackBar() {
return () => clearTimeout(showTimer);
}, []);
// Animate height in on entrance — appearing 3s after load, an instant 32px
// bar shoves the whole preview stack up mid-task.
// Animate height and opacity on entrance.
useEffect(() => {
if (!visible) return;
const raf = requestAnimationFrame(() => setEntered(true));
@@ -150,7 +149,7 @@ export const StudioFeedbackBar = memo(function StudioFeedbackBar() {
return (
<div
className={[
"flex items-center gap-3 px-4 overflow-hidden border-t border-neutral-800/50 bg-neutral-900/80 text-[11px] transition-all duration-300 motion-reduce:transition-none",
"fixed inset-x-0 bottom-0 z-50 flex items-center gap-3 px-4 overflow-hidden border-t border-neutral-800/50 bg-neutral-900/80 text-[11px] transition-all duration-300 motion-reduce:transition-none",
entered && !exiting ? "h-8 opacity-100" : "h-0 opacity-0 border-t-transparent",
].join(" ")}
>
@@ -0,0 +1,107 @@
// @vitest-environment happy-dom
import React, { act } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { useCaptionSync } from "../captions/hooks/useCaptionSync";
import { useCaptionStore } from "../captions/store";
import { useCaptionDetection } from "./useCaptionDetection";
vi.mock("../captions/parser", () => ({
parseCaptionComposition: () => ({
width: 1920,
height: 1080,
duration: 10,
segments: new Map(),
groups: new Map(),
groupOrder: [],
}),
}));
Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });
let root: Root | null = null;
beforeEach(() => {
useCaptionStore.getState().reset();
});
afterEach(() => {
act(() => root?.unmount());
root = null;
document.body.innerHTML = "";
useCaptionStore.getState().reset();
vi.unstubAllGlobals();
});
async function settleDetection(): Promise<void> {
await act(async () => {
await new Promise<void>((resolve) => setTimeout(resolve, 0));
});
}
describe("useCaptionDetection", () => {
it("keeps explicit caption mode exit sticky for the mounted Studio session", async () => {
const fetchMock = vi.fn(async () => ({
json: async () => ({ content: "caption composition source" }),
}));
vi.stubGlobal("fetch", fetchMock);
const iframe = document.createElement("iframe");
document.body.append(iframe);
const iframeDocument = iframe.contentDocument;
if (!iframeDocument) throw new Error("iframe document unavailable");
iframeDocument.body.innerHTML = `
<div data-composition-id="captions" data-width="1920" data-height="1080" data-duration="10">
<div data-composition-src="captions.html">
<span class="caption-group"></span>
</div>
</div>
`;
const previewIframeRef = { current: iframe };
const compIdToSrc = new Map([["captions", "captions.html"]]);
const setRightCollapsed = vi.fn();
const host = document.createElement("div");
document.body.append(host);
root = createRoot(host);
function Harness({ activeCompPath }: { activeCompPath: string }) {
const captionEditMode = useCaptionStore((state) => state.isEditMode);
const captionHasSelection = useCaptionStore((state) => state.selectedSegmentIds.size > 0);
const captionSync = useCaptionSync(null);
const exitCaptionMode = useCaptionDetection({
projectId: "project-1",
activeCompPath,
compIdToSrc,
captionEditMode,
captionHasSelection,
previewIframeRef,
captionSync,
setRightCollapsed,
});
return (
<button type="button" onClick={exitCaptionMode}>
{captionEditMode ? "Caption mode" : "Normal inspector"}
</button>
);
}
act(() => root?.render(<Harness activeCompPath="captions.html" />));
await settleDetection();
expect(useCaptionStore.getState().isEditMode).toBe(true);
const exitButton = document.querySelector("button");
if (!(exitButton instanceof HTMLButtonElement)) throw new Error("exit button not rendered");
act(() => exitButton.click());
expect(useCaptionStore.getState().isEditMode).toBe(false);
expect(setRightCollapsed).toHaveBeenLastCalledWith(false);
act(() => root?.render(<Harness activeCompPath="alternate-captions.html" />));
await settleDetection();
expect(useCaptionStore.getState().isEditMode).toBe(false);
expect(fetchMock).toHaveBeenCalledOnce();
});
});
@@ -1,4 +1,4 @@
import { useEffect } from "react";
import { useCallback, useEffect, useRef } from "react";
import { useCaptionStore } from "../captions/store";
import { acceptStudioRuntimeMessage } from "../player/lib/runtimeProtocol";
import { useCaptionSync } from "../captions/hooks/useCaptionSync";
@@ -25,6 +25,13 @@ export function useCaptionDetection({
captionSync,
setRightCollapsed,
}: UseCaptionDetectionParams) {
const autoActivationDismissedRef = useRef(false);
const exitCaptionMode = useCallback(() => {
autoActivationDismissedRef.current = true;
useCaptionStore.getState().setEditMode(false);
setRightCollapsed(false);
}, [setRightCollapsed]);
// eslint-disable-next-line no-restricted-syntax
useEffect(() => {
if (!projectId) return;
@@ -32,7 +39,11 @@ export function useCaptionDetection({
let activating = false;
const tryActivateCaptions = () => {
if (useCaptionStore.getState().isEditMode || activating) {
if (
autoActivationDismissedRef.current ||
useCaptionStore.getState().isEditMode ||
activating
) {
return;
}
@@ -90,7 +101,15 @@ export function useCaptionDetection({
fetch(`/api/projects/${projectId}/files/${encodeURIComponent(srcPath)}`)
.then((r) => r.json())
.then((data: { content?: string }) => {
if (!data.content || !doc || !win || useCaptionStore.getState().isEditMode) return;
if (
autoActivationDismissedRef.current ||
!data.content ||
!doc ||
!win ||
useCaptionStore.getState().isEditMode
) {
return;
}
const root = doc.querySelector("[data-composition-id]");
const w = parseInt(root?.getAttribute("data-width") ?? "1920", 10);
const h = parseInt(root?.getAttribute("data-height") ?? "1080", 10);
@@ -131,4 +150,6 @@ export function useCaptionDetection({
setRightCollapsed(!captionHasSelection);
}
}, [captionHasSelection, captionEditMode, setRightCollapsed]);
return exitCaptionMode;
}