Skip to content

Commit ea3e20a

Browse files
fix(web): double-clicking another thread in the sidebar renames it instead of focusing its composer (pingdotgg#27287)
1 parent ed57b9b commit ea3e20a

3 files changed

Lines changed: 63 additions & 10 deletions

File tree

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { resolveNewThreadModelSelection } from "@t3tools/shared/usageLimits";
55
import { queuedRunEditBlockedReason } from "@t3tools/client-runtime/queued-run-edit";
66
import { ScrollToEndButton, WorkspaceFileDropOverlay } from "./chat/TimelineOverlays";
77
import { usePasteToFocusComposer } from "./chat/usePasteToFocusComposer";
8+
import { useFocusComposerOnThreadOpen } from "./chat/useFocusComposerOnThreadOpen";
89
import { configuredHostedAppUrl } from "../hostedPairing";
910
import { announcedRun, isDetailBehindShell } from "./chat/threadActivityAnnouncement";
1011
import {
@@ -2116,16 +2117,11 @@ export default function ChatView(props: ChatViewProps) {
21162117
setTimelineAnchor,
21172118
});
21182119

2119-
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- schedules a frame to focus the composer DOM element when a thread opens with the terminal closed
2120-
useEffect(() => {
2121-
if (embedded || !activeThread?.id || terminalUiState.terminalOpen) return;
2122-
const frame = window.requestAnimationFrame(() => {
2123-
focusComposerStable();
2124-
});
2125-
return () => {
2126-
window.cancelAnimationFrame(frame);
2127-
};
2128-
}, [activeThread?.id, embedded, focusComposerStable, terminalUiState.terminalOpen]);
2120+
useFocusComposerOnThreadOpen(
2121+
activeThread?.id,
2122+
!embedded && !terminalUiState.terminalOpen,
2123+
focusComposerStable,
2124+
);
21292125

21302126
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- window focus listener that refocuses the composer after the window regains focus
21312127
useEffect(() => {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { useRef } from "react";
2+
import { expect, it, vi } from "vite-plus/test";
3+
4+
import { act, fakeTimers, render } from "../../../test/render";
5+
import { useFocusComposerOnThreadOpen } from "./useFocusComposerOnThreadOpen";
6+
7+
function ThreadProbe(props: { threadId: string }) {
8+
const composerRef = useRef<HTMLTextAreaElement>(null);
9+
useFocusComposerOnThreadOpen(props.threadId, true, () => composerRef.current?.focus());
10+
return (
11+
<>
12+
<div data-sidebar-row="thread:beta">
13+
<input aria-label="Thread title" />
14+
</div>
15+
<textarea aria-label="Message" ref={composerRef} />
16+
</>
17+
);
18+
}
19+
20+
// regression: #27195 double-clicking another thread's row lost its rename field to the composer
21+
it("keeps the sidebar rename input when a double-click opens another thread", async () => {
22+
// The first render schedules the frame.
23+
using _frames = fakeTimers();
24+
await using screen = await render(<ThreadProbe threadId="alpha" />, {
25+
// A bare hook probe; nothing here navigates.
26+
router: false,
27+
});
28+
await act(() => vi.advanceTimersToNextFrame());
29+
await expect.element(screen.getByRole("textbox", { name: "Message" })).toHaveFocus();
30+
31+
const renameInput = screen.getByRole("textbox", { name: "Thread title" });
32+
renameInput.element().focus();
33+
await screen.rerender(<ThreadProbe threadId="beta" />);
34+
await act(() => vi.advanceTimersToNextFrame());
35+
await expect.element(renameInput).toHaveFocus();
36+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { useEffect } from "react";
2+
3+
/** Lands the caret in the composer a frame after a thread opens. */
4+
export function useFocusComposerOnThreadOpen(
5+
threadId: string | undefined,
6+
enabled: boolean,
7+
focusComposer: () => void,
8+
) {
9+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- schedules a frame to focus the composer DOM element when a thread opens with the terminal closed
10+
useEffect(() => {
11+
if (!threadId || !enabled) return;
12+
const frame = window.requestAnimationFrame(() => {
13+
// Double-clicking a sidebar row opens its thread and its rename field in one gesture.
14+
if (document.activeElement?.matches("[data-sidebar-row] input")) return;
15+
focusComposer();
16+
});
17+
return () => {
18+
window.cancelAnimationFrame(frame);
19+
};
20+
}, [threadId, enabled, focusComposer]);
21+
}

0 commit comments

Comments
 (0)