Skip to content

Commit 71dfe27

Browse files
fix(web): an untouched sidebar thread rename keeps a title that arrived while it was open (#35174)
1 parent e93f2a5 commit 71dfe27

3 files changed

Lines changed: 63 additions & 3 deletions

File tree

‎apps/web/src/components/Sidebar.browser.test.tsx‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import { useHandleNewThread } from "../hooks/useHandleNewThread";
1717
import { __setClientSettingsForTests, getClientSettings } from "../hooks/useSettings";
1818
import { useThreadRouteTarget } from "../hooks/useThreadRouteTarget";
1919
import { useProjects, useQuietThreadShells } from "../state/entities";
20+
import { readEnvironmentScope, useEnvironmentScope } from "../state/session";
21+
import { useOrchestrationCommand } from "../state/use-orchestration-command";
2022
import { useTerminalUiStateStore } from "../terminalUiStateStore";
2123
import type { Project } from "../types";
2224
import Sidebar from "./Sidebar";
@@ -28,6 +30,8 @@ vi.mock("../state/entities", { spy: true });
2830
vi.mock("../hooks/useHandleNewThread", { spy: true });
2931
vi.mock("../hooks/useThreadRouteTarget", { spy: true });
3032
vi.mock("./sidebar/SidebarThreadRow", { spy: true });
33+
vi.mock("../state/session", { spy: true });
34+
vi.mock("../state/use-orchestration-command", { spy: true });
3135
vi.mock(import("../state/server"), async (original) => {
3236
const { Atom } = await import("effect/reactivity");
3337
const { compileResolvedKeybindingsConfig } = await import("@t3tools/shared/keybindings");
@@ -190,3 +194,56 @@ describe("sidebar render isolation", () => {
190194
expect(jumpLabelsSince(beforeRelease)).toEqual([]);
191195
});
192196
});
197+
198+
describe("sidebar thread rename", () => {
199+
// regression: #35151 an untouched rename wrote back the title it opened with over a newer one
200+
it.each(["{Enter}", "{Tab}"])(
201+
"ending an untouched rename with %s keeps a title that arrived while it was open",
202+
async (finish) => {
203+
const activeProject = project("active");
204+
const routeThreadRef: ScopedThreadRef = {
205+
environmentId: activeProject.environmentId,
206+
threadId: ThreadId.make("thread-0"),
207+
};
208+
const shellTitled = (title: string) => ({ ...threadShell(activeProject.id, 0), title });
209+
const updateMetadata = vi.fn().mockResolvedValue({ _tag: "Success" });
210+
vi.mocked(useOrchestrationCommand).mockReturnValue(updateMetadata);
211+
vi.mocked(readEnvironmentScope).mockReturnValue(true);
212+
vi.mocked(useEnvironmentScope).mockReturnValue(true);
213+
vi.mocked(useProjects).mockReturnValue([activeProject]);
214+
vi.mocked(useQuietThreadShells).mockReturnValue([shellTitled("Fix the login redirect")]);
215+
vi.mocked(useThreadRouteTarget).mockReturnValue({
216+
kind: "server",
217+
threadRef: routeThreadRef,
218+
});
219+
vi.mocked(useHandleNewThread).mockReturnValue({
220+
activeDraftThread: null,
221+
activeThread: null,
222+
defaultProjectRef: {
223+
environmentId: activeProject.environmentId,
224+
projectId: activeProject.id,
225+
},
226+
defaultProjectFallbackReady: true,
227+
handleNewThread: vi.fn().mockResolvedValue(null),
228+
routeDraftId: null,
229+
routeThreadRef,
230+
});
231+
const tree = () => (
232+
<SidebarProvider>
233+
<Sidebar routeReveal={{ observedKey: null, pendingKey: null }} />
234+
</SidebarProvider>
235+
);
236+
await using screen = await render(tree());
237+
await userEvent.dblClick(screen.getByText("Fix the login redirect").first());
238+
const input = screen.getByRole("textbox", { name: "Thread title" });
239+
await expect.element(input).toHaveValue("Fix the login redirect");
240+
241+
vi.mocked(useQuietThreadShells).mockReturnValue([shellTitled("Generated title")]);
242+
await screen.rerender(tree());
243+
await userEvent.keyboard(finish);
244+
245+
await expect.element(input).not.toBeInTheDocument();
246+
expect(updateMetadata).not.toHaveBeenCalled();
247+
},
248+
);
249+
});

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
10401040
? scopedThreadKey(renamingThreadRef)
10411041
: null;
10421042
const [renamingTitle, setRenamingTitle] = useState("");
1043+
const renamingOriginalTitleRef = useRef("");
10431044
const settlingThreadKeysRef = useRef(new Set<string>());
10441045
const threadListRef = useRef<HTMLUListElement | null>(null);
10451046
const searchResultsListRef = useRef<HTMLUListElement | null>(null);
@@ -1533,9 +1534,11 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
15331534
if (!readEnvironmentScope(threadRef.environmentId, AuthOrchestrationOperateScope)) return;
15341535
setRenamingThreadRef(threadRef);
15351536
setRenamingTitle(title);
1537+
renamingOriginalTitleRef.current = title;
15361538
};
15371539
const cancelThreadRename = () => setRenamingThreadRef(null);
1538-
const commitThreadRename = (threadRef: ScopedThreadRef, title: string, originalTitle: string) => {
1540+
const commitThreadRename = (threadRef: ScopedThreadRef, title: string) => {
1541+
const originalTitle = renamingOriginalTitleRef.current;
15391542
void (async () => {
15401543
if (!readEnvironmentScope(threadRef.environmentId, AuthOrchestrationOperateScope)) return;
15411544
const trimmed = title.trim();

‎apps/web/src/components/sidebar/SidebarThreadRow.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ type SidebarThreadRowProps = {
579579
onThreadActivate: (threadRef: ScopedThreadRef) => void;
580580
onStartRename: (threadRef: ScopedThreadRef, title: string) => void;
581581
onRenameTitleChange: (title: string) => void;
582-
onCommitRename: (threadRef: ScopedThreadRef, title: string, originalTitle: string) => void;
582+
onCommitRename: (threadRef: ScopedThreadRef, title: string) => void;
583583
onCancelRename: () => void;
584584
isRenaming: boolean;
585585
renamingTitle: string;
@@ -1034,7 +1034,7 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowP
10341034
<ThreadRenameInput
10351035
title={renamingTitle}
10361036
onTitleChange={onRenameTitleChange}
1037-
onCommit={() => onCommitRename(threadRef, renamingTitle, thread.title)}
1037+
onCommit={() => onCommitRename(threadRef, renamingTitle)}
10381038
onCancel={onCancelRename}
10391039
/>
10401040
) : (

0 commit comments

Comments
 (0)