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.6 KB
Raw Blame
import { useValues } from 'kea'
import { useMemo } from 'react'

import { IconPerson } from '@posthog/icons'
import { LemonTag, Tooltip } from '@posthog/lemon-ui'

import { LemonTable, LemonTableColumns } from 'lib/lemon-ui/LemonTable'
import { capitalizeFirstLetter } from 'lib/utils/strings'

import { groupsModel } from '~/models/groupsModel'
import { ActorType, GroupActorType } from '~/types'

import { relatedGroupsLogic } from 'products/groups/frontend/logics/relatedGroupsLogic'
import { GroupActorDisplay } from 'products/persons/frontend/components/GroupActorDisplay'
import { PersonDisplay } from 'products/persons/frontend/components/PersonDisplay'

export interface RelatedGroupsProps {
    groupTypeIndex: number | null
    id: string
    type?: 'person' | 'group'
    pageSize?: number
    embedded?: boolean
    /** Tag the group row whose key matches this value with `highlightLabel`. */
    highlightGroupKey?: string | null
    /** Label for the tag on the highlighted row. Required for the tag to render. */
    highlightLabel?: string
    /** Tooltip shown on the highlight label tag (caller supplies the context-specific wording). */
    highlightLabelTooltip?: string
    /** Also show a "Stale" tag on the highlighted row. */
    highlightStale?: boolean
    /** Tooltip shown on the "Stale" tag (caller supplies the context-specific wording). */
    highlightStaleTooltip?: string
    /** Extra group rows to append (deduped by id), e.g. a group not in the live related list. */
    extraActors?: ActorType[]
    /** Extra content shown under a group's name, e.g. figures the caller loads for that group. */
    renderGroupDetail?: (actor: GroupActorType) => React.ReactNode
}

export function RelatedGroups({
    groupTypeIndex,
    id,
    type,
    pageSize,
    embedded = false,
    highlightGroupKey,
    highlightLabel,
    highlightLabelTooltip,
    highlightStale = false,
    highlightStaleTooltip,
    extraActors,
    renderGroupDetail,
}: RelatedGroupsProps): JSX.Element {
    const { relatedActors, relatedPeople, relatedActorsLoading } = useValues(relatedGroupsLogic({ groupTypeIndex, id }))
    const { aggregationLabel } = useValues(groupsModel)

    const dataSource = useMemo(() => {
        if (type === 'person') {
            return relatedPeople
        }
        const extraGroups = (extraActors ?? []).filter((extra) => !relatedActors.some((actor) => actor.id === extra.id))
        return [...relatedActors, ...extraGroups]
    }, [type, relatedPeople, relatedActors, extraActors])

    const columns: LemonTableColumns<ActorType> = useMemo(
        () => [
            {
                title: 'Type',
                key: 'type',
                render: function RenderActor(_, actor: ActorType) {
                    if (actor.type === 'group') {
                        return <>{capitalizeFirstLetter(aggregationLabel(actor.group_type_index).singular)}</>
                    }
                    return (
                        <>
                            <IconPerson /> Person
                        </>
                    )
                },
            },
            {
                title: 'id',
                key: 'id',
                render: function RenderActor(_, actor: ActorType) {
                    if (actor.type === 'group') {
                        const isHighlighted = highlightGroupKey != null && actor.group_key === highlightGroupKey
                        const nameRow = (
                            <div className="flex items-center gap-2">
                                <GroupActorDisplay actor={actor} />
                                {isHighlighted && highlightLabel && (
                                    <Tooltip title={highlightLabelTooltip}>
                                        <LemonTag type="muted" size="small">
                                            {highlightLabel}
                                        </LemonTag>
                                    </Tooltip>
                                )}
                                {isHighlighted && highlightStale && (
                                    <Tooltip title={highlightStaleTooltip}>
                                        <LemonTag type="warning" size="small">
                                            Stale
                                        </LemonTag>
                                    </Tooltip>
                                )}
                            </div>
                        )
                        if (!renderGroupDetail) {
                            return nameRow
                        }
                        return (
                            <div className="flex flex-col gap-1">
                                {nameRow}
                                {renderGroupDetail(actor)}
                            </div>
                        )
                    }
                    return <PersonDisplay person={actor} withIcon={false} />
                },
            },
        ],
        [
            aggregationLabel,
            highlightGroupKey,
            highlightLabel,
            highlightLabelTooltip,
            highlightStale,
            highlightStaleTooltip,
            renderGroupDetail,
        ]
    )

    const nouns: [string, string] =
        type === 'person' ? ['related person', 'related people'] : ['related group', 'related groups']

    return (
        <LemonTable
            dataSource={dataSource}
            columns={columns}
            embedded={embedded}
            rowKey="id"
            pagination={{ pageSize: pageSize || 30, hideOnSinglePage: true }}
            loading={relatedActorsLoading}
            nouns={nouns}
            emptyState="No related groups found"
        />
    )
}