: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, key, listeners, path, props, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import posthog from 'posthog-js'
import { ApiError } from 'lib/api-error'
import { FEATURE_FLAGS } from 'lib/constants'
import { featureFlagLogic, type FeatureFlagsSet } from 'lib/logic/featureFlagLogic'
import { getCurrentTeamId } from 'lib/utils/getAppContext'
import { subscriptionsSummariesList } from 'products/subscriptions/frontend/generated/api'
import type { SubscriptionSummaryApi } from 'products/subscriptions/frontend/generated/api.schemas'
export interface SubscriptionSummariesLogicProps {
insightId?: number
dashboardId?: number
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface subscriptionSummariesLogicValues {
featureFlags: FeatureFlagsSet // featureFlagLogic
latestSummary: SubscriptionSummaryApi | null
summaries: SubscriptionSummaryApi[] | null
summariesLoading: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface subscriptionSummariesLogicActions {
loadSummaries: () => any
loadSummariesFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadSummariesSuccess: (
summaries: SubscriptionSummaryApi[] | null,
payload?: any
) => {
summaries: SubscriptionSummaryApi[] | null
payload?: any
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface subscriptionSummariesLogicMeta {
key: string
__keaTypeGenInternalSelectorTypes: {
latestSummary: (
summaries: SubscriptionSummaryApi[] | null,
featureFlags: FeatureFlagsSet
) => SubscriptionSummaryApi | null
}
}
export type subscriptionSummariesLogicType = MakeLogicType<
subscriptionSummariesLogicValues,
subscriptionSummariesLogicActions,
SubscriptionSummariesLogicProps,
subscriptionSummariesLogicMeta
>
function sourceType(props: SubscriptionSummariesLogicProps): 'dashboard' | 'insight' {
return props.dashboardId ? 'dashboard' : 'insight'
}
export const subscriptionSummariesLogic = kea<subscriptionSummariesLogicType>([
path((key) => [
'products',
'subscriptions',
'components',
'SubscriptionSummaries',
'subscriptionSummariesLogic',
key,
]),
props({} as SubscriptionSummariesLogicProps),
key((props) => `${sourceType(props)}-${props.dashboardId ?? props.insightId}`),
connect(() => ({
values: [featureFlagLogic, ['featureFlags']],
})),
loaders(({ props }) => ({
// null = not loaded yet
summaries: [
null as SubscriptionSummaryApi[] | null,
{
loadSummaries: async (): Promise<SubscriptionSummaryApi[] | null> => {
const params = props.dashboardId ? { dashboard: props.dashboardId } : { insight: props.insightId }
try {
const response = await subscriptionsSummariesList(String(getCurrentTeamId()), params)
return response.results
} catch (error) {
// A deleted source or one from another team returns 404, which means there is nothing to show.
if (error instanceof ApiError && error.status === 404) {
return []
}
throw error
}
},
},
],
})),
selectors({
// Depends on the flag too, so disabling it mid-session hides the panel immediately instead of
// waiting for a remount.
latestSummary: [
(s) => [s.summaries, s.featureFlags],
(summaries: SubscriptionSummaryApi[] | null, featureFlags: FeatureFlagsSet) =>
featureFlags[FEATURE_FLAGS.SUBSCRIPTION_SOURCE_SUMMARIES] ? (summaries?.[0] ?? null) : null,
],
}),
listeners(({ actions, props, values }) => ({
loadSummariesSuccess: ({ summaries }) => {
if (!summaries?.length) {
return
}
// pinned: analytics event name — renaming breaks dashboards
posthog.capture('subscription summaries viewed', {
source_type: sourceType(props),
summary_count: summaries.length,
subscription_count: new Set(summaries.map((summary) => summary.subscription)).size,
})
},
// The rollout can flip after mount, so a late assignment still loads summaries instead of
// waiting for the component to remount.
[featureFlagLogic.actionTypes.setFeatureFlags]: () => {
if (!props.dashboardId && !props.insightId) {
return
}
if (
values.featureFlags[FEATURE_FLAGS.SUBSCRIPTION_SOURCE_SUMMARIES] &&
values.summaries === null &&
!values.summariesLoading
) {
actions.loadSummaries()
}
},
})),
afterMount(({ actions, props, values }) => {
if (!values.featureFlags[FEATURE_FLAGS.SUBSCRIPTION_SOURCE_SUMMARIES]) {
return
}
if (!props.dashboardId && !props.insightId) {
return
}
actions.loadSummaries()
}),
])