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(customer-analytics): restore saved columns on fresh tabs (#101072)

Arthur Moreira de Deus6d19a59ca9af
diff --git a/docs/internal/customer-analytics-accounts-table.md b/docs/internal/customer-analytics-accounts-table.md
index 054f9aeeba5f..7a486bd4e64c 100644
--- a/docs/internal/customer-analytics-accounts-table.md
+++ b/docs/internal/customer-analytics-accounts-table.md
@@ -20,6 +20,10 @@ Restoration uses this order:
3. Without a draft, the list restores the last selected saved view, if available.
4. Otherwise, the list uses its defaults and the shared My accounts preference.
+On a fresh tab, restoring the My accounts preference does not create a draft or mark default columns as a restored selection.
+Draft and URL writes wait while the last selected saved view loads, then store the resolved view.
+Relationship definitions can load before or after saved views without replacing saved columns.
+
Selecting a saved view replaces the draft.
Clearing filters keeps them cleared on return, even when a saved view remains selected.
Saving or updating a view still requires an explicit action.
diff --git a/products/customer_analytics/frontend/components/Accounts/AGENTS.md b/products/customer_analytics/frontend/components/Accounts/AGENTS.md
index 75094100f99c..58b10d5c7d0d 100644
--- a/products/customer_analytics/frontend/components/Accounts/AGENTS.md
+++ b/products/customer_analytics/frontend/components/Accounts/AGENTS.md
@@ -251,6 +251,8 @@ For the same reason, anything that rewrites the Customer analytics URL (the scen
Drafts use project-and-user-scoped `sessionStorage`, including cleared filters and unsaved changes to a selected view.
The draft key comes from loaded `teamLogic` and `userLogic` state. Draft hydration and automatic saved-view restoration wait for both IDs.
An explicit shared URL wins over the draft. The draft wins over automatic saved-view restoration.
+Restoring only the My accounts preference keeps column initialization active and does not create a fallback draft.
+While `awaitingSavedView` is true, draft and URL writes wait for the saved-view decision.
Selecting a saved view explicitly replaces the draft.
See [Accounts table](../../../../../docs/internal/customer-analytics-accounts-table.md) when changing persistence or navigation.
diff --git a/products/customer_analytics/frontend/components/Accounts/accountsColumnConfigLogic.ts b/products/customer_analytics/frontend/components/Accounts/accountsColumnConfigLogic.ts
index f506d9a57399..e8b8e294db66 100644
--- a/products/customer_analytics/frontend/components/Accounts/accountsColumnConfigLogic.ts
+++ b/products/customer_analytics/frontend/components/Accounts/accountsColumnConfigLogic.ts
@@ -418,6 +418,9 @@ export interface accountsColumnConfigLogicActions {
setColumnDisplayConfig: (config: AccountColumnDisplayState) => {
config: AccountColumnDisplayState
}
+ setDefaultSelectColumns: (columns: string[]) => {
+ columns: string[]
+ }
setEditingColumnIndex: (index: number | null) => {
index: number | null
}
@@ -523,6 +526,7 @@ export const accountsColumnConfigLogic = kea<accountsColumnConfigLogicType>([
})),
actions({
setSelectColumns: (columns: string[]) => ({ columns }),
+ setDefaultSelectColumns: (columns: string[]) => ({ columns }),
restoreSelectColumns: (columns: string[]) => ({ columns }),
selectColumn: (column: string) => ({ column }),
unselectColumn: (column: string) => ({ column }),
@@ -544,6 +548,7 @@ export const accountsColumnConfigLogic = kea<accountsColumnConfigLogicType>([
[...ACCOUNTS_DEFAULT_COLUMNS],
{
setSelectColumns: (_, { columns }) => ensureNameColumn(columns),
+ setDefaultSelectColumns: (_, { columns }) => ensureNameColumn(columns),
restoreSelectColumns: (_, { columns }) => ensureNameColumn(columns),
selectColumn: (state, { column }) => (state.includes(column) ? state : [...state, column]),
unselectColumn: (state, { column }) =>
@@ -566,6 +571,7 @@ export const accountsColumnConfigLogic = kea<accountsColumnConfigLogicType>([
{
setEditingColumnIndex: (_, { index }) => index,
setSelectColumns: () => null,
+ setDefaultSelectColumns: () => null,
restoreSelectColumns: () => null,
unselectColumn: () => null,
moveColumn: () => null,
@@ -816,7 +822,7 @@ export const accountsColumnConfigLogic = kea<accountsColumnConfigLogicType>([
objectsEqual(values.selectColumns, previousDefault) &&
!objectsEqual(values.defaultSelectColumns, values.selectColumns)
) {
- actions.setSelectColumns(values.defaultSelectColumns)
+ actions.setDefaultSelectColumns(values.defaultSelectColumns)
}
},
resetColumns: () => {
diff --git a/products/customer_analytics/frontend/components/Accounts/accountsLogic.ts b/products/customer_analytics/frontend/components/Accounts/accountsLogic.ts
index c0838b64449a..a6f692bbf4a5 100644
--- a/products/customer_analytics/frontend/components/Accounts/accountsLogic.ts
+++ b/products/customer_analytics/frontend/components/Accounts/accountsLogic.ts
@@ -190,7 +190,7 @@ export type AccountsViewStateSource = 'defaults' | 'draft' | 'saved_view' | 'sha
export interface ApplyAccountsViewStateOptions {
source: AccountsViewStateSource
- columns: 'restore' | 'defaults'
+ columns: 'restore' | 'defaults' | 'keep'
}
function viewStateWithMineOnly(
@@ -1262,7 +1262,7 @@ export const accountsLogic = kea<accountsLogicType>([
try {
if (options.columns === 'restore') {
actions.restoreSelectColumns(viewState.columns)
- } else {
+ } else if (options.columns === 'defaults') {
actions.resetColumns()
}
actions.setColumnDisplayConfig(viewState.columnDisplay)
@@ -1279,12 +1279,16 @@ export const accountsLogic = kea<accountsLogicType>([
} finally {
cache.applyingViewState = false
}
- actions.persistViewState()
+ // A fallback snapshot would become a draft that blocks the pending saved view.
+ if (options.source !== 'defaults') {
+ actions.persistViewState()
+ }
},
persistViewState: ({ search }) => {
const draftIdentity = getAccountsViewDraftIdentity(values.currentTeamId, values.user)
if (
!values.viewStateHydrated ||
+ values.awaitingSavedView ||
cache.applyingViewState ||
!draftIdentity ||
!objectsEqual(cache.viewStateDraftIdentity, draftIdentity) ||
@@ -1298,6 +1302,11 @@ export const accountsLogic = kea<accountsLogicType>([
}
writeAccountsViewDraft(draftIdentity.teamId, draftIdentity.userId, viewState)
},
+ setAwaitingSavedView: ({ awaiting }) => {
+ if (!awaiting) {
+ actions.syncViewStateToUrl()
+ }
+ },
setSearchQuery: () => persistViewStateAndUrl(actions, cache.applyingViewState, values.viewStateHydrated),
setTagsFilter: () => persistViewStateAndUrl(actions, cache.applyingViewState, values.viewStateHydrated),
setSortOrder: () => persistViewStateAndUrl(actions, cache.applyingViewState, values.viewStateHydrated),
@@ -1619,7 +1628,7 @@ export const accountsLogic = kea<accountsLogicType>([
viewStateWithMineOnly(draft ?? values.viewState, mineOnly, values.currentUserId),
{
source: draft ? 'draft' : 'defaults',
- columns: 'restore',
+ columns: draft ? 'restore' : 'keep',
}
)
restored = true
@@ -1646,9 +1655,8 @@ export const accountsLogic = kea<accountsLogicType>([
} else {
actions.applyViewState(viewStateWithMineOnly(values.viewState, true, values.currentUserId), {
source: 'defaults',
- columns: 'restore',
+ columns: 'keep',
})
- restored = true
}
}
}
@@ -1820,6 +1828,9 @@ export const accountsLogic = kea<accountsLogicType>([
}),
actionToUrl(({ values, cache }) => ({
syncViewStateToUrl: () => {
+ if (!values.viewStateHydrated || values.awaitingSavedView) {
+ return undefined
+ }
const pathname = accountsPathToWriteBackTo(values.accountIdFilter)
if (!pathname) {
return undefined
diff --git a/products/customer_analytics/frontend/components/Accounts/accountsViewsLogic.test.ts b/products/customer_analytics/frontend/components/Accounts/accountsViewsLogic.test.ts
index 336fb1c4fadc..aa527a1d32d8 100644
--- a/products/customer_analytics/frontend/components/Accounts/accountsViewsLogic.test.ts
+++ b/products/customer_analytics/frontend/components/Accounts/accountsViewsLogic.test.ts
@@ -13,11 +13,12 @@ import { initKeaTests } from '~/test/init'
import { ColumnConfigurationApi } from 'products/product_analytics/frontend/generated/api.schemas'
+import { customerAnalyticsSceneLogic } from '../../customerAnalyticsSceneLogic'
import { ACCOUNTS_DEFAULT_COLUMNS, accountsColumnConfigLogic } from './accountsColumnConfigLogic'
import { accountsLogic, SEARCH_DEBOUNCE_MS } from './accountsLogic'
import { accountsOverviewTilesLogic } from './accountsOverviewTilesLogic'
import { accountsViewsLogic } from './accountsViewsLogic'
-import { type AccountsViewState, writeAccountsViewDraft } from './accountsViewState'
+import { readAccountsViewDraft, type AccountsViewState, writeAccountsViewDraft } from './accountsViewState'
import { DEFAULT_TILES } from './constants'
const CURRENT_USER_ID = MOCK_DEFAULT_USER.id
@@ -110,6 +111,81 @@ describe('accountsViewsLogic', () => {
expect(accountsLogic.values.searchQuery).toEqual('acme')
})
+ it.each([
+ [false, 'views', true],
+ [true, 'views', true],
+ [false, 'relationships', true],
+ [true, 'relationships', true],
+ [true, 'views', false],
+ [true, 'relationships', false],
+ ] as const)(
+ 'restores a fresh tab with mineOnly=%s, %s loading first, and savedView=%s',
+ async (mineOnly, firstResponse, savedView) => {
+ customerAnalyticsSceneLogic.mount()
+ customerAnalyticsSceneLogic.actions.setMineOnly(mineOnly)
+ let releaseViews!: () => void
+ let releaseRelationships!: () => void
+ const viewsReady = new Promise<void>((resolve) => {
+ releaseViews = resolve
+ })
+ const relationshipsReady = new Promise<void>((resolve) => {
+ releaseRelationships = resolve
+ })
+ useMocks({
+ get: {
+ '/api/projects/:team_id/column_configurations/': async () => {
+ await viewsReady
+ return { count: 1, results: [buildView()] }
+ },
+ '/api/projects/:team_id/account_relationship_definitions/': async () => {
+ await relationshipsReady
+ return {
+ count: 1,
+ results: [
+ {
+ id: '11111111-2222-3333-4444-555555555555',
+ name: 'CSM',
+ description: null,
+ is_single_holder: true,
+ },
+ ],
+ }
+ },
+ },
+ })
+ if (savedView) {
+ localStorage.setItem(
+ `customerAnalytics.accounts.accountsViewsLogic.${MOCK_DEFAULT_TEAM.id}.currentViewId`,
+ JSON.stringify('view-1')
+ )
+ }
+ mountAll()
+
+ if (firstResponse === 'views') {
+ releaseViews()
+ await expectLogic(logic).toDispatchActions(['loadViewsSuccess'])
+ releaseRelationships()
+ } else {
+ releaseRelationships()
+ await expectLogic(accountsColumnConfigLogic).toDispatchActions(['loadRelationshipDefinitionsSuccess'])
+ releaseViews()
+ }
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(accountsColumnConfigLogic.values.selectColumns).toEqual(
+ savedView ? ['name', 'csm'] : [...ACCOUNTS_DEFAULT_COLUMNS, 'csm']
+ )
+ expect(accountsLogic.values.searchQuery).toBe(savedView ? 'acme' : '')
+ expect(accountsLogic.values.assignedToFilter).toEqual(savedView ? [1] : [CURRENT_USER_ID])
+ expect(accountsLogic.values.awaitingSavedView).toBe(false)
+ expect(logic.values.currentViewId).toBe(savedView ? 'view-1' : null)
+ expect(logic.values.isDirty).toBe(false)
+ if (!savedView) {
+ expect(readAccountsViewDraft(MOCK_DEFAULT_TEAM.id, MOCK_DEFAULT_USER.uuid)).toBeNull()
+ }
+ }
+ )
+
it('opens the gate when loading views fails, so the list still fetches', async () => {
useMocks({ get: { '/api/projects/:team_id/column_configurations/': () => [500, {}] } })
localStorage.setItem(