fix(studio): block params survive a preview reload of the selected element, and panel titles are covered

The dismissal compares the selected element by its target key instead of object identity, because a preview reload rebuilds the selection for the same element. A closed and reopened panel keeps its custom title, with a test, and the e2e selector is formatted.
This commit is contained in:
Miguel Angel Simon Sierra
2026-09-19 17:35:09 -04:00
parent b6c63b6ea0
commit 0037e44a05
4 changed files with 36 additions and 11 deletions
@@ -54,7 +54,10 @@ const applySideMinimums = vi.mocked(dockLayout.applySideMinimums);
let root: Root | null = null;
function mount(projectId: string | null) {
function mount(
projectId: string | null,
titles: Partial<Record<(typeof PANEL_IDS)[number], string>> = {},
) {
const host = document.createElement("div");
document.body.append(host);
root = createRoot(host);
@@ -62,7 +65,7 @@ function mount(projectId: string | null) {
root?.render(
<Dock.Root projectId={projectId}>
{PANEL_IDS.map((id) => (
<Dock.Panel key={id} id={id}>
<Dock.Panel key={id} id={id} title={titles[id]}>
<div data-testid={`content-${id}`}>{id}</div>
</Dock.Panel>
))}
@@ -142,6 +145,17 @@ describe("Dock on React 19", () => {
expect(host.querySelector('[data-testid="content-renders"]')).not.toBeNull();
});
it("keeps a panel's custom title when it is closed and reopened", async () => {
const host = mount("p1", { renders: "Renders (2)" });
expect(host.textContent).toContain("Renders (2)");
act(() => useDockLayoutStore.getState().closePanel("renders"));
await act(async () => {
useDockLayoutStore.getState().togglePanel("renders");
await Promise.resolve();
});
expect(host.textContent).toContain("Renders (2)");
});
it("reopens a closed panel as a tab of its zone's group, not a new group", () => {
mount("p1");
act(() => useDockLayoutStore.getState().closePanel("compositions"));
@@ -3,6 +3,7 @@
import React, { act } from "react";
import { createRoot } from "react-dom/client";
import { afterEach, describe, expect, it, vi } from "vitest";
import type { DomEditSelection } from "../components/editor/domEditing";
import { useDockLayoutStore, type DockController } from "../components/dock/dockLayoutStore";
import { PANEL_IDS, type PanelId } from "../components/dock/panelRegistry";
import {
@@ -105,21 +106,26 @@ describe("useSlideshowDockPanel", () => {
});
describe("useBlockParamsDismissal", () => {
const props = { hasBlockParams: true, selection: null as unknown, onDismiss: vi.fn() };
const pick = (id: string) => ({ id, sourceFile: "index.html" }) as unknown as DomEditSelection;
const props = {
hasBlockParams: true,
selection: null as DomEditSelection | null,
onDismiss: vi.fn(),
};
it("dismisses block params when an element is selected", () => {
seed({ visible: ["design"] });
const onDismiss = vi.fn();
const view = mountHook(useBlockParamsDismissal, { ...props, onDismiss });
expect(onDismiss).not.toHaveBeenCalled();
view.render({ ...props, onDismiss, selection: { id: "a" } });
view.render({ ...props, onDismiss, selection: pick("a") });
expect(onDismiss).toHaveBeenCalled();
});
it("keeps block params that open while an element is already selected", () => {
seed({ visible: ["design"] });
const onDismiss = vi.fn();
const selected = { id: "a" };
const selected = pick("a");
const view = mountHook(useBlockParamsDismissal, {
...props,
onDismiss,
@@ -128,7 +134,9 @@ describe("useBlockParamsDismissal", () => {
});
view.render({ ...props, onDismiss, hasBlockParams: true, selection: selected });
expect(onDismiss).not.toHaveBeenCalled();
view.render({ ...props, onDismiss, hasBlockParams: true, selection: { id: "b" } });
view.render({ ...props, onDismiss, hasBlockParams: true, selection: pick("a") });
expect(onDismiss).not.toHaveBeenCalled();
view.render({ ...props, onDismiss, hasBlockParams: true, selection: pick("b") });
expect(onDismiss).toHaveBeenCalled();
});
@@ -1,5 +1,7 @@
import { useEffect, useRef } from "react";
import { useDockLayoutStore, visiblePanelInZone } from "../components/dock/dockLayoutStore";
import type { DomEditSelection } from "../components/editor/domEditing";
import { domEditSelectionsTargetSame } from "../utils/domEditHelpers";
/** Opens the Slideshow panel when the file becomes a slideshow and closes it when it stops being one; a user's own close sticks. */
export function useSlideshowDockPanel(isSlideshowComposition: boolean) {
@@ -19,7 +21,7 @@ export function useBlockParamsDismissal({
onDismiss,
}: {
hasBlockParams: boolean;
selection: unknown;
selection: DomEditSelection | null;
onDismiss: () => void;
}) {
const designVisible = useDockLayoutStore((state) => state.visiblePanels.has("design"));
@@ -28,7 +30,8 @@ export function useBlockParamsDismissal({
if (!hasBlockParams) selectionWhenOpened.current = selection;
}, [hasBlockParams, selection]);
useEffect(() => {
const picked = selection != null && selection !== selectionWhenOpened.current;
const picked =
selection != null && !domEditSelectionsTargetSame(selection, selectionWhenOpened.current);
if (hasBlockParams && (picked || !designVisible)) onDismiss();
}, [hasBlockParams, selection, designVisible, onDismiss]);
}
@@ -351,9 +351,9 @@ async function findMountedThumbnailUrl(page, sourceFile, previous = null) {
{ source: sourceFile, old: previous },
);
return page.evaluate((source) => {
const image = [
...document.querySelectorAll('.hf-dock img[src*="/thumbnail/"]'),
].find((candidate) => candidate.src.includes(source));
const image = [...document.querySelectorAll('.hf-dock img[src*="/thumbnail/"]')].find(
(candidate) => candidate.src.includes(source),
);
return image?.src ?? null;
}, sourceFile);
}