: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,
LogicWrapper,
actions,
connect,
kea,
key,
listeners,
path,
props,
reducers,
selectors,
} from 'kea'
import type { TreeDataItem } from 'lib/lemon-ui/LemonTree/LemonTree'
import { databaseTableListLogic } from 'scenes/data-management/database/databaseTableListLogic'
import { BIConfig, BIDataSource } from '~/queries/schema/schema-business-intelligence'
import { DatabaseSchemaTable } from '~/queries/schema/schema-general'
import {
buildBIDataSourceTree,
getBIDataSourceFolderIds,
searchBIDataSourceTree,
} from 'products/business_intelligence/frontend/biDataSourceTree'
import { biEditorLogic } from 'products/business_intelligence/frontend/biEditorLogic'
import { getBIDataSourceKey } from 'products/business_intelligence/frontend/biEditorTypes'
import { connectionSelectorLogic } from 'products/data_warehouse/frontend/shared/logics/connectionSelectorLogic'
import type { ExternalDataSourceConnectionOptionApi } from 'products/warehouse_sources/frontend/generated/api.schemas'
export interface BIDataSourcePickerLogicProps {
tabId: string
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface biDataSourcePickerLogicValues {
config: BIConfig // biEditorLogic
databaseLoading: boolean // biEditorLogic
selectableDataSources: BIDataSource[] // biEditorLogic
connectionOptions: ExternalDataSourceConnectionOptionApi[] | null // connectionSelectorLogic
allTables: DatabaseSchemaTable[] // databaseTableListLogic
connectionId: string | null // databaseTableListLogic
databaseLoadError: string | null // databaseTableListLogic
expandedIds: string[] | null
filteredTree: TreeDataItem[]
open: boolean
search: string
selectedDirectSource: ExternalDataSourceConnectionOptionApi | undefined
sourceTree: TreeDataItem[]
treeData: TreeDataItem[]
visibleExpandedIds: string[]
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface biDataSourcePickerLogicActions {
setDataSource: (source: BIDataSource) => {
source: BIDataSource
} // biEditorLogic
refreshDatabaseSchema: () => {
value: true
} // databaseTableListLogic
selectSource: (id: string) => {
id: string
}
setExpandedIds: (ids: string[]) => {
ids: string[]
}
setOpen: (open: boolean) => {
open: boolean
}
setSearch: (search: string) => {
search: string
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface biDataSourcePickerLogicMeta {
key: string
__keaTypeGenInternalSelectorTypes: {
selectedDirectSource: (
connectionOptions: ExternalDataSourceConnectionOptionApi[] | null,
connectionId: string | null
) => ExternalDataSourceConnectionOptionApi | undefined
treeData: (allTables: DatabaseSchemaTable[]) => TreeDataItem[]
sourceTree: (
treeData: TreeDataItem[],
selectableDataSources: BIDataSource[],
connectionId: string | null,
selectedDirectSource: ExternalDataSourceConnectionOptionApi | undefined
) => TreeDataItem[]
filteredTree: (sourceTree: TreeDataItem[], search: string) => TreeDataItem[]
visibleExpandedIds: (filteredTree: TreeDataItem[], search: string, expandedIds: string[] | null) => string[]
}
}
export type biDataSourcePickerLogicType = MakeLogicType<
biDataSourcePickerLogicValues,
biDataSourcePickerLogicActions,
BIDataSourcePickerLogicProps,
biDataSourcePickerLogicMeta
>
export const biDataSourcePickerLogic: LogicWrapper<biDataSourcePickerLogicType> = kea<biDataSourcePickerLogicType>([
props({} as BIDataSourcePickerLogicProps),
key((props) => props.tabId),
path((key) => ['products', 'business_intelligence', 'frontend', 'biDataSourcePickerLogic', key]),
connect((props: BIDataSourcePickerLogicProps) => ({
values: [
biEditorLogic({ tabId: props.tabId }),
['selectableDataSources', 'config', 'databaseLoading'],
databaseTableListLogic,
['allTables', 'connectionId', 'databaseLoadError'],
connectionSelectorLogic,
['connectionOptions'],
],
actions: [
biEditorLogic({ tabId: props.tabId }),
['setDataSource'],
databaseTableListLogic,
['refreshDatabaseSchema'],
],
})),
actions({
setOpen: (open: boolean) => ({ open }),
setSearch: (search: string) => ({ search }),
setExpandedIds: (ids: string[]) => ({ ids }),
selectSource: (id: string) => ({ id }),
}),
reducers({
open: [false, { setOpen: (_, { open }) => open }],
search: ['', { setSearch: (_, { search }) => search, setOpen: () => '' }],
expandedIds: [
null as string[] | null,
{ setExpandedIds: (_, { ids }) => ids, setOpen: () => null, setSearch: () => null },
],
}),
selectors({
selectedDirectSource: [
(s) => [s.connectionOptions, s.connectionId],
(options: ExternalDataSourceConnectionOptionApi[] | null, id: string | null) =>
options?.find((source) => source.id === id),
],
treeData: [
(s) => [s.allTables],
(tables: DatabaseSchemaTable[]): TreeDataItem[] => {
const groups = new Map<string, TreeDataItem[]>()
for (const table of tables) {
const group =
table.type === 'posthog'
? 'PostHog'
: table.type === 'view'
? 'Views'
: table.type === 'system'
? 'System'
: 'Warehouse'
const children = groups.get(group) ?? []
children.push({
id: table.name,
name: table.name,
record: { type: table.type === 'view' ? 'view' : 'table', certification: table.certification },
})
groups.set(group, children)
}
return Array.from(groups, ([name, children]) => ({ id: name, name, children }))
},
],
sourceTree: [
(s) => [s.treeData, s.selectableDataSources, s.connectionId, s.selectedDirectSource],
(
tree: TreeDataItem[],
sources: BIDataSource[],
connectionId: string | null,
selectedDirectSource: ExternalDataSourceConnectionOptionApi | undefined
) => buildBIDataSourceTree(tree, sources, !!connectionId, selectedDirectSource?.schema_name, connectionId),
],
filteredTree: [
(s) => [s.sourceTree, s.search],
(tree: TreeDataItem[], search: string) => searchBIDataSourceTree(tree, search),
],
visibleExpandedIds: [
(s) => [s.filteredTree, s.search, s.expandedIds],
(tree: TreeDataItem[], _search: string, expandedIds: string[] | null): string[] =>
expandedIds === null ? getBIDataSourceFolderIds(tree) : expandedIds,
],
}),
listeners(({ actions, values }) => ({
selectSource: ({ id }) => {
const source = values.selectableDataSources.find((candidate) => getBIDataSourceKey(candidate) === id)
if (source) {
actions.setDataSource(source)
actions.setOpen(false)
}
},
})),
])