: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, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import api from 'lib/api'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { teamLogic } from 'scenes/teamLogic'
import type { TeamPublicType, TeamType } from '../../types'
export interface DefaultEvaluationContext {
id: number | string
name: string
}
export interface DefaultEvaluationContextsResponse {
default_evaluation_contexts: DefaultEvaluationContext[]
available_contexts: string[]
hidden_contexts: string[]
enabled: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface defaultEvaluationContextsLogicValues {
currentTeam: TeamPublicType | TeamType | null // teamLogic
availableContexts: string[]
canAddMoreContexts: boolean
contexts: DefaultEvaluationContext[]
defaultEvaluationContexts: DefaultEvaluationContextsResponse | null
defaultEvaluationContextsLoading: boolean
hiddenContexts: string[]
isAdding: boolean
isEnabled: boolean
newContextInput: string
pendingContextName: string | null
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface defaultEvaluationContextsLogicActions {
updateCurrentTeam: (payload: Partial<TeamType>) => Partial<TeamType> // teamLogic
addContext: (contextName: string) => {
contextName: string
}
addContextFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
addContextSuccess: (
defaultEvaluationContexts: {
available_contexts: any[]
default_evaluation_contexts: {
id: any
name: any
}[]
enabled: boolean
hidden_contexts: string[]
} | null,
payload?: {
contextName: string
}
) => {
defaultEvaluationContexts: {
available_contexts: any[]
default_evaluation_contexts: {
id: any
name: any
}[]
enabled: boolean
hidden_contexts: string[]
} | null
payload?: {
contextName: string
}
}
hideContext: (contextName: string) => {
contextName: string
}
hideContextFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
hideContextSuccess: (
defaultEvaluationContexts: {
available_contexts: string[]
default_evaluation_contexts: DefaultEvaluationContext[]
enabled: boolean
hidden_contexts: string[]
} | null,
payload?: {
contextName: string
}
) => {
defaultEvaluationContexts: {
available_contexts: string[]
default_evaluation_contexts: DefaultEvaluationContext[]
enabled: boolean
hidden_contexts: string[]
} | null
payload?: {
contextName: string
}
}
loadDefaultEvaluationContexts: () => {
value: true
}
loadDefaultEvaluationContextsFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadDefaultEvaluationContextsSuccess: (
defaultEvaluationContexts: DefaultEvaluationContextsResponse | null,
payload?: {
value: true
}
) => {
defaultEvaluationContexts: DefaultEvaluationContextsResponse | null
payload?: {
value: true
}
}
removeContext: (contextName: string) => {
contextName: string
}
removeContextFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
removeContextSuccess: (
defaultEvaluationContexts: {
available_contexts: string[]
default_evaluation_contexts: DefaultEvaluationContext[]
enabled: boolean
hidden_contexts: string[]
} | null,
payload?: {
contextName: string
}
) => {
defaultEvaluationContexts: {
available_contexts: string[]
default_evaluation_contexts: DefaultEvaluationContext[]
enabled: boolean
hidden_contexts: string[]
} | null
payload?: {
contextName: string
}
}
setIsAdding: (isAdding: boolean) => {
isAdding: boolean
}
setNewContextInput: (value: string) => {
value: string
}
toggleEnabled: (enabled: boolean) => {
enabled: boolean
}
unhideContext: (contextName: string) => {
contextName: string
}
unhideContextFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
unhideContextSuccess: (
defaultEvaluationContexts: {
available_contexts: string[]
default_evaluation_contexts: DefaultEvaluationContext[]
enabled: boolean
hidden_contexts: string[]
} | null,
payload?: {
contextName: string
}
) => {
defaultEvaluationContexts: {
available_contexts: string[]
default_evaluation_contexts: DefaultEvaluationContext[]
enabled: boolean
hidden_contexts: string[]
} | null
payload?: {
contextName: string
}
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface defaultEvaluationContextsLogicMeta {
__keaTypeGenInternalSelectorTypes: {
contexts: (defaultEvaluationContexts: DefaultEvaluationContextsResponse | null) => DefaultEvaluationContext[]
availableContexts: (defaultEvaluationContexts: DefaultEvaluationContextsResponse | null) => string[]
hiddenContexts: (defaultEvaluationContexts: DefaultEvaluationContextsResponse | null) => string[]
isEnabled: (currentTeam: TeamPublicType | TeamType | null) => boolean
canAddMoreContexts: (contexts: DefaultEvaluationContext[]) => boolean
}
}
export type defaultEvaluationContextsLogicType = MakeLogicType<
defaultEvaluationContextsLogicValues,
defaultEvaluationContextsLogicActions,
Record<string, any>,
defaultEvaluationContextsLogicMeta
>
export const defaultEvaluationContextsLogic = kea<defaultEvaluationContextsLogicType>([
path(['scenes', 'feature-flags', 'defaultEvaluationContextsLogic']),
connect(() => ({
values: [teamLogic, ['currentTeam']],
actions: [teamLogic, ['updateCurrentTeam']],
})),
actions({
loadDefaultEvaluationContexts: true,
addContext: (contextName: string) => ({ contextName }),
removeContext: (contextName: string) => ({ contextName }),
hideContext: (contextName: string) => ({ contextName }),
unhideContext: (contextName: string) => ({ contextName }),
toggleEnabled: (enabled: boolean) => ({ enabled }),
setNewContextInput: (value: string) => ({ value }),
setIsAdding: (isAdding: boolean) => ({ isAdding }),
}),
reducers({
newContextInput: [
'',
{
setNewContextInput: (_, { value }) => value,
addContext: () => '',
},
],
isAdding: [
false,
{
setIsAdding: (_, { isAdding }) => isAdding,
addContext: () => false,
},
],
pendingContextName: [
null as string | null,
{
hideContext: (_, { contextName }) => contextName,
unhideContext: (_, { contextName }) => contextName,
hideContextSuccess: () => null,
hideContextFailure: () => null,
unhideContextSuccess: () => null,
unhideContextFailure: () => null,
},
],
}),
loaders(({ values }) => ({
defaultEvaluationContexts: [
null as DefaultEvaluationContextsResponse | null,
{
loadDefaultEvaluationContexts: async () => {
const teamId = values.currentTeam?.id
if (!teamId) {
return null
}
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. organizationsProjectsDefaultEvaluationContextsRetrieve() from '~/generated/core/api' serves this route, but its generated types do not describe this call yet, so fix the endpoint's OpenAPI schema first.
const response = await api.get(`/api/projects/${teamId}/default_evaluation_contexts/`)
return response as DefaultEvaluationContextsResponse
},
addContext: async ({ contextName }) => {
const teamId = values.currentTeam?.id
if (!teamId) {
throw new Error('No team selected')
}
try {
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. organizationsProjectsDefaultEvaluationContextsCreate() from '~/generated/core/api' serves this route, but its generated types do not describe this call yet, so fix the endpoint's OpenAPI schema first.
const response = await api.create(`/api/projects/${teamId}/default_evaluation_contexts/`, {
context_name: contextName,
})
const currentData = values.defaultEvaluationContexts
if (!currentData) {
return null
}
if (response.created) {
// The server unhides the name when an admin adds it as a default, so drop it
// from hidden_contexts when the response reports it's no longer hidden.
const hiddenContexts = response.hidden_from_suggestions
? currentData.hidden_contexts
: currentData.hidden_contexts.filter((name) => name !== response.name)
return {
...currentData,
default_evaluation_contexts: [
...currentData.default_evaluation_contexts,
{ id: response.id, name: response.name },
],
available_contexts: [...currentData.available_contexts, response.name].sort(),
hidden_contexts: hiddenContexts,
}
}
return currentData
} catch (error: any) {
lemonToast.error(error.error || error.detail || 'Failed to add context')
throw error
}
},
removeContext: async ({ contextName }) => {
const teamId = values.currentTeam?.id
if (!teamId) {
throw new Error('No team selected')
}
try {
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. organizationsProjectsDefaultEvaluationContextsDestroy() from '~/generated/core/api' serves this route, but its generated types do not describe this call yet, so fix the endpoint's OpenAPI schema first.
await api.delete(
`/api/projects/${teamId}/default_evaluation_contexts/?context_name=${encodeURIComponent(contextName)}`
)
const currentData = values.defaultEvaluationContexts
if (!currentData) {
return null
}
return {
...currentData,
default_evaluation_contexts: currentData.default_evaluation_contexts.filter(
(ctx: DefaultEvaluationContext) => ctx.name !== contextName
),
}
} catch (error: any) {
lemonToast.error(error.error || error.detail || 'Failed to remove context')
throw error
}
},
hideContext: async ({ contextName }) => {
const teamId = values.currentTeam?.id
if (!teamId) {
throw new Error('No team selected')
}
try {
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. Use organizationsProjectsEvaluationContextSuggestionsCreate() from 'products/feature_flags/frontend/generated/api' instead.
await api.create(`/api/projects/${teamId}/evaluation_context_suggestions/`, {
context_name: contextName,
})
const currentData = values.defaultEvaluationContexts
if (!currentData) {
return currentData
}
return {
...currentData,
available_contexts: currentData.available_contexts.filter((name) => name !== contextName),
hidden_contexts: [...currentData.hidden_contexts, contextName].sort(),
}
} catch (error: any) {
lemonToast.error(error.error || error.detail || 'Failed to hide suggestion')
throw error
}
},
unhideContext: async ({ contextName }) => {
const teamId = values.currentTeam?.id
if (!teamId) {
throw new Error('No team selected')
}
try {
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. Use organizationsProjectsEvaluationContextSuggestionsDestroy() from 'products/feature_flags/frontend/generated/api' instead.
await api.delete(
`/api/projects/${teamId}/evaluation_context_suggestions/?context_name=${encodeURIComponent(contextName)}`
)
const currentData = values.defaultEvaluationContexts
if (!currentData) {
return currentData
}
return {
...currentData,
available_contexts: [...currentData.available_contexts, contextName].sort(),
hidden_contexts: currentData.hidden_contexts.filter((name) => name !== contextName),
}
} catch (error: any) {
lemonToast.error(error.error || error.detail || 'Failed to restore suggestion')
throw error
}
},
},
],
})),
listeners(({ actions }) => ({
toggleEnabled: async ({ enabled }) => {
actions.updateCurrentTeam({
default_evaluation_contexts_enabled: enabled,
})
},
addContextSuccess: () => {
lemonToast.success('Context added to default evaluation contexts')
},
removeContextSuccess: () => {
lemonToast.success('Context removed from default evaluation contexts')
},
hideContextSuccess: () => {
lemonToast.success('Context hidden from suggestions')
},
unhideContextSuccess: () => {
lemonToast.success('Context restored to suggestions')
},
})),
selectors({
contexts: [
(s) => [s.defaultEvaluationContexts],
(data: DefaultEvaluationContextsResponse | null): DefaultEvaluationContext[] =>
data?.default_evaluation_contexts || [],
],
availableContexts: [
(s) => [s.defaultEvaluationContexts],
(data: DefaultEvaluationContextsResponse | null): string[] => data?.available_contexts || [],
],
hiddenContexts: [
(s) => [s.defaultEvaluationContexts],
(data: DefaultEvaluationContextsResponse | null): string[] => data?.hidden_contexts || [],
],
isEnabled: [
(s) => [s.currentTeam],
(team: null | import('../../types').TeamPublicType | import('../../types').TeamType): boolean =>
team?.default_evaluation_contexts_enabled || false,
],
canAddMoreContexts: [
(s) => [s.contexts],
(contexts: DefaultEvaluationContext[]): boolean => contexts.length < 10,
],
}),
afterMount(({ actions }) => {
actions.loadDefaultEvaluationContexts()
}),
])