: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, path, props, reducers, selectors, useValues } from 'kea'
import { loaders } from 'kea-loaders'
import api from 'lib/api'
import { DataColorTheme, DataColorToken, FALLBACK_DATA_COLOR_THEME } from 'lib/colors'
import { teamLogic } from 'scenes/teamLogic'
import { DataColorThemeModel } from '~/types'
import type { TeamPublicType, TeamType } from '../../types'
export function getColorFromToken(theme: DataColorTheme, colorToken: string): string {
const colorNo = parseInt(colorToken.replace('preset-', ''))
const availableColors = Object.keys(theme).length
// once all colors are exhausted, start again from the beginning
const wrappedNum = ((colorNo - 1) % availableColors) + 1
return theme[`preset-${wrappedNum}`]
}
export const ThemeName = ({ id }: { id: number }): JSX.Element => {
const { themes } = useValues(dataThemeLogic)
const theme = themes?.find((theme) => theme.id === id)
return theme ? <span>{theme.name}</span> : <span className="italic">No theme found for id: {id}</span>
}
export type DataThemeLogicProps = {
themes?: DataColorThemeModel[]
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface dataThemeLogicValues {
currentTeam: TeamPublicType | TeamType | null // teamLogic
defaultTheme: DataColorThemeModel | null
getAvailableColorTokens: (themeId: number | string | null | undefined) => DataColorToken[] | null
getColorFromToken: (themeId: number | string | null | undefined, colorToken: string) => string | null
getTheme: (themeId: number | string | null | undefined) => DataColorTheme | null
posthogTheme: DataColorThemeModel | null | undefined
themes: DataColorThemeModel[] | null
themesLoading: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface dataThemeLogicActions {
loadThemes: () => any
loadThemesFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadThemesSuccess: (
themes: DataColorThemeModel[],
payload?: any
) => {
themes: DataColorThemeModel[]
payload?: any
}
setThemes: (themes: any) => {
themes: any
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface dataThemeLogicMeta {
__keaTypeGenInternalSelectorTypes: {
posthogTheme: (themes: DataColorThemeModel[] | null) => DataColorThemeModel | null | undefined
defaultTheme: (
currentTeam: TeamPublicType | TeamType | null,
themes: DataColorThemeModel[] | null,
posthogTheme: DataColorThemeModel | null | undefined
) => DataColorThemeModel | null
getTheme: (
themes: DataColorThemeModel[] | null,
defaultTheme: DataColorThemeModel | null
) => (themeId: number | string | null | undefined) => DataColorTheme | null
getColorFromToken: (
getTheme: (themeId: number | string | null | undefined) => DataColorTheme | null
) => (themeId: number | string | null | undefined, colorToken: string) => string | null
getAvailableColorTokens: (
getTheme: (themeId: number | string | null | undefined) => DataColorTheme | null
) => (themeId: number | string | null | undefined) => DataColorToken[] | null
}
}
export type dataThemeLogicType = MakeLogicType<
dataThemeLogicValues,
dataThemeLogicActions,
DataThemeLogicProps,
dataThemeLogicMeta
>
export const dataThemeLogic = kea<dataThemeLogicType>([
props({} as DataThemeLogicProps),
path(['scenes', 'dataThemeLogic']),
connect(() => ({ values: [teamLogic, ['currentTeam']] })),
actions({ setThemes: (themes) => ({ themes }) }),
loaders(({ props }) => ({
themes: [
props.themes || null,
{
loadThemes: async () => await api.dataColorThemes.list(),
},
],
})),
reducers({
themes: {
setThemes: (_, { themes }) => themes,
loadThemesFailure: () => [],
},
}),
selectors({
posthogTheme: [
(s) => [s.themes],
(themes: DataColorThemeModel[] | null) => {
if (!themes) {
return null
}
return themes.sort((theme) => theme.id).find((theme) => theme.is_global)
},
],
defaultTheme: [
(s) => [s.currentTeam, s.themes, s.posthogTheme],
(
currentTeam: null | import('~/types').TeamPublicType | import('~/types').TeamType,
themes: DataColorThemeModel[] | null,
posthogTheme: DataColorThemeModel | null | undefined
): DataColorThemeModel | null => {
if (!themes) {
return null
}
// use the posthog theme unless someone set a specific theme for the team
const environmentTheme = themes.find(
(theme) => !currentTeam || theme.id === currentTeam.default_data_theme
)
return environmentTheme || posthogTheme || null
},
],
getTheme: [
(s) => [s.themes, s.defaultTheme],
(themes: DataColorThemeModel[] | null, defaultTheme: DataColorThemeModel | null) =>
(themeId: string | number | null | undefined): DataColorTheme | null => {
let customTheme
if (Number.isInteger(themeId) && themes != null) {
customTheme = themes.find((theme) => theme.id === themeId)
}
if (customTheme) {
return customTheme.colors.reduce((theme, color, index) => {
theme[`preset-${index + 1}`] = color
return theme
}, {} as DataColorTheme)
}
if (defaultTheme) {
return defaultTheme.colors.reduce((theme, color, index) => {
theme[`preset-${index + 1}`] = color
return theme
}, {} as DataColorTheme)
}
return themes != null ? FALLBACK_DATA_COLOR_THEME : null
},
],
getColorFromToken: [
(s) => [s.getTheme],
(getTheme: (themeId: string | number | null | undefined) => DataColorTheme | null) =>
(themeId: string | number | null | undefined, colorToken: string): string | null => {
const theme = getTheme(themeId)
if (!theme) {
return null
}
return getColorFromToken(theme, colorToken)
},
],
getAvailableColorTokens: [
(s) => [s.getTheme],
(getTheme: (themeId: string | number | null | undefined) => DataColorTheme | null) =>
(themeId: string | number | null | undefined): DataColorToken[] | null => {
const theme = getTheme(themeId)
if (!theme) {
return null
}
return Object.keys(theme || {}) as DataColorToken[]
},
],
}),
afterMount(({ actions }) => {
if (typeof window !== 'undefined' && window.POSTHOG_RENDER_QUERY_PAYLOAD?.themes) {
actions.setThemes(window.POSTHOG_RENDER_QUERY_PAYLOAD?.themes)
} else {
actions.loadThemes()
}
}),
])