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.

feat(command-k): tidy palette keys, times and the ask ai row (#125567)

Shy Alterd207521f478f
diff --git a/frontend/src/lib/components/CommandKSearch/COMMAND_K_SEARCH_UX.md b/frontend/src/lib/components/CommandKSearch/COMMAND_K_SEARCH_UX.md
index 10c99d4f9173..2df647fe9172 100644
--- a/frontend/src/lib/components/CommandKSearch/COMMAND_K_SEARCH_UX.md
+++ b/frontend/src/lib/components/CommandKSearch/COMMAND_K_SEARCH_UX.md
@@ -177,7 +177,7 @@ These are the rules reviewers check. Breaking one is a bug.
| Key | Action |
| ----------------------- | --------------------------------------------------------------------------------------------------------------------------- |
-| `↑` / `↓` | Move highlight. Wraps at ends. Skips section headers. |
+| `↑` / `↓` | Move highlight. Skips section headers. `↑` on the first row returns to the input. `↓` stops at the last row. |
| `↵` | Filter key or value row: complete it. Result row: open it. |
| `⌘↵` | Open result in a new tab. |
| `Tab` | Highlighted key or value row: complete it. Any other row, with a query: ask PostHog AI. With neither, focus moves as usual. |
@@ -185,7 +185,7 @@ These are the rules reviewers check. Breaking one is a bug.
| `⌫` at start of text | First press selects the previous chip. Second press removes it. |
| `↵` on a selected chip | Turns the chip back into editable text (`is:dashboard`) with the cursor after it. |
| `←` / `→` at text edges | Move between chips. |
-| `Esc` | Text in the input: clear it. Empty input: close the palette. |
+| `Esc` | Close the palette. |
| `⌘A` | Selects the chips and the text together. `⌫`, typing, or cut then replaces all of it. Copy includes the chips as text. |
## Edge cases
@@ -195,7 +195,7 @@ These are the rules reviewers check. Breaking one is a bug.
- **Pasted query** (`is:flag createdBy:me checkout`): parse into chips immediately.
- **Same key twice** (`is:dashboard is:insight`): the later value replaces the earlier chip. While a value for a key that already has a chip is being typed, results preview the new value instead of the chip.
- **Invalid value** (`is:banana`): keep it as plain text, no chip, and show "No type called banana" in the Suggestions section.
-- **Ask PostHog AI**: the last section in Search mode. Without filters, the question is the input text as typed. With filters and free text, the filters read as a sentence around the text (`Find notebooks created by me called "untitled"`). With filters and no free text, the section does not show and Tab does not ask AI.
+- **Ask PostHog AI**: the last section in Search mode, shown only when no other row matches. When other rows match, Tab and the **Press Tab to ask AI** button ask AI. Without filters, the question is the input text as typed. With filters and free text, the filters read as a sentence around the text (`Find notebooks created by me called "untitled"`). With filters and no free text, the section does not show and Tab does not ask AI.
Asking AI does not send the question. It closes the palette and opens the PostHog AI side panel with the question in the composer, so the user can edit it and send it with Enter.
## Out of scope for v1
diff --git a/frontend/src/lib/components/CommandKSearch/CommandKSearchFooter.tsx b/frontend/src/lib/components/CommandKSearch/CommandKSearchFooter.tsx
index edfcf6aff9cf..8f921464e1a5 100644
--- a/frontend/src/lib/components/CommandKSearch/CommandKSearchFooter.tsx
+++ b/frontend/src/lib/components/CommandKSearch/CommandKSearchFooter.tsx
@@ -7,7 +7,7 @@ import { todayShellLogic } from '~/layout/today/todayShellLogic'
import { commandKSearchLogic } from './commandKSearchLogic'
export function CommandKSearchFooter(): JSX.Element {
- const { highlightIsFilterRow, chips, mode, chipSelection, tabAsksAi } = useValues(commandKSearchLogic)
+ const { highlightIsFilterRow, chips, chipSelection, tabAsksAi } = useValues(commandKSearchLogic)
const { todayRailEnabled } = useValues(todayShellLogic)
return (
@@ -49,7 +49,7 @@ export function CommandKSearchFooter(): JSX.Element {
</span>
)}
<span className="flex items-center gap-1">
- <Kbd>Esc</Kbd> {mode === 'empty' ? 'close' : 'clear'}
+ <Kbd>Esc</Kbd> close
</span>
</div>
)
diff --git a/frontend/src/lib/components/CommandKSearch/CommandKSearchInput.tsx b/frontend/src/lib/components/CommandKSearch/CommandKSearchInput.tsx
index 785b8b7b875f..a30205e67c7c 100644
--- a/frontend/src/lib/components/CommandKSearch/CommandKSearchInput.tsx
+++ b/frontend/src/lib/components/CommandKSearch/CommandKSearchInput.tsx
@@ -52,8 +52,7 @@ export function CommandKSearchInput(): JSX.Element {
if (!('keepDefault' in intent)) {
event.preventDefault()
}
- if (intent.type === 'clear-or-close') {
- // Stop the dialog from closing, so the first press only clears the query.
+ if (intent.type === 'close') {
event.stopPropagation()
}
applyKeyIntent(intent)
diff --git a/frontend/src/lib/components/CommandKSearch/CommandKSearchRankedItem.tsx b/frontend/src/lib/components/CommandKSearch/CommandKSearchRankedItem.tsx
index 0676a6cfd104..0b9b88a3150f 100644
--- a/frontend/src/lib/components/CommandKSearch/CommandKSearchRankedItem.tsx
+++ b/frontend/src/lib/components/CommandKSearch/CommandKSearchRankedItem.tsx
@@ -1,11 +1,11 @@
import { getIconForItem, getItemTypeDisplayName } from 'lib/components/Search/searchItemDisplay'
import { SearchItem } from 'lib/components/Search/searchItems'
-import { formatRelativeTimeShort } from 'lib/components/Search/utils'
import { capitalizeFirstLetter } from 'lib/utils/strings'
import { splitPath } from '~/layout/panel-layout/ProjectTree/utils'
import { highlightSegments } from './commandKRanking'
+import { CommandKSearchTime } from './CommandKSearchTime'
import { CommandKRow } from './commandKSections'
const userFolder = (item: SearchItem): string | null => {
@@ -13,18 +13,21 @@ const userFolder = (item: SearchItem): string | null => {
return segments.length === 0 || segments[0] === 'Unfiled' ? null : segments.join(' › ')
}
-function rankedContext(item: SearchItem, showCreator: boolean): { detail: string | null; tail: string | null } {
+function rankedContext(
+ item: SearchItem,
+ showCreator: boolean
+): { detail: string | null; tail: JSX.Element | string | null } {
if (item.category !== 'results') {
return {
detail: item.parentName ? `in ${item.parentName}` : null,
tail: item.matchedSearchKeyword ? `Matches "${item.matchedSearchKeyword}"` : null,
}
}
- const age = item.lastViewedAt
- ? `viewed ${formatRelativeTimeShort(item.lastViewedAt)}`
- : item.record?.created_at
- ? `created ${formatRelativeTimeShort(String(item.record.created_at))}`
- : null
+ const age = item.lastViewedAt ? (
+ <CommandKSearchTime time={item.lastViewedAt} prefix="viewed" tooltipLabel="Last used" />
+ ) : item.record?.created_at ? (
+ <CommandKSearchTime time={String(item.record.created_at)} prefix="created" tooltipLabel="Created" />
+ ) : null
const detail = [userFolder(item), showCreator ? item.createdByName : null].filter(Boolean).join(' · ')
return { detail: detail || null, tail: age }
}
diff --git a/frontend/src/lib/components/CommandKSearch/CommandKSearchRow.tsx b/frontend/src/lib/components/CommandKSearch/CommandKSearchRow.tsx
index 684dce97ff96..fb1b64b5d492 100644
--- a/frontend/src/lib/components/CommandKSearch/CommandKSearchRow.tsx
+++ b/frontend/src/lib/components/CommandKSearch/CommandKSearchRow.tsx
@@ -5,7 +5,6 @@ import { IconArrowRight, IconFilter, IconFolder, IconPerson, IconSparkles } from
import { Button, Skeleton, cn } from '@posthog/quill'
import { getIconForItem, getItemTypeDisplayName } from 'lib/components/Search/searchItemDisplay'
-import { formatRelativeTimeShort } from 'lib/components/Search/utils'
import { capitalizeFirstLetter } from 'lib/utils/strings'
import { ProductIconWrapper, iconForType } from '~/layout/panel-layout/ProjectTree/defaultTree'
@@ -13,6 +12,7 @@ import { FileSystemIconType } from '~/queries/schema/schema-general'
import { commandKSearchLogic } from './commandKSearchLogic'
import { CommandKSearchRankedItem } from './CommandKSearchRankedItem'
+import { CommandKSearchTime } from './CommandKSearchTime'
import { CommandKRow } from './commandKSections'
export const rowDomId = (key: string): string => `command-k-row-${key.replace(/[^a-zA-Z0-9_-]/g, '_')}`
@@ -96,11 +96,11 @@ function RowContent({ row }: { row: CommandKRow }): JSX.Element | null {
{item.lastViewedAt && (
<span
className={cn(
- 'shrink-0 whitespace-nowrap text-xs text-muted-foreground',
+ 'shrink-0 text-xs text-muted-foreground',
item.matchedSearchKeyword ? 'ml-2' : 'ml-auto'
)}
>
- {formatRelativeTimeShort(item.lastViewedAt)}
+ <CommandKSearchTime time={item.lastViewedAt} tooltipLabel="Last used" />
</span>
)}
</>
diff --git a/frontend/src/lib/components/CommandKSearch/CommandKSearchTime.tsx b/frontend/src/lib/components/CommandKSearch/CommandKSearchTime.tsx
new file mode 100644
index 000000000000..4d09d78860ff
--- /dev/null
+++ b/frontend/src/lib/components/CommandKSearch/CommandKSearchTime.tsx
@@ -0,0 +1,29 @@
+import { Tooltip, TooltipContent, TooltipTrigger } from '@posthog/quill'
+
+import { formatRelativeTimeShort } from 'lib/components/Search/utils'
+import { humanFriendlyDetailedTime } from 'lib/utils/datetime'
+
+export function CommandKSearchTime({
+ time,
+ prefix,
+ tooltipLabel,
+}: {
+ time: string
+ prefix?: string
+ tooltipLabel: string
+}): JSX.Element {
+ const relative = formatRelativeTimeShort(time)
+ return (
+ <Tooltip>
+ <TooltipTrigger
+ render={<span className="shrink-0 whitespace-nowrap" translate="no" />}
+ data-attr="command-k-row-time"
+ >
+ {prefix ? `${prefix} ${relative}` : relative}
+ </TooltipTrigger>
+ <TooltipContent>
+ {`${tooltipLabel} ${humanFriendlyDetailedTime(time, 'MMMM D, YYYY', 'h:mm A', { timestampStyle: 'absolute' })}`}
+ </TooltipContent>
+ </Tooltip>
+ )
+}
diff --git a/frontend/src/lib/components/CommandKSearch/commandKKeys.ts b/frontend/src/lib/components/CommandKSearch/commandKKeys.ts
index ddd7026cf86b..9c2dc4182687 100644
--- a/frontend/src/lib/components/CommandKSearch/commandKKeys.ts
+++ b/frontend/src/lib/components/CommandKSearch/commandKKeys.ts
@@ -10,7 +10,7 @@ export type KeyIntent =
| { type: 'remove-chip'; index: number }
| { type: 'edit-chip'; index: number }
| { type: 'clear' }
- | { type: 'clear-or-close' }
+ | { type: 'close' }
export interface KeyState {
/** The caret sits at the very start of the text with nothing selected. */
@@ -49,7 +49,7 @@ export function keyIntent(
case 'Tab':
return state.tabHasAction ? { type: 'complete' } : null
case 'Escape':
- return { type: 'clear-or-close' }
+ return { type: 'close' }
case 'Delete':
return chipSelection === 'all' ? { type: 'clear' } : null
case 'Backspace':
diff --git a/frontend/src/lib/components/CommandKSearch/commandKSearchLogic.test.ts b/frontend/src/lib/components/CommandKSearch/commandKSearchLogic.test.ts
index 52bb680a3542..4291b4a6dc6a 100644
--- a/frontend/src/lib/components/CommandKSearch/commandKSearchLogic.test.ts
+++ b/frontend/src/lib/components/CommandKSearch/commandKSearchLogic.test.ts
@@ -39,6 +39,21 @@ describe('commandKSearchLogic', () => {
expect(logic.values.remote === remoteBefore).toBe(!failed)
})
+ it('arrow up from the first row returns to the input, and arrow down stops at the last row', () => {
+ const rows = logic.values.selectableRows
+ expect(rows.length).toBeGreaterThan(1)
+
+ logic.actions.applyKeyIntent({ type: 'move', direction: -1 })
+ expect(logic.values.highlightedRow).toBeNull()
+
+ logic.actions.applyKeyIntent({ type: 'move', direction: 1 })
+ expect(logic.values.highlightedRow).toBe(rows[0])
+
+ logic.actions.setHighlightedKey(rows[rows.length - 1].key)
+ logic.actions.applyKeyIntent({ type: 'move', direction: 1 })
+ expect(logic.values.highlightedRow).toBe(rows[rows.length - 1])
+ })
+
it('a See all row narrows the search to its type and keeps the words', () => {
logic.actions.inputChanged('revenue', 7, false)
diff --git a/frontend/src/lib/components/CommandKSearch/commandKSearchLogic.tsx b/frontend/src/lib/components/CommandKSearch/commandKSearchLogic.tsx
index c787f25da482..86e0358fc2c3 100644
--- a/frontend/src/lib/components/CommandKSearch/commandKSearchLogic.tsx
+++ b/frontend/src/lib/components/CommandKSearch/commandKSearchLogic.tsx
@@ -84,6 +84,7 @@ import { REMOTE_REQUESTS, RemoteContext, RemoteResults, RemoteSource, settleRequ
const REMOTE_DEBOUNCE_MS = 150
const OBJECTS_DEBOUNCE_MS = 50
const FAST_REQUESTS = ['objects', 'persons']
+const INPUT_HIGHLIGHT_KEY = 'input'
export type CommitVia = 'tab' | 'enter' | 'space' | 'paste' | 'click' | 'typed' | 'see-all'
@@ -739,7 +740,9 @@ export const commandKSearchLogic = kea<commandKSearchLogicType>([
highlightedRow: [
(s) => [s.selectableRows, s.highlightedKey],
(rows: CommandKRow[], highlightedKey: string | null): CommandKRow | null =>
- rows.find((row) => row.key === highlightedKey) ?? rows[0] ?? null,
+ highlightedKey === INPUT_HIGHLIGHT_KEY
+ ? null
+ : (rows.find((row) => row.key === highlightedKey) ?? rows[0] ?? null),
],
highlightIsFilterRow: [
(s) => [s.highlightedRow],
@@ -982,12 +985,8 @@ export const commandKSearchLogic = kea<commandKSearchLogicType>([
case 'edit-chip':
actions.editChip(intent.index)
break
- case 'clear-or-close':
- if (values.isPaletteEmpty) {
- actions.closeCommand()
- } else {
- actions.clearQuery()
- }
+ case 'close':
+ actions.closeCommand()
break
}
},
@@ -997,7 +996,8 @@ export const commandKSearchLogic = kea<commandKSearchLogicType>([
return
}
const currentIndex = values.highlightedRow ? rows.indexOf(values.highlightedRow) : -1
- actions.setHighlightedKey(rows[(currentIndex + direction + rows.length) % rows.length].key)
+ const nextIndex = Math.min(currentIndex + direction, rows.length - 1)
+ actions.setHighlightedKey(nextIndex < 0 ? INPUT_HIGHLIGHT_KEY : rows[nextIndex].key)
},
activateRow: ({ row, newTab, via }) => {
if (row.kind === 'filter-key') {
diff --git a/frontend/src/lib/components/CommandKSearch/commandKSections.test.ts b/frontend/src/lib/components/CommandKSearch/commandKSections.test.ts
index 82f492bfd7b1..8b7104ffa79c 100644
--- a/frontend/src/lib/components/CommandKSearch/commandKSections.test.ts
+++ b/frontend/src/lib/components/CommandKSearch/commandKSections.test.ts
@@ -197,16 +197,16 @@ describe('buildSections', () => {
['results', 'loading-more'],
],
[
- 'PostHog AI without waiting for slow person searches',
+ 'no PostHog AI row while other rows match',
{ events: [dashboard('e', '8')] },
false,
- ['results', 'events', 'ask-ai', 'loading-more'],
+ ['results', 'events', 'loading-more'],
],
[
- 'persons last, below PostHog AI, once they load',
+ 'persons last, once they load',
{ events: [dashboard('e', '8')], persons: [dashboard('p', '9')] },
false,
- ['results', 'events', 'ask-ai', 'persons'],
+ ['results', 'events', 'persons'],
],
[
'only result skeletons while persons that go first load',
@@ -237,6 +237,35 @@ describe('buildSections', () => {
expect(sections.map((section) => section.key)).toEqual(expectedKeys)
})
+ test.each([
+ [
+ 'while persons load',
+ undefined,
+ [
+ ['ask-ai', ['ask-ai']],
+ ['loading-more', ['loading-more']],
+ ],
+ ],
+ ['when nothing else matches', [], [['ask-ai', ['ask-ai']]]],
+ ['unless persons match', [dashboard('p', '9')], [['persons', ['item']]]],
+ ])('offers PostHog AI %s', (_, persons, expected) => {
+ const remote = emptyRemoteSlots()
+ remote.results = { cache: { xyzzy: [] } }
+ remote.events = { cache: { xyzzy: [] } }
+ if (persons) {
+ remote.persons = { cache: { xyzzy: persons } }
+ }
+ const sections = buildSections(
+ input('search', {
+ remote,
+ remoteQueries: { results: 'xyzzy', events: 'xyzzy', persons: 'xyzzy' },
+ localQuery: 'xyzzy',
+ askAiQuestion: 'xyzzy',
+ })
+ )
+ expect(sectionKinds(sections)).toEqual(expected)
+ })
+
it('ranks a person who matches every word of an email search above results that match fewer', () => {
const person: SearchItem = {
id: 'person-1',
diff --git a/frontend/src/lib/components/CommandKSearch/commandKSections.ts b/frontend/src/lib/components/CommandKSearch/commandKSections.ts
index bf0b13b656cb..723a17f14724 100644
--- a/frontend/src/lib/components/CommandKSearch/commandKSections.ts
+++ b/frontend/src/lib/components/CommandKSearch/commandKSections.ts
@@ -369,13 +369,20 @@ function lowerSections(
if (!appendInOrder(REMOTE_SOURCES.filter((source) => source !== 'results' && !TRAILING_SOURCES.includes(source)))) {
return loadingMore()
}
- if (input.askAiQuestion) {
+ const hasItems = shown.size > 0 || loaded().some((section) => section.rows.some((row) => row.kind === 'item'))
+ if (input.askAiQuestion && !hasItems) {
sections.push({
...readySection('ask-ai', [{ kind: 'ask-ai', key: 'ask-ai/ask', question: input.askAiQuestion }]),
state,
})
}
- return appendInOrder(TRAILING_SOURCES) ? loaded() : loadingMore()
+ if (!appendInOrder(TRAILING_SOURCES)) {
+ return loadingMore()
+ }
+ const all = loaded()
+ return all.some((section) => section.key !== 'ask-ai' && section.rows.some((row) => row.kind === 'item'))
+ ? all.filter((section) => section.key !== 'ask-ai')
+ : all
}
export function buildSections(input: SectionsInput): CommandKSection[] {