: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(workflows): keep workflows and broadcasts list filters on return (#121091)
Meikel Ratza1f6448eb9a3
diff --git a/products/workflows/frontend/Broadcasts/broadcastsLogic.test.ts b/products/workflows/frontend/Broadcasts/broadcastsLogic.test.tsnew file mode 100644index 000000000000..a3f5c6e85109--- /dev/null+++ b/products/workflows/frontend/Broadcasts/broadcastsLogic.test.ts@@ -0,0 +1,35 @@+import { router } from 'kea-router'++import { useMocks } from '~/mocks/jest'+import { initKeaTests } from '~/test/init'++import { broadcastsLogic } from './broadcastsLogic'++describe('broadcastsLogic', () => {+ let logic: ReturnType<typeof broadcastsLogic.build>++ beforeEach(() => {+ useMocks({+ get: {+ '/api/environments/:team_id/hog_flows/': { results: [], count: 0 },+ '/api/projects/:team_id/hog_flows/': { results: [], count: 0 },+ },+ })+ initKeaTests()+ logic = broadcastsLogic()+ logic.mount()+ })++ afterEach(() => logic.unmount())++ it('keeps the filters when a link back to the list carries no query params', () => {+ router.actions.push('/broadcasts?status=sent&search=welcome')+ expect(logic.values.filters.status).toEqual('sent')++ router.actions.push('/broadcasts')++ expect(logic.values.filters.status).toEqual('sent')+ expect(logic.values.filters.search).toEqual('welcome')+ expect(router.values.searchParams).toMatchObject({ status: 'sent', search: 'welcome' })+ })+})diff --git a/products/workflows/frontend/Broadcasts/broadcastsLogic.ts b/products/workflows/frontend/Broadcasts/broadcastsLogic.tsindex 6d59ac71140d..dee309c34f87 100644--- a/products/workflows/frontend/Broadcasts/broadcastsLogic.ts+++ b/products/workflows/frontend/Broadcasts/broadcastsLogic.ts@@ -6,6 +6,7 @@ import { lemonToast } from '@posthog/lemon-ui'import { loadAppMetricsTotals } from 'lib/components/AppMetrics/appMetricsLogic'import { dayjs } from 'lib/dayjs'+import { buildUserScopedPersistenceConfig } from 'lib/logic/persistence'import { objectsEqual } from 'lib/utils/objects'import { projectLogic } from 'scenes/projectLogic'import { teamLogic } from 'scenes/teamLogic'@@ -63,6 +64,10 @@ export interface BroadcastsFilters {const DEFAULT_FILTERS: BroadcastsFilters = { search: '', status: 'all', createdBy: null, page: 1 }+// The search params actionToUrl writes below. A URL that carries none of them states no filters at all,+// which is different from stating that every filter is off.+const FILTER_URL_PARAMS = ['search', 'status', 'created_by', 'page']+type FlowStep = { id?: string; type?: string; config?: Record<string, any> }/** Mirrors the list API's broadcast_eligible filter: a batch trigger, one email step, nothing else. */@@ -309,7 +314,7 @@ export const broadcastsLogic = kea<broadcastsLogicType>([},],})),- reducers({+ reducers(() => ({rowDetailsById: [{} as Record<string, BroadcastRowDetails>,{@@ -355,13 +360,17 @@ export const broadcastsLogic = kea<broadcastsLogicType>([],filters: [DEFAULT_FILTERS,+ // The list unmounts when a broadcast opens, so the filters are persisted to survive that and+ // be restored when the user comes back to the list. The key carries the user and the team, so+ // another account or another project never reads these filters.+ buildUserScopedPersistenceConfig('broadcasts_list_filters__'),{// Any change other than the page itself starts the list over from the first page.setFilters: (state, { filters }) => ({ ...state, page: 1, ...filters }),setFiltersFromUrl: (_, { filters }) => filters,},],- }),+ })),selectors({filtersPending: [(s) => [s.filters, s.loadedFilters],@@ -482,6 +491,15 @@ export const broadcastsLogic = kea<broadcastsLogicType>([})),urlToAction(({ actions, values }) => ({[urls.broadcasts()]: (_, searchParams) => {+ // An in-app link back to the list, such as the breadcrumb out of a broadcast, carries no filter+ // params. Parsing that URL would clear every filter, so the filters held in state are restored+ // and written back into the URL instead.+ if (!FILTER_URL_PARAMS.some((param) => param in searchParams)) {+ if (!objectsEqual(values.filters, DEFAULT_FILTERS)) {+ actions.setFilters(values.filters)+ }+ return+ }const status = searchParams['status']const parsed: BroadcastsFilters = {// The API rejects longer search terms.diff --git a/products/workflows/frontend/Workflows/workflowsLogic.test.ts b/products/workflows/frontend/Workflows/workflowsLogic.test.tsindex 5d9bb2f6ab34..b4a901e757e7 100644--- a/products/workflows/frontend/Workflows/workflowsLogic.test.ts+++ b/products/workflows/frontend/Workflows/workflowsLogic.test.ts@@ -91,4 +91,21 @@ describe('workflowsLogic', () => {expect(bodies).toEqual([['growth'], ['growth', 'billing']])expect(logic.values.localWorkflowTags).toEqual({ 'flow-1': ['growth', 'billing'] })})++ it('parses a url whose only filter is a tag', () => {+ router.actions.push(`/workflows?tags=${encodeURIComponent('["growth"]')}`)++ expect(logic.values.filters.tags).toEqual(['growth'])+ })++ it('keeps the filters when a link back to the list carries no query params', () => {+ router.actions.push('/workflows?status=active&type=messaging')+ expect(logic.values.filters.status).toEqual('active')++ router.actions.push('/workflows')++ expect(logic.values.filters.status).toEqual('active')+ expect(logic.values.filters.type).toEqual('messaging')+ expect(router.values.searchParams).toMatchObject({ status: 'active', type: 'messaging' })+ })})diff --git a/products/workflows/frontend/Workflows/workflowsLogic.ts b/products/workflows/frontend/Workflows/workflowsLogic.tsindex 1d2ecd7ca764..0f83ce837e32 100644--- a/products/workflows/frontend/Workflows/workflowsLogic.ts+++ b/products/workflows/frontend/Workflows/workflowsLogic.ts@@ -6,6 +6,7 @@ import posthog from 'posthog-js'import { LemonDialog, PaginationManual, lemonToast } from '@posthog/lemon-ui'import api, { ApiConfig, CountedPaginatedResponse, HogFlowListType } from 'lib/api'+import { buildUserScopedPersistenceConfig } from 'lib/logic/persistence'import { objectsEqual } from 'lib/utils/objects'import { urls } from 'scenes/urls'@@ -58,6 +59,10 @@ export interface WorkflowsFilters {page: number}+// The search params actionToUrl writes below. A URL that carries none of them states no filters at all,+// which is different from stating that every filter is off.+const FILTER_URL_PARAMS = ['search', 'created_by', 'status', 'type', 'trigger_type', 'tags', 'page']+const DEFAULT_FILTERS: WorkflowsFilters = {search: '',createdBy: null,@@ -312,9 +317,13 @@ export const workflowsLogic = kea<workflowsLogicType>([setSavedWorkflowTags: (id: string, tags: string[]) => ({ id, tags }),setTagsFilter: (tags: string[], source: 'filter' | 'tag_click') => ({ tags, source }),}),- reducers({+ reducers(() => ({filters: [DEFAULT_FILTERS,+ // The list unmounts when a workflow opens, so the filters are persisted to survive that and+ // be restored when the user comes back to the list. The key carries the user and the team, so+ // another account or another project never reads these filters.+ buildUserScopedPersistenceConfig('workflows_list_filters__'),{setFilters: (state, { filters, replace }) => {const next = replace ? { ...DEFAULT_FILTERS, ...filters } : { ...state, ...filters }@@ -359,7 +368,7 @@ export const workflowsLogic = kea<workflowsLogicType>([loadWorkflowsSuccess: () => ({}),},],- }),+ })),loaders(({ actions, values }) => ({workflows: [{ results: [], count: 0 } as WorkflowsResult,@@ -621,6 +630,15 @@ export const workflowsLogic = kea<workflowsLogicType>([}),urlToAction(({ actions, values }) => {const syncFiltersFromUrl = (_: Record<string, any>, searchParams: Record<string, any>): void => {+ // An in-app link back to the list, such as the breadcrumb out of a workflow, carries no filter+ // params. Parsing that URL would clear every filter, so the filters held in state are restored+ // and written back into the URL instead.+ if (!FILTER_URL_PARAMS.some((param) => param in searchParams)) {+ if (!objectsEqual(values.filters, DEFAULT_FILTERS)) {+ actions.setFilters(values.filters)+ }+ return+ }const status = searchParams['status']const type = searchParams['type']const triggerType = searchParams['trigger_type']