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(cohorts): persist persons table columns across refresh (#64430)

Phill97567bc413ad
diff --git a/frontend/src/scenes/cohorts/CohortEdit.tsx b/frontend/src/scenes/cohorts/CohortEdit.tsx
index 8411f021065c..ed4219fc4d14 100644
--- a/frontend/src/scenes/cohorts/CohortEdit.tsx
+++ b/frontend/src/scenes/cohorts/CohortEdit.tsx
@@ -152,7 +152,7 @@ export function CohortEdit({ id, attachTo }: CohortEditProps): JSX.Element {
cohort,
cohortLoading,
cohortMissing,
- query,
+ effectiveQuery,
creationPersonQuery,
personsToCreateStaticCohort,
canRemovePersonFromCohort,
@@ -168,7 +168,7 @@ export function CohortEdit({ id, attachTo }: CohortEditProps): JSX.Element {
const isNewCohort = cohort.id === 'new' || cohort.id === undefined
const dataNodeLogicKey = createCohortDataNodeLogicKey(cohort.id)
- const warningLogic = cohortCountWarningLogic({ cohort, query, dataNodeLogicKey })
+ const warningLogic = cohortCountWarningLogic({ cohort, query: effectiveQuery, dataNodeLogicKey })
const { shouldShowCountWarning } = useValues(warningLogic)
const cohortId = typeof cohort.id === 'number' ? cohort.id : null
@@ -716,7 +716,7 @@ export function CohortEdit({ id, attachTo }: CohortEditProps): JSX.Element {
</div>
) : (
<Query
- query={query}
+ query={effectiveQuery}
setQuery={setQuery}
context={{
refresh: 'force_blocking',
diff --git a/frontend/src/scenes/cohorts/cohortEditLogic.test.ts b/frontend/src/scenes/cohorts/cohortEditLogic.test.ts
index f924ba06cdf4..b4373844aca7 100644
--- a/frontend/src/scenes/cohorts/cohortEditLogic.test.ts
+++ b/frontend/src/scenes/cohorts/cohortEditLogic.test.ts
@@ -73,6 +73,9 @@ describe('cohortEditLogic', () => {
}
beforeEach(async () => {
+ // Persisted column selection lives in localStorage keyed by cohort id — clear it so
+ // column state doesn't leak between tests that reuse the same cohort id.
+ window.localStorage.clear()
useMocks({
get: {
'/api/projects/:team_id/cohorts/': toPaginatedResponse([mockCohort]),
@@ -1085,6 +1088,50 @@ describe('cohortEditLogic', () => {
// Custom columns should still be preserved after save
expect((logic.values.query.source as ActorsQuery).select).toEqual(customColumns)
})
+
+ const defaultColumns = ['person_display_name -- Person', 'id', 'created_at']
+ const customColumns = [...defaultColumns, 'properties.$browser']
+
+ it.each([
+ ['the same cohort restores the persisted columns', 1, customColumns],
+ ["another cohort ignores the first cohort's columns and uses defaults", 2, defaultColumns],
+ ])('after a refresh (remount), %s', async (_name, remountId, expectedSelect) => {
+ await initCohortLogic({ id: 1 })
+
+ await expectLogic(logic, () => {
+ logic.actions.setQuery({
+ ...logic.values.query,
+ source: { ...(logic.values.query.source as ActorsQuery), select: customColumns },
+ } as DataTableNode)
+ }).toDispatchActions(['setQuery'])
+
+ // Simulate a refresh: tear down and rebuild the logic
+ logic.unmount()
+ logic = cohortEditLogic({ id: remountId })
+ logic.mount()
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect((logic.values.effectiveQuery.source as ActorsQuery).select).toEqual(expectedSelect)
+ })
+
+ it('does not carry columns from one unsaved draft cohort over to the next', async () => {
+ await initCohortLogic({ id: 'new' })
+
+ await expectLogic(logic, () => {
+ logic.actions.setQuery({
+ ...logic.values.query,
+ source: { ...(logic.values.query.source as ActorsQuery), select: customColumns },
+ } as DataTableNode)
+ }).toDispatchActions(['setQuery'])
+
+ // Abandon the draft and start a fresh one — both share the 'new' logic key
+ logic.unmount()
+ logic = cohortEditLogic({ id: 'new' })
+ logic.mount()
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect((logic.values.effectiveQuery.source as ActorsQuery).select).toEqual(defaultColumns)
+ })
})
describe('cohort duplication', () => {
diff --git a/frontend/src/scenes/cohorts/cohortEditLogic.ts b/frontend/src/scenes/cohorts/cohortEditLogic.ts
index 8b3d6cdd7609..2cab4788bfaf 100644
--- a/frontend/src/scenes/cohorts/cohortEditLogic.ts
+++ b/frontend/src/scenes/cohorts/cohortEditLogic.ts
@@ -26,6 +26,8 @@ import { ENTITY_MATCH_TYPE } from 'lib/constants'
import { scrollToFormError } from 'lib/forms/scrollToFormError'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { eventUsageLogic } from 'lib/utils/eventUsageLogic'
+import { getCurrentTeamId } from 'lib/utils/getAppContext'
+import { objectsEqual } from 'lib/utils/objects'
import { isOperatorDate } from 'lib/utils/operators'
import { NEW_COHORT, NEW_CRITERIA, NEW_CRITERIA_GROUP } from 'scenes/cohorts/CohortFilters/constants'
import { BehavioralFilterKey } from 'scenes/cohorts/CohortFilters/types'
@@ -273,6 +275,30 @@ export const cohortEditLogic = kea<cohortEditLogicType>([
},
},
],
+ // User's column selection for this cohort's persons table, persisted per-cohort in
+ // localStorage. Lets a refresh restore the columns even when the user hasn't saved them
+ // as a team-wide default. This is a local-only fallback that sits alongside the
+ // server-side column persistence in `columnConfiguratorLogic` (the persons table sets
+ // `showPersistentColumnConfigurator` and `contextKey: cohort:<id>` in the query above) —
+ // if you're extending column persistence, prefer doing it there.
+ persistedColumns: [
+ null as string[] | null,
+ {
+ persist: true,
+ // Scope by team so columns don't leak across projects (e.g. after impersonation).
+ storageKey: `scenes.cohorts.cohortEditLogic.${getCurrentTeamId()}.${props.id}.persistedColumns`,
+ },
+ {
+ setQuery: (state, { query }) => {
+ // Don't capture for unsaved drafts — every new cohort shares the 'new' logic
+ // key, so persisting here would bleed columns from one draft into the next.
+ if (!props.id || props.id === 'new' || !isDataTableNode(query)) {
+ return state
+ }
+ return (query.source as ActorsQuery).select ?? state
+ },
+ },
+ ],
creationPersonQuery: [
{
kind: NodeKind.ActorsQuery,
@@ -314,6 +340,21 @@ export const cohortEditLogic = kea<cohortEditLogicType>([
})),
selectors({
+ // The persons table query with the user's persisted column selection applied. Deriving
+ // this in a selector (instead of dispatching a corrective `setQuery` from a listener)
+ // avoids a render with default columns before the persisted ones kick in.
+ effectiveQuery: [
+ (s) => [s.query, s.persistedColumns],
+ (query: DataTableNode, persistedColumns: string[] | null): DataTableNode => {
+ if (persistedColumns && isDataTableNode(query)) {
+ const source = query.source as ActorsQuery
+ if (!objectsEqual(source.select, persistedColumns)) {
+ return { ...query, source: { ...source, select: persistedColumns } }
+ }
+ }
+ return query
+ },
+ ],
canRemovePersonFromCohort: [
(s) => [s.cohort],
(cohort: CohortType) => {