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 { IconTrash } from '@posthog/icons'
import { LemonButton, LemonDialog, LemonInput, Link } from '@posthog/lemon-ui'

import { RestrictionScope, useRestrictedArea } from 'lib/components/RestrictedArea'
import { TeamMembershipLevel } from 'lib/constants'
import { GroupsAccessStatus, groupsAccessLogic } from 'lib/introductions/groupsAccessLogic'
import { LemonBanner } from 'lib/lemon-ui/LemonBanner'
import { LemonTable, LemonTableColumns } from 'lib/lemon-ui/LemonTable'

import { GroupType } from '~/types'

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

import { groupAnalyticsConfigLogic } from './groupAnalyticsConfigLogic'

export interface DeleteGroupTypeDialogProps {
    onConfirm: () => void
    groupTypeName: string
}

export function openDeleteGroupTypeDialog({ onConfirm, groupTypeName }: DeleteGroupTypeDialogProps): void {
    const groupType = groupTypeName.toLowerCase()
    LemonDialog.open({
        title: `Delete ${groupType} group type`,
        description: (
            <div className="mt-2 w-150">
                Deleting a group type is irreversible.
                <br />
                <br />
                You will not be able to assign existing events from this group type to another group type created in the
                future, only new events.
                <br />
                <br />
                For more information about groups, see{' '}
                <Link to="https://posthog.com/docs/product-analytics/group-analytics" target="_blank">
                    the docs
                </Link>
            </div>
        ),
        secondaryButton: {
            type: 'secondary',
            children: 'Cancel',
        },
        primaryButton: {
            type: 'primary',
            status: 'danger',
            onClick: onConfirm,
            children: `Delete ${groupType}`,
        },
    })
}

export function GroupAnalyticsConfig(): JSX.Element | null {
    const { groupTypes, groupTypesLoading, singularChanges, pluralChanges, hasChanges } =
        useValues(groupAnalyticsConfigLogic)
    const { setSingular, setPlural, reset, save, deleteGroupType } = useActions(groupAnalyticsConfigLogic)

    const { groupsAccessStatus, needsUpgradeForGroups } = useValues(groupsAccessLogic)
    const restrictedReason = useRestrictedArea({
        scope: RestrictionScope.Project,
        minimumAccessLevel: TeamMembershipLevel.Admin,
    })

    if (needsUpgradeForGroups) {
        return <GroupsIntroduction />
    }

    const columns: LemonTableColumns<GroupType> = [
        {
            title: 'Group type',
            tooltip: 'As used in code',
            dataIndex: 'group_type',
            key: 'name',
            render: function RenderName(name) {
                return name
            },
        },
        {
            title: 'Singular name',
            key: 'singular',
            render: function Render(_, groupType) {
                return (
                    <LemonInput
                        value={
                            singularChanges[groupType.group_type_index] ||
                            groupType.name_singular ||
                            groupType.group_type
                        }
                        onChange={(e) => setSingular(groupType.group_type_index, e)}
                        disabledReason={restrictedReason}
                    />
                )
            },
        },
        {
            title: 'Plural name',
            key: 'plural',
            render: function Render(_, groupType) {
                return (
                    <LemonInput
                        value={
                            pluralChanges[groupType.group_type_index] ||
                            groupType.name_plural ||
                            `${groupType.group_type}(s)`
                        }
                        onChange={(e) => setPlural(groupType.group_type_index, e)}
                        disabledReason={restrictedReason}
                    />
                )
            },
        },
        {
            title: '',
            key: 'delete',
            width: 24,
            render: function Render(_, groupType) {
                return (
                    <LemonButton
                        status="danger"
                        size="small"
                        icon={<IconTrash />}
                        onClick={() =>
                            openDeleteGroupTypeDialog({
                                onConfirm: () => deleteGroupType(groupType.group_type_index),
                                groupTypeName: groupType.group_type,
                            })
                        }
                        disabledReason={restrictedReason}
                    />
                )
            },
        },
    ]

    return (
        <>
            {groupsAccessStatus !== GroupsAccessStatus.AlreadyUsing && (
                <LemonBanner type="info" className="mb-4">
                    Group types will show up here after you send your first event associated with a group. Take a look
                    at{' '}
                    <Link to="https://posthog.com/docs/product-analytics/group-analytics" target="_blank">
                        this guide
                    </Link>{' '}
                    for more information on getting started.
                </LemonBanner>
            )}

            <LemonTable columns={columns} dataSource={Array.from(groupTypes.values())} loading={groupTypesLoading} />

            <div className="flex gap-2 mt-4">
                <LemonButton
                    type="primary"
                    disabledReason={hasChanges ? restrictedReason : 'Make some changes before saving'}
                    onClick={save}
                >
                    Save
                </LemonButton>
                <LemonButton disabledReason={hasChanges ? restrictedReason : 'Revert any changes made'} onClick={reset}>
                    Cancel
                </LemonButton>
            </div>
        </>
    )
}