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.0 KB
Raw Blame
import { useActions, useValues } from 'kea'
import { router } from 'kea-router'

import { IconPeople } from '@posthog/icons'
import { Button } from '@posthog/quill'

import { useFeatureFlag } from 'lib/hooks/useFeatureFlag'
import { groupsAccessLogic } from 'lib/introductions/groupsAccessLogic'
import { LemonButton } from 'lib/lemon-ui/LemonButton'
import { Link } from 'lib/lemon-ui/Link'
import { capitalizeFirstLetter } from 'lib/utils/strings'
import { ActivitySceneTabs } from 'scenes/activity/ActivitySceneTabs'
import { SceneExport } from 'scenes/sceneTypes'
import { teamLogic } from 'scenes/teamLogic'
import { urls } from 'scenes/urls'

import { SceneContent } from '~/layout/scenes/components/SceneContent'
import { SceneTitleSection } from '~/layout/scenes/components/SceneTitleSection'
import { useTodayPeopleTitle } from '~/layout/today/todayPeopleRows'
import { groupsModel } from '~/models/groupsModel'
import { Query } from '~/queries/Query/Query'
import { ProductKey } from '~/queries/schema/schema-general'
import { QueryContext } from '~/queries/types'

import { FeedbackButton } from 'products/customer_analytics/frontend/components/FeedbackButton'
import { GroupsIntroduction } from 'products/groups/frontend/components/GroupsIntroduction'

import { groupsListLogic } from '../logics/groupsListLogic'
import { groupsSceneLogic } from '../logics/groupsSceneLogic'

export const scene: SceneExport = {
    component: GroupsScene,
    logic: groupsSceneLogic,
    productKey: ProductKey.GROUP_ANALYTICS,
}

export function GroupsScene(): JSX.Element {
    const { groupTypeIndex, groupTypeName, groupTypeNamePlural } = useValues(groupsSceneLogic)

    const mountedGroupsListLogic = groupsListLogic({ groupTypeIndex })
    const { query, queryWasModified } = useValues(mountedGroupsListLogic)
    const { setQuery } = useActions(mountedGroupsListLogic)

    const { shouldShowGroupsIntroduction } = useValues(groupsAccessLogic)
    const { aggregationLabel } = useValues(groupsModel)
    const { baseCurrency } = useValues(teamLogic)
    const hasCustomerAnalyticsEnabled = useFeatureFlag('CUSTOMER_ANALYTICS')
    const todayRailNav = useFeatureFlag('TODAY_RAIL_NAV')
    const groupTitle = useTodayPeopleTitle('group')

    if (groupTypeIndex === undefined) {
        throw new Error('groupTypeIndex is undefined')
    }

    if (shouldShowGroupsIntroduction) {
        return (
            <SceneContent>
                <ActivitySceneTabs activeKey={`groups-${groupTypeIndex}`} />
                <SceneTitleSection
                    name="Groups"
                    description="Associate events with a group or entity - such as a company, community, or project. Analyze these events as if they were sent by that entity itself. Great for B2B, marketplaces, and more."
                    resourceType={{
                        type: groupTypeName,
                        forceIcon: <IconPeople />,
                    }}
                />
                <GroupsIntroduction />
            </SceneContent>
        )
    }

    let columns = {
        group_name: {
            title: groupTypeName,
        },
    } as QueryContext['columns']

    return (
        <SceneContent>
            <ActivitySceneTabs activeKey={`groups-${groupTypeIndex}`} />

            <SceneTitleSection
                name={capitalizeFirstLetter(groupTypeNamePlural)}
                description={`A catalog of all ${groupTypeNamePlural} for this project`}
                resourceType={groupTitle?.resourceType ?? { type: 'cohort' }}
                actions={
                    <>
                        <FeedbackButton id="customer-analytics-groups-list-feedback-button" />
                        {hasCustomerAnalyticsEnabled &&
                            (todayRailNav ? (
                                <Button
                                    variant="primary"
                                    data-attr={`new-group-${groupTypeIndex}`}
                                    onClick={() => router.actions.push(urls.group(groupTypeIndex, 'new', false))}
                                >
                                    New {aggregationLabel(groupTypeIndex).singular}
                                </Button>
                            ) : (
                                <LemonButton
                                    type="primary"
                                    size="small"
                                    data-attr={`new-group-${groupTypeIndex}`}
                                    onClick={() => router.actions.push(urls.group(groupTypeIndex, 'new', false))}
                                >
                                    New {aggregationLabel(groupTypeIndex).singular}
                                </LemonButton>
                            ))}
                    </>
                }
            />

            <Query
                uniqueKey="groups-query"
                attachTo={groupsSceneLogic()}
                query={{ ...query, showCount: true, showTableViews: true }}
                setQuery={setQuery}
                context={{
                    refresh: 'blocking',
                    emptyStateHeading: queryWasModified
                        ? `No ${groupTypeNamePlural} found`
                        : `No ${groupTypeNamePlural} exist because none have been identified`,
                    emptyStateDetail: queryWasModified ? (
                        'Try changing the date range or property filters.'
                    ) : (
                        <>
                            Go to the{' '}
                            <Link to="https://posthog.com/docs/product-analytics/group-analytics#how-to-create-groups">
                                group analytics docs
                            </Link>{' '}
                            to learn what needs to be done
                        </>
                    ),
                    columns,
                    groupTypeLabel: groupTypeNamePlural,
                    baseCurrency,
                }}
                dataAttr="groups-table"
            />
        </SceneContent>
    )
}