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.

7.8 KB
Raw Blame
import { useActions, useValues } from 'kea'
import { BindLogic } from 'kea'

import { IconEndpoints, IconPlus } from '@posthog/icons'

import { IconInsightRetention, IconInsightTrends } from 'lib/lemon-ui/icons'
import { LemonButton } from 'lib/lemon-ui/LemonButton'
import { LemonModal } from 'lib/lemon-ui/LemonModal'
import { Popover } from 'lib/lemon-ui/Popover'
import { addSavedInsightsModalLogic } from 'scenes/saved-insights/addSavedInsightsModalLogic'
import { INSIGHT_TYPES_METADATA } from 'scenes/saved-insights/insightTypesMetadata'
import { SavedInsightsTable } from 'scenes/saved-insights/SavedInsightsTable'
import { urls } from 'scenes/urls'

import { isNodeWithSource } from '~/queries/schema/node-guards'
import { EndpointQueryNode, HogQLQuery, NodeKind } from '~/queries/schema/schema-general'
import { InsightType, InsightModel } from '~/types'

import { EndpointFromInsightModal } from './EndpointFromInsightModal'
import { endpointLogic } from './endpointLogic'
import { insightPickerEndpointModalLogic } from './insightPickerEndpointModalLogic'

const UNSUPPORTED_INSIGHT_TYPES = new Set([
    InsightType.FUNNELS,
    InsightType.PATHS,
    InsightType.JOURNEYS,
    InsightType.STICKINESS,
    InsightType.JSON,
    InsightType.HOG,
])

const UNSUPPORTED_QUERY_KINDS = new Set([
    NodeKind.FunnelsQuery,
    NodeKind.PathsQuery,
    NodeKind.PathsV2Query,
    NodeKind.StickinessQuery,
])

function isInsightSupported(insight: InsightModel): boolean {
    const query = insight.query
    if (!query) {
        return true
    }
    const kind = isNodeWithSource(query) ? (query as { source?: { kind?: string } }).source?.kind : query.kind
    return !kind || !UNSUPPORTED_QUERY_KINDS.has(kind as NodeKind)
}

const QUICK_CREATE_TYPES = [
    { type: InsightType.TRENDS, icon: IconInsightTrends, label: 'Trend' },
    { type: InsightType.RETENTION, icon: IconInsightRetention, label: 'Retention' },
]

export function InsightPickerEndpointModal(): JSX.Element {
    const { isOpen, selectedInsight, showMoreInsightTypes } = useValues(insightPickerEndpointModalLogic)
    const { closeModal, selectInsight, toggleShowMoreInsightTypes } = useActions(insightPickerEndpointModalLogic)
    const { openCreateFromInsightModal } = useActions(endpointLogic)

    // Safe cast: unsupported query types (FunnelsQuery, PathsQuery, StickinessQuery)
    // are filtered out via isInsightSupported on the SavedInsightsTable
    const insightQuery: HogQLQuery | EndpointQueryNode | null = selectedInsight?.query
        ? isNodeWithSource(selectedInsight.query)
            ? (selectedInsight.query.source as unknown as HogQLQuery | EndpointQueryNode)
            : (selectedInsight.query as unknown as HogQLQuery | EndpointQueryNode)
        : null

    const additionalTypes = Object.entries(INSIGHT_TYPES_METADATA).filter(
        ([type, meta]) =>
            meta.inMenu &&
            !UNSUPPORTED_INSIGHT_TYPES.has(type as InsightType) &&
            !QUICK_CREATE_TYPES.some((qt) => qt.type === type)
    )

    return (
        <>
            <BindLogic logic={addSavedInsightsModalLogic} props={{}}>
                <LemonModal
                    title="New insight-based endpoint"
                    onClose={closeModal}
                    isOpen={isOpen}
                    width="min(80vw, 64rem)"
                >
                    <div className="space-y-4">
                        <div className="flex flex-wrap items-center gap-3 p-4 bg-surface-secondary rounded-lg">
                            <IconPlus className="text-2xl text-secondary shrink-0" />
                            <div className="flex-1 min-w-[200px]">
                                <div className="font-semibold text-base">Create an endpoint from a new insight</div>
                                <div className="text-sm text-secondary">
                                    <>
                                        Once the insight is saved, open the right side panel and click
                                        <br /> <IconEndpoints /> <code>Create endpoint</code>.
                                    </>
                                </div>
                            </div>
                            <div className="flex flex-wrap items-center gap-2">
                                {QUICK_CREATE_TYPES.map(({ type, icon: Icon, label }) => (
                                    <LemonButton
                                        key={type}
                                        type="primary"
                                        icon={<Icon />}
                                        to={urls.insightNew({ type, sceneSource: 'endpoints' })}
                                        tooltip={INSIGHT_TYPES_METADATA[type]?.description}
                                        data-attr={`endpoint-quick-create-${type.toLowerCase()}`}
                                    >
                                        {label}
                                    </LemonButton>
                                ))}
                                <Popover
                                    visible={showMoreInsightTypes}
                                    onClickOutside={() => toggleShowMoreInsightTypes()}
                                    overlay={
                                        <div className="p-2 space-y-1 min-w-48">
                                            {additionalTypes.map(([type, metadata]) => {
                                                const Icon = metadata.icon
                                                return (
                                                    <LemonButton
                                                        key={type}
                                                        type="tertiary"
                                                        fullWidth
                                                        icon={Icon ? <Icon /> : undefined}
                                                        to={urls.insightNew({
                                                            type: type as InsightType,
                                                            sceneSource: 'endpoints',
                                                        })}
                                                        data-attr={`endpoint-create-${type.toLowerCase()}`}
                                                    >
                                                        {metadata.name}
                                                    </LemonButton>
                                                )
                                            })}
                                        </div>
                                    }
                                >
                                    <LemonButton type="secondary" onClick={() => toggleShowMoreInsightTypes()}>
                                        More
                                    </LemonButton>
                                </Popover>
                            </div>
                        </div>

                        <div>
                            <div className="font-semibold text-base mb-2">
                                Create an endpoint from an existing insight
                            </div>
                            <SavedInsightsTable
                                onToggle={(insight: InsightModel) => {
                                    selectInsight(insight)
                                    openCreateFromInsightModal()
                                }}
                                filterFn={isInsightSupported}
                            />
                        </div>
                    </div>
                </LemonModal>
            </BindLogic>

            {insightQuery && (
                <EndpointFromInsightModal insightQuery={insightQuery} insightShortId={selectedInsight?.short_id} />
            )}
        </>
    )
}