: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 { loaders } from 'kea-loaders'
import { propertyDefinitionsList } from '~/generated/core/api'
import { AIEventPropertyEnumApi, AccessLevelEnumApi } from 'products/access_control/frontend/generated/api.schemas'
import { AccessPropertyRule, accessDetailLogic } from './accessDetailLogic'
import type { ScopeType } from './types'
export interface PropertyOption {
id: string
name: string
}
export interface AddPropertyRestrictionModalLogicProps {
projectId: string
scopeType: ScopeType
subjectId: string
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface addPropertyRestrictionModalLogicValues {
properties: AccessPropertyRule[] // accessDetailLogic
ruleSaving: boolean // accessDetailLogic
displayPropertyOptions: PropertyOption[]
existingRule: AccessPropertyRule | null
isOpen: boolean
level: AccessLevelEnumApi
propertyId: string | null
propertyOptions: PropertyOption[]
propertyOptionsLoading: boolean
propertyType: 'ai' | 'event' | 'person'
search: string
selectedProperty: PropertyOption | null
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface addPropertyRestrictionModalLogicActions {
propertyRuleSaved: () => {
value: true
} // accessDetailLogic
setPropertyRule: (
propertyDefinitionId: string,
level: AccessLevelEnumApi | null,
aiProperty?: AIEventPropertyEnumApi | undefined
) => {
aiProperty: AIEventPropertyEnumApi | undefined
level: AccessLevelEnumApi | null
propertyDefinitionId: string
} // accessDetailLogic
closeModal: () => {
value: true
}
loadPropertyOptions: (_: any) => any
loadPropertyOptionsFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadPropertyOptionsSuccess: (
propertyOptions: {
id: string
name: string
}[],
payload?: any
) => {
propertyOptions: {
id: string
name: string
}[]
payload?: any
}
openModal: () => {
value: true
}
setLevel: (level: AccessLevelEnumApi) => {
level: AccessLevelEnumApi
}
setPropertyId: (propertyId: string | null) => {
propertyId: string | null
}
setPropertyType: (propertyType: 'ai' | 'event' | 'person') => {
propertyType: 'ai' | 'event' | 'person'
}
setSearch: (search: string) => {
search: string
}
setSelectedProperty: (option: PropertyOption | null) => {
option: PropertyOption | null
}
submitRule: () => {
value: true
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface addPropertyRestrictionModalLogicMeta {
key: string
__keaTypeGenInternalSelectorTypes: {
existingRule: (
properties: AccessPropertyRule[],
propertyId: string | null,
propertyType: 'ai' | 'event' | 'person'
) => AccessPropertyRule | null
displayPropertyOptions: (
propertyOptions: PropertyOption[],
selectedProperty: PropertyOption | null,
propertyOptionsLoading: boolean
) => PropertyOption[]
}
}
export type addPropertyRestrictionModalLogicType = MakeLogicType<
addPropertyRestrictionModalLogicValues,
addPropertyRestrictionModalLogicActions,
AddPropertyRestrictionModalLogicProps,
addPropertyRestrictionModalLogicMeta
>
export const addPropertyRestrictionModalLogic = kea<addPropertyRestrictionModalLogicType>([
path((key) => ['scenes', 'access_control', 'addPropertyRestrictionModalLogic', key]),
props({} as AddPropertyRestrictionModalLogicProps),
key((props) => `${props.projectId}:${props.scopeType}:${props.subjectId}`),
connect((props: AddPropertyRestrictionModalLogicProps) => ({
actions: [accessDetailLogic(props), ['setPropertyRule', 'propertyRuleSaved']],
values: [accessDetailLogic(props), ['properties', 'ruleSaving']],
})),
actions({
openModal: true,
closeModal: true,
setPropertyType: (propertyType: 'person' | 'event' | 'ai') => ({ propertyType }),
setSearch: (search: string) => ({ search }),
setPropertyId: (propertyId: string | null) => ({ propertyId }),
setSelectedProperty: (option: PropertyOption | null) => ({ option }),
setLevel: (level: AccessLevelEnumApi) => ({ level }),
submitRule: true,
}),
reducers({
isOpen: [false, { openModal: () => true, closeModal: () => false }],
propertyType: [
'person' as 'person' | 'event' | 'ai',
{ setPropertyType: (_, { propertyType }) => propertyType, closeModal: () => 'person' },
],
search: ['', { setSearch: (_, { search }) => search, setPropertyType: () => '', closeModal: () => '' }],
propertyId: [
null as string | null,
{ setPropertyId: (_, { propertyId }) => propertyId, setPropertyType: () => null, closeModal: () => null },
],
/** The picked option, kept so its label survives the option list reloading. */
selectedProperty: [
null as PropertyOption | null,
{ setSelectedProperty: (_, { option }) => option, setPropertyType: () => null, closeModal: () => null },
],
level: [
AccessLevelEnumApi.Read as AccessLevelEnumApi,
{ setLevel: (_, { level }) => level, closeModal: () => AccessLevelEnumApi.Read },
],
}),
loaders(({ props, values }) => ({
propertyOptions: [
[] as PropertyOption[],
{
loadPropertyOptions: async (_, breakpoint) => {
await breakpoint(300)
if (values.propertyType === 'ai') {
const words = values.search.trim().toLowerCase().split(/\s+/)
return Object.values(AIEventPropertyEnumApi)
.map((name) => ({
id: name,
name: `${name.slice(4)} (${name})`,
}))
.filter((option) => words.every((word) => option.name.toLowerCase().includes(word)))
.sort((a, b) => a.name.localeCompare(b.name))
}
const response = await propertyDefinitionsList(props.projectId, {
type: values.propertyType,
limit: 20,
search: values.search,
})
breakpoint()
return response.results.map((item) => ({ id: item.id, name: item.name }))
},
},
],
})),
selectors({
/** The rule already stored for the picked property, so the modal offers an update, not a silent overwrite. */
existingRule: [
(s) => [s.properties, s.propertyId, s.propertyType],
(
properties: AccessPropertyRule[],
propertyId: string | null,
propertyType: 'ai' | 'event' | 'person'
): AccessPropertyRule | null =>
propertyId
? (properties.find((p) =>
propertyType === 'ai'
? p.property_type === 'event' && p.property === propertyId
: p.property_definition_id === propertyId
) ?? null)
: null,
],
// Selecting an option resets the search, which reloads the options; keep the picked one in
// the list so LemonInputSelect can render its name instead of the raw id
displayPropertyOptions: [
(s) => [s.propertyOptions, s.selectedProperty, s.propertyOptionsLoading],
(
propertyOptions: PropertyOption[],
selectedProperty: PropertyOption | null,
loading: boolean
): PropertyOption[] => {
const options = loading ? [] : propertyOptions
return selectedProperty && !options.some((o) => o.id === selectedProperty.id)
? [selectedProperty, ...options]
: options
},
],
}),
listeners(({ actions, values }) => ({
openModal: () => actions.loadPropertyOptions(null),
setPropertyType: () => actions.loadPropertyOptions(null),
setSearch: () => actions.loadPropertyOptions(null),
setPropertyId: ({ propertyId }) => {
actions.setSelectedProperty(
propertyId ? (values.propertyOptions.find((o) => o.id === propertyId) ?? null) : null
)
},
propertyRuleSaved: () => actions.closeModal(),
submitRule: () => {
if (!values.isOpen || !values.propertyId || values.ruleSaving || values.propertyOptionsLoading) {
return
}
const aiProperty =
values.propertyType === 'ai'
? Object.values(AIEventPropertyEnumApi).find((name) => name === values.propertyId)
: undefined
actions.setPropertyRule(
values.existingRule?.property_definition_id ?? values.propertyId,
values.level,
aiProperty
)
},
})),
])