: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, connect, events, kea, key, listeners, path, props, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import api from 'lib/api'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { DataManagementTab } from 'scenes/data-management/DataManagementScene'
import { Scene } from 'scenes/sceneTypes'
import { urls } from 'scenes/urls'
import { SIDE_PANEL_CONTEXT_KEY, SidePanelSceneContext } from '~/layout/navigation-3000/sidepanel/types'
import { ActionStepType, ActionType, ActivityScope, Breadcrumb, HogFunctionType, ProjectTreeRef } from '~/types'
import type { FeatureFlagsSet } from '../../../../frontend/src/lib/logic/featureFlagLogic'
import { actionEditLogic } from './actionEditLogic'
export interface ActionLogicProps {
id: ActionType['id']
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface actionLogicValues {
featureFlags: FeatureFlagsSet // featureFlagLogic
action: ActionType | null
actionLoading: boolean
breadcrumbs: Breadcrumb[]
hasCohortFilters: boolean
isComplete: boolean
matchingHogFunctions: HogFunctionType[] | null
matchingHogFunctionsLoading: boolean
pollTimeout: number | null
projectTreeRef: ProjectTreeRef
sidePanelContext: SidePanelSceneContext | null
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface actionLogicActions {
checkIsFinished: (action: any) => {
action: any
}
loadAction: () => any
loadActionFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadActionSuccess: (
action: ActionType,
payload?: any
) => {
action: ActionType
payload?: any
}
loadMatchingHogFunctions: () => any
loadMatchingHogFunctionsFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadMatchingHogFunctionsSuccess: (
matchingHogFunctions: HogFunctionType[],
payload?: any
) => {
matchingHogFunctions: HogFunctionType[]
payload?: any
}
setIsComplete: (isComplete: any) => {
isComplete: any
}
setPollTimeout: (pollTimeout: any) => {
pollTimeout: any
}
updateAction: (action: Partial<ActionType>) => {
action: Partial<ActionType>
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface actionLogicMeta {
key: string
__keaTypeGenInternalSelectorTypes: {
projectTreeRef: (id: number) => ProjectTreeRef
hasCohortFilters: (action: ActionType | null) => boolean
breadcrumbs: (action: ActionType | null, arg: string | null) => Breadcrumb[]
sidePanelContext: (action: ActionType | null) => SidePanelSceneContext | null
}
}
export type actionLogicType = MakeLogicType<actionLogicValues, actionLogicActions, ActionLogicProps, actionLogicMeta>
export const actionLogic = kea<actionLogicType>([
props({} as ActionLogicProps),
key((props) => `${props.id || 'new'}`),
connect(() => ({
values: [featureFlagLogic, ['featureFlags']],
})),
path((key) => ['scenes', 'actions', 'actionLogic', key]),
actions(() => ({
updateAction: (action: Partial<ActionType>) => ({ action }),
checkIsFinished: (action) => ({ action }),
setPollTimeout: (pollTimeout) => ({ pollTimeout }),
setIsComplete: (isComplete) => ({ isComplete }),
})),
loaders(({ props }) => ({
action: [
null as ActionType | null,
{
loadAction: async () => {
if (!props.id) {
throw new Error('Cannot fetch an unsaved action from the API.')
}
return await api.actions.get(props.id)
},
},
],
matchingHogFunctions: [
null as HogFunctionType[] | null,
{
loadMatchingHogFunctions: async () => {
const res = await api.hogFunctions.list({
filter_groups: [{ actions: [{ id: `${props.id}`, type: 'actions' }] }],
})
return res.results
},
},
],
})),
reducers(() => ({
action: [
null as ActionType | null,
{
updateAction: (state, { action }) => (state ? { ...state, ...action } : null),
},
],
pollTimeout: [
null as number | null,
{
setPollTimeout: (_, { pollTimeout }) => pollTimeout,
},
],
isComplete: [
false as boolean,
{
setIsComplete: (_, { isComplete }) => isComplete,
},
],
})),
selectors({
projectTreeRef: [(_, p) => [p.id], (id: number): ProjectTreeRef => ({ type: 'action', ref: String(id) })],
hasCohortFilters: [
(s) => [s.action],
(action: ActionType | null) =>
action?.steps?.some((step: ActionStepType) => step.properties?.find((p: any) => p.type === 'cohort')) ??
false,
],
breadcrumbs: [
(s) => [
s.action,
(state, props) => actionEditLogic.findMounted({ id: props?.id })?.selectors.action(state).name || null,
],
(action: ActionType | null, inProgressName: string | null): Breadcrumb[] => [
{
key: DataManagementTab.Actions,
name: 'Actions',
path: urls.actions(),
iconType: 'action',
},
{
key: [Scene.Action, action?.id || 'new'],
name: inProgressName ?? (action?.name || ''),
forceEditMode: !action?.id,
iconType: 'action',
},
],
],
[SIDE_PANEL_CONTEXT_KEY]: [
(s) => [s.action],
(action: ActionType | null): SidePanelSceneContext | null => {
return action?.id
? {
activity_scope: ActivityScope.ACTION,
activity_item_id: `${action.id}`,
access_control_resource: 'action',
access_control_resource_id: `${action.id}`,
}
: null
},
],
}),
listeners(({ actions, values }) => ({
checkIsFinished: ({ action }) => {
if (action.is_calculating) {
actions.setPollTimeout(setTimeout(() => actions.loadAction(), 1000))
} else {
actions.setIsComplete(new Date())
values.pollTimeout && clearTimeout(values.pollTimeout)
}
},
loadActionSuccess: ({ action }) => {
actions.setIsComplete(false)
actions.checkIsFinished(action)
},
})),
events(({ values, actions, props }) => ({
afterMount: () => {
props.id && actions.loadAction()
},
beforeUnmount: () => {
values.pollTimeout && clearTimeout(values.pollTimeout)
},
})),
])