: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(logs): keep selected facet values visible in the facet rail (#68627)
Jon McCallum1a99527cf332
diff --git a/products/logs/frontend/components/LogsViewer/FacetRail/FacetRail.tsx b/products/logs/frontend/components/LogsViewer/FacetRail/FacetRail.tsxindex 76248a8ea025..4a12aeadd697 100644--- a/products/logs/frontend/components/LogsViewer/FacetRail/FacetRail.tsx+++ b/products/logs/frontend/components/LogsViewer/FacetRail/FacetRail.tsx@@ -12,7 +12,14 @@ import { logsViewerFiltersLogic } from 'products/logs/frontend/components/LogsViimport { Facet, FacetOption } from './Facet'import { facetCountsLogic } from './facetCountsLogic'import { facetRailLogic } from './facetRailLogic'-import { FacetConfig, FacetFilterKey, facetsByGroup, filterFacetsByName, resourceAttributeValues } from './facets'+import {+ FacetConfig,+ FacetFilterKey,+ facetsByGroup,+ filterFacetsByName,+ mergeSelectedIntoOptions,+ resourceAttributeValues,+} from './facets'const DEFAULT_WIDTH_PX = 240const COLLAPSE_THRESHOLD_PX = 120@@ -95,12 +102,18 @@ export function FacetRail({ id }: FacetRailProps): JSX.Element {)}- // Dynamic facet: values + counts come straight from the cross-filtered endpoint (zeros never appear).+ // Dynamic facet: values + counts come from the cross-filtered endpoint, plus any selected+ // values it didn't return (zero matches in scope, or below the top-N cutoff) so an active+ // filter — e.g. from an old saved-view URL — is always visible and removable.return (<Facetkey={facet.key}title={facet.title}- options={fetched}+ options={mergeSelectedIntoOptions(+ fetched,+ selected,+ facet.searchable ? facetSearch[facet.key] : undefined+ )}selected={selected}onToggle={onToggle}loading={loading}diff --git a/products/logs/frontend/components/LogsViewer/FacetRail/facets.test.ts b/products/logs/frontend/components/LogsViewer/FacetRail/facets.test.tsindex 6f77cc53d9df..0c421b2d601f 100644--- a/products/logs/frontend/components/LogsViewer/FacetRail/facets.test.ts+++ b/products/logs/frontend/components/LogsViewer/FacetRail/facets.test.ts@@ -1,4 +1,5 @@-import { FacetConfig, filterFacetsByName } from './facets'+import { FacetOption } from './Facet'+import { FacetConfig, filterFacetsByName, mergeSelectedIntoOptions } from './facets'const facet = (key: string, title: string, group: string): FacetConfig => ({key,@@ -16,17 +17,50 @@ const FACETS: FacetConfig[] = [facet('host', 'Host', 'Infrastructure'),]-describe('filterFacetsByName', () => {- it.each([- ['blank query returns all', '', ['level', 'service', 'namespace', 'pod', 'host']],- ['whitespace-only returns all', ' ', ['level', 'service', 'namespace', 'pod', 'host']],- ['matches a field title', 'namespace', ['namespace']],- ['title match is case-insensitive', 'NAMESPACE', ['namespace']],- ['partial title match', 'serv', ['service']],- ['matches a whole group by name', 'kubernetes', ['namespace', 'pod']],- ['group match is case-insensitive', 'INFRA', ['host']],- ['no match returns empty', 'zzz', []],- ])('%s', (_, query, expectedKeys) => {- expect(filterFacetsByName(FACETS, query).map((f) => f.key)).toEqual(expectedKeys)+describe('facets', () => {+ describe('filterFacetsByName', () => {+ it.each([+ ['blank query returns all', '', ['level', 'service', 'namespace', 'pod', 'host']],+ ['whitespace-only returns all', ' ', ['level', 'service', 'namespace', 'pod', 'host']],+ ['matches a field title', 'namespace', ['namespace']],+ ['title match is case-insensitive', 'NAMESPACE', ['namespace']],+ ['partial title match', 'serv', ['service']],+ ['matches a whole group by name', 'kubernetes', ['namespace', 'pod']],+ ['group match is case-insensitive', 'INFRA', ['host']],+ ['no match returns empty', 'zzz', []],+ ])('%s', (_, query, expectedKeys) => {+ expect(filterFacetsByName(FACETS, query).map((f) => f.key)).toEqual(expectedKeys)+ })+ })++ describe('mergeSelectedIntoOptions', () => {+ const fetched: FacetOption[] = [+ { value: 'api', label: 'api', count: 10 },+ { value: 'worker', label: 'worker', count: 5 },+ ]++ it.each<[string, string[], string | undefined, string[]]>([+ ['no selected values leaves fetched unchanged', [], undefined, ['api', 'worker']],+ ['selected value already fetched is not duplicated', ['api'], undefined, ['api', 'worker']],+ ['missing selected value is prepended', ['batch-exports'], undefined, ['batch-exports', 'api', 'worker']],+ ['multiple missing values keep selection order', ['b', 'a'], undefined, ['b', 'a', 'api', 'worker']],+ ['missing selected value not matching the search is omitted', ['batch-exports'], 'api', ['api', 'worker']],+ [+ 'search match is a case-insensitive substring',+ ['Batch-Exports'],+ 'batch',+ ['Batch-Exports', 'api', 'worker'],+ ],+ ])('%s', (_, selected, search, expectedValues) => {+ expect(mergeSelectedIntoOptions(fetched, selected, search).map((o) => o.value)).toEqual(expectedValues)+ })++ it('injects missing selected values with a zero count', () => {+ expect(mergeSelectedIntoOptions(fetched, ['batch-exports'], undefined)[0]).toEqual({+ value: 'batch-exports',+ label: 'batch-exports',+ count: 0,+ })+ })})})diff --git a/products/logs/frontend/components/LogsViewer/FacetRail/facets.ts b/products/logs/frontend/components/LogsViewer/FacetRail/facets.tsindex f69359c7755e..561ac4338d1f 100644--- a/products/logs/frontend/components/LogsViewer/FacetRail/facets.ts+++ b/products/logs/frontend/components/LogsViewer/FacetRail/facets.ts@@ -197,6 +197,23 @@ export function filterFacetsByName(facets: FacetConfig[], query: string): FacetC)}+/**+ * Ensure every selected value of a dynamic facet renders even when absent from the fetched list —+ * a filter from a URL or saved view can reference a value with no matches in the current scope+ * (or one below the top-N cutoff), and without a visible row it can't be seen or toggled off.+ * Missing values are prepended with a zero count. An active type-ahead search still applies to+ * injected rows, matching the server-side substring semantics of the fetched ones.+ */+export function mergeSelectedIntoOptions(fetched: FacetOption[], selected: string[], search?: string): FacetOption[] {+ const needle = (search ?? '').trim().toLowerCase()+ const fetchedValues = new Set(fetched.map((option) => option.value))+ const missing = selected+ .filter((value) => !fetchedValues.has(value))+ .filter((value) => !needle || value.toLowerCase().includes(needle))+ .map((value) => ({ value, label: value, count: 0 }))+ return missing.length > 0 ? [...missing, ...fetched] : fetched+}+/** Group facets by `group`, preserving first-appearance order of both groups and facets. */export function facetsByGroup(facets: FacetConfig[]): [string, FacetConfig[]][] {const groups: [string, FacetConfig[]][] = []