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(alerts): persist the creator filter in query parameters (#118108)

Matt Puacde1bcb4fdd1
diff --git a/products/alerts/frontend/logic/alertsLogic.test.ts b/products/alerts/frontend/logic/alertsLogic.test.ts
index 3f22e480f251..028c6b6b033b 100644
--- a/products/alerts/frontend/logic/alertsLogic.test.ts
+++ b/products/alerts/frontend/logic/alertsLogic.test.ts
@@ -1,6 +1,8 @@
+import { router } from 'kea-router'
import { expectLogic } from 'kea-test-utils'
import api from 'lib/api'
+import { urls } from 'scenes/urls'
import { AlertState } from '~/queries/schema/schema-general'
import { initKeaTests } from '~/test/init'
@@ -41,6 +43,46 @@ describe('alertsLogic', () => {
updateSpy?.mockRestore()
})
+ it('keeps the creator filter and URL in sync', async () => {
+ router.actions.push(
+ urls.alerts(),
+ { tab: 'insights', alert_id: alert.id, created_by: 'creator-from-link' },
+ { section: 'alerts' }
+ )
+ const logic = alertsLogic()
+ logic.mount()
+ await expectLogic(logic).toFinishAllListeners()
+ expect(listSpy).toHaveBeenCalledTimes(1)
+ expect(hogFunctionsListSpy).toHaveBeenCalledTimes(1)
+ expect(logic.values.filters.createdBy).toEqual('creator-from-link')
+ expect(listSpy).toHaveBeenLastCalledWith(
+ undefined,
+ expect.objectContaining({ created_by: 'creator-from-link' })
+ )
+
+ await expectLogic(logic, () => logic.actions.setFilters({ createdBy: 'creator-1' })).toFinishAllListeners()
+ expect(router.values.searchParams).toEqual({
+ tab: 'insights',
+ alert_id: alert.id,
+ created_by: 'creator-1',
+ })
+ expect(router.values.hashParams).toEqual({ section: 'alerts' })
+ expect(listSpy).toHaveBeenLastCalledWith(undefined, expect.objectContaining({ created_by: 'creator-1' }))
+
+ await expectLogic(logic, () => logic.actions.setFilters({ createdBy: 'All users' })).toFinishAllListeners()
+ expect(router.values.searchParams).toEqual({ tab: 'insights', alert_id: alert.id })
+ expect(listSpy.mock.lastCall?.[1]).not.toHaveProperty('created_by')
+
+ await expectLogic(logic, () => router.actions.push(urls.alerts(), { created_by: 'creator-2' }))
+ .toFinishAllListeners()
+ .toMatchValues({ filters: { search: '', createdBy: 'creator-2' } })
+
+ await expectLogic(logic, () => router.actions.push(urls.alerts()))
+ .toFinishAllListeners()
+ .toMatchValues({ filters: { search: '', createdBy: 'All users' } })
+ logic.unmount()
+ })
+
it('keeps a deleted alert out of a reloaded list', async () => {
deleteSpy = jest.spyOn(api.alerts, 'delete').mockResolvedValue()
const nextAlert = { ...alert, id: 'alert-2', name: 'Newer alert' }
diff --git a/products/alerts/frontend/logic/alertsLogic.ts b/products/alerts/frontend/logic/alertsLogic.ts
index c9ceca4308b2..32c872bab5c7 100644
--- a/products/alerts/frontend/logic/alertsLogic.ts
+++ b/products/alerts/frontend/logic/alertsLogic.ts
@@ -1,11 +1,13 @@
import { MakeLogicType, actions, afterMount, kea, listeners, path, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
+import { actionToUrl, router, urlToAction } from 'kea-router'
import { PaginationManual } from '@posthog/lemon-ui'
import api from 'lib/api'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { objectClean } from 'lib/utils/objects'
+import { urls } from 'scenes/urls'
import { AlertState } from '~/queries/schema/schema-general'
@@ -347,7 +349,42 @@ export const alertsLogic = kea<alertsLogicType>([
},
})),
- afterMount(({ actions }) => actions.loadAlerts()),
+ actionToUrl(({ values }) => ({
+ setFilters: ({ filters }) => {
+ if (!('createdBy' in filters)) {
+ return
+ }
+ const { pathname, searchParams, hashParams } = router.values.currentLocation
+ if (!pathname.endsWith(urls.alerts())) {
+ return
+ }
+ return [
+ pathname,
+ {
+ ...searchParams,
+ created_by:
+ values.filters.createdBy === DEFAULT_ALERTS_FILTERS.createdBy
+ ? undefined
+ : values.filters.createdBy,
+ },
+ hashParams,
+ { replace: true },
+ ]
+ },
+ })),
+ urlToAction(({ actions, values }) => ({
+ [urls.alerts()]: (_, searchParams) => {
+ const createdBy = searchParams.created_by || DEFAULT_ALERTS_FILTERS.createdBy
+ if (createdBy !== values.filters.createdBy) {
+ actions.setFilters({ createdBy })
+ }
+ },
+ })),
+ afterMount(({ actions, values }) => {
+ if (!values.alertsResponseLoading) {
+ actions.loadAlerts()
+ }
+ }),
])
const alertComparatorKey = (alert: AlertType): number => {
diff --git a/products/alerts/frontend/views/InsightAlerts.tsx b/products/alerts/frontend/views/InsightAlerts.tsx
index 10cd0cc177b5..1eac446b6d78 100644
--- a/products/alerts/frontend/views/InsightAlerts.tsx
+++ b/products/alerts/frontend/views/InsightAlerts.tsx
@@ -70,6 +70,8 @@ function AlertRowMenu({ alert, destinationCount, deleting, onDelete }: AlertRowM
export function InsightAlerts({ alertId }: InsightAlertsProps): JSX.Element {
const { push } = useActions(router)
+ const { searchParams } = useValues(router)
+ const listSearchParams = { ...searchParams, alert_id: undefined }
const logic = alertsLogic()
const { deleteAlert, loadAlerts, toggleAlertEnabled } = useActions(logic)
const {
@@ -97,7 +99,12 @@ export function InsightAlerts({ alertId }: InsightAlertsProps): JSX.Element {
return (
<>
<LemonTableLink
- to={urls.alert(alert.id)}
+ to={
+ urls.alert(alert.id) +
+ (searchParams.created_by
+ ? `&created_by=${encodeURIComponent(searchParams.created_by)}`
+ : '')
+ }
className={alert.enabled ? '' : 'text-muted'}
title={
<div className="flex flex-row gap-3 items-center">
@@ -223,19 +230,19 @@ export function InsightAlerts({ alertId }: InsightAlertsProps): JSX.Element {
<>
{alertForEditModal && (
<EditAlertModal
- onClose={() => push(urls.alerts())}
+ onClose={() => push(urls.alerts(), listSearchParams)}
isOpen
alert={alertForEditModal}
useAlertCheckPreview
onEditSuccess={() => {
loadAlerts()
- push(urls.alerts())
+ push(urls.alerts(), listSearchParams)
}}
/>
)}
{alertId && !alertForEditModal && !alertLoading && (
- <AlertNotFoundModal isOpen onClose={() => push(urls.alerts())} />
+ <AlertNotFoundModal isOpen onClose={() => push(urls.alerts(), listSearchParams)} />
)}
<AlertsFiltersBar />