: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
import { MakeLogicType, afterMount, connect, kea, listeners, path } from 'kea'
import { loaders } from 'kea-loaders'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { organizationLogic } from 'scenes/organizationLogic'
import { teamLogic } from 'scenes/teamLogic'
import * as api from 'products/product_analytics/frontend/generated/api'
import type {
EventsRetentionApi,
OrganizationEventsRetentionApi,
} from 'products/product_analytics/frontend/generated/api.schemas'
import { dataRetentionBannerLogic } from 'products/product_analytics/frontend/insights/dataRetention/dataRetentionBannerLogic'
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface eventsRetentionSettingsLogicValues {
currentOrganizationId: string // organizationLogic
currentTeamId: number | null // teamLogic
organizationRetention: OrganizationEventsRetentionApi | null
organizationRetentionLoading: boolean
projectRetention: EventsRetentionApi | null
projectRetentionLoading: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface eventsRetentionSettingsLogicActions {
loadOrganizationRetention: () => any
loadOrganizationRetentionFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadOrganizationRetentionSuccess: (
organizationRetention: OrganizationEventsRetentionApi | null,
payload?: any
) => {
organizationRetention: OrganizationEventsRetentionApi | null
payload?: any
}
loadProjectRetention: () => any
loadProjectRetentionFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadProjectRetentionSuccess: (
projectRetention: EventsRetentionApi | null,
payload?: any
) => {
projectRetention: EventsRetentionApi | null
payload?: any
}
updateOrganizationRetention: (retentionMonths: number | null) => number | null
updateOrganizationRetentionFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
updateOrganizationRetentionSuccess: (
organizationRetention: OrganizationEventsRetentionApi | null,
payload?: number | null
) => {
organizationRetention: OrganizationEventsRetentionApi | null
payload?: number | null
}
updateProjectRetention: (retentionMonths: number | null) => number | null
updateProjectRetentionFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
updateProjectRetentionSuccess: (
projectRetention: EventsRetentionApi | null,
payload?: number | null
) => {
projectRetention: EventsRetentionApi | null
payload?: number | null
}
}
export type eventsRetentionSettingsLogicType = MakeLogicType<
eventsRetentionSettingsLogicValues,
eventsRetentionSettingsLogicActions
>
export const eventsRetentionSettingsLogic = kea<eventsRetentionSettingsLogicType>([
path(['products', 'product_analytics', 'frontend', 'settings', 'eventsRetentionSettingsLogic']),
connect(() => ({
values: [organizationLogic, ['currentOrganizationId'], teamLogic, ['currentTeamId']],
})),
loaders(({ values }) => ({
projectRetention: [
null as EventsRetentionApi | null,
{
loadProjectRetention: async () => {
if (values.currentTeamId === null) {
return null
}
return await api.eventsRetentionRetrieve(String(values.currentTeamId))
},
updateProjectRetention: async (retentionMonths: number | null) => {
if (values.currentTeamId === null) {
return values.projectRetention
}
return await api.eventsRetentionUpdate(String(values.currentTeamId), {
retention_months: retentionMonths,
})
},
},
],
organizationRetention: [
null as OrganizationEventsRetentionApi | null,
{
loadOrganizationRetention: async () => {
if (!values.currentOrganizationId) {
return null
}
return await api.organizationEventsRetentionRetrieve(values.currentOrganizationId)
},
updateOrganizationRetention: async (retentionMonths: number | null) => {
if (!values.currentOrganizationId) {
return values.organizationRetention
}
return await api.organizationEventsRetentionUpdate(values.currentOrganizationId, {
retention_months: retentionMonths,
})
},
},
],
})),
listeners(({ actions }) => ({
updateProjectRetentionSuccess: () => {
lemonToast.success('Events retention updated')
dataRetentionBannerLogic.findMounted()?.actions.loadRetentionMonths()
},
updateOrganizationRetentionSuccess: () => {
lemonToast.success('Events retention updated')
actions.loadProjectRetention()
dataRetentionBannerLogic.findMounted()?.actions.loadRetentionMonths()
},
})),
afterMount(({ actions }) => {
actions.loadProjectRetention()
actions.loadOrganizationRetention()
}),
])