: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, kea, key, listeners, path, props, reducers, selectors } from 'kea'
import posthog from 'posthog-js'
import { createFuse } from 'lib/utils/fuseSearch'
import { Scene } from 'scenes/sceneTypes'
import { teamLogic } from 'scenes/teamLogic'
import { userLogic } from 'scenes/userLogic'
import { dashboardsModel } from '~/models/dashboardsModel'
import { DashboardBasicType } from '~/types'
import type { DashboardType, TeamPublicType, TeamType, UserType } from '../../../types'
export type DashboardCompatibleScenes =
| Scene.ProjectHomepage
| Scene.Person
| Scene.Group
| Scene.ProductAnalyticsHomeTab
export interface SceneDashboardChoiceModalProps {
scene: DashboardCompatibleScenes
}
export type DashboardChoiceView = 'starter' | 'existing'
export const sceneDescription: Record<DashboardCompatibleScenes, string> = {
[Scene.Person]: 'persons',
[Scene.Group]: 'groups',
[Scene.ProjectHomepage]: 'this project',
[Scene.ProductAnalyticsHomeTab]: 'the product analytics Home tab',
}
// Scenes whose choice is team-scoped (currentTeam.<field>) rather than the per-user
// user.scene_personalisation default that other scenes use.
const TEAM_SCOPED_DASHBOARD_FIELD: Partial<Record<DashboardCompatibleScenes, keyof TeamType>> = {
[Scene.ProjectHomepage]: 'primary_dashboard',
[Scene.ProductAnalyticsHomeTab]: 'home_tab_dashboard',
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sceneDashboardChoiceModalLogicValues {
nameSortedDashboards: (DashboardBasicType | DashboardType)[] // dashboardsModel
currentTeam: TeamPublicType | TeamType | null // teamLogic
user: UserType | null // userLogic
currentDashboardId: number | null
dashboardChoiceView: DashboardChoiceView
dashboards: (DashboardBasicType | DashboardType)[]
isOpen: boolean
searchTerm: string | null
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sceneDashboardChoiceModalLogicActions {
loadDashboardsIfNeeded: () => {
value: true
} // dashboardsModel
updateCurrentTeam: (payload: Partial<TeamType>) => Partial<TeamType> // teamLogic
setUserScenePersonalisation: (
scene: DashboardCompatibleScenes,
dashboard: number
) => {
dashboard: number
scene: DashboardCompatibleScenes
} // userLogic
closeSceneDashboardChoiceModal: () => boolean
setSceneDashboardChoice: (dashboardId: number | null) => {
dashboardId: number | null
}
setSearchTerm: (searchTerm: string) => {
searchTerm: string
}
showDashboardPicker: () => boolean
showSceneDashboardChoiceModal: () => boolean
showStarterOptions: () => boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sceneDashboardChoiceModalLogicMeta {
key: string
__keaTypeGenInternalSelectorTypes: {
currentDashboardId: (currentTeam: TeamPublicType | TeamType | null, user: UserType | null) => number | null
dashboards: (
searchTerm: string | null,
nameSortedDashboards: (DashboardBasicType | DashboardType)[]
) => (DashboardBasicType | DashboardType)[]
}
}
export type sceneDashboardChoiceModalLogicType = MakeLogicType<
sceneDashboardChoiceModalLogicValues,
sceneDashboardChoiceModalLogicActions,
SceneDashboardChoiceModalProps,
sceneDashboardChoiceModalLogicMeta
>
export const sceneDashboardChoiceModalLogic = kea<sceneDashboardChoiceModalLogicType>([
path((key) => ['lib', 'components', 'SceneDashboardChoice', 'sceneDashboardChoiceModalLogic', key || 'unknown']),
props({} as SceneDashboardChoiceModalProps),
key((props) => `${props.scene}`),
connect(() => ({
actions: [
teamLogic,
['updateCurrentTeam'],
userLogic,
['setUserScenePersonalisation'],
dashboardsModel,
['loadDashboardsIfNeeded'],
],
values: [teamLogic, ['currentTeam'], userLogic, ['user'], dashboardsModel, ['nameSortedDashboards']],
})),
actions({
showSceneDashboardChoiceModal: () => true,
closeSceneDashboardChoiceModal: () => true,
setSceneDashboardChoice: (dashboardId: number | null) => ({ dashboardId }),
setSearchTerm: (searchTerm: string) => ({ searchTerm }),
showDashboardPicker: () => true,
showStarterOptions: () => true,
}),
reducers({
searchTerm: [null as string | null, { setSearchTerm: (_, { searchTerm }) => searchTerm }],
isOpen: [false, { showSceneDashboardChoiceModal: () => true, closeSceneDashboardChoiceModal: () => false }],
dashboardChoiceView: [
'existing' as DashboardChoiceView,
{
showDashboardPicker: () => 'existing',
showStarterOptions: () => 'starter',
},
],
}),
selectors(({ props }) => ({
currentDashboardId: [
(s) => [s.currentTeam, s.user],
(
currentTeam: null | import('~/types').TeamPublicType | import('~/types').TeamType,
user: null | import('~/types').UserType
) => {
const teamScopedField = TEAM_SCOPED_DASHBOARD_FIELD[props.scene]
let currentDashboard: number | DashboardBasicType | null = teamScopedField
? ((currentTeam?.[teamScopedField] as number | null) ?? null)
: (user?.scene_personalisation?.find((choice) => choice.scene === props.scene)?.dashboard ?? null)
return (typeof currentDashboard === 'number' ? currentDashboard : currentDashboard?.id) ?? null
},
],
dashboards: [
(s) => [s.searchTerm, s.nameSortedDashboards],
(searchTerm: string | null, dashboards: (DashboardBasicType | import('~/types').DashboardType)[]) => {
dashboards = dashboards
.filter((d) => !d.deleted)
.sort((a, b) => (a.name ?? 'Untitled').localeCompare(b.name ?? 'Untitled'))
if (!searchTerm) {
return dashboards
}
return createFuse(dashboards, {
keys: ['key', 'name', 'description', 'tags'],
})
.search(searchTerm)
.map((result) => result.item)
},
],
})),
listeners(({ actions, props }) => ({
setSceneDashboardChoice: async ({ dashboardId }) => {
const teamScopedField = TEAM_SCOPED_DASHBOARD_FIELD[props.scene]
if (teamScopedField) {
actions.updateCurrentTeam({ [teamScopedField]: dashboardId ?? null })
posthog.capture(
props.scene === Scene.ProjectHomepage ? 'primary dashboard changed' : 'home tab dashboard changed'
)
} else {
actions.setUserScenePersonalisation(props.scene, dashboardId ?? 0)
posthog.capture('scene dashboard choice set', { scene: props.scene, dashboardId: dashboardId })
}
},
showSceneDashboardChoiceModal: async () => {
if (props.scene === Scene.ProductAnalyticsHomeTab) {
actions.showStarterOptions()
}
actions.loadDashboardsIfNeeded()
if (props.scene === Scene.ProjectHomepage) {
posthog.capture('primary dashboard modal opened')
} else if (props.scene === Scene.ProductAnalyticsHomeTab) {
posthog.capture('home tab dashboard modal opened')
} else {
posthog.capture('scene dashboard choice modal opened', { scene: props.scene })
}
},
})),
])