: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(cohorts): stop the new-cohort page sending a query with no cohort id (#106128)
posthog[bot]161496737e3c
diff --git a/frontend/src/scenes/cohorts/cohortCountWarningLogic.test.ts b/frontend/src/scenes/cohorts/cohortCountWarningLogic.test.tsindex f9917558a2f9..2f2ec73528cc 100644--- a/frontend/src/scenes/cohorts/cohortCountWarningLogic.test.ts+++ b/frontend/src/scenes/cohorts/cohortCountWarningLogic.test.ts@@ -1,4 +1,5 @@import { BuiltLogic } from 'kea'+import { expectLogic } from 'kea-test-utils'import { toPaginatedResponse } from '~/mocks/handlers'import { useMocks } from '~/mocks/jest'@@ -362,5 +363,22 @@ describe('cohortCountWarningLogic', () => {expect(logic.props.dataNodeLogicKey).toBe(dataNodeLogicKey)expect(logic.props.query).toBe(query)})++ it.each([+ ['an unsaved cohort does not run the persons query', 'new' as const, null],+ ['a saved cohort runs it', 1, { results: [] }],+ ])('%s', async (_name, cohortId, expectedResponse) => {+ const dataNodeLogicKey = `cohort-count-warning-load-${String(cohortId)}`+ const query = createMockQuery(1)+ const logic = createLogicWithProps({+ cohort: createMockCohort({ id: cohortId }),+ query,+ dataNodeLogicKey,+ })+ logic.mount()+ await expectLogic(dataNodeLogic.findMounted({ key: dataNodeLogicKey })!).toFinishAllListeners()++ expect(logic.values.response).toEqual(expectedResponse)+ })})})diff --git a/frontend/src/scenes/cohorts/cohortCountWarningLogic.ts b/frontend/src/scenes/cohorts/cohortCountWarningLogic.tsindex 0ebf37c56303..2c1642a87f08 100644--- a/frontend/src/scenes/cohorts/cohortCountWarningLogic.ts+++ b/frontend/src/scenes/cohorts/cohortCountWarningLogic.ts@@ -86,7 +86,15 @@ export const cohortCountWarningLogic = kea<cohortCountWarningLogicType>([path(['scenes', 'cohorts', 'cohortCountWarningLogic']),connect((props: CohortCountWarningLogicProps) => ({- values: [dataNodeLogic({ key: props.dataNodeLogicKey, query: props.query }), ['response']],+ values: [+ dataNodeLogic({+ key: props.dataNodeLogicKey,+ query: props.query,+ // A draft cohort has no id to filter on, so the query would list every person.+ doNotLoad: typeof props.cohort.id !== 'number',+ }),+ ['response'],+ ],})),selectors(({ props }) => ({diff --git a/frontend/src/scenes/cohorts/cohortEditLogic.test.ts b/frontend/src/scenes/cohorts/cohortEditLogic.test.tsindex e959ecf523d7..8261fda3d76a 100644--- a/frontend/src/scenes/cohorts/cohortEditLogic.test.ts+++ b/frontend/src/scenes/cohorts/cohortEditLogic.test.ts@@ -1351,6 +1351,19 @@ describe('cohortEditLogic', () => {expect((logic.values.effectiveQuery.source as ActorsQuery).select).toEqual(expectedSelect)})+ it('pins the cohort filter to the saved cohort after a draft is saved', async () => {+ await initCohortLogic({ id: 'new' })+ expect((logic.values.effectiveQuery.source as ActorsQuery).fixedProperties).toEqual([])++ await expectLogic(logic, () => {+ logic.actions.setCohort({ ...mockCohort, id: 42 })+ }).toDispatchActions(['setCohort'])++ expect((logic.values.effectiveQuery.source as ActorsQuery).fixedProperties).toEqual([+ { type: PropertyFilterType.Cohort, key: 'id', value: 42, operator: PropertyOperator.In },+ ])+ })+it('does not carry columns from one unsaved draft cohort over to the next', async () => {await initCohortLogic({ id: 'new' })diff --git a/frontend/src/scenes/cohorts/cohortEditLogic.ts b/frontend/src/scenes/cohorts/cohortEditLogic.tsindex e9506ffc8b50..9d8212622f3e 100644--- a/frontend/src/scenes/cohorts/cohortEditLogic.ts+++ b/frontend/src/scenes/cohorts/cohortEditLogic.ts@@ -54,6 +54,7 @@ import { isDataTableNode } from '~/queries/utils'import {AnyCohortCriteriaType,AnyCohortGroupType,+ AnyPersonScopeFilter,CohortCriteriaGroupFilter,CohortGroupType,CohortType,@@ -533,6 +534,14 @@ export type cohortEditLogicType = MakeLogicType<cohortEditLogicMeta>+// A draft cohort has no id, so the persons table gets no cohort filter until `setCohort` supplies+// a saved id. A filter with a missing id serializes to null, which the API rejects.+function cohortFixedProperties(id: CohortType['id'] | undefined): AnyPersonScopeFilter[] {+ return typeof id === 'number'+ ? [{ type: PropertyFilterType.Cohort, key: 'id', value: id, operator: PropertyOperator.In }]+ : []+}+export const cohortEditLogic = kea<cohortEditLogicType>([props({} as CohortLogicProps),key((props) => (props.id === 'new' || !props.id ? 'new' : props.id)),@@ -697,9 +706,7 @@ export const cohortEditLogic = kea<cohortEditLogicType>([kind: NodeKind.DataTableNode,source: {kind: NodeKind.ActorsQuery,- fixedProperties: [- { type: PropertyFilterType.Cohort, key: 'id', value: parseInt(String(props.id)) },- ],+ fixedProperties: cohortFixedProperties(props.id),},full: true,showPropertyFilter: false,@@ -720,6 +727,7 @@ export const cohortEditLogic = kea<cohortEditLogicType>([source: {...source,select: source.select ?? defaultSelect,+ fixedProperties: cohortFixedProperties(cohort.id),},}},