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): keep command k rows still while results load (#121956)

Shy Alter6234c6bd2761
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 cb1cac66b005..91f8746a478b 100644
--- a/frontend/src/lib/components/CommandKSearch/COMMAND_K_SEARCH_UX.md
+++ b/frontend/src/lib/components/CommandKSearch/COMMAND_K_SEARCH_UX.md
@@ -95,8 +95,9 @@ The input is always in exactly one mode, decided from the token under the cursor
- Sections, in this fixed order, covering everything the current palette finds:
1. **Results**: file system objects (insights, dashboards, feature flags, experiments, surveys, notebooks, cohorts, actions, early access features, replay playlists, data pipelines), and products. For a search with words, see [Ranked results](#ranked-results).
2. In-memory lists, filtered on every keystroke: **Data management**, **People**, **Health**, **Misc**, **Settings** (with a Dark mode / Light mode row for "dark", "light", "theme", "appearance"), **Create new**. For a search with words they keep only items that match every word with no typo. Products rank with the results instead.
- 3. Remote lists that the file system does not hold: **Events**, **Properties**, **Workflows** (one unified search request), **Accounts** (behind the customer analytics flag), **Support tickets** (3+ characters or a ticket number), **Persons**, **Groups**.
+ 3. Remote lists that the file system does not hold: **Events**, **Properties**, **Workflows** (one unified search request), **Accounts** (behind the customer analytics flag), **Support tickets** (3+ characters or a ticket number).
4. **Ask PostHog AI**.
+ 5. **Persons**, **Groups**. Person and group searches can take seconds, so they come last and nothing above them waits for them. The person search starts with the file system search, after 50 ms.
- With any filter (a chip, or a valid `key:value` still being typed), only **Results** and **Ask PostHog AI** show, because filters apply only to objects.
- With an `is:` chip (not negated), **Results** holds a **View all <types>** row (`View all feature flags`) that opens the list page of that type. With no search text it is the first row; once you type a search it moves below the results. It shows only for types with a list page.
@@ -153,13 +154,13 @@ Rule for adding a filter: it ships only when its values can be suggested (static
These are the rules reviewers check. Breaking one is a bug.
1. **Fixed section order.** Sections render in the order in this doc for a given mode. A section never moves above another because its data arrived first. Ranked groups are ordered by their best result for each query, and do not move while that query's rows stay on screen.
-2. **Reserve space while loading.** The Results section renders skeleton rows at its last known height (or 3 on its first load) while it loads. It does not collapse to zero and then expand. Sections below Results that usually come back empty (Events, Properties, Workflows, Accounts, Support tickets, Persons, Groups) reserve nothing on their first load and appear only once they have rows, so skeletons never show up just to collapse.
-3. **Never blank on keystroke.** Keep the previous result set visible (dimmed slightly after 150 ms) until the new set arrives. Swap in one commit.
+2. **Reserve space while loading.** On its first load, the Results section renders 3 skeleton rows, and nothing renders below it until the file system search returns. Products wait too, because they rank with the saved objects. Below Results, a section appears only once it and every section above it have loaded, so a late source never pushes rows on screen down. A section that comes back empty reserves nothing. While a section below Results is still loading, a quiet **Loading more…** row with a spinner sits at the very bottom, so people know more is coming. It fades in after 150 ms, so fast searches never flash it, and nothing above it ever moves.
+3. **Never blank on keystroke.** Keep the previous query's whole list visible (dimmed slightly after 150 ms) until the new query's results arrive. Then swap Results and the sections below it in one commit.
4. **No mid-query re-rank.** Once rows for a query string are painted, later responses for the same query append to their own section. They never reorder rows above them.
5. **Highlight follows identity, not index.** The highlighted row is tracked by item key (`section/type:ref`). Once the person moves the highlight (arrow keys or pointer), it stays on that item through result swaps for the same query while the item still exists. If it is gone, or the person never moved it, the first row is highlighted. Any change to the query (typing, clearing, adding or removing a chip) starts again at the first row.
6. **Fixed row height.** Every row is one line, same height. Long names truncate with an ellipsis; the type label and timestamp never wrap.
7. **Instant local, deferred remote.** Local sources (recents, starred, products, filter keys, type values, cached members) filter synchronously on every keystroke. Remote search debounces at 150 ms, except the file system search, which debounces at 50 ms.
-8. **Latest wins.** Each remote request is aborted when a newer one starts (`AbortController`). A late response for an older query is dropped, never painted.
+8. **Latest wins.** Each remote request is aborted when a newer one starts (`AbortController`). A late response for an older query is dropped, never painted. A request that has not answered after 3 s settles empty, so the sections below it never wait longer than that.
9. **Chips don't reflow the list.** Committing a chip changes the query but follows rules 3 to 5 like any other edit.
## Performance budget
diff --git a/frontend/src/lib/components/CommandKSearch/CommandKSearchRow.tsx b/frontend/src/lib/components/CommandKSearch/CommandKSearchRow.tsx
index 20047fbfc6e2..684dce97ff96 100644
--- a/frontend/src/lib/components/CommandKSearch/CommandKSearchRow.tsx
+++ b/frontend/src/lib/components/CommandKSearch/CommandKSearchRow.tsx
@@ -141,6 +141,7 @@ const rowContent = (row: CommandKRow): unknown => {
case 'message':
return row.text
case 'skeleton':
+ case 'loading-more':
return null
}
}
diff --git a/frontend/src/lib/components/CommandKSearch/CommandKSearchSection.tsx b/frontend/src/lib/components/CommandKSearch/CommandKSearchSection.tsx
index eb419a02acf2..af1b71573e13 100644
--- a/frontend/src/lib/components/CommandKSearch/CommandKSearchSection.tsx
+++ b/frontend/src/lib/components/CommandKSearch/CommandKSearchSection.tsx
@@ -1,4 +1,4 @@
-import { MenuLabel, cn } from '@posthog/quill'
+import { MenuLabel, Spinner, cn } from '@posthog/quill'
import { CommandKSearchNotice } from './CommandKSearchNotice'
import { CommandKSearchRow } from './CommandKSearchRow'
@@ -20,6 +20,18 @@ export function CommandKSearchSection({
</div>
)
}
+ if (section.key === 'loading-more') {
+ return (
+ <div
+ role="status"
+ className="flex h-8 items-center gap-2 px-3 text-xs text-muted-foreground opacity-100 delay-150 duration-200 starting:opacity-0 motion-safe:transition-opacity"
+ data-attr="command-k-section-loading-more"
+ >
+ <Spinner aria-hidden className="size-3.5" />
+ <span>Loading more…</span>
+ </div>
+ )
+ }
return (
<div
role="group"
diff --git a/frontend/src/lib/components/CommandKSearch/commandKRanking.test.ts b/frontend/src/lib/components/CommandKSearch/commandKRanking.test.ts
index 468fe6135fe9..53685f818e3d 100644
--- a/frontend/src/lib/components/CommandKSearch/commandKRanking.test.ts
+++ b/frontend/src/lib/components/CommandKSearch/commandKRanking.test.ts
@@ -1,4 +1,4 @@
-import { compareRanks, highlightSegments, rankLocalItem, strictLocalMatches } from './commandKRanking'
+import { compareRanks, freeWords, highlightSegments, rankLocalItem, strictLocalMatches } from './commandKRanking'
const marked = (name: string, words: string[]): string =>
highlightSegments(name, words)
@@ -6,6 +6,19 @@ const marked = (name: string, words: string[]): string =>
.join('')
describe('commandKRanking', () => {
+ test.each([
+ { query: '-name:"old report"', words: [] },
+ { query: '!path:"Quarterly reports" revenue', words: ['revenue'] },
+ { query: 'name:"old report" rev', words: ['rev'] },
+ { query: '-in:x revenue', words: ['revenue'] },
+ ])('finds the free words $words in $query', ({ query, words }) => {
+ expect(freeWords(query)).toEqual(words)
+ })
+
+ it('ranks a pasted wall of text without failing', () => {
+ expect(rankLocalItem(`experiments ${'x'.repeat(35000)}`, 'Experiments')).not.toBeNull()
+ })
+
test.each([
{
name: 'Revenue: signup to first paid invoice',
diff --git a/frontend/src/lib/components/CommandKSearch/commandKRanking.ts b/frontend/src/lib/components/CommandKSearch/commandKRanking.ts
index fbc4a1f3549f..ca55cc056316 100644
--- a/frontend/src/lib/components/CommandKSearch/commandKRanking.ts
+++ b/frontend/src/lib/components/CommandKSearch/commandKRanking.ts
@@ -3,11 +3,14 @@ const WORD = /[\p{L}\p{N}_]+/gu
export type RankKey = number[]
const MIN_ALIAS_QUERY_LENGTH = 3
+const MAX_LOCAL_QUERY_LENGTH = 200
const STARTS_WITH = 1
export const searchWords = (text: string): string[] => text.toLowerCase().match(WORD) ?? []
-export const FILTER_TOKEN = /(^|\s)([-!]\S+|(path|name|user|author|type|ref):("[^"]*"|\S*))/gi
+const FILTER_KEY = '(?:path|name|user|author|type|ref)'
+
+export const FILTER_TOKEN = new RegExp(`(^|\\s)([-!](?:${FILTER_KEY}:"[^"]*"|\\S+)|${FILTER_KEY}:("[^"]*"|\\S*))`, 'gi')
export const freeWords = (query: string): string[] => searchWords(query.replace(FILTER_TOKEN, ' '))
@@ -156,7 +159,7 @@ interface LocalRank {
}
export function rankLocalItem(query: string, name: string, aliases: string[] = []): LocalRank | null {
- const normalized = query.trim().toLowerCase().replace(/\s+/g, ' ')
+ const normalized = query.trim().toLowerCase().replace(/\s+/g, ' ').slice(0, MAX_LOCAL_QUERY_LENGTH)
const tokens = searchWords(normalized)
if (!tokens.length) {
return null
diff --git a/frontend/src/lib/components/CommandKSearch/commandKSearchLogic.test.ts b/frontend/src/lib/components/CommandKSearch/commandKSearchLogic.test.ts
index 6f8f24974fda..c5eec99a9ad2 100644
--- a/frontend/src/lib/components/CommandKSearch/commandKSearchLogic.test.ts
+++ b/frontend/src/lib/components/CommandKSearch/commandKSearchLogic.test.ts
@@ -26,6 +26,17 @@ describe('commandKSearchLogic', () => {
expect(logic.values.highlightedKey).toBeNull()
})
+ test.each([
+ ['retries a query whose request failed', true, undefined],
+ ['reuses a query whose request answered', false, []],
+ ])('%s when it is searched again', (_, failed, cached) => {
+ logic.actions.remoteLoaded('ada', { persons: [] }, failed)
+
+ logic.actions.requestStarted('persons', 'ada')
+
+ expect(logic.values.remote.persons.cache['ada']).toEqual(cached)
+ })
+
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 9d0a302bde0d..c7c7f40a2592 100644
--- a/frontend/src/lib/components/CommandKSearch/commandKSearchLogic.tsx
+++ b/frontend/src/lib/components/CommandKSearch/commandKSearchLogic.tsx
@@ -11,7 +11,7 @@ import {
selectors,
} from 'kea'
-import api, { isAbortError } from 'lib/api'
+import api from 'lib/api'
import { commandLogic } from 'lib/components/Command/commandLogic'
import { SearchItem, fileSystemEntryToSearchItem } from 'lib/components/Search/searchItems'
import { searchListsLogic } from 'lib/components/Search/searchListsLogic'
@@ -64,17 +64,21 @@ import {
LocalLists,
RECENTS_LIMIT,
RemoteSlots,
+ SectionsQuery,
ViewAll,
buildSections,
buildSuggestions,
emptyRemoteSlots,
isSelectable,
+ pickDisplayedQuery,
+ resultsSettled,
themeSearchItem,
} from './commandKSections'
-import { REMOTE_REQUESTS, RemoteContext, RemoteResults, RemoteSource, settleSources } from './commandKSources'
+import { REMOTE_REQUESTS, RemoteContext, RemoteResults, RemoteSource, settleRequest } from './commandKSources'
const REMOTE_DEBOUNCE_MS = 150
const OBJECTS_DEBOUNCE_MS = 50
+const FAST_REQUESTS = ['objects', 'persons']
export type CommitVia = 'tab' | 'enter' | 'space' | 'paste' | 'click' | 'typed' | 'see-all'
@@ -101,8 +105,13 @@ export interface commandKSearchLogicValues {
askAiQuestion: string | null
chipSelection: ChipSelection
chips: QueryChip[]
+ currentQuery: SectionsQuery
cursor: number
cursorContext: CursorContext
+ displayedQuery: {
+ query: SectionsQuery
+ stale: boolean
+ }
filterOptions: FilterOptions
folders: FileSystemEntry[] | null
highlightIsFilterRow: boolean
@@ -122,6 +131,7 @@ export interface commandKSearchLogicValues {
resultOpened: boolean
sections: CommandKSection[]
selectableRows: CommandKRow[]
+ shownQuery: SectionsQuery | null
suggestionsSection: CommandKSection | null
tabAsksAi: boolean
text: string
@@ -207,8 +217,10 @@ export interface commandKSearchLogicActions {
}
remoteLoaded: (
query: string,
- results: RemoteResults
+ results: RemoteResults,
+ failed?: boolean
) => {
+ failed: boolean
query: string
results: Partial<
Record<
@@ -242,6 +254,9 @@ export interface commandKSearchLogicActions {
setHighlightedKey: (key: string | null) => {
key: string | null
}
+ showQuery: (query: SectionsQuery) => {
+ query: SectionsQuery
+ }
syncRemote: () => {
value: true
}
@@ -293,12 +308,7 @@ export interface commandKSearchLogicMeta {
) => LocalLists
themeItem: (localQuery: string, isDarkModeOn: boolean) => SearchItem | null
viewAll: (resolvedQuery: ResolvedQuery) => ViewAll | null
- sections: (
- mode: CommandKMode,
- cursorContext: CursorContext,
- suggestionsSection: CommandKSection | null,
- localLists: LocalLists,
- remote: RemoteSlots,
+ currentQuery: (
remoteQueries: Partial<
Record<
'accounts' | 'events' | 'groups' | 'persons' | 'properties' | 'results' | 'tickets' | 'workflows',
@@ -309,6 +319,25 @@ export interface commandKSearchLogicMeta {
themeItem: SearchItem | null,
askAiQuestion: string | null,
viewAll: ViewAll | null
+ ) => SectionsQuery
+ displayedQuery: (
+ currentQuery: SectionsQuery,
+ shownQuery: SectionsQuery | null,
+ remote: RemoteSlots
+ ) => {
+ query: SectionsQuery
+ stale: boolean
+ }
+ sections: (
+ mode: CommandKMode,
+ cursorContext: CursorContext,
+ suggestionsSection: CommandKSection | null,
+ localLists: LocalLists,
+ remote: RemoteSlots,
+ displayedQuery: {
+ query: SectionsQuery
+ stale: boolean
+ }
) => CommandKSection[]
selectableRows: (sections: CommandKSection[]) => CommandKRow[]
highlightedRow: (selectableRows: CommandKRow[], highlightedKey: string | null) => CommandKRow | null
@@ -388,7 +417,8 @@ export const commandKSearchLogic = kea<commandKSearchLogicType>([
clearQuery: true,
syncRemote: true,
requestStarted: (requestId: string, query: string) => ({ requestId, query }),
- remoteLoaded: (query: string, results: RemoteResults) => ({ query, results }),
+ remoteLoaded: (query: string, results: RemoteResults, failed: boolean = false) => ({ query, results, failed }),
+ showQuery: (query: SectionsQuery) => ({ query }),
loadFolders: true,
foldersLoaded: (folders: FileSystemEntry[]) => ({ folders }),
markResultOpened: true,
@@ -438,15 +468,36 @@ export const commandKSearchLogic = kea<commandKSearchLogicType>([
remote: [
emptyRemoteSlots(),
{
- remoteLoaded: (state, { query, results }) => {
+ remoteLoaded: (state, { query, results, failed }) => {
const next = { ...state }
for (const [source, items] of Object.entries(results) as [RemoteSource, SearchItem[]][]) {
- next[source] = { cache: { ...state[source].cache, [query]: items }, displayedKey: query }
+ const { [query]: _, ...stillFailed } = state[source].failed ?? {}
+ next[source] = {
+ cache: { ...state[source].cache, [query]: items },
+ failed: failed ? { ...stillFailed, [query]: true } : stillFailed,
+ }
+ }
+ return next
+ },
+ requestStarted: (state, { requestId, query }) => {
+ const next = { ...state }
+ for (const source of REMOTE_REQUESTS.find((request) => request.id === requestId)?.sources ?? []) {
+ if (state[source].failed?.[query]) {
+ const { [query]: _items, ...cache } = state[source].cache
+ const { [query]: _failed, ...failed } = state[source].failed ?? {}
+ next[source] = { cache, failed }
+ }
}
return next
},
},
],
+ shownQuery: [
+ null as SectionsQuery | null,
+ {
+ showQuery: (_, { query }) => query,
+ },
+ ],
folders: [
null as FileSystemEntry[] | null,
{
@@ -606,30 +657,33 @@ export const commandKSearchLogic = kea<commandKSearchLogicType>([
}),
],
viewAll: [(s) => [s.resolvedQuery], (resolved: ResolvedQuery): ViewAll | null => viewAllFor(resolved)],
+ currentQuery: [
+ (s) => [s.remoteQueries, s.localQuery, s.themeItem, s.askAiQuestion, s.viewAll],
+ (
+ remoteQueries: Partial<Record<RemoteSource, string>>,
+ localQuery: string,
+ themeItem: SearchItem | null,
+ askAiQuestion: string | null,
+ viewAll: ViewAll | null
+ ): SectionsQuery => ({ remoteQueries, localQuery, themeItem, askAiQuestion, viewAll }),
+ ],
+ displayedQuery: [
+ (s) => [s.currentQuery, s.shownQuery, s.remote],
+ (
+ current: SectionsQuery,
+ shown: SectionsQuery | null,
+ remote: RemoteSlots
+ ): { query: SectionsQuery; stale: boolean } => pickDisplayedQuery(current, shown, remote),
+ ],
sections: [
- (s) => [
- s.mode,
- s.cursorContext,
- s.suggestionsSection,
- s.localLists,
- s.remote,
- s.remoteQueries,
- s.localQuery,
- s.themeItem,
- s.askAiQuestion,
- s.viewAll,
- ],
+ (s) => [s.mode, s.cursorContext, s.suggestionsSection, s.localLists, s.remote, s.displayedQuery],
(
mode: CommandKMode,
context: CursorContext,
suggestions: CommandKSection | null,
local: LocalLists,
remote: RemoteSlots,
- remoteQueries: Partial<Record<RemoteSource, string>>,
- localQuery: string,
- themeItem: SearchItem | null,
- askAiQuestion: string | null,
- viewAll: ViewAll | null
+ displayed: { query: SectionsQuery; stale: boolean }
): CommandKSection[] =>
buildSections({
mode,
@@ -637,11 +691,8 @@ export const commandKSearchLogic = kea<commandKSearchLogicType>([
suggestions,
local,
remote,
- remoteQueries,
- localQuery,
- themeItem,
- askAiQuestion,
- viewAll,
+ ...displayed.query,
+ stale: displayed.stale,
}),
],
selectableRows: [
@@ -669,6 +720,12 @@ export const commandKSearchLogic = kea<commandKSearchLogicType>([
],
}),
listeners(({ actions, values, cache }) => {
+ const showSettledQuery = (): void => {
+ if (values.shownQuery !== values.currentQuery && resultsSettled(values.currentQuery, values.remote)) {
+ actions.showQuery(values.currentQuery)
+ }
+ }
+
const trackCommit = (chip: QueryChip, via: CommitVia): void => {
posthog.capture('command k filter committed', {
key: chip.key,
@@ -902,23 +959,20 @@ export const commandKSearchLogic = kea<commandKSearchLogicType>([
)
continue
}
- const debounceMs = request.id === 'objects' ? OBJECTS_DEBOUNCE_MS : REMOTE_DEBOUNCE_MS
+ const debounceMs = FAST_REQUESTS.includes(request.id) ? OBJECTS_DEBOUNCE_MS : REMOTE_DEBOUNCE_MS
// One disposable per request covers the debounce and the fetch, so a newer query
// cancels both, and unmounting cancels everything.
cache.disposables.add(
() => {
const controller = new AbortController()
const timer = setTimeout(() => {
- request
- .fetch(query, controller.signal, values.remoteContext)
- .then((results) => settleSources(request, results))
- // A failed source settles empty so its section never stays on skeletons.
- .catch((error) => (isAbortError(error) ? null : settleSources(request, {})))
- .then((results) => {
- if (results && values.requestedQueries[request.id] === query) {
- actions.remoteLoaded(query, results)
+ void settleRequest(request, query, controller.signal, values.remoteContext).then(
+ (settled) => {
+ if (settled && values.requestedQueries[request.id] === query) {
+ actions.remoteLoaded(query, settled.results, settled.failed)
}
- })
+ }
+ )
}, debounceMs)
return () => {
clearTimeout(timer)
@@ -930,7 +984,9 @@ export const commandKSearchLogic = kea<commandKSearchLogicType>([
{ pauseOnPageHidden: false }
)
}
+ showSettledQuery()
},
+ remoteLoaded: showSettledQuery,
loadFolders: async () => {
if (cache.foldersRequested) {
return
diff --git a/frontend/src/lib/components/CommandKSearch/commandKSections.test.ts b/frontend/src/lib/components/CommandKSearch/commandKSections.test.ts
index d96041b3eda0..e91719e9d9eb 100644
--- a/frontend/src/lib/components/CommandKSearch/commandKSections.test.ts
+++ b/frontend/src/lib/components/CommandKSearch/commandKSections.test.ts
@@ -5,9 +5,11 @@ import {
LocalLists,
RECENTS_LIMIT,
SectionsInput,
+ SectionsQuery,
ViewAll,
buildSections,
emptyRemoteSlots,
+ pickDisplayedQuery,
} from './commandKSections'
import { RESULTS_LIMIT } from './commandKSources'
@@ -48,12 +50,25 @@ const input = (mode: CommandKMode, overrides: Partial<SectionsInput>): SectionsI
themeItem: null,
askAiQuestion: null,
viewAll: null,
+ stale: false,
...overrides,
})
+const query = (overrides: Partial<SectionsQuery>): SectionsQuery => ({
+ remoteQueries: {},
+ localQuery: '',
+ themeItem: null,
+ askAiQuestion: null,
+ viewAll: null,
+ ...overrides,
+})
+
+const sectionKinds = (sections: ReturnType<typeof buildSections>): [string, string[]][] =>
+ sections.map((section) => [section.key, section.rows.map((row) => row.kind)])
+
const remoteResults = (items: SearchItem[]): Partial<SectionsInput> => {
const remote = emptyRemoteSlots()
- remote.results = { cache: { 'type:dashboard': items }, displayedKey: 'type:dashboard' }
+ remote.results = { cache: { 'type:dashboard': items } }
return { remote, remoteQueries: { results: 'type:dashboard' } }
}
@@ -99,7 +114,6 @@ describe('buildSections', () => {
const remote = emptyRemoteSlots()
remote.results = {
cache: { 'type:dashboard revenue': [{ ...dashboard('a', '1'), rankKey: [-1, 0, 0, 0, -1, 0, 2, 0, 0] }] },
- displayedKey: null,
}
const sections = buildSections(
input('search', {
@@ -117,10 +131,7 @@ describe('buildSections', () => {
it('lists filter-only results without ranking them', () => {
const remote = emptyRemoteSlots()
- remote.results = {
- cache: { 'type:dashboard': [{ ...dashboard('a', '1'), rankKey: [] }] },
- displayedKey: 'type:dashboard',
- }
+ remote.results = { cache: { 'type:dashboard': [{ ...dashboard('a', '1'), rankKey: [] }] } }
const sections = buildSections(input('search', { remote, remoteQueries: { results: 'type:dashboard' } }))
expect(sections.find((section) => section.key === 'results')?.rows.map((row) => row.key)).toEqual([
'results/dashboard:1',
@@ -135,9 +146,12 @@ describe('buildSections', () => {
parentName: 'Session replay',
searchKeywords: ['playlists'],
}
+ const remote = emptyRemoteSlots()
+ remote.results = { cache: { playlists: [] } }
const sections = buildSections(
input('search', {
local: { ...emptyLocal(), products: [tab] },
+ remote,
remoteQueries: { results: 'playlists' },
localQuery: 'playlists',
})
@@ -147,15 +161,61 @@ describe('buildSections', () => {
it('shows skeletons instead of unranked filter-only rows while ranked results load', () => {
const remote = emptyRemoteSlots()
- remote.results = { cache: { 'type:dashboard': [dashboard('a', '1')] }, displayedKey: 'type:dashboard' }
+ remote.results = { cache: { 'type:dashboard': [dashboard('a', '1')] } }
+ const current = query({ remoteQueries: { results: 'type:dashboard revenue' }, localQuery: 'revenue' })
+ const shown = query({ remoteQueries: { results: 'type:dashboard' } })
+
+ expect(pickDisplayedQuery(current, shown, remote)).toEqual({ query: current, stale: false })
+ })
+
+ it('shows the current query loading when the last loaded one had nothing to search', () => {
+ const current = query({ remoteQueries: { results: 'type:dashboard' } })
+
+ expect(pickDisplayedQuery(current, query({}), emptyRemoteSlots())).toEqual({ query: current, stale: false })
+ })
+
+ it('keeps the last loaded query on screen until the current one loads', () => {
+ const remote = emptyRemoteSlots()
+ remote.results = { cache: { rev: [dashboard('a', '1')] } }
+ const shown = query({ remoteQueries: { results: 'rev' }, localQuery: 'rev' })
+ const current = query({ remoteQueries: { results: 'reve' }, localQuery: 'reve' })
+
+ expect(pickDisplayedQuery(current, shown, remote)).toEqual({ query: shown, stale: true })
+
+ remote.results = { cache: { ...remote.results.cache, reve: [] } }
+ expect(pickDisplayedQuery(current, shown, remote)).toEqual({ query: current, stale: false })
+ })
+
+ test.each<[string, Partial<Record<'events' | 'persons', SearchItem[]>>, string[]]>([
+ [
+ 'only a loading row below Results while an earlier source loads',
+ { persons: [dashboard('p', '9')] },
+ ['results', 'loading-more'],
+ ],
+ [
+ 'PostHog AI without waiting for slow person searches',
+ { events: [dashboard('e', '8')] },
+ ['results', 'events', 'ask-ai', 'loading-more'],
+ ],
+ [
+ 'persons last, below PostHog AI, once they load',
+ { events: [dashboard('e', '8')], persons: [dashboard('p', '9')] },
+ ['results', 'events', 'ask-ai', 'persons'],
+ ],
+ ])('reveals %s', (_, loaded, expectedKeys) => {
+ const remote = emptyRemoteSlots()
+ remote.results = { cache: { 'type:dashboard': [dashboard('a', '1')] } }
+ for (const [source, items] of Object.entries(loaded)) {
+ remote[source as 'events' | 'persons'] = { cache: { rev: items } }
+ }
const sections = buildSections(
- input('search', { remote, remoteQueries: { results: 'type:dashboard revenue' }, localQuery: 'revenue' })
+ input('search', {
+ remote,
+ remoteQueries: { results: 'type:dashboard', events: 'rev', persons: 'rev' },
+ askAiQuestion: 'rev',
+ })
)
- expect(sections.find((section) => section.key === 'results')?.rows.map((row) => row.kind)).toEqual([
- 'skeleton',
- 'skeleton',
- 'skeleton',
- ])
+ expect(sections.map((section) => section.key)).toEqual(expectedKeys)
})
it('says which words were dropped when a filter is active', () => {
@@ -176,7 +236,6 @@ describe('buildSections', () => {
},
],
},
- displayedKey: 'type:dashboard xyzzy report',
}
const sections = buildSections(
input('search', { remote, remoteQueries: { results: 'type:dashboard xyzzy report' }, localQuery: '' })
@@ -184,20 +243,13 @@ describe('buildSections', () => {
expect(sections.map((section) => section.key)).toContain('notice')
})
- it('shows ranked products while saved objects load', () => {
- const sections = buildSections(
- input('search', {
- local: {
- ...emptyLocal(),
- products: [{ id: 'product-surveys', name: 'Surveys', category: 'products' }],
- },
- remoteQueries: { results: 'surv' },
- localQuery: 'surv',
- })
- )
- expect(sections.map((section) => [section.key, section.rows.map((row) => row.kind)])).toEqual([
- ['best-matches', ['item']],
- ['results', ['skeleton', 'skeleton', 'skeleton']],
- ])
+ it('holds ranked products back until saved objects load, so they never reorder', () => {
+ const local = { ...emptyLocal(), products: [{ id: 'product-surveys', name: 'Surveys', category: 'products' }] }
+ const loading = input('search', { local, remoteQueries: { results: 'surv' }, localQuery: 'surv' })
+ const remote = emptyRemoteSlots()
+ remote.results = { cache: { surv: [] } }
+
+ expect(sectionKinds(buildSections(loading))).toEqual([['results', ['skeleton', 'skeleton', 'skeleton']]])
+ expect(sectionKinds(buildSections({ ...loading, remote }))).toEqual([['best-matches', ['item']]])
})
})
diff --git a/frontend/src/lib/components/CommandKSearch/commandKSections.ts b/frontend/src/lib/components/CommandKSearch/commandKSections.ts
index 5b3d6f580c24..ec4eb00b3d52 100644
--- a/frontend/src/lib/components/CommandKSearch/commandKSections.ts
+++ b/frontend/src/lib/components/CommandKSearch/commandKSections.ts
@@ -10,6 +10,8 @@ export const RECENTS_LIMIT = 5
export const SUGGESTIONS_LIMIT = 8
export const DEFAULT_SKELETON_ROWS = 3
+const TRAILING_SOURCES: RemoteSource[] = ['persons', 'groups']
+
/** Lists that filter in memory on every keystroke, in the order they render in Search mode. */
const LOCAL_SOURCES = ['products', 'data-management', 'people', 'health', 'misc', 'settings', 'create'] as const
type LocalSource = (typeof LOCAL_SOURCES)[number]
@@ -25,6 +27,7 @@ export type CommandKSectionKey =
| RemoteSource
| 'ask-ai'
| 'notice'
+ | 'loading-more'
| 'best-matches'
| `group-${string}`
@@ -47,6 +50,7 @@ export type CommandKRow =
| { kind: 'ask-ai'; key: string; question: string }
| { kind: 'scope'; key: string; type: string; label: string }
| { kind: 'notice'; key: string; reason: 'partial' | 'typos'; missing: string[]; shown: string[] }
+ | { kind: 'loading-more'; key: string }
export interface CommandKSection {
key: CommandKSectionKey
@@ -66,13 +70,13 @@ export interface ViewAll {
/** What a remote section can render. Which request is in flight lives elsewhere, so starting one never re-renders. */
export interface RemoteSlot {
cache: Record<string, SearchItem[]>
- displayedKey: string | null
+ failed?: Record<string, true>
}
export type RemoteSlots = Record<RemoteSource, RemoteSlot>
export const emptyRemoteSlots = (): RemoteSlots =>
- Object.fromEntries(REMOTE_SOURCES.map((source) => [source, { cache: {}, displayedKey: null }])) as RemoteSlots
+ Object.fromEntries(REMOTE_SOURCES.map((source) => [source, { cache: {} }])) as RemoteSlots
const SECTION_LABELS: Record<Exclude<CommandKSectionKey, `group-${string}`>, string> = {
filters: 'Filters',
@@ -97,10 +101,11 @@ const SECTION_LABELS: Record<Exclude<CommandKSectionKey, `group-${string}`>, str
'ask-ai': 'PostHog AI',
'best-matches': 'Best matches',
notice: '',
+ 'loading-more': '',
}
export const isSelectable = (row: CommandKRow): boolean =>
- row.kind !== 'message' && row.kind !== 'skeleton' && row.kind !== 'notice'
+ row.kind !== 'message' && row.kind !== 'skeleton' && row.kind !== 'notice' && row.kind !== 'loading-more'
const itemIdentity = (item: SearchItem): string => {
const type = item.record?.type
@@ -144,25 +149,15 @@ const loadingSection = (key: Exclude<CommandKSectionKey, `group-${string}`>): Co
state: 'loading',
})
-/**
- * A remote section shows the rows for the current query when they are cached, and the previous
- * query's rows while the new ones load. On its very first load, Results reserves skeleton rows. The
- * other sources usually come back empty, so they reserve nothing rather than flash skeletons that
- * then collapse.
- */
-function remoteSection(source: RemoteSource, slot: RemoteSlot, query: string): CommandKSection | null {
+const isSettled = (slot: RemoteSlot, query: string | undefined): boolean => !query || query in slot.cache
+
+function remoteSection(
+ source: RemoteSource,
+ items: SearchItem[],
+ state: CommandKSection['state']
+): CommandKSection | null {
const limit = source === 'results' ? RESULTS_LIMIT : SECONDARY_LIMIT
- const current = slot.cache[query]
- if (current) {
- return current.length > 0 ? readySection(source, itemRows(source, current, limit)) : null
- }
- const displayed = slot.displayedKey !== null ? slot.cache[slot.displayedKey] : undefined
- if (displayed) {
- return displayed.length > 0
- ? { ...readySection(source, itemRows(source, displayed, limit)), state: 'stale' }
- : null
- }
- return source === 'results' ? loadingSection(source) : null
+ return items.length > 0 ? { ...readySection(source, itemRows(source, items, limit)), state } : null
}
export interface SuggestionsLoading {
@@ -210,12 +205,7 @@ export function buildSuggestions(
export type LocalLists = Record<LocalSource | 'recents' | 'starred', SearchItem[]>
-export interface SectionsInput {
- mode: CommandKMode
- context: CursorContext
- suggestions: CommandKSection | null
- local: LocalLists
- remote: RemoteSlots
+export interface SectionsQuery {
/** The query each remote source searches for this input. Sources missing here do not apply. */
remoteQueries: Partial<Record<RemoteSource, string>>
/** The free text the in-memory lists filter on. Empty while a filter limits the palette to objects. */
@@ -226,6 +216,35 @@ export interface SectionsInput {
viewAll: ViewAll | null
}
+export interface SectionsInput extends SectionsQuery {
+ mode: CommandKMode
+ context: CursorContext
+ suggestions: CommandKSection | null
+ local: LocalLists
+ remote: RemoteSlots
+ stale: boolean
+}
+
+const isRanked = (query: SectionsQuery): boolean =>
+ !!query.remoteQueries.results && freeWords(query.remoteQueries.results).length > 0
+
+export function pickDisplayedQuery(
+ current: SectionsQuery,
+ shown: SectionsQuery | null,
+ remote: RemoteSlots
+): { query: SectionsQuery; stale: boolean } {
+ if (!shown?.remoteQueries.results || isSettled(remote.results, current.remoteQueries.results)) {
+ return { query: current, stale: false }
+ }
+ if (isRanked(current) && !isRanked(shown)) {
+ return { query: current, stale: false }
+ }
+ return { query: shown, stale: true }
+}
+
+export const resultsSettled = (query: SectionsQuery, remote: RemoteSlots): boolean =>
+ isSettled(remote.results, query.remoteQueries.results)
+
const filterLocal = (source: LocalSource, items: SearchItem[], query: string): SearchItem[] =>
source === 'create' ? filterNewItems(items, query) : filterSearchItems(items, query)
@@ -242,24 +261,67 @@ function withViewAll(results: CommandKSection | null, viewAll: ViewAll | null):
}
function rankedResultsSections(
- slot: RemoteSlot,
+ results: SearchItem[],
query: string,
local: LocalLists,
- localQuery: string
+ localQuery: string,
+ state: CommandKSection['state']
): CommandKSection[] {
- const current = slot.cache[query]
- const previous = slot.displayedKey !== null ? slot.cache[slot.displayedKey] : undefined
- const displayed = current ?? (previous?.every((item) => item.rankKey?.length) ? previous : undefined)
const words = freeWords(query)
- const ranked = buildRankedResults({
+ return buildRankedResults({
query: words.join(' '),
highlightWords: words,
- results: displayed ?? [],
+ results,
products: localQuery ? local.products : [],
- state: !displayed || current ? 'ready' : 'stale',
+ state,
scoped: (query.match(FILTER_TOKEN) ?? []).some((token) => token.trim().startsWith('type:')),
})
- return displayed ? ranked : [...ranked, loadingSection('results')]
+}
+
+function lowerSections(input: SectionsInput, ranked: boolean, state: CommandKSection['state']): CommandKSection[] {
+ const { local, remote, remoteQueries, localQuery } = input
+ const sections: (CommandKSection | null)[] = []
+ if (localQuery) {
+ for (const source of LOCAL_SOURCES) {
+ if (ranked && source === 'products') {
+ continue
+ }
+ const filtered =
+ ranked && source !== 'create'
+ ? strictLocalMatches(filterLocal(source, local[source], localQuery), localQuery)
+ : filterLocal(source, local[source], localQuery)
+ const themeItem = source === 'settings' ? input.themeItem : null
+ const rows = themeItem
+ ? itemRows(source, [themeItem, ...filtered], SECONDARY_LIMIT + 1)
+ : itemRows(source, filtered, SECONDARY_LIMIT)
+ sections.push(rows.length > 0 ? { ...readySection(source, rows), state } : null)
+ }
+ }
+ const loaded = (): CommandKSection[] => sections.filter((section): section is CommandKSection => section !== null)
+ const loadingMore = (): CommandKSection[] =>
+ state === 'stale'
+ ? loaded()
+ : [...loaded(), readySection('loading-more', [{ kind: 'loading-more', key: 'loading-more/row' }])]
+ const appendInOrder = (sources: RemoteSource[]): boolean => {
+ for (const source of sources) {
+ const query = remoteQueries[source]
+ if (!isSettled(remote[source], query)) {
+ return false
+ }
+ sections.push(query ? remoteSection(source, remote[source].cache[query], state) : null)
+ }
+ return true
+ }
+ if (!appendInOrder(REMOTE_SOURCES.filter((source) => source !== 'results' && !TRAILING_SOURCES.includes(source)))) {
+ return loadingMore()
+ }
+ if (input.askAiQuestion) {
+ sections.push({
+ ...readySection('ask-ai', [{ kind: 'ask-ai', key: 'ask-ai/ask', question: input.askAiQuestion }]),
+ state,
+ })
+ }
+ return appendInOrder(TRAILING_SOURCES) ? loaded() : loadingMore()
}
export function buildSections(input: SectionsInput): CommandKSection[] {
@@ -279,10 +341,7 @@ export function buildSections(input: SectionsInput): CommandKSection[] {
].filter((section) => section.rows.length > 0)
}
- const remoteSectionFor = (source: RemoteSource): CommandKSection | null => {
- const query = remoteQueries[source]
- return query ? remoteSection(source, remote[source], query) : null
- }
+ const state: CommandKSection['state'] = input.stale ? 'stale' : 'ready'
const sections: (CommandKSection | null)[] = []
if (context.kind === 'key') {
sections.push(
@@ -297,11 +356,17 @@ export function buildSections(input: SectionsInput): CommandKSection[] {
)
)
}
- const ranked = !!remoteQueries.results && freeWords(remoteQueries.results).length > 0
- if (ranked) {
- const rankedSections = rankedResultsSections(remote.results, remoteQueries.results ?? '', local, localQuery)
+ sections.push(input.suggestions)
+ const resultsQuery = remoteQueries.results
+ const results = resultsQuery ? remote.results.cache[resultsQuery] : undefined
+ const ranked = isRanked(input)
+ if (resultsQuery && !results) {
+ sections.push(withViewAll(loadingSection('results'), input.viewAll))
+ return sections.filter((section): section is CommandKSection => section !== null)
+ }
+ if (ranked && results && resultsQuery) {
+ const rankedSections = rankedResultsSections(results, resultsQuery, local, localQuery, state)
sections.push(
- input.suggestions,
...(rankedSections.some((section) => section.key === 'results')
? rankedSections.map((section) =>
section.key === 'results' ? withViewAll(section, input.viewAll) : section
@@ -309,31 +374,10 @@ export function buildSections(input: SectionsInput): CommandKSection[] {
: [...rankedSections, withViewAll(null, input.viewAll)])
)
} else {
- sections.push(input.suggestions, withViewAll(remoteSectionFor('results'), input.viewAll))
- }
- if (mode !== 'value' && localQuery) {
- for (const source of LOCAL_SOURCES) {
- if (ranked && source === 'products') {
- continue
- }
- const filtered =
- ranked && source !== 'create'
- ? strictLocalMatches(filterLocal(source, local[source], localQuery), localQuery)
- : filterLocal(source, local[source], localQuery)
- const themeItem = source === 'settings' ? input.themeItem : null
- const rows = themeItem
- ? itemRows(source, [themeItem, ...filtered], SECONDARY_LIMIT + 1)
- : itemRows(source, filtered, SECONDARY_LIMIT)
- sections.push(rows.length > 0 ? readySection(source, rows) : null)
- }
+ sections.push(withViewAll(results ? remoteSection('results', results, state) : null, input.viewAll))
}
if (mode !== 'value') {
- sections.push(...REMOTE_SOURCES.filter((source) => source !== 'results').map(remoteSectionFor))
- if (input.askAiQuestion) {
- sections.push(
- readySection('ask-ai', [{ kind: 'ask-ai', key: 'ask-ai/ask', question: input.askAiQuestion }])
- )
- }
+ sections.push(...lowerSections(input, ranked, state))
}
return sections.filter((section): section is CommandKSection => section !== null)
}
diff --git a/frontend/src/lib/components/CommandKSearch/commandKSources.test.ts b/frontend/src/lib/components/CommandKSearch/commandKSources.test.ts
new file mode 100644
index 000000000000..fe02ac5f23d6
--- /dev/null
+++ b/frontend/src/lib/components/CommandKSearch/commandKSources.test.ts
@@ -0,0 +1,76 @@
+import api from 'lib/api'
+
+import { GroupTypeIndex } from '~/types'
+
+import { REMOTE_REQUESTS, RemoteContext, RemoteRequest, settleRequest } from './commandKSources'
+
+const context: RemoteContext = {
+ currentTeamId: 1,
+ groupTypes: [],
+ aggregationLabel: () => ({ singular: 'organization', plural: 'organizations' }),
+ customerAnalyticsEnabled: false,
+}
+
+const stalled: RemoteRequest = {
+ id: 'persons',
+ query: 'text',
+ sources: ['persons'],
+ fetch: (_query, signal) =>
+ new Promise((_resolve, reject) =>
+ signal.addEventListener('abort', () => reject(new DOMException('Aborted', 'AbortError')))
+ ),
+}
+
+describe('settleRequest', () => {
+ beforeEach(() => jest.useFakeTimers())
+ afterEach(() => jest.useRealTimers())
+
+ test.each([
+ ['settles a stalled request empty at the deadline', false, { results: { persons: [] }, failed: true }],
+ ['drops a request a newer query aborted', true, null],
+ ])('%s', async (_, superseded, expected) => {
+ const controller = new AbortController()
+ const settled = settleRequest(stalled, 'ada', controller.signal, context, 3000)
+
+ if (superseded) {
+ controller.abort()
+ } else {
+ jest.advanceTimersByTime(3000)
+ }
+
+ await expect(settled).resolves.toEqual(expected)
+ })
+
+ it('keeps the group types that answered when the deadline passes', async () => {
+ jest.spyOn(api.groups, 'listClickhouse').mockImplementation((params, options) =>
+ params.group_type_index === 0
+ ? Promise.resolve({
+ columns: ['group_name'],
+ results: [[{ display_name: 'Acme', key: 'acme' }]],
+ } as any)
+ : new Promise((_resolve, reject) =>
+ options?.signal?.addEventListener('abort', () => reject(options.signal?.reason))
+ )
+ )
+ const groups = REMOTE_REQUESTS.find((request) => request.id === 'groups')!
+ const settled = settleRequest(
+ groups,
+ 'acme',
+ new AbortController().signal,
+ {
+ ...context,
+ groupTypes: [0, 1].map((index) => ({
+ group_type: `type-${index}`,
+ group_type_index: index as GroupTypeIndex,
+ })) as RemoteContext['groupTypes'],
+ },
+ 3000
+ )
+
+ await jest.advanceTimersByTimeAsync(3000)
+
+ const result = await settled
+ expect(result?.failed).toBe(true)
+ expect(result?.results.groups?.map((item) => item.id)).toEqual(['group-0-acme'])
+ })
+})
diff --git a/frontend/src/lib/components/CommandKSearch/commandKSources.ts b/frontend/src/lib/components/CommandKSearch/commandKSources.ts
index b94ba32845bc..f69bf0c02a7e 100644
--- a/frontend/src/lib/components/CommandKSearch/commandKSources.ts
+++ b/frontend/src/lib/components/CommandKSearch/commandKSources.ts
@@ -1,4 +1,4 @@
-import api from 'lib/api'
+import api, { isAbortError } from 'lib/api'
import {
SearchItem,
accountToSearchItem,
@@ -27,6 +27,9 @@ import { personsList } from 'products/persons/frontend/generated/api'
import { freeWords } from './commandKRanking'
export const RESULTS_LIMIT = 20
+export const REQUEST_DEADLINE_MS = 3000
+// An AbortError, so the API layer treats it like any cancelled request and reports nothing.
+const DEADLINE_PASSED = new DOMException('The request deadline passed', 'AbortError')
export const SECONDARY_LIMIT = 5
const RANKED_RESULTS_LIMIT = 40
const RANKED_PER_TYPE = 4
@@ -101,7 +104,10 @@ const fetchGroups = async (term: string, signal: AbortSignal, context: RemoteCon
)
)
)
- signal.throwIfAborted()
+ // At the deadline, the group types that already answered still show.
+ if (signal.reason !== DEADLINE_PASSED) {
+ signal.throwIfAborted()
+ }
return {
groups: settled.flatMap((result, index) => {
if (result.status !== 'fulfilled') {
@@ -241,3 +247,30 @@ export const REMOTE_REQUESTS: RemoteRequest[] = [
/** Results for every source of a request, empty where the response held none. */
export const settleSources = (request: RemoteRequest, results: RemoteResults): RemoteResults =>
Object.fromEntries(request.sources.map((source) => [source, results[source] ?? []]))
+
+export function settleRequest(
+ request: RemoteRequest,
+ query: string,
+ signal: AbortSignal,
+ context: RemoteContext,
+ deadlineMs = REQUEST_DEADLINE_MS
+): Promise<{ results: RemoteResults; failed: boolean } | null> {
+ const controller = new AbortController()
+ const abort = (): void => controller.abort()
+ signal.addEventListener('abort', abort, { once: true })
+ const deadline = setTimeout(() => controller.abort(DEADLINE_PASSED), deadlineMs)
+ const timedOut = (): boolean => controller.signal.reason === DEADLINE_PASSED
+ return (
+ request
+ .fetch(query, controller.signal, context)
+ .then((results) => ({ results: settleSources(request, results), failed: timedOut() }))
+ // A failed source settles empty so its section never stays on skeletons.
+ .catch((error) =>
+ isAbortError(error) && !timedOut() ? null : { results: settleSources(request, {}), failed: true }
+ )
+ .finally(() => {
+ clearTimeout(deadline)
+ signal.removeEventListener('abort', abort)
+ })
+ )
+}