Skip to content

Commit 15ad7ae

Browse files
wesbillmanBrain
andauthored
fix(desktop): stop search shortcut from hijacking the sidebar (#1447)
Signed-off-by: Wes <[email protected]> Co-authored-by: Brain <21994759fc7a6fa6b965551d35cfd7897d262f2495467f2d78694ddcfa6a5c7e@sprout-oss.stage.blox.sqprod.co>
1 parent 89c4f76 commit 15ad7ae

4 files changed

Lines changed: 40 additions & 25 deletions

File tree

‎desktop/src/features/search/ui/TopbarSearch.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,13 +538,15 @@ export function TopbarSearch({
538538
],
539539
);
540540

541+
// Edge-trigger: the counter never resets, so `!== 0` would replay on remount.
542+
const lastFocusRequestRef = React.useRef(focusRequest);
541543
React.useEffect(() => {
542-
if (focusRequest === 0) {
544+
if (focusRequest === lastFocusRequestRef.current) {
543545
return;
544546
}
547+
lastFocusRequestRef.current = focusRequest;
545548

546549
openSearchDialog();
547-
triggerRef.current?.focus();
548550
}, [focusRequest, openSearchDialog]);
549551

550552
React.useEffect(() => {

‎desktop/src/features/sidebar/ui/AppSidebar.tsx‎

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ import {
6262
SidebarMenu,
6363
SidebarMenuItem,
6464
SidebarRail,
65-
useSidebar,
6665
} from "@/shared/ui/sidebar";
6766

6867
type CollapsibleSidebarGroup =
@@ -240,24 +239,6 @@ export function AppSidebar({
240239
const scrollRef = React.useRef<HTMLDivElement>(null);
241240
useSidebarScrollLock(scrollRef);
242241

243-
// Search lives in the sidebar's pinned header, so a ⌘K focus request must
244-
// first reveal the sidebar. When collapsed (offcanvas) on desktop the input
245-
// is mounted but translated off-screen; on mobile it is unmounted entirely.
246-
// Open the sidebar on each focus-request bump so the input the shortcut
247-
// focuses is actually visible. Skips the initial mount (request === 0).
248-
const { isMobile, setOpen, setOpenMobile } = useSidebar();
249-
React.useEffect(() => {
250-
if (searchFocusRequest === 0) {
251-
return;
252-
}
253-
254-
if (isMobile) {
255-
setOpenMobile(true);
256-
} else {
257-
setOpen(true);
258-
}
259-
}, [searchFocusRequest, isMobile, setOpen, setOpenMobile]);
260-
261242
React.useEffect(() => {
262243
const scrollElement = scrollRef.current;
263244
if (!scrollElement) return;

‎desktop/tests/e2e/navigation.spec.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,26 @@ test("settings shortcut returns without opening search dialog", async ({
274274
await expect(page.getByTestId("chat-title")).toHaveText("general");
275275
const channelUrl = page.url();
276276

277+
// Open search via the ⌘K shortcut so the focus-request counter is non-zero,
278+
// then close it. The Settings subtree remounts the sidebar + search on close,
279+
// which must not replay the stale counter and resurrect search.
280+
await page.evaluate(() => {
281+
const isMac = /mac|iphone|ipad|ipod/i.test(navigator.platform);
282+
window.dispatchEvent(
283+
new KeyboardEvent("keydown", {
284+
bubbles: true,
285+
cancelable: true,
286+
code: "KeyK",
287+
ctrlKey: !isMac,
288+
key: "k",
289+
metaKey: isMac,
290+
}),
291+
);
292+
});
293+
await expect(page.getByTestId("search-dialog-input")).toBeFocused();
294+
await page.keyboard.press("Escape");
295+
await expect(page.getByTestId("search-results")).not.toBeVisible();
296+
277297
await page.keyboard.press(
278298
process.platform === "darwin" ? "Meta+Comma" : "Control+Comma",
279299
);

‎desktop/tests/e2e/smoke.spec.ts‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ test("closes sidebar search with Escape", async ({ page }) => {
318318
await expect(page.getByTestId("open-search")).toBeFocused();
319319
});
320320

321-
test("reopens the collapsed sidebar when the search shortcut fires", async ({
321+
test("search shortcut opens search without disturbing the collapsed sidebar", async ({
322322
page,
323323
}) => {
324324
await page.goto("/");
@@ -334,11 +334,23 @@ test("reopens the collapsed sidebar when the search shortcut fires", async ({
334334
.click();
335335
await expect(sidebarRoot).toHaveAttribute("data-state", "collapsed");
336336

337-
await focusSidebarSearchWithShortcut(page);
337+
await page.evaluate(() => {
338+
const isMac = /mac|iphone|ipad|ipod/i.test(navigator.platform);
339+
window.dispatchEvent(
340+
new KeyboardEvent("keydown", {
341+
bubbles: true,
342+
cancelable: true,
343+
code: "KeyK",
344+
ctrlKey: !isMac,
345+
key: "k",
346+
metaKey: isMac,
347+
}),
348+
);
349+
});
338350

339-
// The shortcut reveals the sidebar and focuses the dialog search input.
340-
await expect(sidebarRoot).toHaveAttribute("data-state", "expanded");
351+
// Search opens in its portal dialog; the sidebar must not react.
341352
await expect(page.getByTestId("search-dialog-input")).toBeFocused();
353+
await expect(sidebarRoot).toHaveAttribute("data-state", "collapsed");
342354
});
343355

344356
test("search results use your resolved profile label instead of You", async ({

0 commit comments

Comments
 (0)