Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(search): open command k search with an empty query every time (#129902)

Adam Leith85e3eba151e3
diff --git a/frontend/src/lib/components/Command/Command.test.tsx b/frontend/src/lib/components/Command/Command.test.tsx
new file mode 100644
index 000000000000..f2bbad7f7b57
--- /dev/null
+++ b/frontend/src/lib/components/Command/Command.test.tsx
@@ -0,0 +1,41 @@
+import { act, fireEvent, render, screen } from '@testing-library/react'
+
+import { FEATURE_FLAGS } from 'lib/constants'
+import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
+
+import { initKeaTests } from '~/test/init'
+
+import { Command } from './Command'
+import { commandLogic } from './commandLogic'
+
+// On CI, the first open mounts enough connected logics to outlast the findBy and Jest default timeouts.
+jest.setTimeout(20000)
+
+const findSearchInput = (): Promise<HTMLInputElement> =>
+ screen.findByPlaceholderText('Search or ask PostHog AI… try is:dashboard', {}, { timeout: 10000 })
+
+describe('Command', () => {
+ // Keeps the cold load of the lazy chunk out of the test. The lazy component still suspends on the first open.
+ beforeAll(() => import('lib/components/CommandKSearch/CommandKSearch'))
+
+ beforeEach(() => {
+ initKeaTests()
+ featureFlagLogic.mount()
+ featureFlagLogic.actions.setFeatureFlags([FEATURE_FLAGS.NEW_COMMAND_K_SEARCH], {
+ [FEATURE_FLAGS.NEW_COMMAND_K_SEARCH]: true,
+ })
+ commandLogic.mount()
+ })
+
+ it('opens Command K search with an empty query after it closes with a query', async () => {
+ render(<Command />)
+ act(() => commandLogic.actions.openCommand('keyboard-shortcut'))
+ fireEvent.change(await findSearchInput(), { target: { value: 'work', selectionStart: 4 } })
+ expect((await findSearchInput()).value).toBe('work')
+
+ act(() => commandLogic.actions.closeCommand())
+ act(() => commandLogic.actions.openCommand('keyboard-shortcut'))
+
+ expect((await findSearchInput()).value).toBe('')
+ })
+})
diff --git a/frontend/src/lib/components/CommandKSearch/CommandKSearch.tsx b/frontend/src/lib/components/CommandKSearch/CommandKSearch.tsx
index c8935e6a0dab..ff9247c3b281 100644
--- a/frontend/src/lib/components/CommandKSearch/CommandKSearch.tsx
+++ b/frontend/src/lib/components/CommandKSearch/CommandKSearch.tsx
@@ -1,63 +1,12 @@
-import { useValues } from 'kea'
-import { useEffect } from 'react'
+import { useEffect, useState } from 'react'
-import { ScrollArea } from '@posthog/quill'
-
-import { CommandKSearchFooter } from './CommandKSearchFooter'
-import { COMMAND_K_LISTBOX_ID, CommandKSearchInput } from './CommandKSearchInput'
-import { commandKSearchLogic } from './commandKSearchLogic'
-import { rowDomId } from './CommandKSearchRow'
-import { CommandKSearchSection } from './CommandKSearchSection'
+import { CommandKSearchPalette } from './CommandKSearchPalette'
/** Command K search with smart filters. The UX contract lives in COMMAND_K_SEARCH_UX.md next to this file. */
-export function CommandKSearch(): JSX.Element {
- const { sections, highlightedRow } = useValues(commandKSearchLogic)
- const highlightedKey = highlightedRow?.key ?? null
-
- useEffect(() => {
- const row = highlightedKey ? document.getElementById(rowDomId(highlightedKey)) : null
- const viewport = row?.closest<HTMLElement>('[data-slot="scroll-area-viewport"]')
- if (!row || !viewport) {
- return
- }
- // scrollIntoView also scrolls the dialog, which clips the input, so scroll only the list.
- // The sticky section header covers the top of the viewport, so keep rows below it.
- const header = row.closest('[role="group"]')?.querySelector<HTMLElement>('[data-slot="menu-label"]')
- const headerHeight = header?.offsetHeight ?? 0
- const rowRect = row.getBoundingClientRect()
- const viewportRect = viewport.getBoundingClientRect()
- if (rowRect.top < viewportRect.top + headerHeight) {
- viewport.scrollTop -= viewportRect.top + headerHeight - rowRect.top
- } else if (rowRect.bottom > viewportRect.bottom) {
- viewport.scrollTop += rowRect.bottom - viewportRect.bottom
- }
- }, [highlightedKey])
-
- return (
- <div
- data-quill
- className="flex min-h-0 flex-1 flex-col bg-background text-foreground"
- data-attr="command-k-search"
- >
- <CommandKSearchInput />
- <ScrollArea
- // Sticky section headers sit at z-10, so lift the scrollbar above them.
- className="flex min-h-0 flex-1 flex-col border-t border-border [&>[data-slot=scroll-area-scrollbar]]:z-20"
- viewportClassName="h-auto min-h-0 flex-1"
- >
- <div
- id={COMMAND_K_LISTBOX_ID}
- role="listbox"
- aria-label="Search results"
- // The colorful-product-icons group turns on each product's brand color, as in the navbar.
- className="group/colorful-product-icons colorful-product-icons-true bg-card pb-1"
- >
- {sections.map((section) => (
- <CommandKSearchSection key={section.key} section={section} highlightedKey={highlightedKey} />
- ))}
- </div>
- </ScrollArea>
- <CommandKSearchFooter />
- </div>
- )
+export function CommandKSearch(): JSX.Element | null {
+ // kea mounts a logic during render, and React can restart the render that loads this chunk without an unmount,
+ // which leaks the logic and its query. Rendering after the first commit mounts the logic once for each open.
+ const [hasCommitted, setHasCommitted] = useState(false)
+ useEffect(() => setHasCommitted(true), [])
+ return hasCommitted ? <CommandKSearchPalette /> : null
}
diff --git a/frontend/src/lib/components/CommandKSearch/CommandKSearchPalette.tsx b/frontend/src/lib/components/CommandKSearch/CommandKSearchPalette.tsx
new file mode 100644
index 000000000000..0e4aff9a25d6
--- /dev/null
+++ b/frontend/src/lib/components/CommandKSearch/CommandKSearchPalette.tsx
@@ -0,0 +1,62 @@
+import { useValues } from 'kea'
+import { useEffect } from 'react'
+
+import { ScrollArea } from '@posthog/quill'
+
+import { CommandKSearchFooter } from './CommandKSearchFooter'
+import { COMMAND_K_LISTBOX_ID, CommandKSearchInput } from './CommandKSearchInput'
+import { commandKSearchLogic } from './commandKSearchLogic'
+import { rowDomId } from './CommandKSearchRow'
+import { CommandKSearchSection } from './CommandKSearchSection'
+
+export function CommandKSearchPalette(): JSX.Element {
+ const { sections, highlightedRow } = useValues(commandKSearchLogic)
+ const highlightedKey = highlightedRow?.key ?? null
+
+ useEffect(() => {
+ const row = highlightedKey ? document.getElementById(rowDomId(highlightedKey)) : null
+ const viewport = row?.closest<HTMLElement>('[data-slot="scroll-area-viewport"]')
+ if (!row || !viewport) {
+ return
+ }
+ // scrollIntoView also scrolls the dialog, which clips the input, so scroll only the list.
+ // The sticky section header covers the top of the viewport, so keep rows below it.
+ const header = row.closest('[role="group"]')?.querySelector<HTMLElement>('[data-slot="menu-label"]')
+ const headerHeight = header?.offsetHeight ?? 0
+ const rowRect = row.getBoundingClientRect()
+ const viewportRect = viewport.getBoundingClientRect()
+ if (rowRect.top < viewportRect.top + headerHeight) {
+ viewport.scrollTop -= viewportRect.top + headerHeight - rowRect.top
+ } else if (rowRect.bottom > viewportRect.bottom) {
+ viewport.scrollTop += rowRect.bottom - viewportRect.bottom
+ }
+ }, [highlightedKey])
+
+ return (
+ <div
+ data-quill
+ className="flex min-h-0 flex-1 flex-col bg-background text-foreground"
+ data-attr="command-k-search"
+ >
+ <CommandKSearchInput />
+ <ScrollArea
+ // Sticky section headers sit at z-10, so lift the scrollbar above them.
+ className="flex min-h-0 flex-1 flex-col border-t border-border [&>[data-slot=scroll-area-scrollbar]]:z-20"
+ viewportClassName="h-auto min-h-0 flex-1"
+ >
+ <div
+ id={COMMAND_K_LISTBOX_ID}
+ role="listbox"
+ aria-label="Search results"
+ // The colorful-product-icons group turns on each product's brand color, as in the navbar.
+ className="group/colorful-product-icons colorful-product-icons-true bg-card pb-1"
+ >
+ {sections.map((section) => (
+ <CommandKSearchSection key={section.key} section={section} highlightedKey={highlightedKey} />
+ ))}
+ </div>
+ </ScrollArea>
+ <CommandKSearchFooter />
+ </div>
+ )
+}