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.

6.1 KB
Raw Blame
import { deepEqual as equal } from 'fast-equals'
import { MakeLogicType, actions, connect, kea, path, reducers, selectors } from 'kea'
import { urlToAction } from 'kea-router'

import { lemonToast } from '@posthog/lemon-ui'

import { trackedActionToUrl } from 'lib/logic/scenes/trackedActionToUrl'
import { sceneConfigurations } from 'scenes/scenes'
import { Scene } from 'scenes/sceneTypes'
import { teamLogic } from 'scenes/teamLogic'
import { urls } from 'scenes/urls'

import { defaultDataTableColumns } from '~/queries/nodes/DataTable/utils'
import { DataTableNode, NodeKind, ProductKey } from '~/queries/schema/schema-general'
import { Breadcrumb } from '~/types'
import type { TeamPublicType, TeamType } from '~/types'

export const PEOPLE_LIST_CONTEXT_KEY = 'people-list'

function buildDefaultQuery(personLastSeenAtEnabled: boolean): DataTableNode {
    const columns = [...defaultDataTableColumns(NodeKind.ActorsQuery, personLastSeenAtEnabled), 'person.$delete']
    return {
        kind: NodeKind.DataTableNode,
        source: {
            kind: NodeKind.ActorsQuery,
            tags: { productKey: ProductKey.CUSTOMER_ANALYTICS },
            select: columns,
        },
        defaultColumns: columns,
        full: true,
        propertiesViaUrl: true,
        contextKey: PEOPLE_LIST_CONTEXT_KEY,
    } as DataTableNode
}

export const PEOPLE_LIST_DEFAULT_QUERY = buildDefaultQuery(false)

// The scene's real default gains a `last_seen_at` column when the team turns on that setting,
// so a caller that checks for an untouched default has to accept both shapes.
const PEOPLE_LIST_DEFAULT_QUERY_WITH_LAST_SEEN_AT = buildDefaultQuery(true)

/** Takes an `ActorsQuery` source, not the whole `DataTableNode`. */
export function isPeopleListDefaultQuery(source: unknown): boolean {
    return (
        equal(source, PEOPLE_LIST_DEFAULT_QUERY.source) ||
        equal(source, PEOPLE_LIST_DEFAULT_QUERY_WITH_LAST_SEEN_AT.source)
    )
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface personsSceneLogicValues {
    currentTeam: TeamPublicType | TeamType | null // teamLogic
    breadcrumbs: Breadcrumb[]
    defaultColumns: string[]
    defaultQuery: DataTableNode
    isBannerLoading: boolean
    query: DataTableNode
    showDisplayNameNudge: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface personsSceneLogicActions {
    setIsBannerLoading: (isBannerLoading: boolean) => {
        isBannerLoading: boolean
    }
    setQuery: (query: DataTableNode) => {
        query: DataTableNode
    }
    setShowDisplayNameNudge: (showDisplayNameNudge: boolean) => {
        showDisplayNameNudge: boolean
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface personsSceneLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        defaultQuery: (currentTeam: TeamPublicType | TeamType | null) => DataTableNode
        defaultColumns: (defaultQuery: DataTableNode) => string[]
    }
}

export type personsSceneLogicType = MakeLogicType<
    personsSceneLogicValues,
    personsSceneLogicActions,
    Record<string, any>,
    personsSceneLogicMeta
>

export const personsSceneLogic = kea<personsSceneLogicType>([
    path(['scenes', 'persons', 'personsSceneLogic']),

    connect({ values: [teamLogic, ['currentTeam']] }),

    actions({
        setQuery: (query: DataTableNode) => ({ query }),
        setShowDisplayNameNudge: (showDisplayNameNudge: boolean) => ({ showDisplayNameNudge }),
        setIsBannerLoading: (isBannerLoading: boolean) => ({ isBannerLoading }),
    }),

    reducers({
        query: [
            PEOPLE_LIST_DEFAULT_QUERY,
            {
                setQuery: (state, { query }) => ({
                    ...query,
                    defaultColumns: query.defaultColumns ?? state.defaultColumns,
                }),
            },
        ],
        showDisplayNameNudge: [
            false,
            {
                setShowDisplayNameNudge: (_, { showDisplayNameNudge }) => showDisplayNameNudge,
            },
        ],
        isBannerLoading: [
            false,
            {
                setIsBannerLoading: (_, { isBannerLoading }) => isBannerLoading,
            },
        ],
    }),

    selectors({
        defaultQuery: [
            (s) => [s.currentTeam],
            (currentTeam: null | import('~/types').TeamPublicType | import('~/types').TeamType): DataTableNode =>
                buildDefaultQuery(currentTeam?.extra_settings?.person_last_seen_at_enabled === true),
        ],
        defaultColumns: [
            (s) => [s.defaultQuery],
            (defaultQuery: DataTableNode): string[] => defaultQuery.defaultColumns ?? [],
        ],
        breadcrumbs: [
            () => [],
            (): Breadcrumb[] => [
                {
                    key: 'persons',
                    name: sceneConfigurations[Scene.Persons].name,
                    iconType: sceneConfigurations[Scene.Persons].iconType || 'default_icon_type',
                },
            ],
        ],
    }),

    trackedActionToUrl(({ values }) => ({
        setQuery: () => [
            urls.persons(),
            {},
            equal(values.query, values.defaultQuery) ? {} : { q: values.query },
            { replace: true },
        ],
    })),

    urlToAction(({ actions, values }) => ({
        [urls.persons()]: (_, __, { q: queryParam }): void => {
            if (!equal(queryParam, values.query)) {
                if (!queryParam) {
                    actions.setQuery({
                        ...values.defaultQuery,
                        defaultColumns: values.defaultColumns,
                    })
                } else {
                    if (typeof queryParam === 'object') {
                        actions.setQuery({
                            ...queryParam,
                            defaultColumns: values.defaultColumns,
                        })
                    } else {
                        lemonToast.error('Invalid query in URL')
                        console.error({ queryParam })
                    }
                }
            }
        },
    })),
])