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.3 KB
Raw Blame
import { BuiltLogic, LogicWrapper, useValues } from 'kea'
import { useState } from 'react'

import { AutoSizer } from 'lib/components/AutoSizer'
import { Spinner } from 'lib/lemon-ui/Spinner/Spinner'
import { useAttachedLogic } from 'lib/logic/scenes/useAttachedLogic'
import { CodeEditor } from 'lib/monaco/CodeEditor'

import { dataNodeLogic } from '~/queries/nodes/DataNode/dataNodeLogic'
import { OpenEditorButton } from '~/queries/nodes/Node/OpenEditorButton'
import { AnyResponseType, DataNode as DataNodeType, DataTableNode } from '~/queries/schema/schema-general'

interface DataNodeProps {
    query: DataNodeType
    setQuery?: (query: DataTableNode) => void
    /* Cached Results are provided when shared or exported,
    the data node logic becomes read only implicitly */
    cachedResults?: AnyResponseType
    /** Attach ourselves to another logic, such as the scene logic */
    attachTo?: BuiltLogic | LogicWrapper
}

let uniqueNode = 0

/** Default renderer for data nodes. Display the JSON in a Monaco editor.  */
export function DataNode(props: DataNodeProps): JSX.Element {
    const [key] = useState(() => `DataNode.${uniqueNode++}`)
    const logic = dataNodeLogic({ ...props, key, cachedResults: props.cachedResults, dataNodeCollectionId: key })
    const { response, responseLoading, responseErrorObject } = useValues(logic)

    useAttachedLogic(logic, props.attachTo)

    return (
        <div className="relative">
            <div className="absolute right-0 z-10 p-1 mr-3">
                <OpenEditorButton query={props.query} />
            </div>
            {responseLoading ? (
                <div className="text-2xl">
                    <Spinner />
                </div>
            ) : (
                <AutoSizer
                    disableWidth={true}
                    renderProp={({ height }) =>
                        height ? (
                            <CodeEditor
                                className="border"
                                language="json"
                                value={JSON.stringify(response ?? responseErrorObject, null, 2)}
                                height={Math.max(height, 300)}
                            />
                        ) : null
                    }
                />
            )}
        </div>
    )
}