Skip to content

The agent that grows with you

fix(desktop): tear down find-in-page highlights on navigation and pin the keybind registration

teknium15557b10fb6a3
diff --git a/apps/desktop/src/components/find-bar.test.tsx b/apps/desktop/src/components/find-bar.test.tsx
index a25b4151413..fa0d7f478c1 100644
--- a/apps/desktop/src/components/find-bar.test.tsx
+++ b/apps/desktop/src/components/find-bar.test.tsx
@@ -1,9 +1,14 @@
import { act, cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
+import { MemoryRouter, useNavigate } from 'react-router-dom'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { FindBar } from '@/components/find-bar'
import { I18nProvider } from '@/i18n'
+import { en } from '@/i18n/en'
+import { zh } from '@/i18n/zh'
import { findBarClaimsCombo, findBarKeyAction, formatMatchLabel } from '@/lib/find-in-page'
+import { KEYBIND_ACTIONS } from '@/lib/keybinds/actions'
+import { comboAllowedInInput } from '@/lib/keybinds/combo'
import {
$findInPage,
closeFindBar,
@@ -183,6 +188,47 @@ describe('findBarClaimsCombo', () => {
})
})
+// ── Keybind registration ────────────────────────────────────────────────────
+
+describe('find-in-page keybind registration', () => {
+ const byId = new Map(KEYBIND_ACTIONS.map(action => [action.id, action]))
+
+ it('registers view.findInPage on mod+f in the view category', () => {
+ const action = byId.get('view.findInPage')
+
+ expect(action).toBeTruthy()
+ expect(action?.category).toBe('view')
+ expect(action?.defaults).toEqual(['mod+f'])
+ })
+
+ it('mod+f fires from inside a textarea (browser find behavior)', () => {
+ // The runtime consults comboAllowedInInput before dispatching a combo
+ // while an editable element owns focus; if mod combos ever stop
+ // qualifying, ⌘F from the composer would type 'f' instead of opening find.
+ expect(comboAllowedInInput('mod+f')).toBe(true)
+ })
+
+ it('registers the step pair unbound so it cannot conflict with view.toggleReview', () => {
+ const next = byId.get('view.findNext')
+ const previous = byId.get('view.findPrevious')
+
+ expect(next?.category).toBe('view')
+ expect(previous?.category).toBe('view')
+ // mod+g stays with view.toggleReview by default; the open find bar claims
+ // it at dispatch time instead (findBarClaimsCombo above).
+ expect(next?.defaults).toEqual([])
+ expect(previous?.defaults).toEqual([])
+ expect(byId.get('view.toggleReview')?.defaults).toEqual(['mod+g'])
+ })
+
+ it('every registered find action has an i18n label (keybinds panel row)', () => {
+ for (const id of ['view.findInPage', 'view.findNext', 'view.findPrevious']) {
+ expect(en.keybinds.actions[id], id).toBeTruthy()
+ expect(zh.keybinds.actions[id], id).toBeTruthy()
+ }
+ })
+})
+
// ── Store: open/close state + dispatch ──────────────────────────────────────
describe('find-in-page store', () => {
@@ -318,14 +364,38 @@ function actStore(mutate: () => void) {
})
}
-function renderFindBar() {
+function renderFindBar(initialPath = '/') {
return render(
- <I18nProvider configClient={null} initialLocale="en">
- <FindBar />
- </I18nProvider>
+ <MemoryRouter initialEntries={[initialPath]}>
+ <I18nProvider configClient={null} initialLocale="en">
+ <FindBar />
+ </I18nProvider>
+ </MemoryRouter>
)
}
+/** Harness that can navigate the route the mounted FindBar observes. */
+function renderFindBarWithNavigation(initialPath = '/session/a') {
+ let navigateRef: ReturnType<typeof useNavigate> | undefined
+
+ function CaptureNavigate() {
+ navigateRef = useNavigate()
+
+ return null
+ }
+
+ const view = render(
+ <MemoryRouter initialEntries={[initialPath]}>
+ <I18nProvider configClient={null} initialLocale="en">
+ <CaptureNavigate />
+ <FindBar />
+ </I18nProvider>
+ </MemoryRouter>
+ )
+
+ return { ...view, navigate: (to: string) => act(() => navigateRef?.(to)) }
+}
+
describe('FindBar', () => {
it('renders nothing while closed', () => {
renderFindBar()
@@ -515,4 +585,29 @@ describe('FindBar', () => {
expect(bridge.stopFindInPage).not.toHaveBeenCalled()
})
+
+ it('navigating to another route closes the bar and clears the highlights', async () => {
+ const { navigate } = renderFindBarWithNavigation('/session/a')
+
+ actStore(openFindBar)
+ actStore(() => $findInPage.set({ active: true, query: 'needle', matchOrdinal: 2, matchCount: 7 }))
+ await waitFor(() => expect(screen.getByRole('search')).toBeTruthy())
+
+ navigate('/session/b')
+
+ // Bar gone, state reset, and the native selection cleared — stale
+ // highlights must not survive a session switch.
+ await waitFor(() => expect(screen.queryByRole('search')).toBeNull())
+ expect($findInPage.get()).toEqual({ active: false, query: '', matchOrdinal: 0, matchCount: 0 })
+ expect(bridge.stopFindInPage).toHaveBeenCalledTimes(1)
+ })
+
+ it('navigation while the bar is closed does not reach into the bridge', async () => {
+ const { navigate } = renderFindBarWithNavigation('/session/a')
+
+ navigate('/session/b')
+
+ await waitFor(() => expect(screen.queryByRole('search')).toBeNull())
+ expect(bridge.stopFindInPage).not.toHaveBeenCalled()
+ })
})
diff --git a/apps/desktop/src/components/find-bar.tsx b/apps/desktop/src/components/find-bar.tsx
index 20aad96e450..c7e0dd74dad 100644
--- a/apps/desktop/src/components/find-bar.tsx
+++ b/apps/desktop/src/components/find-bar.tsx
@@ -1,5 +1,6 @@
import { useStore } from '@nanostores/react'
import { useEffect, useRef, useState } from 'react'
+import { useLocation } from 'react-router-dom'
import { Tip } from '@/components/ui/tooltip'
import { useI18n } from '@/i18n'
@@ -36,6 +37,21 @@ export function FindBar() {
const { active, query, matchOrdinal, matchCount } = useStore($findInPage)
const inputRef = useRef<HTMLInputElement>(null)
const [localQuery, setLocalQuery] = useState('')
+ const { pathname } = useLocation()
+
+ // Navigating away (opening another session, a settings page, …) closes the
+ // bar and clears the native highlight. Electron's findInPage selection is
+ // per-webContents, not per-route: without this, highlights (and a stale
+ // match counter) from the previous chat would survive onto the next view.
+ // Implemented as effect cleanup so the first render never fires it, a
+ // pathname change tears down the previous route's search, and unmount
+ // (session/profile switches that remount the shell) gets the same teardown.
+ // closeFindBar is idempotent, so a closed bar never re-enters the bridge.
+ useEffect(() => {
+ void pathname
+
+ return () => closeFindBar()
+ }, [pathname])
// Focus input when find bar opens.
useEffect(() => {