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: Guard against `group` being undefined (#64906)

Luke Beltonad5ecb07dd3a
diff --git a/frontend/src/lib/components/ObjectTags/ObjectTags.tsx b/frontend/src/lib/components/ObjectTags/ObjectTags.tsx
index ee1355268a82..b396c2e92956 100644
--- a/frontend/src/lib/components/ObjectTags/ObjectTags.tsx
+++ b/frontend/src/lib/components/ObjectTags/ObjectTags.tsx
@@ -1,6 +1,6 @@
import clsx from 'clsx'
import { useActions, useValues } from 'kea'
-import { ComponentProps, CSSProperties, useMemo } from 'react'
+import { ComponentProps, CSSProperties, useId } from 'react'
import { IconPencil, IconPlus } from '@posthog/icons'
import { LemonInputSelect, LemonTag, LemonTagType } from '@posthog/lemon-ui'
@@ -42,8 +42,6 @@ const COLOR_OVERRIDES: Record<string, LemonTagType> = {
deprecated: 'danger',
}
-let uniqueMemoizedIndex = 1
-
export function ObjectTags({
tags,
onChange, // Required unless `staticOnly`
@@ -56,7 +54,7 @@ export function ObjectTags({
actionButtonSize = 'small',
'data-attr': dataAttr,
}: ObjectTagsProps): JSX.Element {
- const objectTagId = useMemo(() => uniqueMemoizedIndex++, [])
+ const objectTagId = useId()
const logic = objectTagsLogic({ id: objectTagId, onChange })
const { editingTags } = useValues(logic)
const { setEditingTags, setTags } = useActions(logic)
diff --git a/frontend/src/lib/components/ObjectTags/objectTagsLogic.ts b/frontend/src/lib/components/ObjectTags/objectTagsLogic.ts
index 7d8e42f4716b..9e1d32d792c6 100644
--- a/frontend/src/lib/components/ObjectTags/objectTagsLogic.ts
+++ b/frontend/src/lib/components/ObjectTags/objectTagsLogic.ts
@@ -5,7 +5,7 @@ import { uniqueBy } from 'lib/utils/arrays'
import type { objectTagsLogicType } from './objectTagsLogicType'
export interface ObjectTagsLogicProps {
- id: number
+ id: string | number
onChange?: (tags: string[]) => void
}
diff --git a/frontend/src/lib/components/PropertyFilters/components/PropertyFilterBetween.tsx b/frontend/src/lib/components/PropertyFilters/components/PropertyFilterBetween.tsx
index d4e10e6bb52a..dfab3eade248 100644
--- a/frontend/src/lib/components/PropertyFilters/components/PropertyFilterBetween.tsx
+++ b/frontend/src/lib/components/PropertyFilters/components/PropertyFilterBetween.tsx
@@ -1,5 +1,5 @@
import { useActions, useValues } from 'kea'
-import { useMemo } from 'react'
+import { useId } from 'react'
import { LemonInput } from 'lib/lemon-ui/LemonInput/LemonInput'
@@ -14,11 +14,11 @@ export interface PropertyFilterBetweenProps {
size?: 'xsmall' | 'small' | 'medium'
}
-let uniqueMemoizedIndex = 0
const usePropertyFilterBetweenLogic = (
props: PropertyFilterBetweenProps
): ReturnType<typeof propertyFilterBetweenLogic.build> => {
- const logicKey = useMemo(() => props.logicKey || `prop-filter-between-${uniqueMemoizedIndex++}`, [props.logicKey])
+ const generatedKey = useId()
+ const logicKey = props.logicKey || `prop-filter-between-${generatedKey}`
return propertyFilterBetweenLogic({ ...props, key: logicKey })
}
diff --git a/frontend/src/lib/components/PropertyFilters/components/TaxonomicPropertyFilter.tsx b/frontend/src/lib/components/PropertyFilters/components/TaxonomicPropertyFilter.tsx
index a46f9c65d02f..aea708cef488 100644
--- a/frontend/src/lib/components/PropertyFilters/components/TaxonomicPropertyFilter.tsx
+++ b/frontend/src/lib/components/PropertyFilters/components/TaxonomicPropertyFilter.tsx
@@ -2,7 +2,7 @@ import './TaxonomicPropertyFilter.scss'
import clsx from 'clsx'
import { useActions, useValues } from 'kea'
-import { useMemo } from 'react'
+import { useId } from 'react'
import { IconPlusSmall } from '@posthog/icons'
import { LemonButton, LemonDropdown, Link } from '@posthog/lemon-ui'
@@ -49,7 +49,6 @@ import { joinsLogic } from 'products/data_warehouse/frontend/shared/logics/joins
import { OperandTag } from './OperandTag'
import { taxonomicPropertyFilterLogic } from './taxonomicPropertyFilterLogic'
-let uniqueMemoizedIndex = 0
export const DEFAULT_TAXONOMIC_GROUP_TYPES = [
TaxonomicFilterGroupType.EventProperties,
TaxonomicFilterGroupType.PersonProperties,
@@ -90,7 +89,8 @@ export function TaxonomicPropertyFilter({
hogQLGlobals,
triggerVariant = 'button',
}: PropertyFilterInternalProps): JSX.Element {
- const pageKey = useMemo(() => pageKeyInput || `filter-${uniqueMemoizedIndex++}`, [pageKeyInput])
+ const generatedKey = useId()
+ const pageKey = pageKeyInput || `filter-${generatedKey}`
const baseGroupTypes = taxonomicGroupTypes || DEFAULT_TAXONOMIC_GROUP_TYPES
const groupTypes = [TaxonomicFilterGroupType.SuggestedFilters, ...baseGroupTypes]
const taxonomicOnChange: (group: TaxonomicFilterGroup, value: TaxonomicFilterValue, item: any) => void = (
diff --git a/frontend/src/lib/components/TaxonomicFilter/InfiniteList.tsx b/frontend/src/lib/components/TaxonomicFilter/InfiniteList.tsx
index 4595b735eb71..16c5fc3cc1d2 100644
--- a/frontend/src/lib/components/TaxonomicFilter/InfiniteList.tsx
+++ b/frontend/src/lib/components/TaxonomicFilter/InfiniteList.tsx
@@ -331,7 +331,7 @@ const canSelectItem = (
interface InfiniteListRowProps {
results: (TaxonomicDefinitionTypes | SkeletonItem)[]
taxonomicGroups: TaxonomicFilterGroup[]
- group: TaxonomicFilterGroup
+ group: TaxonomicFilterGroup | undefined
listGroupType: TaxonomicFilterGroupType
groupType: TaxonomicFilterGroupType | undefined
value: string | number | null | undefined
@@ -472,6 +472,9 @@ export const InfiniteListRow = ({
if (showNonCapturedEventOption && rowIndex === 0) {
const selectNonCapturedEvent = (): void => {
+ if (!itemGroup) {
+ return
+ }
selectItem(
itemGroup,
trimmedSearchQuery,
@@ -544,7 +547,7 @@ export const InfiniteListRow = ({
if (item && itemGroup) {
const isDisabledItem = itemGroup?.getIsDisabled?.(item) ?? false
const isPinnable = !canSelectItem(listGroupType, dataWarehousePopoverFields) && !isDisabledItem
- const isCrossGroupItem = !!group.isLocalOnly && itemGroup.type !== listGroupType
+ const isCrossGroupItem = !!group?.isLocalOnly && itemGroup.type !== listGroupType
const localListLabel = getLocalListLabel(item)
const localListGroup = hasLocalListContext(item)
? taxonomicGroups.find((g) => g.type === listGroupType)
@@ -562,7 +565,7 @@ export const InfiniteListRow = ({
listGroupType,
isCrossGroupItem,
localListGroup,
- fallbackGroup: group,
+ fallbackGroup: group ?? itemGroup,
})
return (
@@ -623,7 +626,7 @@ export const InfiniteListRow = ({
aria-label="Show more items"
onClick={expand}
>
- {group.expandLabel?.({ count: totalResultCount, expandedCount }) ??
+ {group?.expandLabel?.({ count: totalResultCount, expandedCount }) ??
`See ${expandedCount - totalResultCount} more ${pluralize(
expandedCount - totalResultCount,
'row',
@@ -857,6 +860,7 @@ export function InfiniteList({ popupAnchorElement, definitionPopoverRenderer }:
/>
)}
{isActiveTab &&
+ selectedItemGroup &&
selectedItemHasPopover(selectedItem, selectedItemGroup, taxonomicGroups) &&
showPopover &&
selectedItem ? (
@@ -992,8 +996,8 @@ function resolveItemRendering({
export function getItemGroup(
item: TaxonomicDefinitionTypes | undefined,
groups: TaxonomicFilterGroup[],
- defaultGroup: TaxonomicFilterGroup
-): TaxonomicFilterGroup {
+ defaultGroup: TaxonomicFilterGroup | undefined
+): TaxonomicFilterGroup | undefined {
let group = defaultGroup
const sourceType = item ? getSourceGroupType(item) : undefined
diff --git a/frontend/src/lib/components/TaxonomicFilter/TaxonomicFilter.tsx b/frontend/src/lib/components/TaxonomicFilter/TaxonomicFilter.tsx
index 8170bf2053dd..edaf377e34e9 100644
--- a/frontend/src/lib/components/TaxonomicFilter/TaxonomicFilter.tsx
+++ b/frontend/src/lib/components/TaxonomicFilter/TaxonomicFilter.tsx
@@ -2,7 +2,7 @@ import './TaxonomicFilter.scss'
import clsx from 'clsx'
import { BindLogic, useActions, useValues } from 'kea'
-import { forwardRef, useEffect, useMemo, useRef, useState } from 'react'
+import { forwardRef, useEffect, useId, useRef, useState } from 'react'
import { Link } from '@posthog/lemon-ui'
@@ -24,8 +24,6 @@ import { CategoryDropdown } from './CategoryDropdown'
import { InfiniteSelectResults } from './InfiniteSelectResults'
import { defaultDataWarehousePopoverFields, taxonomicFilterLogic } from './taxonomicFilterLogic'
-let uniqueMemoizedIndex = 0
-
export function TaxonomicFilter({
taxonomicFilterLogicKey: taxonomicFilterLogicKeyInput,
groupType,
@@ -63,11 +61,8 @@ export function TaxonomicFilter({
selectingKeyOnly,
collapseUrlsToContainsRow,
}: TaxonomicFilterProps): JSX.Element {
- // Generate a unique key for each unique TaxonomicFilter that's rendered
- const taxonomicFilterLogicKey = useMemo(
- () => taxonomicFilterLogicKeyInput || `taxonomic-filter-${uniqueMemoizedIndex++}`,
- [taxonomicFilterLogicKeyInput]
- )
+ const generatedKey = useId()
+ const taxonomicFilterLogicKey = taxonomicFilterLogicKeyInput || `taxonomic-filter-${generatedKey}`
const searchInputRef = useRef<HTMLInputElement | null>(null)
const focusInput = (): void => searchInputRef.current?.focus()
diff --git a/frontend/src/lib/components/TaxonomicFilter/infiniteListLogic.test.ts b/frontend/src/lib/components/TaxonomicFilter/infiniteListLogic.test.ts
index ce9c875620db..3f0015b5b98c 100644
--- a/frontend/src/lib/components/TaxonomicFilter/infiniteListLogic.test.ts
+++ b/frontend/src/lib/components/TaxonomicFilter/infiniteListLogic.test.ts
@@ -457,6 +457,31 @@ describe('infiniteListLogic', () => {
})
})
+ describe('listGroupType with no matching built group', () => {
+ let noGroupLogic: ReturnType<typeof infiniteListLogic.build>
+ // A group-analytics index with no matching group type mapping, so `group` resolves to undefined.
+ const groupsListType = `${TaxonomicFilterGroupType.GroupsPrefix}_55` as TaxonomicFilterGroupType
+
+ beforeEach(() => {
+ noGroupLogic = infiniteListLogic({
+ taxonomicFilterLogicKey: 'testListNoGroup',
+ listGroupType: groupsListType,
+ taxonomicGroupTypes: [groupsListType],
+ showNumericalPropsOnly: false,
+ })
+ noGroupLogic.mount()
+ })
+
+ it('returns empty localItems instead of throwing', async () => {
+ await expectLogic(noGroupLogic)
+ .toFinishAllListeners()
+ .toMatchValues({
+ group: undefined,
+ localItems: partial({ count: 0, results: [] }),
+ })
+ })
+ })
+
describe('data warehouse pin lifecycle', () => {
beforeEach(() => {
const databaseLogic = databaseTableListLogic()
diff --git a/frontend/src/lib/components/TaxonomicFilter/infiniteListLogic.ts b/frontend/src/lib/components/TaxonomicFilter/infiniteListLogic.ts
index 29a323ba731e..ec87fcbae997 100644
--- a/frontend/src/lib/components/TaxonomicFilter/infiniteListLogic.ts
+++ b/frontend/src/lib/components/TaxonomicFilter/infiniteListLogic.ts
@@ -110,7 +110,7 @@ export function getInitialPinnedRowIndex({
}: {
results: (TaxonomicDefinitionTypes | SkeletonItem)[]
taxonomicGroups: TaxonomicFilterGroup[]
- group: TaxonomicFilterGroup
+ group: TaxonomicFilterGroup | undefined
listGroupType: TaxonomicFilterGroupType
groupType: TaxonomicFilterGroupType | undefined
value: string | number | null | undefined
@@ -485,8 +485,8 @@ export const infiniteListLogic = kea<infiniteListLogicType>([
isLoading: [(s) => [s.remoteItemsLoading], (remoteItemsLoading) => remoteItemsLoading],
group: [
(s) => [s.listGroupType, s.taxonomicGroups],
- (listGroupType, taxonomicGroups): TaxonomicFilterGroup =>
- taxonomicGroups.find((g) => g.type === listGroupType) as TaxonomicFilterGroup,
+ (listGroupType, taxonomicGroups): TaxonomicFilterGroup | undefined =>
+ taxonomicGroups.find((g) => g.type === listGroupType),
],
remoteEndpoint: [(s) => [s.group], (group) => group?.endpoint || null],
minSearchQueryLength: [
@@ -646,7 +646,7 @@ export const infiniteListLogic = kea<infiniteListLogicType>([
// maps e.g. "selector" to its display value "CSS Selector"
// so a search of "css" matches something
function asPostHogName(
- g: TaxonomicFilterGroup,
+ g: TaxonomicFilterGroup | undefined,
item: EventDefinition | CohortType
): string | undefined {
return g ? getCoreFilterDefinition(g.getName?.(item), g.type)?.label : undefined
@@ -675,7 +675,10 @@ export const infiniteListLogic = kea<infiniteListLogicType>([
localItems: [
(s) => [s.rawLocalItems, s.searchQuery, s.fuse, s.group],
(rawLocalItems, searchQuery, fuse, group): ListStorage => {
- if (group?.localItemsSearch) {
+ if (!group) {
+ return createEmptyListStorage()
+ }
+ if (group.localItemsSearch) {
const filtered = group.localItemsSearch(rawLocalItems || [], searchQuery)
return {
results: filtered,
@@ -1089,8 +1092,8 @@ export const infiniteListLogic = kea<infiniteListLogicType>([
const itemGroup = getItemGroup(selectedItem, values.taxonomicGroups, values.group)
const isDisabledItem = selectedItem && itemGroup?.getIsDisabled?.(selectedItem)
- if (!isDisabledItem) {
- const itemValue = selectedItem ? itemGroup?.getValue?.(selectedItem) : null
+ if (!isDisabledItem && itemGroup) {
+ const itemValue = selectedItem ? itemGroup.getValue?.(selectedItem) : null
actions.selectItem(itemGroup, itemValue ?? null, selectedItem, {
position: values.index,
})
diff --git a/frontend/src/lib/components/TaxonomicPopover/TaxonomicPopover.tsx b/frontend/src/lib/components/TaxonomicPopover/TaxonomicPopover.tsx
index 847446ee6628..2273f9a7c76e 100644
--- a/frontend/src/lib/components/TaxonomicPopover/TaxonomicPopover.tsx
+++ b/frontend/src/lib/components/TaxonomicPopover/TaxonomicPopover.tsx
@@ -1,6 +1,6 @@
import { Placement } from '@floating-ui/react'
import { useValues } from 'kea'
-import { Ref, forwardRef, useEffect, useState } from 'react'
+import { Ref, forwardRef, useEffect, useId, useState } from 'react'
import { IconX } from '@posthog/icons'
@@ -113,6 +113,8 @@ export const TaxonomicPopover = forwardRef(function TaxonomicPopover_<
const { useNewMenu } = useValues(taxonomicMenuPreferenceLogic)
const menuRebuildEnabled = !!featureFlags[FEATURE_FLAGS.TAXONOMIC_FILTER_MENU_REBUILD]
+ const generatedKey = useId()
+ const taxonomicFilterLogicKey = `taxonomic-popover-${generatedKey}`
const [localValue, setLocalValue] = useState<ValueType>(value || ('' as ValueType))
const [visible, setVisible] = useState(false)
@@ -142,6 +144,7 @@ export const TaxonomicPopover = forwardRef(function TaxonomicPopover_<
<LemonDropdown
overlay={
<TaxonomicFilter
+ taxonomicFilterLogicKey={taxonomicFilterLogicKey}
groupType={groupType}
value={value}
filter={filter}
diff --git a/frontend/src/scenes/cohorts/CohortFilters/CohortField.tsx b/frontend/src/scenes/cohorts/CohortFilters/CohortField.tsx
index 0bc0dcf44e09..780650620c39 100644
--- a/frontend/src/scenes/cohorts/CohortFilters/CohortField.tsx
+++ b/frontend/src/scenes/cohorts/CohortFilters/CohortField.tsx
@@ -2,7 +2,7 @@ import './CohortField.scss'
import clsx from 'clsx'
import { useActions, useValues } from 'kea'
-import { useEffect, useMemo, useRef } from 'react'
+import { useEffect, useId, useRef } from 'react'
import { DateFilter } from 'lib/components/DateFilter/DateFilter'
import { PropertyValue } from 'lib/components/PropertyFilters/components/PropertyValue'
@@ -38,13 +38,9 @@ import {
PropertyType,
} from '~/types'
-let uniqueMemoizedIndex = 0
-
const useCohortFieldLogic = (props: CohortFieldBaseProps): { logic: ReturnType<typeof cohortFieldLogic.build> } => {
- const cohortFilterLogicKey = useMemo(
- () => props.cohortFilterLogicKey || `cohort-filter-${uniqueMemoizedIndex++}`,
- [props.cohortFilterLogicKey]
- )
+ const generatedKey = useId()
+ const cohortFilterLogicKey = props.cohortFilterLogicKey || `cohort-filter-${generatedKey}`
return {
logic: cohortFieldLogic({ ...props, cohortFilterLogicKey }),
}