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.

refactor(taxonomic-filter): extract cohortTaxonomicGroupsLogic (#59818)

Paul D'Ambra9c587f515b34
diff --git a/frontend/src/lib/components/TaxonomicFilter/cohortTaxonomicGroupsLogic.tsx b/frontend/src/lib/components/TaxonomicFilter/cohortTaxonomicGroupsLogic.tsx
new file mode 100644
index 000000000000..d0c9fe8d5ab7
--- /dev/null
+++ b/frontend/src/lib/components/TaxonomicFilter/cohortTaxonomicGroupsLogic.tsx
@@ -0,0 +1,80 @@
+import { connect, kea, key, path, props, selectors } from 'kea'
+import { combineUrl } from 'kea-router'
+
+import {
+ TaxonomicFilterGroup,
+ TaxonomicFilterGroupType,
+ TaxonomicFilterLogicProps,
+} from 'lib/components/TaxonomicFilter/types'
+import { IconCohort } from 'lib/lemon-ui/icons'
+import { Link } from 'lib/lemon-ui/Link'
+import { COHORT_BEHAVIORAL_LIMITATIONS_URL } from 'scenes/feature-flags/constants'
+import { projectLogic } from 'scenes/projectLogic'
+
+import { CohortType } from '~/types'
+
+import type { cohortTaxonomicGroupsLogicType } from './cohortTaxonomicGroupsLogicType'
+
+// Stable reference for CohortsWithAllUsers options to prevent cascading re-renders.
+// taxonomicGroups has 14 dependencies that change during initial mount. Each change creates
+// new group objects with inline options arrays, causing rawLocalItems → fuse → localItems →
+// items → selectedItem reference changes. With CohortsWithAllUsers, selectedItemHasPopover
+// returns true (getValue returns 'all'), so ControlledDefinitionPopover renders and its
+// useEffect dispatches setDefinition on every selectedItem change, triggering kea store updates
+// that combined with react-window's layout effect setState exceed React's 50-update limit.
+const COHORTS_WITH_ALL_USERS_OPTIONS: CohortType[] = [{ id: 'all', name: 'All Users*' } as unknown as CohortType]
+
+export const cohortTaxonomicGroupsLogic = kea<cohortTaxonomicGroupsLogicType>([
+ props({} as TaxonomicFilterLogicProps),
+ key((props) => `${props.taxonomicFilterLogicKey}`),
+ path((key) => ['lib', 'components', 'TaxonomicFilter', 'cohortTaxonomicGroupsLogic', key]),
+
+ connect(() => ({
+ values: [projectLogic, ['currentProjectId']],
+ })),
+
+ selectors({
+ hideBehavioralCohorts: [
+ () => [(_, props) => props.hideBehavioralCohorts],
+ (hideBehavioralCohorts: boolean | undefined) => hideBehavioralCohorts ?? false,
+ ],
+ cohortTaxonomicGroups: [
+ (s) => [s.currentProjectId, s.hideBehavioralCohorts],
+ (projectId, hideBehavioralCohorts): TaxonomicFilterGroup[] => [
+ {
+ name: 'Cohorts',
+ searchPlaceholder: 'cohorts',
+ type: TaxonomicFilterGroupType.Cohorts,
+ endpoint: combineUrl(`api/projects/${projectId}/cohorts/`).url,
+ value: 'cohorts',
+ getName: (cohort: CohortType) => cohort.name || `Cohort ${cohort.id}`,
+ getValue: (cohort: CohortType) => cohort.id,
+ getPopoverHeader: (cohort: CohortType) => `${cohort.is_static ? 'Static' : 'Dynamic'} Cohort`,
+ getIcon: function _getIcon(): JSX.Element {
+ return <IconCohort className="taxonomy-icon taxonomy-icon-muted" />
+ },
+ footerMessage: hideBehavioralCohorts ? (
+ <>
+ <Link to={COHORT_BEHAVIORAL_LIMITATIONS_URL} target="_blank">
+ Some cohorts excluded due to containing behavioral filters.
+ </Link>
+ </>
+ ) : undefined,
+ },
+ {
+ name: 'Cohorts',
+ searchPlaceholder: 'cohorts',
+ type: TaxonomicFilterGroupType.CohortsWithAllUsers,
+ endpoint: combineUrl(`api/projects/${projectId}/cohorts/`).url,
+ options: COHORTS_WITH_ALL_USERS_OPTIONS,
+ getName: (cohort: CohortType) => cohort.name || `Cohort ${cohort.id}`,
+ getValue: (cohort: CohortType) => cohort.id,
+ getPopoverHeader: () => `All Users`,
+ getIcon: function _getIcon(): JSX.Element {
+ return <IconCohort className="taxonomy-icon taxonomy-icon-muted" />
+ },
+ },
+ ],
+ ],
+ }),
+])
diff --git a/frontend/src/lib/components/TaxonomicFilter/taxonomicFilterLogic.tsx b/frontend/src/lib/components/TaxonomicFilter/taxonomicFilterLogic.tsx
index bbf358c8349d..ef583f5591d2 100644
--- a/frontend/src/lib/components/TaxonomicFilter/taxonomicFilterLogic.tsx
+++ b/frontend/src/lib/components/TaxonomicFilter/taxonomicFilterLogic.tsx
@@ -50,8 +50,6 @@ import {
isQuickFilterItem,
} from 'lib/components/TaxonomicFilter/types'
import { FEATURE_FLAGS } from 'lib/constants'
-import { IconCohort } from 'lib/lemon-ui/icons'
-import { Link } from 'lib/lemon-ui/Link'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { isString, objectsEqual, pluralize } from 'lib/utils'
import { isDefinitionStale } from 'lib/utils/definitions'
@@ -64,7 +62,6 @@ import {
} from 'scenes/data-management/events/DefinitionHeader'
import { dataWarehouseSettingsSceneLogic } from 'scenes/data-warehouse/settings/dataWarehouseSettingsSceneLogic'
import { experimentsLogic } from 'scenes/experiments/experimentsLogic'
-import { COHORT_BEHAVIORAL_LIMITATIONS_URL } from 'scenes/feature-flags/constants'
import {
getProductEventFilterOptions,
getProductEventPropertyFilterOptions,
@@ -84,7 +81,6 @@ import { getCoreFilterDefinition, getFilterLabel } from '~/taxonomy/helpers'
import { CORE_FILTER_DEFINITIONS_BY_GROUP } from '~/taxonomy/taxonomy'
import {
ActionType,
- CohortType,
CoreFilterDefinition,
DashboardType,
EventDefinition,
@@ -105,6 +101,7 @@ import { joinsLogic } from 'products/data_warehouse/frontend/shared/logics/joins
import { HogFlowTaxonomicFilters } from 'products/workflows/frontend/Workflows/hogflows/filters/HogFlowTaxonomicFilters'
import { PROPERTY_FILTER_TYPE_TO_TAXONOMIC_FILTER_GROUP_TYPE } from '../PropertyFilters/utils'
+import { cohortTaxonomicGroupsLogic } from './cohortTaxonomicGroupsLogic'
import { groupAnalyticsTaxonomicGroupsLogic } from './groupAnalyticsTaxonomicGroupsLogic'
import { InlineHogQLEditor } from './InlineHogQLEditor'
import type { taxonomicFilterLogicType } from './taxonomicFilterLogicType'
@@ -293,15 +290,6 @@ function withKeywordShortcuts<T>(
}
}
-// Stable reference for CohortsWithAllUsers options to prevent cascading re-renders.
-// taxonomicGroups has 14 dependencies that change during initial mount. Each change creates
-// new group objects with inline options arrays, causing rawLocalItems → fuse → localItems →
-// items → selectedItem reference changes. With CohortsWithAllUsers, selectedItemHasPopover
-// returns true (getValue returns 'all'), so ControlledDefinitionPopover renders and its
-// useEffect dispatches setDefinition on every selectedItem change, triggering kea store updates
-// that combined with react-window's layout effect setState exceed React's 50-update limit.
-const COHORTS_WITH_ALL_USERS_OPTIONS: CohortType[] = [{ id: 'all', name: 'All Users*' } as unknown as CohortType]
-
export const defaultDataWarehousePopoverFields: DataWarehousePopoverField[] = [
{
key: 'id_field',
@@ -341,6 +329,8 @@ export const taxonomicFilterLogic = kea<taxonomicFilterLogicType>([
['primaryProperties'],
groupAnalyticsTaxonomicGroupsLogic,
['groupAnalyticsTaxonomicGroups', 'groupAnalyticsTaxonomicGroupNames'],
+ cohortTaxonomicGroupsLogic,
+ ['cohortTaxonomicGroups'],
],
actions: [primaryEventPropertiesModel, ['ensureLoadedForEvents']],
})),
@@ -544,10 +534,6 @@ export const taxonomicFilterLogic = kea<taxonomicFilterLogicType>([
() => [(_, props) => props.allowNonCapturedEvents],
(allowNonCapturedEvents: boolean | undefined) => allowNonCapturedEvents ?? false,
],
- hideBehavioralCohorts: [
- () => [(_, props) => props.hideBehavioralCohorts],
- (hideBehavioralCohorts: boolean | undefined) => hideBehavioralCohorts ?? false,
- ],
hogQLExpressionComponentProps: [
() => [(_, props) => props.hogQLGlobals, (_, props) => props.hogQLExpressionShowBreakdownLabelHint],
(
@@ -576,7 +562,7 @@ export const taxonomicFilterLogic = kea<taxonomicFilterLogicType>([
s.propertyFilters,
s.eventMetadataPropertyDefinitions,
s.maxContextOptions,
- s.hideBehavioralCohorts,
+ s.cohortTaxonomicGroups,
s.endpointFilters,
s.hogQLExpressionComponentProps,
s.featureFlags,
@@ -597,7 +583,7 @@ export const taxonomicFilterLogic = kea<taxonomicFilterLogicType>([
propertyFilters,
eventMetadataPropertyDefinitions: PropertyDefinition[],
maxContextOptions: MaxContextTaxonomicFilterOption[],
- hideBehavioralCohorts: boolean,
+ cohortTaxonomicGroups: TaxonomicFilterGroup[],
endpointFilters: Record<string, any> | undefined,
hogQLExpressionComponentProps: {
globals?: Record<string, any>
@@ -1105,39 +1091,7 @@ export const taxonomicFilterLogic = kea<taxonomicFilterLogicType>([
propertyAllowList?.[TaxonomicFilterGroupType.PersonProperties]?.filter(isString),
...propertyTaxonomicGroupProps(CORE_FILTER_DEFINITIONS_BY_GROUP.person_properties),
},
- {
- name: 'Cohorts',
- searchPlaceholder: 'cohorts',
- type: TaxonomicFilterGroupType.Cohorts,
- endpoint: combineUrl(`api/projects/${projectId}/cohorts/`).url,
- value: 'cohorts',
- getName: (cohort: CohortType) => cohort.name || `Cohort ${cohort.id}`,
- getValue: (cohort: CohortType) => cohort.id,
- getPopoverHeader: (cohort: CohortType) => `${cohort.is_static ? 'Static' : 'Dynamic'} Cohort`,
- getIcon: function _getIcon(): JSX.Element {
- return <IconCohort className="taxonomy-icon taxonomy-icon-muted" />
- },
- footerMessage: hideBehavioralCohorts ? (
- <>
- <Link to={COHORT_BEHAVIORAL_LIMITATIONS_URL} target="_blank">
- Some cohorts excluded due to containing behavioral filters.
- </Link>
- </>
- ) : undefined,
- },
- {
- name: 'Cohorts',
- searchPlaceholder: 'cohorts',
- type: TaxonomicFilterGroupType.CohortsWithAllUsers,
- endpoint: combineUrl(`api/projects/${projectId}/cohorts/`).url,
- options: COHORTS_WITH_ALL_USERS_OPTIONS,
- getName: (cohort: CohortType) => cohort.name || `Cohort ${cohort.id}`,
- getValue: (cohort: CohortType) => cohort.id,
- getPopoverHeader: () => `All Users`,
- getIcon: function _getIcon(): JSX.Element {
- return <IconCohort className="taxonomy-icon taxonomy-icon-muted" />
- },
- },
+ ...cohortTaxonomicGroups,
// PageviewUrls returns a URL string value, used in paths and property filters.
// PageviewEvents creates a $pageview event with $current_url property filter,
// used in trends and funnels series pickers.