: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,
props,
propsChanged,
reducers,
selectors,
} from 'kea'
import { iframedToolbarBrowserLogic } from 'lib/components/IframedToolbarBrowser/iframedToolbarBrowserLogic'
import { PostHogAppToolbarEvent } from 'lib/components/IframedToolbarBrowser/utils'
import { isEmptyObject } from 'lib/utils/guards'
import { ActionType, BaseMathType, DashboardTemplateVariableType, EntityTypes, TemplateVariableStep } from '~/types'
export interface DashboardTemplateVariablesLogicProps {
variables: DashboardTemplateVariableType[]
}
const FALLBACK_EVENT = {
id: '$pageview',
math: BaseMathType.UniqueUsers,
type: EntityTypes.EVENTS,
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface dashboardTemplateVariablesLogicValues {
activeVariable: DashboardTemplateVariableType
activeVariableCustomEventName: string | null | undefined
activeVariableIndex: number
allVariablesAreTouched: boolean
hasTouchedAnyVariable: boolean
isCurrentlySelectingElement: boolean
variables: DashboardTemplateVariableType[]
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface dashboardTemplateVariablesLogicActions {
disableElementSelector: () => {
value: true
} // iframedToolbarBrowserLogic
toolbarMessageReceived: (
type: PostHogAppToolbarEvent,
payload: Record<string, any>
) => {
payload: Record<string, any>
type: PostHogAppToolbarEvent
} // iframedToolbarBrowserLogic
goToNextUntouchedActiveVariableIndex: () => {
value: true
}
incrementActiveVariableIndex: () => {
value: true
}
maybeResetActiveVariableCustomEventName: () => {
value: true
}
possiblyIncrementActiveVariableIndex: () => {
value: true
}
resetVariable: (variableId: string) => {
variableId: string
}
setActiveVariableCustomEventName: (customEventName?: string | null) => {
customEventName: string | null | undefined
}
setActiveVariableIndex: (index: number) => {
index: number
}
setIsCurrentlySelectingElement: (isSelecting: boolean) => {
isSelecting: boolean
}
setVariable: (
variableName: string,
step: TemplateVariableStep | undefined
) => {
step: TemplateVariableStep | undefined
variable_name: string
}
setVariableForPageview: (
variableName: string,
url: string
) => {
url: string
variableName: string
}
setVariableForScreenview: (variableName: string) => {
variableName: string
}
setVariableFromAction: (
variableName: string,
action: ActionType
) => {
action: ActionType
variableName: string
}
setVariables: (variables: DashboardTemplateVariableType[]) => {
variables: DashboardTemplateVariableType[]
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface dashboardTemplateVariablesLogicMeta {
__keaTypeGenInternalSelectorTypes: {
activeVariable: (
variables: DashboardTemplateVariableType[],
activeVariableIndex: number
) => DashboardTemplateVariableType
allVariablesAreTouched: (variables: DashboardTemplateVariableType[]) => boolean
hasTouchedAnyVariable: (variables: DashboardTemplateVariableType[]) => boolean
}
}
export type dashboardTemplateVariablesLogicType = MakeLogicType<
dashboardTemplateVariablesLogicValues,
dashboardTemplateVariablesLogicActions,
DashboardTemplateVariablesLogicProps,
dashboardTemplateVariablesLogicMeta
>
export const dashboardTemplateVariablesLogic = kea<dashboardTemplateVariablesLogicType>([
path(['scenes', 'dashboard', 'DashboardTemplateVariablesLogic']),
props({ variables: [] } as DashboardTemplateVariablesLogicProps),
connect(() => ({
actions: [iframedToolbarBrowserLogic, ['toolbarMessageReceived', 'disableElementSelector']],
})),
actions({
setVariables: (variables: DashboardTemplateVariableType[]) => ({ variables }),
setVariable: (variableName: string, step: TemplateVariableStep | undefined) => ({
variable_name: variableName,
step,
}),
setVariableFromAction: (variableName: string, action: ActionType) => ({ variableName, action }),
setVariableForPageview: (variableName: string, url: string) => ({ variableName, url }),
setVariableForScreenview: (variableName: string) => ({ variableName }),
setActiveVariableIndex: (index: number) => ({ index }),
incrementActiveVariableIndex: true,
possiblyIncrementActiveVariableIndex: true,
resetVariable: (variableId: string) => ({ variableId }),
goToNextUntouchedActiveVariableIndex: true,
setIsCurrentlySelectingElement: (isSelecting: boolean) => ({ isSelecting }),
setActiveVariableCustomEventName: (customEventName?: string | null) => ({ customEventName }),
maybeResetActiveVariableCustomEventName: true,
}),
reducers({
variables: [
[] as DashboardTemplateVariableType[],
{
setVariables: (_, { variables }) => {
return variables.map((v) => {
if (v.default && !isEmptyObject(v.default)) {
return v
}
return { ...v, default: FALLBACK_EVENT } as unknown as DashboardTemplateVariableType
})
},
setVariable: (state, { variable_name: variableName, step }): DashboardTemplateVariableType[] => {
if (!step) {
return state
}
return state.map((v: DashboardTemplateVariableType) => {
if (v.name === variableName) {
return { ...v, default: step, touched: true }
}
return { ...v }
})
},
resetVariable: (state, { variableId }) => {
return state.map((v: DashboardTemplateVariableType) => {
if (v.id === variableId) {
return { ...v, default: FALLBACK_EVENT, touched: false }
}
return { ...v }
})
},
},
],
activeVariableIndex: [
0,
{
setActiveVariableIndex: (_, { index }) => index,
incrementActiveVariableIndex: (state) => state + 1,
},
],
activeVariableCustomEventName: [
null as string | null | undefined,
{
setActiveVariableCustomEventName: (_, { customEventName }) => customEventName,
},
],
isCurrentlySelectingElement: [
false as boolean,
{
setIsCurrentlySelectingElement: (_, { isSelecting }) => isSelecting,
},
],
}),
selectors(() => ({
activeVariable: [
(s) => [s.variables, s.activeVariableIndex],
(variables: DashboardTemplateVariableType[], activeVariableIndex: number) => {
return variables[activeVariableIndex]
},
],
allVariablesAreTouched: [
(s) => [s.variables],
(variables: DashboardTemplateVariableType[]) => {
return variables.every((v) => v.touched)
},
],
hasTouchedAnyVariable: [
(s) => [s.variables],
(variables: DashboardTemplateVariableType[]) => {
return variables.some((v) => v.touched)
},
],
})),
listeners(({ actions, props, values }) => ({
possiblyIncrementActiveVariableIndex: () => {
if (props.variables.length > 0 && values.activeVariableIndex < props.variables.length - 1) {
actions.incrementActiveVariableIndex()
}
},
goToNextUntouchedActiveVariableIndex: () => {
let nextIndex = values.variables.findIndex((v, i) => !v.touched && i > values.activeVariableIndex)
if (nextIndex !== -1) {
actions.setActiveVariableIndex(nextIndex)
return
}
if (nextIndex == -1) {
nextIndex = values.variables.findIndex((v) => !v.touched)
if (nextIndex == -1) {
nextIndex = values.activeVariableIndex
}
}
actions.setActiveVariableIndex(nextIndex)
},
setVariableFromAction: ({ variableName, action }) => {
const originalVariableName = variableName.replace(/\s-\s\d+/g, '')
const step: TemplateVariableStep = {
id: action.id.toString(),
math: BaseMathType.UniqueUsers,
name: action.name,
custom_name: originalVariableName,
order: 0,
type: EntityTypes.ACTIONS,
selector: action.steps?.[0]?.selector,
href: action.steps?.[0]?.href,
url: action.steps?.[0]?.url,
}
actions.setVariable(originalVariableName, step)
actions.setIsCurrentlySelectingElement(false)
},
setVariableForPageview: ({ variableName, url }) => {
const step: TemplateVariableStep = {
id: '$pageview',
math: BaseMathType.UniqueUsers,
type: EntityTypes.EVENTS,
order: 0,
name: '$pageview',
custom_name: variableName,
properties: [
{
key: '$current_url',
value: url,
operator: 'icontains',
type: 'event',
},
],
}
actions.setVariable(variableName, step)
actions.setIsCurrentlySelectingElement(false)
},
setVariableForScreenview: ({ variableName }) => {
const step: TemplateVariableStep = {
id: '$screenview',
math: BaseMathType.UniqueUsers,
type: EntityTypes.EVENTS,
order: 0,
name: '$screenview',
custom_name: variableName,
}
actions.setVariable(variableName, step)
actions.setIsCurrentlySelectingElement(false)
},
toolbarMessageReceived: ({ type, payload }) => {
if (type === PostHogAppToolbarEvent.PH_NEW_ACTION_CREATED) {
actions.setVariableFromAction(payload.action.name, payload.action as ActionType)
actions.disableElementSelector()
}
},
maybeResetActiveVariableCustomEventName: () => {
if (!values.activeVariable?.touched || !values.activeVariable?.default?.custom_event) {
actions.setActiveVariableCustomEventName(null)
} else if (values.activeVariable?.default?.custom_event) {
actions.setActiveVariableCustomEventName(values.activeVariable.default.id)
}
},
})),
propsChanged(({ actions, props }, oldProps) => {
if (props.variables !== oldProps.variables) {
actions.setVariables(props.variables)
}
}),
afterMount(({ actions, props }) => {
if (props.variables) {
actions.setVariables(props.variables)
}
}),
])