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.

3.1 KB
Raw Blame
import { BindLogic, BuiltLogic, LogicWrapper, useActions, useValues } from 'kea'
import { useMemo } from 'react'

import { useOnMountEffect } from 'lib/hooks/useOnMountEffect'
import { useAttachedLogic } from 'lib/logic/scenes/useAttachedLogic'
import { eventUsageLogic } from 'lib/utils/eventUsageLogic'
import { Notebook } from 'scenes/notebooks/Notebook/Notebook'
import { NotebookLogicProps, notebookLogic } from 'scenes/notebooks/Notebook/notebookLogic'

import { groupsModel } from '~/models/groupsModel'
import { AnyPropertyFilter, CustomerProfileScope, Group, PropertyFilterType, PropertyOperator } from '~/types'

import { groupLogic } from 'products/groups/frontend/logics/groupLogic'

import { customerProfileLogic } from '../customerProfileLogic'
import { CustomerProfileMenu } from './CustomerProfileMenu'

interface GroupProfileCanvasProps {
    group: Group
    attachTo: BuiltLogic | LogicWrapper
}

export const GroupProfileCanvas = ({ group, attachTo }: GroupProfileCanvasProps): JSX.Element => {
    const { aggregationLabel } = useValues(groupsModel)
    const { reportGroupProfileViewed } = useActions(eventUsageLogic)

    const groupKey = group.group_key
    const groupTypeIndex = group.group_type_index
    const mode = 'canvas'
    const shortId = `${mode}-${groupKey}`
    const attrs = useMemo(
        () => ({
            groupKey,
            groupTypeIndex,
        }),
        [groupKey, groupTypeIndex]
    )
    const customerProfileLogicProps = {
        attrs,
        scope: CustomerProfileScope[`GROUP_${groupTypeIndex}`],
        key: `customer-profile-${groupKey}`,
        canvasShortId: shortId,
    }
    const { content } = useValues(customerProfileLogic(customerProfileLogicProps))

    const groupFilter: AnyPropertyFilter[] = [
        {
            type: PropertyFilterType.EventMetadata,
            key: `$group_${groupTypeIndex}`,
            label: aggregationLabel(groupTypeIndex).singular,
            value: groupKey,
            operator: PropertyOperator.Exact,
        },
    ]

    useOnMountEffect(() => {
        reportGroupProfileViewed()
    })
    const notebookLogicProps: NotebookLogicProps = {
        shortId,
        mode,
        canvasFiltersOverride: groupFilter,
    }
    const mountedNotebookLogic = notebookLogic(notebookLogicProps)
    useAttachedLogic(mountedNotebookLogic, attachTo)

    return (
        <BindLogic logic={notebookLogic} props={notebookLogicProps}>
            <BindLogic logic={groupLogic} props={{ groupKey, groupTypeIndex }}>
                <BindLogic logic={customerProfileLogic} props={customerProfileLogicProps}>
                    <CustomerProfileMenu />
                    <Notebook
                        editable={false}
                        shortId={shortId}
                        mode={mode}
                        className="NotebookProfileCanvas"
                        canvasFiltersOverride={groupFilter}
                        initialContent={{
                            type: 'doc',
                            content,
                        }}
                    />
                </BindLogic>
            </BindLogic>
        </BindLogic>
    )
}