: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 {
LogicWrapper,
MakeLogicType,
actions,
afterMount,
connect,
kea,
key,
listeners,
path,
props,
propsChanged,
reducers,
selectors,
} from 'kea'
import { loaders } from 'kea-loaders'
import { projectLogic } from 'scenes/projectLogic'
import * as api from '~/generated/core/api'
import { PaginatedEnterprisePropertyDefinitionListApi } from '~/generated/core/api.schemas'
import { BIField } from '~/queries/schema/schema-business-intelligence'
import { captureBIWorksheetAction } from './biEditorAnalytics'
import { biEditorLogic } from './biEditorLogic'
import { buildBIPropertyFields, getBIPropertyTarget } from './biPropertyFields'
export interface BIPropertyFieldsLogicProps {
tabId: string
field: BIField
dataPaneSearch?: string
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface biPropertyFieldsLogicValues {
currentProjectId: number | null // projectLogic
error: string | null
expanded: boolean
fields: BIField[]
localSearch: string
manuallyExpanded: boolean
page: PaginatedEnterprisePropertyDefinitionListApi | null
pageLoading: boolean
search: string
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface biPropertyFieldsLogicActions {
loadPage: ({ offset }: { offset: number }) => {
offset: number
}
loadPageFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadPageSuccess: (
page: PaginatedEnterprisePropertyDefinitionListApi | null,
payload?: {
offset: number
}
) => {
page: PaginatedEnterprisePropertyDefinitionListApi | null
payload?: {
offset: number
}
}
restorePage: (page: PaginatedEnterprisePropertyDefinitionListApi | null) => {
page: PaginatedEnterprisePropertyDefinitionListApi | null
}
setSearch: (search: string) => {
search: string
}
toggleExpanded: () => {
value: true
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface biPropertyFieldsLogicMeta {
key: string
__keaTypeGenInternalSelectorTypes: {
expanded: (manuallyExpanded: boolean, arg: any) => boolean
search: (localSearch: string, arg: any) => string
fields: (page: PaginatedEnterprisePropertyDefinitionListApi | null) => BIField[]
}
}
export type biPropertyFieldsLogicType = MakeLogicType<
biPropertyFieldsLogicValues,
biPropertyFieldsLogicActions,
BIPropertyFieldsLogicProps,
biPropertyFieldsLogicMeta
>
export const biPropertyFieldsLogic: LogicWrapper<biPropertyFieldsLogicType> = kea<biPropertyFieldsLogicType>([
props({} as BIPropertyFieldsLogicProps),
key(({ tabId, field }) => JSON.stringify([tabId, field.id])),
path((key) => ['products', 'business_intelligence', 'frontend', 'biPropertyFieldsLogic', key]),
connect({ values: [projectLogic, ['currentProjectId']] }),
actions({
toggleExpanded: true,
setSearch: (search: string) => ({ search }),
restorePage: (page: PaginatedEnterprisePropertyDefinitionListApi | null) => ({ page }),
}),
loaders(({ props, values, cache }) => ({
page: [
null as PaginatedEnterprisePropertyDefinitionListApi | null,
{
loadPage: async (
{ offset }: { offset: number },
breakpoint
): Promise<PaginatedEnterprisePropertyDefinitionListApi | null> => {
const searchVersion = cache.searchVersion
const target = getBIPropertyTarget(props.field)
if (!target || !values.currentProjectId) {
return null
}
if (values.search) {
await breakpoint(250)
}
if (searchVersion !== cache.searchVersion) {
return values.page
}
try {
const response = await api.propertyDefinitionsList(String(values.currentProjectId), {
type: target.type,
group_type_index: target.groupTypeIndex,
exclude_hidden: true,
exclude_restricted: true,
search: values.search.trim() || undefined,
limit: 25,
offset,
})
breakpoint()
if (searchVersion !== cache.searchVersion) {
return values.page
}
const editor = biEditorLogic.findMounted({ tabId: props.tabId })
if (offset === 0 && editor) {
captureBIWorksheetAction(
values.search.trim() ? 'properties_searched' : 'properties_browsed',
editor.values.config
)
}
return {
...response,
results: [...(offset ? (values.page?.results ?? []) : []), ...response.results],
}
} catch (error) {
if (searchVersion !== cache.searchVersion) {
return values.page
}
throw error
}
},
},
],
})),
reducers({
manuallyExpanded: [false, { toggleExpanded: (expanded) => !expanded }],
localSearch: ['', { setSearch: (_, { search }) => search }],
page: [
null as PaginatedEnterprisePropertyDefinitionListApi | null,
{
loadPage: (page, { offset }) => (offset ? page : null),
restorePage: (_, { page }) => page,
},
],
pageLoading: [false, { restorePage: () => false }],
error: [
null as string | null,
{ loadPage: () => null, restorePage: () => null, loadPageFailure: (_, { error }) => error },
],
}),
selectors(({ props }) => ({
expanded: [
(s) => [s.manuallyExpanded, (_, props) => props.dataPaneSearch?.trim()],
(manuallyExpanded: boolean, dataPaneSearch: string | undefined): boolean =>
manuallyExpanded || !!dataPaneSearch,
],
search: [
(s) => [s.localSearch, (_, props) => props.dataPaneSearch?.trim()],
(localSearch: string, dataPaneSearch: string | undefined): string =>
dataPaneSearch
? props.field.name.toLowerCase().includes(dataPaneSearch.toLowerCase())
? ''
: dataPaneSearch
: localSearch,
],
fields: [
(s) => [s.page],
(page: PaginatedEnterprisePropertyDefinitionListApi | null): BIField[] =>
buildBIPropertyFields(props.field, page?.results ?? []),
],
})),
listeners(({ actions, values }) => ({
toggleExpanded: () => {
if (values.expanded && !values.page && !values.pageLoading) {
actions.loadPage({ offset: 0 })
}
},
setSearch: () => actions.loadPage({ offset: 0 }),
})),
afterMount(({ actions, props }) => {
if (props.dataPaneSearch?.trim()) {
actions.loadPage({ offset: 0 })
}
}),
propsChanged(({ actions, props, values, cache }, oldProps) => {
const search = props.dataPaneSearch?.trim()
if (search !== oldProps.dataPaneSearch?.trim()) {
cache.searchVersion = (cache.searchVersion ?? 0) + 1
if (!oldProps.dataPaneSearch?.trim()) {
cache.localPage = values.page
cache.localPageSearch = values.localSearch
}
if (!search && !values.manuallyExpanded) {
actions.restorePage(cache.localPageSearch === values.localSearch ? (cache.localPage ?? null) : null)
} else {
actions.loadPage({ offset: 0 })
}
}
}),
])