: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, listeners, path, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import { PaginationManual } from '@posthog/lemon-ui'
import api from 'lib/api'
import { eventUsageLogic } from 'lib/utils/eventUsageLogic'
import { toParams } from 'lib/utils/url'
import { FLAGS_PER_PAGE } from 'scenes/feature-flags/featureFlagsLogic'
import { teamLogic } from 'scenes/teamLogic'
import { FeatureFlagType } from '~/types'
import type { TeamPublicType, TeamType } from '~/types'
import type { FeatureFlagModalFilters } from 'products/experiments/frontend/scenes/experimentsLogic'
// Canonical definition lives in experimentsLogic; re-exported so existing importers keep working.
export type { FeatureFlagModalFilters }
const DEFAULT_FILTERS: FeatureFlagModalFilters = {
active: undefined,
created_by_id: undefined,
search: undefined,
order: undefined,
page: 1,
evaluation_runtime: undefined,
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface selectExistingFeatureFlagModalLogicValues {
currentProjectId: number | string // teamLogic
currentTeam: TeamPublicType | TeamType | null // teamLogic
featureFlags: {
count: number
results: FeatureFlagType[]
}
featureFlagsLoading: boolean
filters: FeatureFlagModalFilters
isEvaluationTagsRequired: boolean
isModalOpen: boolean
pagination: PaginationManual
paramsFromFilters: Record<string, any>
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface selectExistingFeatureFlagModalLogicActions {
reportExperimentFeatureFlagModalOpened: () => {} // eventUsageLogic
loadCurrentTeamSuccess: (
currentTeam: TeamPublicType | null,
payload?: any
) => {
currentTeam: TeamPublicType | null
payload?: any
} // teamLogic
updateCurrentTeamSuccess: (
currentTeam: TeamPublicType | TeamType,
payload?: Partial<TeamType> | undefined
) => {
currentTeam: TeamPublicType | TeamType
payload?: Partial<TeamType>
} // teamLogic
closeSelectExistingFeatureFlagModal: () => {
value: true
}
loadFeatureFlags: () => any
loadFeatureFlagsFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadFeatureFlagsForAutocomplete: () => {
value: true
}
loadFeatureFlagsSuccess: (
featureFlags: {
count: number
results: FeatureFlagType[]
},
payload?: any
) => {
featureFlags: {
count: number
results: FeatureFlagType[]
}
payload?: any
}
openSelectExistingFeatureFlagModal: () => {
value: true
}
resetFilters: () => {
value: true
}
setFilters: (
filters: Partial<FeatureFlagModalFilters>,
replace?: boolean
) => {
filters: Partial<FeatureFlagModalFilters>
replace: boolean | undefined
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface selectExistingFeatureFlagModalLogicMeta {
__keaTypeGenInternalSelectorTypes: {
paramsFromFilters: (
filters: FeatureFlagModalFilters,
currentTeam: TeamPublicType | TeamType | null
) => Record<string, any>
pagination: (
filters: FeatureFlagModalFilters,
featureFlags: {
count: number
results: FeatureFlagType[]
}
) => PaginationManual
isEvaluationTagsRequired: (currentTeam: TeamPublicType | TeamType | null) => boolean
}
}
export type selectExistingFeatureFlagModalLogicType = MakeLogicType<
selectExistingFeatureFlagModalLogicValues,
selectExistingFeatureFlagModalLogicActions,
Record<string, any>,
selectExistingFeatureFlagModalLogicMeta
>
export const selectExistingFeatureFlagModalLogic = kea<selectExistingFeatureFlagModalLogicType>([
path(['scenes', 'experiments', 'create', 'selectExistingFeatureFlagModalLogic']),
connect(() => ({
actions: [
eventUsageLogic,
['reportExperimentFeatureFlagModalOpened'],
teamLogic,
['loadCurrentTeamSuccess', 'updateCurrentTeamSuccess'],
],
values: [teamLogic, ['currentTeam', 'currentProjectId']],
})),
actions({
openSelectExistingFeatureFlagModal: true,
closeSelectExistingFeatureFlagModal: true,
setFilters: (filters: Partial<FeatureFlagModalFilters>, replace?: boolean) => ({ filters, replace }),
resetFilters: true,
loadFeatureFlagsForAutocomplete: true,
}),
reducers({
isModalOpen: [
false,
{
openSelectExistingFeatureFlagModal: () => true,
closeSelectExistingFeatureFlagModal: () => false,
},
],
filters: [
DEFAULT_FILTERS,
{
setFilters: (state, { filters, replace }) => {
if (replace) {
return { ...DEFAULT_FILTERS, ...filters }
}
return { ...state, ...filters }
},
resetFilters: () => DEFAULT_FILTERS,
},
],
}),
listeners(({ actions, values }) => ({
setFilters: async (_, breakpoint) => {
await breakpoint(300)
actions.loadFeatureFlags()
},
resetFilters: () => {
actions.loadFeatureFlags()
},
openSelectExistingFeatureFlagModal: () => {
actions.reportExperimentFeatureFlagModalOpened()
actions.loadFeatureFlags()
},
loadFeatureFlagsForAutocomplete: () => {
actions.loadFeatureFlags()
},
loadCurrentTeamSuccess: () => {
if (values.isModalOpen) {
actions.loadFeatureFlags()
}
},
updateCurrentTeamSuccess: () => {
if (values.isModalOpen) {
actions.loadFeatureFlags()
}
},
})),
loaders(({ values }) => ({
featureFlags: [
{ results: [], count: 0 } as { results: FeatureFlagType[]; count: number },
{
loadFeatureFlags: async () => {
const url = `api/projects/${values.currentProjectId}/feature_flags/?${toParams({
...values.paramsFromFilters,
eligible_for_experiment: true,
})}`
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a URL built at runtime and an unchecked response type. Use a generated function if one covers this endpoint.
const response = await api.get(url)
return response
},
},
],
})),
selectors({
paramsFromFilters: [
(s) => [s.filters, s.currentTeam],
(
filters: FeatureFlagModalFilters,
currentTeam: null | import('~/types').TeamPublicType | import('~/types').TeamType
) => {
const params: Record<string, any> = {
...filters,
limit: FLAGS_PER_PAGE,
offset: filters.page ? (filters.page - 1) * FLAGS_PER_PAGE : 0,
}
// Add evaluation contexts filter if required by team
if (currentTeam?.require_evaluation_contexts) {
params.has_evaluation_contexts = true
}
return params
},
],
pagination: [
(s) => [s.filters, s.featureFlags],
(
filters: FeatureFlagModalFilters,
featureFlags: {
count: number
results: FeatureFlagType[]
}
): PaginationManual => {
const currentPage = filters.page || 1
const hasNextPage = featureFlags.count > currentPage * FLAGS_PER_PAGE
const hasPreviousPage = currentPage > 1
const needsPagination = featureFlags.count > FLAGS_PER_PAGE
return {
controlled: true,
pageSize: FLAGS_PER_PAGE,
currentPage,
entryCount: featureFlags.count,
onForward:
needsPagination && hasNextPage
? () => {
selectExistingFeatureFlagModalLogic.actions.setFilters({ page: currentPage + 1 })
}
: undefined,
onBackward:
needsPagination && hasPreviousPage
? () => {
selectExistingFeatureFlagModalLogic.actions.setFilters({
page: Math.max(1, currentPage - 1),
})
}
: undefined,
}
},
],
isEvaluationTagsRequired: [
(s) => [s.currentTeam],
(currentTeam: null | import('~/types').TeamPublicType | import('~/types').TeamType) =>
currentTeam?.require_evaluation_contexts || false,
],
}),
])