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.

feat(persons): add reset all filters to events tab on person and group profiles (#58725)

posthog[bot]ee0e80db01a4
diff --git a/frontend/src/scenes/groups/Group.tsx b/frontend/src/scenes/groups/Group.tsx
index 60fb2e8607f8..666745b6993f 100644
--- a/frontend/src/scenes/groups/Group.tsx
+++ b/frontend/src/scenes/groups/Group.tsx
@@ -1,6 +1,8 @@
import { useActions, useMountedLogic, useValues } from 'kea'
import { router } from 'kea-router'
+import { IconRefresh } from '@posthog/icons'
+import { LemonButton } from '@posthog/lemon-ui'
import { useFeatureFlagVariantKey } from '@posthog/react'
import { ActivityLog } from 'lib/components/ActivityLog/ActivityLog'
@@ -63,10 +65,18 @@ export function Group({ tabId }: { tabId?: string }): JSX.Element {
throw new Error('GroupScene rendered with no tabId')
}
const mountedGroupLogic = useMountedLogic(groupLogic)
- const { logicProps, groupData, groupDataLoading, groupTypeName, groupType, groupTab, groupEventsQuery } =
- useValues(mountedGroupLogic)
+ const {
+ logicProps,
+ groupData,
+ groupDataLoading,
+ groupTypeName,
+ groupType,
+ groupTab,
+ groupEventsQuery,
+ groupEventsQueryIsDirty,
+ } = useValues(mountedGroupLogic)
const { groupKey, groupTypeIndex } = logicProps
- const { setGroupEventsQuery, editProperty, deleteProperty } = useActions(mountedGroupLogic)
+ const { setGroupEventsQuery, resetGroupEventsQuery, editProperty, deleteProperty } = useActions(mountedGroupLogic)
const { currentTeam } = useValues(teamLogic)
const { featureFlags } = useValues(featureFlagLogic)
const { aggregationLabel } = useValues(groupsModel)
@@ -164,7 +174,22 @@ export function Group({ tabId }: { tabId?: string }): JSX.Element {
<Query
query={groupEventsQuery}
setQuery={setGroupEventsQuery}
- context={{ refresh: 'force_blocking' }}
+ context={{
+ refresh: 'force_blocking',
+ customActions: (
+ <LemonButton
+ key="reset-group-events-filters"
+ type="secondary"
+ size="small"
+ icon={<IconRefresh />}
+ onClick={() => resetGroupEventsQuery()}
+ disabledReason={groupEventsQueryIsDirty ? undefined : 'No active filters'}
+ data-attr="group-events-reset-filters"
+ >
+ Reset all filters
+ </LemonButton>
+ ),
+ }}
/>
) : (
<Spinner />
diff --git a/frontend/src/scenes/groups/groupLogic.ts b/frontend/src/scenes/groups/groupLogic.ts
index 636539a3c8d0..084bd1fa4ce3 100644
--- a/frontend/src/scenes/groups/groupLogic.ts
+++ b/frontend/src/scenes/groups/groupLogic.ts
@@ -7,7 +7,7 @@ import api from 'lib/api'
import { FEATURE_FLAGS } from 'lib/constants'
import { lemonToast } from 'lib/lemon-ui/LemonToast'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
-import { toParams } from 'lib/utils'
+import { objectsEqual, toParams } from 'lib/utils'
import { capitalizeFirstLetter } from 'lib/utils'
import { eventUsageLogic } from 'lib/utils/eventUsageLogic'
import { groupDisplayId } from 'scenes/persons/GroupActorDisplay'
@@ -74,6 +74,7 @@ export const groupLogic = kea<groupLogicType>([
setGroupData: (group: Group) => ({ group }),
setGroupTab: (groupTab: string | null) => ({ groupTab }),
setGroupEventsQuery: (query: Node) => ({ query }),
+ resetGroupEventsQuery: true,
editProperty: (key: string, newValue?: string | number | boolean | null) => ({ key, newValue }),
deleteProperty: (key: string) => ({ key }),
})),
@@ -129,7 +130,10 @@ export const groupLogic = kea<groupLogicType>([
},
],
})),
- listeners(({ actions, values }) => ({
+ listeners(({ actions, values, props }) => ({
+ resetGroupEventsQuery: () => {
+ actions.setGroupEventsQuery(getGroupEventsQuery(props.groupTypeIndex, props.groupKey))
+ },
editProperty: async ({ key, newValue }) => {
const group = values.groupData
@@ -214,6 +218,16 @@ export const groupLogic = kea<groupLogicType>([
selectors({
logicProps: [() => [(_, props) => props], (props): GroupLogicProps => props],
+ groupEventsQueryIsDirty: [
+ (s, p) => [s.groupEventsQuery, p.groupTypeIndex, p.groupKey],
+ (groupEventsQuery, groupTypeIndex, groupKey): boolean => {
+ if (!groupEventsQuery) {
+ return false
+ }
+ return !objectsEqual(groupEventsQuery, getGroupEventsQuery(groupTypeIndex, groupKey))
+ },
+ ],
+
groupTypeName: [
(s, p) => [s.aggregationLabel, p.groupTypeIndex],
(aggregationLabel, index): string => aggregationLabel(index).singular,
diff --git a/frontend/src/scenes/persons/PersonScene.tsx b/frontend/src/scenes/persons/PersonScene.tsx
index 8490bdafc544..f3a271fc2e16 100644
--- a/frontend/src/scenes/persons/PersonScene.tsx
+++ b/frontend/src/scenes/persons/PersonScene.tsx
@@ -1,6 +1,6 @@
import { useActions, useMountedLogic, useValues } from 'kea'
-import { IconChevronDown, IconCopy, IconInfo, IconTrash } from '@posthog/icons'
+import { IconChevronDown, IconCopy, IconInfo, IconRefresh, IconTrash } from '@posthog/icons'
import { LemonButton, LemonButtonProps, LemonDivider, LemonMenu, LemonSelect, LemonTag, Link } from '@posthog/lemon-ui'
import api from 'lib/api'
@@ -178,6 +178,7 @@ export function PersonScene({ tabId }: { tabId?: string }): JSX.Element | null {
distinctId,
primaryDistinctId,
eventsQuery,
+ eventsQueryIsDirty,
exceptionsQuery,
surveyResponsesQuery,
} = useValues(mountedPersonsLogic)
@@ -189,6 +190,7 @@ export function PersonScene({ tabId }: { tabId?: string }): JSX.Element | null {
setSplitMergeModalShown,
setDistinctId,
setEventsQuery,
+ resetEventsQuery,
setExceptionsQuery,
setSurveyResponsesQuery,
} = useActions(mountedPersonsLogic)
@@ -313,6 +315,19 @@ export function PersonScene({ tabId }: { tabId?: string }): JSX.Element | null {
tabId,
dataNodeCollectionId: eventsQueryLogicKey,
},
+ customActions: (
+ <LemonButton
+ key="reset-events-filters"
+ type="secondary"
+ size="small"
+ icon={<IconRefresh />}
+ onClick={() => resetEventsQuery()}
+ disabledReason={eventsQueryIsDirty ? undefined : 'No active filters'}
+ data-attr="person-events-reset-filters"
+ >
+ Reset all filters
+ </LemonButton>
+ ),
}}
/>
),
diff --git a/frontend/src/scenes/persons/personsLogic.test.ts b/frontend/src/scenes/persons/personsLogic.test.ts
index 90f9e29972e4..5ad38c8c6644 100644
--- a/frontend/src/scenes/persons/personsLogic.test.ts
+++ b/frontend/src/scenes/persons/personsLogic.test.ts
@@ -543,4 +543,64 @@ describe('personsLogic', () => {
})
})
})
+
+ describe('resetEventsQuery', () => {
+ const person: PersonType = {
+ id: 'person-1',
+ uuid: 'uuid-1',
+ distinct_ids: ['did-1'],
+ properties: {},
+ is_identified: true,
+ created_at: '2024-01-01',
+ }
+
+ beforeEach(async () => {
+ useMocks({
+ get: {
+ '/api/environments/:team_id/persons/': () => [200, { results: [person] }],
+ },
+ })
+ await expectLogic(logic, () => {
+ logic.actions.loadPerson('did-1')
+ }).toDispatchActions(['loadPerson', 'loadPersonSuccess'])
+ })
+
+ it('eventsQueryIsDirty is false right after the person loads', async () => {
+ await expectLogic(logic).toMatchValues({ eventsQueryIsDirty: false })
+ })
+
+ it('eventsQueryIsDirty becomes true after modifying filters', async () => {
+ const current = logic.values.eventsQuery!
+ logic.actions.setEventsQuery({
+ ...current,
+ source: { ...current.source, events: ['$pageview'] } as any,
+ } as DataTableNode)
+
+ await expectLogic(logic).toMatchValues({ eventsQueryIsDirty: true })
+ })
+
+ it('resetEventsQuery restores the initial query', async () => {
+ const current = logic.values.eventsQuery!
+ logic.actions.setEventsQuery({
+ ...current,
+ source: { ...current.source, events: ['$pageview'], after: '-7d' } as any,
+ } as DataTableNode)
+
+ await expectLogic(logic).toMatchValues({ eventsQueryIsDirty: true })
+
+ await expectLogic(logic, () => {
+ logic.actions.resetEventsQuery()
+ })
+ .toDispatchActions(['resetEventsQuery', 'setEventsQuery'])
+ .toMatchValues({ eventsQueryIsDirty: false })
+
+ const resetSource = logic.values.eventsQuery?.source as any
+ expect(resetSource).toMatchObject({
+ kind: NodeKind.EventsQuery,
+ personId: 'person-1',
+ after: '-24h',
+ })
+ expect(resetSource.events).toBeUndefined()
+ })
+ })
})
diff --git a/frontend/src/scenes/persons/personsLogic.tsx b/frontend/src/scenes/persons/personsLogic.tsx
index 0125e00a782e..6df10893bde5 100644
--- a/frontend/src/scenes/persons/personsLogic.tsx
+++ b/frontend/src/scenes/persons/personsLogic.tsx
@@ -10,7 +10,7 @@ import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { tabAwareActionToUrl } from 'lib/logic/scenes/tabAwareActionToUrl'
import { tabAwareUrlToAction } from 'lib/logic/scenes/tabAwareUrlToAction'
-import { toParams } from 'lib/utils'
+import { objectsEqual, toParams } from 'lib/utils'
import { eventUsageLogic } from 'lib/utils/eventUsageLogic'
import { sceneConfigurations } from 'scenes/scenes'
import { Scene } from 'scenes/sceneTypes'
@@ -145,6 +145,7 @@ export const personsLogic = kea<personsLogicType>([
setEventsQuery: (eventsQuery: DataTableNode | null) => ({ eventsQuery }),
setExceptionsQuery: (exceptionsQuery: DataTableNode | null) => ({ exceptionsQuery }),
setSurveyResponsesQuery: (surveyResponsesQuery: DataTableNode | null) => ({ surveyResponsesQuery }),
+ resetEventsQuery: true,
}),
loaders(({ values, actions, props }) => ({
persons: [
@@ -407,8 +408,23 @@ export const personsLogic = kea<personsLogicType>([
return person.distinct_ids.slice().sort((a, b) => scoreDistinctId(b) - scoreDistinctId(a))[0]
},
],
+ eventsQueryIsDirty: [
+ (s) => [s.eventsQuery, s.person],
+ (eventsQuery, person): boolean => {
+ if (!eventsQuery || !person?.id) {
+ return false
+ }
+ return !objectsEqual(eventsQuery, createInitialEventsPayload(person.id))
+ },
+ ],
})),
listeners(({ actions, values }) => ({
+ resetEventsQuery: () => {
+ const person = values.person
+ if (person?.id != null) {
+ actions.setEventsQuery(createInitialEventsPayload(person.id))
+ }
+ },
editProperty: async ({ key, newValue }) => {
const person = values.person