fix: recognize visible Dockview chat panels (#3481)

This commit is contained in:
Carlos Florêncio
2026-09-07 22:17:02 +01:00
committed by GitHub
parent ffa42a8cfd
commit 4ea3ccf336
7 changed files with 210 additions and 27 deletions
@@ -185,6 +185,7 @@ test.describe("Unread divider", () => {
expect(await isScrolledIntoView(scrollContainer, newestRow)).toBe(false);
});
// @covers AC-UI-TRANSCRIPT-AUTO-SCROLL-001.15
test("completed task switch keeps each read cursor and returns to the bottom", async ({
testPage,
apiClient,
@@ -214,6 +215,7 @@ test.describe("Unread divider", () => {
element.scrollTop = element.scrollHeight;
element.dispatchEvent(new Event("scroll"));
});
await testPage.getByTestId("dockview-tab-changes").click();
const taskBMarkRead = testPage.waitForResponse(
(response) =>
@@ -237,6 +239,19 @@ test.describe("Unread divider", () => {
const activeChat = session.activeChat();
const scrollContainer = activeChat.locator(".chat-message-list");
await expect
.poll(() =>
testPage.evaluate(
() =>
(
window as unknown as {
__dockviewApi__?: { activePanel?: { id: string } };
}
).__dockviewApi__?.activePanel?.id ?? null,
),
)
.toBe("changes");
await expect(scrollContainer).toBeVisible();
await expect(activeChat.getByTestId("unread-divider")).toHaveCount(0);
await expect
.poll(() =>
+48 -13
View File
@@ -10,34 +10,53 @@ afterEach(() => {
type FakePanelApi = {
isActive: boolean;
isVisible: boolean;
onDidActiveChange: (cb: (event: { isActive: boolean }) => void) => { dispose: () => void };
onDidVisibilityChange: (cb: (event: { isVisible: boolean }) => void) => {
dispose: () => void;
};
};
type FakeApiHandle = {
fakeApi: FakePanelApi;
fireActiveChange: (isActive: boolean) => void;
fireVisibilityChange: (isVisible: boolean) => void;
};
function makeFakeApi(initialIsActive: boolean): FakeApiHandle {
let listener: ((event: { isActive: boolean }) => void) | null = null;
function makeFakeApi(initialIsActive: boolean, initialIsVisible = initialIsActive): FakeApiHandle {
let activeListener: ((event: { isActive: boolean }) => void) | null = null;
let visibilityListener: ((event: { isVisible: boolean }) => void) | null = null;
const fakeApi: FakePanelApi = {
isActive: initialIsActive,
isVisible: initialIsVisible,
onDidActiveChange: (cb) => {
listener = cb;
return { dispose: () => (listener = null) };
activeListener = cb;
return { dispose: () => (activeListener = null) };
},
onDidVisibilityChange: (cb) => {
visibilityListener = cb;
return { dispose: () => (visibilityListener = null) };
},
};
return {
fakeApi,
fireActiveChange: (isActive: boolean) => {
fakeApi.isActive = isActive;
listener?.({ isActive });
activeListener?.({ isActive });
},
fireVisibilityChange: (isVisible: boolean) => {
fakeApi.isVisible = isVisible;
visibilityListener?.({ isVisible });
},
};
}
function acquirePanel(panelId: string, initialIsActive: boolean): FakeApiHandle {
const handle = makeFakeApi(initialIsActive);
function acquirePanel(
panelId: string,
initialIsActive: boolean,
initialIsVisible = initialIsActive,
): FakeApiHandle {
const handle = makeFakeApi(initialIsActive, initialIsVisible);
// Test double: only the subset of DockviewPanelApi this hook actually
// reads/calls is implemented. Unchecked cast is intentional here — the
// full interface has 30+ unrelated members no test in this file exercises.
@@ -57,7 +76,7 @@ describe("usePanelActive", () => {
expect(result.current).toBe(false);
});
it("reflects the panel's initial isActive state once registered", () => {
it("reflects the panel's initial isVisible state once registered", () => {
acquirePanel("panel-active", true);
const { result } = renderHook(() => usePanelActive("panel-active"));
expect(result.current).toBe(true);
@@ -67,15 +86,31 @@ describe("usePanelActive", () => {
expect(result2.current).toBe(false);
});
it("updates when the panel's active tab changes via onDidActiveChange", () => {
it("treats a selected panel as visible when another Dockview group owns focus", () => {
const handle = acquirePanel("panel-visible-unfocused", false, true);
const { result } = renderHook(() => usePanelActive("panel-visible-unfocused"));
expect(result.current).toBe(true);
// The hook subscribes to onDidVisibilityChange, not onDidActiveChange.
// Firing active-change events is a no-op; the result must not move.
act(() => handle.fireActiveChange(true));
act(() => handle.fireActiveChange(false));
expect(result.current).toBe(true);
act(() => handle.fireVisibilityChange(false));
expect(result.current).toBe(false);
});
it("updates when the panel's visible tab changes via onDidVisibilityChange", () => {
const handle = acquirePanel("panel-toggle", false);
const { result } = renderHook(() => usePanelActive("panel-toggle"));
expect(result.current).toBe(false);
act(() => handle.fireActiveChange(true));
act(() => handle.fireVisibilityChange(true));
expect(result.current).toBe(true);
act(() => handle.fireActiveChange(false));
act(() => handle.fireVisibilityChange(false));
expect(result.current).toBe(false);
});
@@ -103,13 +138,13 @@ describe("usePanelActive", () => {
// without the old one ever being released.
const fresh = acquirePanel(PANEL_ID, false);
act(() => fresh.fireActiveChange(true));
act(() => fresh.fireVisibilityChange(true));
expect(result.current).toBe(true);
// The stale api's listener must have been torn down — firing it no
// longer moves the hook (it would incorrectly flip back to false
// without the resubscribe fix).
act(() => stale.fireActiveChange(false));
act(() => stale.fireVisibilityChange(false));
expect(result.current).toBe(true);
});
});
+8 -6
View File
@@ -5,7 +5,7 @@ import type { DockviewPanelApi } from "dockview-react";
import { panelPortalManager } from "@/lib/layout/panel-portal-manager";
/**
* Tracks whether the dockview panel identified by panelId is the active tab
* Tracks whether the dockview panel identified by panelId is the visible tab
* in its dockview group. Portal-hosted panel content (see
* `lib/layout/panel-portal-manager.ts`) stays mounted while its tab is
* inactive — e.g. the Chat tab keeps running behind an active Files/Changes
@@ -19,8 +19,10 @@ import { panelPortalManager } from "@/lib/layout/panel-portal-manager";
* renders, so the entry is virtually always present by the time this runs.
* Subscribes to both the manager's own add/remove notifications (a panel
* acquired after this hook's first mount, or released while it stays
* mounted) and the panel's own `onDidActiveChange` (tab switches within an
* existing group).
* mounted) and the panel's own `onDidVisibilityChange` (tab switches within
* an existing group). Dockview's `isActive` also requires the panel's group
* to own global focus, so it is not a valid proxy for whether the panel is
* rendered on screen beside another focused group.
*
* Only for dockview-hosted panels — pass the panel's real id. Defaults to
* `false` until a portal entry/api is actually registered for it, rather
@@ -34,7 +36,7 @@ import { panelPortalManager } from "@/lib/layout/panel-portal-manager";
export function usePanelActive(panelId: string): boolean {
const subscribe = useCallback(
(onStoreChange: () => void) => {
// Tracks which api our onDidActiveChange listener is currently bound
// Tracks which api our onDidVisibilityChange listener is currently bound
// to, so a manager notification (panel acquired/released/remounted
// anywhere) can detect when *this* panelId's api was replaced —
// Dockview does this during a layout restore/switch — and rebind
@@ -47,7 +49,7 @@ export function usePanelActive(panelId: string): boolean {
if (api === boundApi) return;
disposable?.dispose();
boundApi = api;
disposable = api?.onDidActiveChange(onStoreChange) ?? null;
disposable = api?.onDidVisibilityChange(onStoreChange) ?? null;
};
rebindIfApiChanged();
@@ -63,7 +65,7 @@ export function usePanelActive(panelId: string): boolean {
[panelId],
);
const getSnapshot = useCallback(
() => panelPortalManager.get(panelId)?.api?.isActive ?? false,
() => panelPortalManager.get(panelId)?.api?.isVisible ?? false,
[panelId],
);
return useSyncExternalStore(subscribe, getSnapshot, () => false);