: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, actions, afterMount, connect, kea, listeners, path } from 'kea'
import { loaders } from 'kea-loaders'
import { subscriptions } from 'kea-subscriptions'
import { productSetupStatusLogic } from 'lib/components/ProductEmptyState/productSetupStatusLogic'
import type { ProductSetupStatus } from 'lib/components/ProductEmptyState/types'
import { teamLogic } from 'scenes/teamLogic'
import { ProductKey } from '~/queries/schema/schema-general'
import type { TeamPublicType, TeamType } from '~/types'
import { conversationsTicketsList } from 'products/conversations/frontend/generated/api'
// While waiting for the first ticket we re-check on a timer so the page advances on
// its own as tickets land. The disposables plugin pauses this on hidden tabs.
const POLL_INTERVAL_MS = 20000
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface supportSetupLogicValues {
setupStatus: ProductSetupStatus // productSetupStatusLogic
currentProjectId: number | string // teamLogic
currentTeam: TeamPublicType | TeamType | null // teamLogic
ticketCount: number | null
ticketCountLoading: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface supportSetupLogicActions {
setDetectedStatus: (status: ProductSetupStatus) => {
status: ProductSetupStatus
} // productSetupStatusLogic
checkSetup: () => {
value: true
}
loadTicketCount: (_: void) => void
loadTicketCountFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadTicketCountSuccess: (
ticketCount: number,
payload?: void
) => {
ticketCount: number
payload?: void
}
}
export type supportSetupLogicType = MakeLogicType<supportSetupLogicValues, supportSetupLogicActions>
/**
* Setup detection for the Support empty state, with the full three-state ladder:
* the team hasn't enabled conversations → `needs-setup` (no query needed); enabled
* but no tickets yet → `waiting-for-data` (kept fresh by a poll until the first
* ticket lands); at least one ticket → `has-data`. The `currentTeam` subscription
* covers the user flipping the enable switch in settings and coming back.
*/
export const supportSetupLogic = kea<supportSetupLogicType>([
path(['products', 'conversations', 'frontend', 'emptyState', 'supportSetupLogic']),
connect(() => ({
actions: [productSetupStatusLogic({ productKey: ProductKey.CONVERSATIONS }), ['setDetectedStatus']],
values: [
productSetupStatusLogic({ productKey: ProductKey.CONVERSATIONS }),
['status as setupStatus'],
teamLogic,
['currentTeam', 'currentProjectId'],
],
})),
actions({
checkSetup: true,
}),
loaders(({ values }) => ({
ticketCount: [
null as number | null,
{
loadTicketCount: async (_: void, breakpoint): Promise<number> => {
const response = await conversationsTicketsList(String(values.currentProjectId), { limit: 1 })
breakpoint()
return response.count
},
},
],
})),
listeners(({ actions, values, cache }) => ({
checkSetup: () => {
// No team loaded yet: leave the status at `loading` and let the
// `currentTeam` subscription re-run this once it arrives.
if (!values.currentTeam) {
return
}
actions.loadTicketCount()
},
loadTicketCountSuccess: () => {
// Tickets outrank the toggle. Turning support off keeps the tickets, and the
// settings copy says so, but this list is the only place to read them.
if (values.ticketCount && values.ticketCount > 0) {
actions.setDetectedStatus('has-data')
cache.disposables.dispose('poll')
} else if (!values.currentTeam?.conversations_enabled) {
// Nothing to wait for while support is off. Enabling it updates the team,
// and the `currentTeam` subscription re-runs the check.
actions.setDetectedStatus('needs-setup')
cache.disposables.dispose('poll')
} else {
actions.setDetectedStatus('waiting-for-data')
}
},
loadTicketCountFailure: () => {
// Never strand the gate on its spinner: if nothing has answered yet, fail
// open to the real scene. Don't downgrade an existing answer on a poll blip.
if (values.setupStatus === 'loading') {
actions.setDetectedStatus('unknown')
}
},
})),
subscriptions(({ actions }) => ({
// kea-subscriptions fires once at mount for any value that isn't `undefined`, and
// `currentTeam` defaults to `null`, so without this guard every mount checked twice:
// once here and once from `afterMount`. Still fires on the null-to-team transition,
// which is the case this subscription exists for.
currentTeam: (_, previousTeam) => {
if (previousTeam !== undefined) {
actions.checkSetup()
}
},
})),
afterMount(({ actions, cache }) => {
actions.checkSetup()
// The poll stops for good once the first ticket arrives, or once a project with no
// tickets is found to have support switched off.
cache.disposables.add(() => {
const id = window.setInterval(() => actions.checkSetup(), POLL_INTERVAL_MS)
return () => clearInterval(id)
}, 'poll')
}),
])