: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
feat(alerts): paginate alerts tab on the insights page (#60674)
Matt Pua58b61658ed0b
diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.tsindex 59cf4b1e4f05..e1cd4112e2a8 100644--- a/frontend/src/lib/api.ts+++ b/frontend/src/lib/api.ts@@ -6114,8 +6114,15 @@ const api = {async update(alertId: AlertType['id'], data: Partial<AlertTypeWrite>): Promise<AlertType> {return await new ApiRequest().alert(alertId).update({ data })},- async list(insightId?: InsightModel['id']): Promise<PaginatedResponse<AlertType>> {- return await new ApiRequest().alerts(undefined, insightId).get()+ async list(+ insightId?: InsightModel['id'],+ params: { limit?: number; offset?: number } = {}+ ): Promise<CountedPaginatedResponse<AlertType>> {+ const queryParams: Record<string, any> = { ...params }+ if (insightId !== undefined) {+ queryParams.insight_id = insightId+ }+ return await new ApiRequest().alerts().withQueryString(toParams(queryParams)).get()},async delete(alertId: AlertType['id']): Promise<void> {return await new ApiRequest().alert(alertId).delete()diff --git a/frontend/src/lib/components/Alerts/alertFormLogic.test.ts b/frontend/src/lib/components/Alerts/alertFormLogic.test.tsindex 873a7ca30780..c2d8bd34c85e 100644--- a/frontend/src/lib/components/Alerts/alertFormLogic.test.ts+++ b/frontend/src/lib/components/Alerts/alertFormLogic.test.ts@@ -107,7 +107,7 @@ describe('alertFormLogic', () => {beforeEach(() => {initKeaTests()- jest.spyOn(api.alerts, 'list').mockResolvedValue({ results: [] })+ jest.spyOn(api.alerts, 'list').mockResolvedValue({ results: [], count: 0 })jest.spyOn(api.alerts, 'get').mockResolvedValue(makeSavedAlert())createSpy = jest.spyOn(api.alerts, 'create').mockResolvedValue(makeSavedAlert())updateSpy = jest.spyOn(api.alerts, 'update').mockResolvedValue(makeSavedAlert())diff --git a/frontend/src/lib/components/Alerts/alertsLogic.ts b/frontend/src/lib/components/Alerts/alertsLogic.tsindex c47d5a51d404..38aeb4313f35 100644--- a/frontend/src/lib/components/Alerts/alertsLogic.ts+++ b/frontend/src/lib/components/Alerts/alertsLogic.ts@@ -1,6 +1,8 @@-import { afterMount, kea, path, selectors } from 'kea'+import { actions, afterMount, kea, listeners, path, reducers, selectors } from 'kea'import { loaders } from 'kea-loaders'+import { PaginationManual } from '@posthog/lemon-ui'+import api from 'lib/api'import { AlertState } from '~/queries/schema/schema-general'@@ -11,25 +13,65 @@ import { AlertType } from './types'export interface AlertsLogicProps extends AlertLogicProps {}+export const ALERTS_PER_PAGE = 30+export const alertsLogic = kea<alertsLogicType>([path(['lib', 'components', 'Alerts', 'alertsLogic']),- loaders({- alerts: {- __default: [] as AlertType[],- loadAlerts: async () => {- const response = await api.alerts.list()- return response.results+ actions({+ setPage: (page: number) => ({ page }),+ }),++ reducers({+ page: [+ 1,+ {+ setPage: (_, { page }) => page,},- },+ ],}),- selectors({+ loaders(({ values }) => ({+ alertsResponse: [+ { results: [], count: 0 } as { results: AlertType[]; count: number },+ {+ loadAlerts: async () => {+ const response = await api.alerts.list(undefined, {+ limit: ALERTS_PER_PAGE,+ offset: (values.page - 1) * ALERTS_PER_PAGE,+ })+ return { results: response.results, count: response.count ?? response.results.length }+ },+ },+ ],+ })),++ selectors(({ actions }) => ({+ alerts: [(s) => [s.alertsResponse], (response): AlertType[] => response.results],+ alertsCount: [(s) => [s.alertsResponse], (response): number => response.count],alertsSortedByState: [(s) => [s.alerts],- (alerts: AlertType[]): AlertType[] => alerts.sort((a, b) => alertComparatorKey(a) - alertComparatorKey(b)),+ (alerts: AlertType[]): AlertType[] =>+ [...alerts].sort((a, b) => alertComparatorKey(a) - alertComparatorKey(b)),],- }),+ pagination: [+ (s) => [s.page, s.alertsCount],+ (page, count): PaginationManual => ({+ controlled: true,+ pageSize: ALERTS_PER_PAGE,+ currentPage: page,+ entryCount: count,+ onBackward: () => actions.setPage(page - 1),+ onForward: () => actions.setPage(page + 1),+ }),+ ],+ })),++ listeners(({ actions }) => ({+ setPage: () => {+ actions.loadAlerts()+ },+ })),afterMount(({ actions }) => actions.loadAlerts()),])diff --git a/frontend/src/lib/components/Alerts/insightAlertsLogic.test.ts b/frontend/src/lib/components/Alerts/insightAlertsLogic.test.tsindex 2ac4009fff23..88c0e9997095 100644--- a/frontend/src/lib/components/Alerts/insightAlertsLogic.test.ts+++ b/frontend/src/lib/components/Alerts/insightAlertsLogic.test.ts@@ -36,7 +36,7 @@ describe('insightAlertsLogic', () => {beforeEach(() => {initKeaTests()- listSpy = jest.spyOn(api.alerts, 'list').mockResolvedValue({ results: [] })+ listSpy = jest.spyOn(api.alerts, 'list').mockResolvedValue({ results: [], count: 0 })})afterEach(() => {diff --git a/frontend/src/lib/components/Alerts/views/Alerts.tsx b/frontend/src/lib/components/Alerts/views/Alerts.tsxindex 5ba851006485..ec97a014a271 100644--- a/frontend/src/lib/components/Alerts/views/Alerts.tsx+++ b/frontend/src/lib/components/Alerts/views/Alerts.tsx@@ -28,7 +28,7 @@ export function Alerts({ alertId }: AlertsProps): JSX.Element {const { push } = useActions(router)const logic = alertsLogic()const { loadAlerts } = useActions(logic)- const { alertsSortedByState, alertsLoading } = useValues(logic)+ const { alertsSortedByState, alertsResponseLoading, pagination, alertsCount } = useValues(logic)const { alert } = useValues(alertLogic({ alertId }))@@ -125,7 +125,7 @@ export function Alerts({ alertId }: AlertsProps): JSX.Element {},]- const isEmpty = alertsSortedByState.length === 0 && !alertsLoading+ const isEmpty = alertsCount === 0 && !alertsResponseLoading// TODO: add info here to sign up for alerts early accessreturn (<>@@ -164,13 +164,14 @@ export function Alerts({ alertId }: AlertsProps): JSX.Element {{isEmpty ? null : (<LemonTable- loading={alertsLoading}+ loading={alertsResponseLoading}columns={columns}dataSource={alertsSortedByState}noSortingCancellationrowKey="id"loadingSkeletonRows={5}nouns={['alert', 'alerts']}+ pagination={pagination}rowClassName={(alert) => (alert.state === AlertState.NOT_FIRING ? null : 'highlighted')}/>)}