Skip to content

: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.

5.3 KB
Raw Blame
import { MakeLogicType, actions, kea, key, listeners, path, props, reducers, selectors } from 'kea'

import { BIField, BIConfig, BIVisualizationNode } from '~/queries/schema/schema-business-intelligence'
import { HogQLQuery } from '~/queries/schema/schema-general'

import { biEditorLogic } from './biEditorLogic'
import { BIEditorView, buildBIFilterOptionsQuery } from './biEditorTypes'
import { biMemberFieldKey } from './biMemberAliases'
import { biSceneLogic } from './biSceneLogic'

interface BIMemberAliasesProps {
    tabId: string
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface biMemberAliasesLogicValues {
    draft: {
        label: string
        value: string | null
    }[]
    draftByValue: Map<string | null, string>
    field: BIField | null
    query: HogQLQuery | null
    savedLabels: Map<string | null, string>
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface biMemberAliasesLogicActions {
    close: () => {
        value: true
    }
    edit: (
        field: BIField,
        aliases: {
            label: string
            value: string | null
        }[]
    ) => {
        aliases: {
            label: string
            value: string | null
        }[]
        field: BIField
    }
    save: () => {
        value: true
    }
    setAlias: (
        value: string | null,
        label: string
    ) => {
        label: string
        value: string | null
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface biMemberAliasesLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        draftByValue: (
            draft: {
                label: string
                value: string | null
            }[]
        ) => Map<string | null, string>
        query: (field: BIField | null, config: BIConfig, worksheet: BIVisualizationNode) => HogQLQuery | null
    }
}

export type biMemberAliasesLogicType = MakeLogicType<
    biMemberAliasesLogicValues,
    biMemberAliasesLogicActions,
    BIMemberAliasesProps,
    biMemberAliasesLogicMeta
>
export const biMemberAliasesLogic = kea<biMemberAliasesLogicType>([
    props({} as BIMemberAliasesProps),
    key(({ tabId }) => tabId),
    path((key) => ['products', 'business_intelligence', 'biMemberAliasesLogic', key]),
    actions({
        edit: (field: BIField, aliases: { value: string | null; label: string }[]) => ({ field, aliases }),
        setAlias: (value: string | null, label: string) => ({ value, label }),
        close: true,
        save: true,
    }),
    reducers({
        field: [null as BIField | null, { edit: (_, { field }) => field, close: () => null }],
        savedLabels: [
            new Map<string | null, string>(),
            { edit: (_, { aliases }) => new Map(aliases.map(({ value, label }) => [value, label])) },
        ],
        draft: [
            [] as { value: string | null; label: string }[],
            {
                edit: (_, { aliases }) => aliases,
                setAlias: (draft, { value, label }) =>
                    draft.some((alias) => alias.value === value)
                        ? draft.map((alias) => (alias.value === value ? { value, label } : alias))
                        : [...draft, { value, label }],
            },
        ],
    }),
    selectors(({ props }) => ({
        draftByValue: [
            (s) => [s.draft],
            (
                draft: {
                    label: string
                    value: string | null
                }[]
            ): Map<string | null, string> => new Map(draft.map(({ value, label }) => [value, label])),
        ],
        query: [
            (s) => [s.field, biEditorLogic(props).selectors.config, biSceneLogic(props).selectors.worksheet],
            (field: BIField | null, config: BIConfig, worksheet: BIVisualizationNode): HogQLQuery | null => {
                const query = field
                    ? buildBIFilterOptionsQuery(
                          { ...config, filters: [{ field, operator: 'in', value: '' }], rowFilterGroup: undefined },
                          0
                      )
                    : null
                return query ? { ...query, variables: worksheet.source.variables } : null
            },
        ],
    })),
    listeners(({ props, values, actions }) => ({
        save: () => {
            if (!values.field) {
                return
            }
            const editor = biEditorLogic(props)
            const field = values.field
            const aliases = values.draft
                .filter((alias) => alias.label.trim())
                .map((alias) => ({ ...alias, label: alias.label.trim() }))
            editor.actions.restoreState({
                editorView: BIEditorView.BI,
                config: {
                    ...editor.values.config,
                    memberAliases: [
                        ...(editor.values.config.memberAliases ?? []).filter(
                            (entry) => biMemberFieldKey(entry.field) !== biMemberFieldKey(field)
                        ),
                        ...(aliases.length ? [{ field, aliases }] : []),
                    ],
                },
            })
            biSceneLogic(props).actions.recordWorksheet()
            editor.actions.runAfterChange()
            actions.close()
        },
    })),
])