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): carry the query into view all list pages (#127113)

Michael Reichenbachd9cabad8cc37
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 80988e0ccc84..c3e258410b08 100644
--- a/frontend/src/lib/components/CommandKSearch/COMMAND_K_SEARCH_UX.md
+++ b/frontend/src/lib/components/CommandKSearch/COMMAND_K_SEARCH_UX.md
@@ -100,6 +100,11 @@ The input is always in exactly one mode, decided from the token under the cursor
5. **Persons**, **Groups**. Person and group searches can take seconds, so they usually come last. The person search starts with the file system search, after 50 ms. A search that contains "@" waits for the person search and ranks the persons it finds with **Results** / **Best matches**. A person who matches every word comes first. Persons that do not rank stay in **Persons** below, with no duplicates. Other searches do not wait for persons or groups.
- 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.
+ - A list page that reads a search from its URL opens with the rest of the query. The type's product manifest names the URL param in `listSearchParam`, and Command K adds the search text to `listHref()`.
+ - Feature flags, experiments, cohorts, surveys, insights, and dashboards take the search text as `?search=`: the `name:` value, then the free text.
+ - These lists match the search text as one phrase, in the order typed. So `?search=` leaves out what a phrase cannot express: excluded words (`-legacy`, `"-legacy"`, `!draft`, a lone `-`), an excluded `name:`, and the quotes that group words (`"checkout v2"`).
+ - `createdBy:` and `in:` do not carry over, because the lists' own filters take user IDs and exact folders, not Command K's values.
+ - Any other type opens its bare list page.
### Ranked results
diff --git a/frontend/src/lib/components/CommandKSearch/commandKFilterOptions.test.ts b/frontend/src/lib/components/CommandKSearch/commandKFilterOptions.test.ts
index 7060a538bd45..e7a0105f5507 100644
--- a/frontend/src/lib/components/CommandKSearch/commandKFilterOptions.test.ts
+++ b/frontend/src/lib/components/CommandKSearch/commandKFilterOptions.test.ts
@@ -1,5 +1,5 @@
import { viewAllFor } from './commandKFilterOptions'
-import { QueryChip, ResolvedQuery } from './commandKQuery'
+import { FilterOptions, QueryChip, ResolvedQuery, resolveQuery } from './commandKQuery'
const chip = (value: string, label: string, overrides: Partial<QueryChip> = {}): QueryChip => ({
key: 'is',
@@ -9,20 +9,129 @@ const chip = (value: string, label: string, overrides: Partial<QueryChip> = {}):
...overrides,
})
-const resolved = (filters: QueryChip[], freeText = ''): ResolvedQuery => ({ filters, freeText, backendSearch: '' })
+const NO_OPTIONS: FilterOptions = { is: [], createdBy: [], in: [], name: [] }
+
+const resolved = (filters: QueryChip[], freeText = ''): ResolvedQuery => resolveQuery(freeText, filters, NO_OPTIONS)
+
+const createdByMe = chip('me', 'Me', { key: 'createdBy' })
describe('viewAllFor', () => {
test.each([
- ['a manifest list page', [chip('feature_flag', 'Feature flag')], 'View all feature flags', '/feature-flags'],
- ['a type with a fallback page', [chip('dashboard', 'Dashboard')], 'View all dashboards', '/dashboard'],
+ [
+ 'a manifest list page',
+ [chip('feature_flag', 'Feature flag')],
+ '',
+ 'View all feature flags',
+ '/feature-flags',
+ ],
+ ['a type with a fallback page', [chip('dashboard', 'Dashboard')], '', 'View all dashboards', '/dashboard'],
[
'other filters alongside',
- [chip('feature_flag', 'Feature flag'), chip('me', 'Me', { key: 'createdBy' })],
+ [chip('feature_flag', 'Feature flag'), createdByMe],
+ '',
'View all feature flags',
'/feature-flags',
],
- ])('names the list page for %s', (_, chips, name, href) => {
- expect(viewAllFor(resolved(chips))?.item).toMatchObject({ name, href })
+ [
+ 'a list that searches the text but not the filters',
+ [chip('feature_flag', 'Feature flag'), createdByMe],
+ 'checkout v2',
+ 'View all feature flags',
+ '/feature-flags?search=checkout%20v2',
+ ],
+ [
+ 'a list that searches quoted text without its quotes',
+ [chip('feature_flag', 'Feature flag')],
+ '"checkout v2" beta',
+ 'View all feature flags',
+ '/feature-flags?search=checkout%20v2%20beta',
+ ],
+ [
+ 'a list that searches a word quoted midway without its quotes',
+ [chip('feature_flag', 'Feature flag')],
+ 'checkout" v2" beta',
+ 'View all feature flags',
+ '/feature-flags?search=checkout%20v2%20beta',
+ ],
+ [
+ 'a list that searches without the excluded words',
+ [chip('feature_flag', 'Feature flag')],
+ 'beta -legacy -"old flow" !draft - "" "-wip" "-"',
+ 'View all feature flags',
+ '/feature-flags?search=beta',
+ ],
+ [
+ 'a list that searches without single-quoted excluded words',
+ [chip('feature_flag', 'Feature flag')],
+ "checkout '-legacy' 'v2 beta'",
+ 'View all feature flags',
+ '/feature-flags?search=checkout%20v2%20beta',
+ ],
+ [
+ 'a list that keeps an apostrophe the search reads as a letter',
+ [chip('feature_flag', 'Feature flag')],
+ "don't -legacy",
+ 'View all feature flags',
+ "/feature-flags?search=don't",
+ ],
+ [
+ 'a list that keeps an unclosed double quote the search reads as a letter',
+ [chip('feature_flag', 'Feature flag')],
+ 'a"b -legacy',
+ 'View all feature flags',
+ '/feature-flags?search=a%22b',
+ ],
+ [
+ 'a list that searches the name filter before the text',
+ [chip('feature_flag', 'Feature flag'), chip('checkout', 'checkout', { key: 'name' })],
+ 'beta',
+ 'View all feature flags',
+ '/feature-flags?search=checkout%20beta',
+ ],
+ [
+ 'a list that searches a single-quoted name without its quotes',
+ [chip('feature_flag', 'Feature flag')],
+ "name:'checkout' beta",
+ 'View all feature flags',
+ '/feature-flags?search=checkout%20beta',
+ ],
+ [
+ 'a list that reads every quote as a letter once one quote in the query is unmatched',
+ [chip('feature_flag', 'Feature flag')],
+ "name:rock'n'roll -don't",
+ 'View all feature flags',
+ "/feature-flags?search=rock'n'roll",
+ ],
+ [
+ 'a list that reads the text as a letter when the name holds the unmatched quote',
+ [chip('feature_flag', 'Feature flag')],
+ "name:don't rock'n'roll",
+ 'View all feature flags',
+ "/feature-flags?search=don't%20rock'n'roll",
+ ],
+ [
+ 'a list that searches without an excluded name',
+ [chip('feature_flag', 'Feature flag'), chip('checkout', 'checkout', { key: 'name', negated: true })],
+ '',
+ 'View all feature flags',
+ '/feature-flags',
+ ],
+ [
+ 'a fallback page that searches the text',
+ [chip('dashboard', 'Dashboard')],
+ 'revenue',
+ 'View all dashboards',
+ '/dashboard?search=revenue',
+ ],
+ [
+ 'a list that reads no query',
+ [chip('action', 'Action'), createdByMe],
+ 'signup',
+ 'View all actions',
+ '/data-management/actions',
+ ],
+ ])('names the list page for %s', (_, chips, freeText, name, href) => {
+ expect(viewAllFor(resolved(chips, freeText))?.item).toMatchObject({ name, href })
})
test.each([
@@ -33,7 +142,7 @@ describe('viewAllFor', () => {
})
test.each([
- ['no is chip', [chip('me', 'Me', { key: 'createdBy' })]],
+ ['no is chip', [createdByMe]],
['a negated is chip', [chip('feature_flag', 'Feature flag', { negated: true })]],
['a type with no list page', [chip('folder', 'Folder')]],
])('returns nothing for %s', (_, chips) => {
diff --git a/frontend/src/lib/components/CommandKSearch/commandKFilterOptions.tsx b/frontend/src/lib/components/CommandKSearch/commandKFilterOptions.tsx
index 25eca40415c8..1594e621a19f 100644
--- a/frontend/src/lib/components/CommandKSearch/commandKFilterOptions.tsx
+++ b/frontend/src/lib/components/CommandKSearch/commandKFilterOptions.tsx
@@ -1,3 +1,5 @@
+import { combineUrl } from 'kea-router'
+
import { IconArrowRight } from '@posthog/icons'
import { SearchItem } from 'lib/components/Search/searchItems'
@@ -8,7 +10,7 @@ import { fileSystemTypes } from '~/products'
import { FileSystemEntry } from '~/queries/schema/schema-general'
import { OrganizationMemberType, UserType } from '~/types'
-import { FilterOptions, ResolvedQuery, ValueOption } from './commandKQuery'
+import { FilterOptions, ResolvedQuery, ValueOption, listSearchText } from './commandKQuery'
import type { ViewAll } from './commandKSections'
/** Short words people type for a type, on top of its name and key. */
@@ -24,6 +26,7 @@ interface FileSystemTypeDefinition {
name?: string
iconType?: string
listHref?: () => string
+ listSearchParam?: 'search'
}
const FILE_SYSTEM_TYPES = fileSystemTypes as Record<string, FileSystemTypeDefinition>
@@ -97,22 +100,44 @@ export function buildFilterOptions(
}
}
-/** List pages for types whose manifest registers no `listHref`. */
-const LIST_HREF_FALLBACKS: Record<string, string> = {
- dashboard: '/dashboard',
- notebook: '/notebooks',
+interface ListPage {
+ href: string
+ searchParam?: 'search'
+}
+
+/** List pages for types whose manifest registers no `listHref`. A manifest `listHref` also makes Library redirect the type to that page. */
+const LIST_PAGE_FALLBACKS: Record<string, ListPage> = {
+ dashboard: { href: '/dashboard', searchParam: 'search' },
+ notebook: { href: '/notebooks' },
+}
+
+function listPageFor(type: string): ListPage | null {
+ const definition = typeDefinition(type)
+ if (definition?.listHref) {
+ return { href: definition.listHref(), searchParam: definition.listSearchParam }
+ }
+ return LIST_PAGE_FALLBACKS[type] ?? null
+}
+
+function listHrefFor({ href, searchParam }: ListPage, resolved: ResolvedQuery): string {
+ if (!searchParam) {
+ return href
+ }
+ const search = listSearchText(resolved)
+ return search ? combineUrl(href, { [searchParam]: search }).url : href
}
// With only filters the list page is the likely target; once there is search text, the matches matter more.
-export function viewAllFor({ filters, freeText }: ResolvedQuery): ViewAll | null {
- const chip = filters.find((candidate) => candidate.key === 'is' && !candidate.negated)
+export function viewAllFor(resolved: ResolvedQuery): ViewAll | null {
+ const chip = resolved.filters.find((candidate) => candidate.key === 'is' && !candidate.negated)
if (!chip) {
return null
}
- const href = typeDefinition(chip.value)?.listHref?.() ?? LIST_HREF_FALLBACKS[chip.value]
- if (!href) {
+ const listPage = listPageFor(chip.value)
+ if (!listPage) {
return null
}
+ const href = listHrefFor(listPage, resolved)
const name = `View all ${pluralTypeName(chip.label)}`
const item: SearchItem = {
id: `view-all-${chip.value}`,
@@ -123,5 +148,5 @@ export function viewAllFor({ filters, freeText }: ResolvedQuery): ViewAll | null
icon: <IconArrowRight className="size-4 shrink-0 text-muted-foreground" />,
record: { type: chip.value },
}
- return { item, placement: freeText.trim() ? 'trail' : 'lead' }
+ return { item, placement: resolved.freeText.trim() ? 'trail' : 'lead' }
}
diff --git a/frontend/src/lib/components/CommandKSearch/commandKQuery.ts b/frontend/src/lib/components/CommandKSearch/commandKQuery.ts
index 794322080e7b..ec2648b67ffa 100644
--- a/frontend/src/lib/components/CommandKSearch/commandKQuery.ts
+++ b/frontend/src/lib/components/CommandKSearch/commandKQuery.ts
@@ -335,6 +335,51 @@ export interface ResolvedQuery {
backendSearch: string
}
+function shellWords(text: string, quotes: string): string[] | null {
+ const words: string[] = []
+ let word: string | null = null
+ let quote: string | null = null
+ for (let i = 0; i < text.length; i++) {
+ const char = text[i]
+ if (quote) {
+ if (char === quote) {
+ quote = null
+ } else if (char === '\\' && quote === '"' && (text[i + 1] === '"' || text[i + 1] === '\\')) {
+ word += text[++i]
+ } else {
+ word += char
+ }
+ } else if (/\s/.test(char)) {
+ if (word !== null) {
+ words.push(word)
+ word = null
+ }
+ } else if (quotes.includes(char)) {
+ word ??= ''
+ quote = char
+ } else if (char === '\\') {
+ if (i + 1 === text.length) {
+ return null
+ }
+ word = (word ?? '') + text[++i]
+ } else {
+ word = (word ?? '') + char
+ }
+ }
+ if (quote) {
+ return null
+ }
+ if (word !== null) {
+ words.push(word)
+ }
+ return words
+}
+
+// Mirrors `tokenize_search` in posthog/api/file_system/file_system.py, which reads every `'` as a letter once one is unmatched.
+function tokenizeSearch(text: string): string[] {
+ return shellWords(text, `"'`) ?? shellWords(text, '"') ?? text.split(/\s+/).filter(Boolean)
+}
+
/**
* Resolves the input once per change, so every consumer agrees on what applies. A typed value
* previews what committing it would do, so `createdBy:Ca` narrows results before it is committed.
@@ -363,6 +408,32 @@ export function resolveQuery(text: string, chips: QueryChip[], options: FilterOp
}
}
+const isQuotedBy = (text: string, quote: string): boolean => text.startsWith(quote) && text.endsWith(quote)
+const stripQuotes = (text: string): string =>
+ isQuotedBy(text, '"') || isQuotedBy(text, "'") ? text.slice(1, -1) : text
+
+const BACKEND_FILTER_FIELDS = new Set(['path', 'name', 'user', 'author', 'type', 'ref'])
+
+// Reads `backendSearch` like the file system search does, so the list searches the words Cmd+K matched.
+// List pages match one phrase, which cannot exclude, so excluded words and filters other than `name:` stay out.
+export function listSearchText({ backendSearch }: ResolvedQuery): string {
+ const words: string[] = []
+ for (const raw of tokenizeSearch(backendSearch)) {
+ if (raw.startsWith('-') || raw.startsWith('!')) {
+ continue
+ }
+ const token = stripQuotes(raw)
+ const colonIndex = token.indexOf(':')
+ const field = colonIndex === -1 ? null : token.slice(0, colonIndex).toLowerCase()
+ if (field === 'name') {
+ words.push(stripQuotes(token.slice(colonIndex + 1).trim()))
+ } else if (field === null || !BACKEND_FILTER_FIELDS.has(field)) {
+ words.push(token)
+ }
+ }
+ return words.filter(Boolean).join(' ')
+}
+
/** The raw query as a person would type it, chips included. Used when copying the input. */
export function queryAsText(text: string, chips: QueryChip[]): string {
return [...chips.map(chipToText), text.trim()].filter(Boolean).join(' ')
diff --git a/frontend/src/products.tsx b/frontend/src/products.tsx
index a48700606f34..f0a35c0a010c 100644
--- a/frontend/src/products.tsx
+++ b/frontend/src/products.tsx
@@ -2077,6 +2077,7 @@ export const fileSystemTypes = {
iconColor: ['var(--color-product-cohorts-light)', 'var(--color-product-cohorts-dark)'] as FileSystemIconColor,
href: (ref: string) => urls.cohort(ref),
listHref: () => urls.cohorts(),
+ listSearchParam: 'search',
filterKey: 'cohort',
},
dashboard: {
@@ -2109,6 +2110,7 @@ export const fileSystemTypes = {
iconType: 'experiment',
href: (ref: string) => urls.experiment(ref),
listHref: () => urls.experiments(),
+ listSearchParam: 'search',
iconColor: ['var(--color-product-experiments-light)'],
filterKey: 'experiment',
},
@@ -2117,6 +2119,7 @@ export const fileSystemTypes = {
iconType: 'feature_flag',
href: (ref: string) => urls.featureFlag(ref),
listHref: () => urls.featureFlags(),
+ listSearchParam: 'search',
iconColor: ['var(--color-product-feature-flags-light)'],
filterKey: 'feature_flag',
},
@@ -2125,6 +2128,7 @@ export const fileSystemTypes = {
iconType: 'product_analytics',
href: (ref: string) => urls.insightView(ref as InsightShortId),
listHref: () => urls.savedInsights(),
+ listSearchParam: 'search',
iconColor: ['var(--color-product-product-analytics-light)'],
filterKey: 'insight',
},
@@ -2162,6 +2166,7 @@ export const fileSystemTypes = {
iconType: 'survey',
href: (ref: string) => urls.survey(ref),
listHref: () => urls.surveys(),
+ listSearchParam: 'search',
iconColor: ['var(--color-product-surveys-light)'],
filterKey: 'survey',
},
diff --git a/frontend/src/types.ts b/frontend/src/types.ts
index 2307de9be1df..67ef514f7bee 100644
--- a/frontend/src/types.ts
+++ b/frontend/src/types.ts
@@ -7910,6 +7910,8 @@ export interface FileSystemType {
href?: (ref: string) => string
// The product's own list page, which Library opens for this type instead of its generic table
listHref?: () => string
+ // The URL param the list page reads its search text from, so Command K's "View all" row can carry the search over
+ listSearchParam?: 'search'
// Visual name of the product
name: string
// Flag to determine if the product is enabled
diff --git a/products/cohorts/manifest.tsx b/products/cohorts/manifest.tsx
index e054b89a6506..ee184ff3b350 100644
--- a/products/cohorts/manifest.tsx
+++ b/products/cohorts/manifest.tsx
@@ -37,6 +37,7 @@ export const manifest: ProductManifest = {
] as FileSystemIconColor,
href: (ref: string) => urls.cohort(ref),
listHref: () => urls.cohorts(),
+ listSearchParam: 'search',
filterKey: 'cohort',
},
},
diff --git a/products/experiments/manifest.tsx b/products/experiments/manifest.tsx
index 8cb8fe2d587c..4a72067de0d8 100644
--- a/products/experiments/manifest.tsx
+++ b/products/experiments/manifest.tsx
@@ -59,6 +59,7 @@ export const manifest: ProductManifest = {
iconType: 'experiment',
href: (ref: string) => urls.experiment(ref),
listHref: () => urls.experiments(),
+ listSearchParam: 'search',
iconColor: ['var(--color-product-experiments-light)'],
filterKey: 'experiment',
},
diff --git a/products/feature_flags/manifest.tsx b/products/feature_flags/manifest.tsx
index e8a64b9f5320..c3b2cd0d2d09 100644
--- a/products/feature_flags/manifest.tsx
+++ b/products/feature_flags/manifest.tsx
@@ -87,6 +87,7 @@ export const manifest: ProductManifest = {
iconType: 'feature_flag',
href: (ref: string) => urls.featureFlag(ref),
listHref: () => urls.featureFlags(),
+ listSearchParam: 'search',
iconColor: ['var(--color-product-feature-flags-light)'],
filterKey: 'feature_flag',
},
diff --git a/products/product_analytics/manifest.tsx b/products/product_analytics/manifest.tsx
index ceb1c2deb7ae..7a7225bbb7c3 100644
--- a/products/product_analytics/manifest.tsx
+++ b/products/product_analytics/manifest.tsx
@@ -118,6 +118,7 @@ export const manifest: ProductManifest = {
iconType: 'product_analytics',
href: (ref: string) => urls.insightView(ref as InsightShortId),
listHref: () => urls.savedInsights(),
+ listSearchParam: 'search',
iconColor: ['var(--color-product-product-analytics-light)'],
filterKey: 'insight',
},
diff --git a/products/surveys/manifest.tsx b/products/surveys/manifest.tsx
index b06ee563c9bf..f4d6816747aa 100644
--- a/products/surveys/manifest.tsx
+++ b/products/surveys/manifest.tsx
@@ -25,6 +25,7 @@ export const manifest: ProductManifest = {
iconType: 'survey',
href: (ref: string) => urls.survey(ref),
listHref: () => urls.surveys(),
+ listSearchParam: 'search',
iconColor: ['var(--color-product-surveys-light)'],
filterKey: 'survey',
},