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.

2.6 KB
Raw Blame
import { BuiltLogic, LogicWrapper, useValues } from 'kea'

import { NotFound } from 'lib/components/NotFound'
import { LoadingBar } from 'lib/lemon-ui/LoadingBar'
import { useAttachedLogic } from 'lib/logic/scenes/useAttachedLogic'
import { insightDataLogic } from 'scenes/insights/insightDataLogic'
import { insightLogic } from 'scenes/insights/insightLogic'

import { Query } from '~/queries/Query/Query'
import { SavedInsightNode } from '~/queries/schema/schema-general'
import { QueryContext } from '~/queries/types'
import { InsightLogicProps } from '~/types'

interface InsightProps {
    query: SavedInsightNode
    context?: QueryContext
    embedded?: boolean
    readOnly?: boolean
    /** Attach ourselves to another logic, such as the scene logic */
    attachTo?: BuiltLogic | LogicWrapper
    editMode?: boolean
}

export function SavedInsight({
    query: propsQuery,
    context,
    embedded,
    readOnly,
    attachTo,
    editMode,
}: InsightProps): JSX.Element {
    const insightProps: InsightLogicProps = {
        dashboardItemId: propsQuery.shortId,
        retainResponseOnUnmount: context?.insightProps?.retainResponseOnUnmount,
    }
    const { insight, insightLoading, insightMissing } = useValues(insightLogic(insightProps))
    const { query: dataQuery } = useValues(insightDataLogic(insightProps))

    useAttachedLogic(insightLogic(insightProps), attachTo)
    useAttachedLogic(insightDataLogic(insightProps), attachTo)

    // A remounted notebook node starts from the retained insight, so it renders while the insight loads again.
    if (insightLoading && !insight.query) {
        return (
            <div className="flex flex-col flex-1 justify-center items-center w-full h-full">
                <LoadingBar />
            </div>
        )
    }

    if (insightMissing) {
        return <NotFound object="insight" />
    }

    // `showResults` is reapplied only when the caller set it: a stored query carrying an explicit
    // `false` would otherwise override an embed that asked for the result body and draw a card with
    // nothing in it, while callers that say nothing keep deferring to what the insight stored.
    const query = {
        ...propsQuery,
        ...dataQuery,
        full: propsQuery.full,
        ...(propsQuery.showResults === undefined ? {} : { showResults: propsQuery.showResults }),
    }

    return (
        <Query
            attachTo={attachTo}
            query={query}
            cachedResults={insightProps.retainResponseOnUnmount ? undefined : insight}
            context={{ ...context, insightProps }}
            embedded={embedded}
            readOnly={readOnly}
            editMode={editMode}
        />
    )
}