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.

26.3 KB
Raw Blame
import { deepEqual as equal } from 'fast-equals'
import {
    MakeLogicType,
    actions,
    beforeUnmount,
    connect,
    kea,
    key,
    listeners,
    path,
    props,
    reducers,
    selectors,
} from 'kea'
import { combineUrl } from 'kea-router'
import { subscriptions } from 'kea-subscriptions'
import posthog from 'posthog-js'

import { JSONContent } from 'lib/components/RichContentEditor/types'
import { FEATURE_FLAGS } from 'lib/constants'
import { copyToClipboard } from 'lib/utils/copyToClipboard'
import { uuid } from 'lib/utils/dom'
import { removeUndefinedAndNull } from 'lib/utils/objects'
import { NotebookNodeType, NotebookTarget } from 'scenes/notebooks/types'
import { Scene } from 'scenes/sceneTypes'
import { urls } from 'scenes/urls'

import { SIDE_PANEL_CONTEXT_KEY, SidePanelSceneContext } from '~/layout/navigation-3000/sidepanel/types'
import { panelLayoutLogic } from '~/layout/panel-layout/panelLayoutLogic'
import { notebooksModel } from '~/models/notebooksModel'
import { DataVisualizationNode, FileSystemIconType, HogQLFilters, NodeKind } from '~/queries/schema/schema-general'
import { Breadcrumb, ProjectTreeRef } from '~/types'

import type { SaveAsTargetId } from 'products/data_warehouse/frontend/sql_editor/saveAsTargets'

import type { FeatureFlagsSet } from '../../../lib/logic/featureFlagLogic'
import type { DataWarehouseSavedQuery, LinkBreadcrumb, InsightModel } from '../../../types'
import { NEW_QUERY, normalizeFiltersForUrl, sqlEditorLogic, toDataVisualizationNode } from './sqlEditorLogic'
import type { QueryTab, SqlEditorSource } from './sqlEditorLogic'
import { SQLEditorMode } from './sqlEditorModes'

export interface SaveAsMenuItem {
    action: SaveAsTargetId
    label: string
    dataAttr?: string
}

function setFiltersHashParam(url: URL, filters: HogQLFilters | null | undefined): void {
    const normalizedFilters = normalizeFiltersForUrl(filters)
    if (!normalizedFilters) {
        return
    }

    const hashParams = new URLSearchParams(url.hash ? url.hash.slice(1) : '')
    hashParams.set('filters', JSON.stringify(normalizedFilters))
    url.hash = hashParams.toString()
}

export const renderTableCount = (count: undefined | number): null | JSX.Element => {
    if (!count) {
        return null
    }

    return (
        <span className="text-xs mr-1 italic text-[color:var(--color-text-secondary-3000)]">
            {`(${new Intl.NumberFormat('en', {
                notation: 'compact',
                compactDisplay: 'short',
            })
                .format(count)
                .toLowerCase()})`}
        </span>
    )
}

export interface EditorSceneLogicProps {
    tabId: string
    mode?: SQLEditorMode
}

export function buildSqlNotebook(
    queryInput: string,
    activeTab: QueryTab | null,
    editingInsight: InsightModel | null,
    featureFlags: FeatureFlagsSet
): { title: string; content: JSONContent[] } {
    const activeTabName = activeTab?.name === NEW_QUERY ? undefined : activeTab?.name
    const sqlNode = featureFlags[FEATURE_FLAGS.REVAMPED_PY_NOTEBOOKS]
        ? {
              type: NotebookNodeType.SQLV2,
              attrs: {
                  nodeId: uuid(),
                  code: queryInput,
                  returnVariable: '',
                  edit: true,
              },
          }
        : {
              type: NotebookNodeType.Query,
              attrs: {
                  query: {
                      kind: NodeKind.DataTableNode,
                      source: {
                          kind: NodeKind.HogQLQuery,
                          query: queryInput,
                      },
                  },
                  edit: true,
              },
          }

    return {
        title: editingInsight?.name || editingInsight?.derived_name || activeTabName || 'SQL query',
        content: [sqlNode],
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface editorSceneLogicValues {
    notebooksLoading: boolean // notebooksModel
    activeTab: QueryTab | null // sqlEditorLogic
    dashboardId: number | null // sqlEditorLogic
    dataLogicKey: string // sqlEditorLogic
    editingInsight: InsightModel | null // sqlEditorLogic
    editingView: DataWarehouseSavedQuery | undefined // sqlEditorLogic
    editorSource: SqlEditorSource // sqlEditorLogic
    editorUrl: string // sqlEditorLogic
    featureFlags: FeatureFlagsSet // sqlEditorLogic
    insightLoading: boolean // sqlEditorLogic
    queryInput: string | null // sqlEditorLogic
    sourceQuery: DataVisualizationNode // sqlEditorLogic
    viewLoading: boolean // sqlEditorLogic
    breadcrumbs: Breadcrumb[]
    isCreateViewFlow: boolean
    isHistoryModalOpen: boolean
    projectTreeRef: ProjectTreeRef | null
    saveAsMenuItems: {
        primary: SaveAsMenuItem
        secondary: SaveAsMenuItem[]
    }
    sidePanelContext: SidePanelSceneContext
    titleSectionProps:
        | {
              description: string
              forceBackTo: LinkBreadcrumb
              name: string
              resourceType: {
                  type: string
              }
          }
        | {
              description?: undefined
              forceBackTo: LinkBreadcrumb
              name: string
              resourceType: {
                  type: string
              }
          }
        | {
              description?: undefined
              forceBackTo?: undefined
              name: string
              resourceType: {
                  type: string
              }
          }
    updateInsightButtonEnabled: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface editorSceneLogicActions {
    createNotebook: (
        location: NotebookTarget,
        title?: string | undefined,
        content?: JSONContent[] | undefined,
        onCreate?:
            | ((
                  notebook: import('node_modules/kea/lib').BuiltLogic<
                      import('../../notebooks/Notebook/notebookLogic').notebookLogicType
                  >
              ) => void)
            | undefined,
        shortId?: string | undefined
    ) => {
        content: JSONContent[] | undefined
        location: NotebookTarget
        onCreate:
            | ((
                  notebook: import('node_modules/kea/lib').BuiltLogic<
                      import('../../notebooks/Notebook/notebookLogic').notebookLogicType
                  >
              ) => void)
            | undefined
        shortId: string | undefined
        title: string | undefined
    } // notebooksModel
    setLayoutNavCollapsedOverride: (
        owner: string,
        collapsed: boolean | null
    ) => {
        collapsed: boolean | null
        owner: string
    } // panelLayoutLogic
    closeHistoryModal: () => {
        value: true
    }
    convertToNotebook: () => {
        value: true
    }
    openHistoryModal: () => {
        value: true
    }
    openSaveAsMenu: () => {
        value: true
    }
    selectSaveAsTarget: (target: SaveAsTargetId) => {
        target: SaveAsTargetId
    }
    shareTab: () => {
        value: true
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface editorSceneLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        projectTreeRef: (
            editingInsight: InsightModel<import('~/queries/schema/schema-general').Node<Record<string, any>>> | null
        ) => ProjectTreeRef | null
        breadcrumbs: (activeTab: QueryTab | null, editorUrl: string) => Breadcrumb[]
        isCreateViewFlow: (
            editorSource: SqlEditorSource,
            editingView: DataWarehouseSavedQuery | undefined,
            editingInsight: InsightModel<import('~/queries/schema/schema-general').Node<Record<string, any>>> | null
        ) => boolean
        titleSectionProps: (
            editingInsight: InsightModel<import('~/queries/schema/schema-general').Node<Record<string, any>>> | null,
            insightLoading: boolean,
            editingView: DataWarehouseSavedQuery | undefined,
            viewLoading: boolean,
            editorSource: SqlEditorSource,
            dashboardId: number | null,
            activeTab: QueryTab | null,
            editorUrl: string
        ) =>
            | {
                  description: string
                  forceBackTo: LinkBreadcrumb
                  name: string
                  resourceType: {
                      type: string
                  }
              }
            | {
                  description?: undefined
                  forceBackTo: LinkBreadcrumb
                  name: string
                  resourceType: {
                      type: string
                  }
              }
            | {
                  description?: undefined
                  forceBackTo?: undefined
                  name: string
                  resourceType: {
                      type: string
                  }
              }
        saveAsMenuItems: (
            editorSource: SqlEditorSource,
            dashboardId: number | null
        ) => {
            primary: SaveAsMenuItem
            secondary: SaveAsMenuItem[]
        }
        updateInsightButtonEnabled: (
            sourceQuery: DataVisualizationNode,
            activeTab: QueryTab | null,
            editingInsight: InsightModel<import('~/queries/schema/schema-general').Node<Record<string, any>>> | null,
            queryInput: string | null
        ) => boolean
    }
}

export type editorSceneLogicType = MakeLogicType<
    editorSceneLogicValues,
    editorSceneLogicActions,
    EditorSceneLogicProps,
    editorSceneLogicMeta
>

// Only the full-scene editor changes the app navigation. An embedded editor sits inside another scene.
const ownsLayoutNav = (props: EditorSceneLogicProps): boolean => !props.mode || props.mode === SQLEditorMode.FullScene
const layoutNavOverrideOwner = (props: EditorSceneLogicProps): string => `sql-editor-${props.tabId}`

export const editorSceneLogic = kea<editorSceneLogicType>([
    path(['data-warehouse', 'editor', 'editorSceneLogic']),
    props({} as EditorSceneLogicProps),
    key((props) => props.tabId),
    connect((props: EditorSceneLogicProps) => ({
        values: [
            sqlEditorLogic({ tabId: props.tabId, ...(props.mode ? { mode: props.mode } : {}) }),
            [
                'activeTab',
                'dashboardId',
                'dataLogicKey',
                'editingInsight',
                'editingView',
                'editorUrl',
                'editorSource',
                'featureFlags',
                'insightLoading',
                'queryInput',
                'sourceQuery',
                'viewLoading',
            ],
            notebooksModel,
            ['notebooksLoading'],
        ],
        actions: [notebooksModel, ['createNotebook'], panelLayoutLogic, ['setLayoutNavCollapsedOverride']],
    })),
    actions({
        convertToNotebook: true,
        shareTab: true,
        openHistoryModal: true,
        closeHistoryModal: true,
        openSaveAsMenu: true,
        selectSaveAsTarget: (target: SaveAsTargetId) => ({ target }),
    }),
    reducers({
        isHistoryModalOpen: [
            false as boolean,
            {
                openHistoryModal: () => true,
                closeHistoryModal: () => false,
            },
        ],
    }),
    selectors({
        projectTreeRef: [
            (s) => [s.editingInsight],
            (editingInsight: InsightModel | null): ProjectTreeRef | null =>
                editingInsight ? { type: 'insight', ref: editingInsight.short_id } : null,
        ],
        breadcrumbs: [
            (s) => [s.activeTab, s.editorUrl],
            (activeTab: null | import('./sqlEditorLogic').QueryTab, editorUrl: string): Breadcrumb[] => {
                const { draft, insight, view } = activeTab || {}
                const biHash =
                    editorUrl === urls.businessIntelligence() && activeTab?.biEditorState
                        ? { mode: activeTab.biEditorState.editorView, bi: activeTab.biEditorState.config }
                        : undefined
                const first = {
                    key: editorUrl === urls.businessIntelligence() ? Scene.BusinessIntelligence : Scene.SQLEditor,
                    name: editorUrl === urls.businessIntelligence() ? 'Business intelligence' : 'SQL query',
                    to: editorUrl,
                    iconType: (editorUrl === urls.businessIntelligence()
                        ? 'business_intelligence'
                        : 'sql_editor') as FileSystemIconType,
                }
                if (view) {
                    return [
                        {
                            key: view.id,
                            name: view.name,
                            path: combineUrl(editorUrl, { open_view: view.id }, biHash).url,
                            iconType: 'sql_editor',
                        },
                    ]
                } else if (insight) {
                    return [
                        first,
                        {
                            key: insight.id,
                            name: insight.name || insight.derived_name || 'Untitled',
                            path: combineUrl(editorUrl, { open_insight: insight.short_id }, biHash).url,
                            iconType: 'sql_editor',
                        },
                    ]
                } else if (draft) {
                    return [
                        first,
                        {
                            key: draft.id,
                            name: draft.name || 'Untitled',
                            path: combineUrl(editorUrl, { open_draft: draft.id }, biHash).url,
                            iconType: 'sql_editor',
                        },
                    ]
                }
                return [first]
            },
        ],
        // Matches the state that titleSectionProps names "New view".
        isCreateViewFlow: [
            (s) => [s.editorSource, s.editingView, s.editingInsight],
            (
                editorSource: import('./sqlEditorLogic').SqlEditorSource,
                editingView: import('~/types').DataWarehouseSavedQuery | undefined,
                editingInsight: null | import('~/types').InsightModel
            ): boolean => editorSource === 'view' && !editingView && !editingInsight,
        ],
        titleSectionProps: [
            (s) => [
                s.editingInsight,
                s.insightLoading,
                s.editingView,
                s.viewLoading,
                s.editorSource,
                s.dashboardId,
                s.activeTab,
                s.editorUrl,
            ],
            (
                editingInsight: null | import('~/types').InsightModel,
                insightLoading: boolean,
                editingView: import('~/types').DataWarehouseSavedQuery | undefined,
                viewLoading: boolean,
                editorSource: import('./sqlEditorLogic').SqlEditorSource,
                dashboardId: number | null,
                activeTab: null | import('./sqlEditorLogic').QueryTab,
                editorUrl: string
            ) => {
                if (editingInsight) {
                    const forceBackTo: Breadcrumb = dashboardId
                        ? {
                              key: 'dashboard',
                              name: 'Back to dashboard',
                              path: urls.dashboard(dashboardId),
                              iconType: 'dashboard',
                          }
                        : {
                              key: editingInsight.short_id,
                              name: 'Back to insight',
                              path: urls.insightView(editingInsight.short_id),
                              iconType: 'insight/hog',
                          }

                    return {
                        forceBackTo,
                        name: activeTab?.name || editingInsight.derived_name || '',
                        description: activeTab?.description ?? editingInsight.description ?? '',
                        resourceType: { type: 'insight/hog' },
                    }
                }

                if (insightLoading) {
                    return {
                        name: 'Loading insight...',
                        resourceType: { type: 'insight/hog' },
                    }
                }

                if (editingView) {
                    return {
                        name: editingView.name,
                        resourceType: {
                            type: editingView.is_materialized ? 'matview' : 'view',
                        },
                    }
                }

                if (viewLoading) {
                    return {
                        name: 'Loading view...',
                        resourceType: { type: 'view' },
                    }
                }

                if (!activeTab) {
                    const searchParams = new URLSearchParams(window.location.search)
                    const hashParams = new URLSearchParams(window.location.hash.slice(1))
                    if (searchParams.get('open_view') || hashParams.get('view')) {
                        return {
                            name: 'Loading view...',
                            resourceType: { type: 'view' },
                        }
                    }

                    if (searchParams.get('open_insight') || hashParams.get('insight')) {
                        return {
                            name: 'Loading insight...',
                            resourceType: { type: 'insight/hog' },
                        }
                    }
                }

                if (editorSource === 'endpoint') {
                    const forceBackTo: Breadcrumb = {
                        key: 'endpoints',
                        name: 'Endpoints',
                        path: urls.endpoints(),
                        iconType: 'endpoints',
                    }

                    return {
                        forceBackTo,
                        name: 'New endpoint',
                        resourceType: { type: 'sql_editor' },
                    }
                }

                if (editorSource === 'view') {
                    const forceBackTo: Breadcrumb = {
                        key: 'models',
                        name: 'Models',
                        path: urls.models(),
                        iconType: 'sql_editor',
                    }

                    return {
                        forceBackTo,
                        name: 'New view',
                        resourceType: { type: 'sql_editor' },
                    }
                }

                if (dashboardId) {
                    const forceBackTo: Breadcrumb = {
                        key: 'dashboard',
                        name: 'Back to dashboard',
                        path: urls.dashboard(dashboardId),
                        iconType: 'dashboard',
                    }

                    return {
                        forceBackTo,
                        name: 'New SQL query',
                        resourceType: { type: 'sql_editor' },
                    }
                }

                return {
                    name: editorUrl === urls.businessIntelligence() ? 'New worksheet' : 'New SQL query',
                    resourceType: {
                        type: editorUrl === urls.businessIntelligence() ? 'business_intelligence' : 'sql_editor',
                    },
                }
            },
        ],
        saveAsMenuItems: [
            (s) => [s.editorSource, s.dashboardId],
            (
                editorSource: import('./sqlEditorLogic').SqlEditorSource,
                dashboardId: number | null
            ): { primary: SaveAsMenuItem; secondary: SaveAsMenuItem[] } => {
                const saveAsInsightItem: SaveAsMenuItem = {
                    action: 'insight',
                    label: dashboardId ? 'Save & add to dashboard' : 'Save as insight',
                }
                const saveAsEndpointItem: SaveAsMenuItem = {
                    action: 'endpoint',
                    label: 'Save as endpoint',
                }
                const saveAsViewItem: SaveAsMenuItem = {
                    action: 'view',
                    label: 'Save as view',
                    dataAttr: 'sql-editor-save-view-button',
                }
                const saveAsMetricItem: SaveAsMenuItem = {
                    action: 'metric',
                    label: 'Save as metric',
                }
                const extraItems = [saveAsEndpointItem, saveAsMetricItem]

                if (editorSource === 'metric') {
                    return {
                        primary: saveAsMetricItem,
                        secondary: [saveAsInsightItem, saveAsViewItem, saveAsEndpointItem],
                    }
                }

                if (editorSource === 'endpoint') {
                    return {
                        primary: saveAsEndpointItem,
                        secondary: [saveAsInsightItem, saveAsViewItem, saveAsMetricItem],
                    }
                }

                if (editorSource === 'view') {
                    return {
                        primary: saveAsViewItem,
                        secondary: [saveAsInsightItem, ...extraItems],
                    }
                }

                return {
                    primary: saveAsInsightItem,
                    secondary: [saveAsViewItem, ...extraItems],
                }
            },
        ],
        updateInsightButtonEnabled: [
            (s) => [s.sourceQuery, s.activeTab, s.editingInsight, s.queryInput],
            (
                sourceQuery: DataVisualizationNode,
                activeTab: null | import('./sqlEditorLogic').QueryTab,
                editingInsight: null | import('~/types').InsightModel,
                queryInput: string | null
            ) => {
                if (!editingInsight?.query) {
                    return false
                }

                const updatedName = activeTab?.name !== editingInsight.name
                const updatedDescription = (activeTab?.description ?? '') !== (editingInsight.description ?? '')
                const sourceQueryWithoutUndefinedAndNullKeys = removeUndefinedAndNull({
                    ...sourceQuery,
                    source: { ...sourceQuery.source, query: queryInput ?? sourceQuery.source.query },
                })
                // Normalize so DataTableNode-based insights don't look "changed" immediately after load.
                const editingInsightQuery = toDataVisualizationNode(editingInsight.query) ?? editingInsight.query

                return (
                    updatedName ||
                    updatedDescription ||
                    !equal(sourceQueryWithoutUndefinedAndNullKeys, removeUndefinedAndNull(editingInsightQuery))
                )
            },
        ],
    }),
    listeners(({ actions, values }) => ({
        openSaveAsMenu: () => {
            // pinned: analytics event name - renaming breaks dashboards
            posthog.capture('sql-editor-save-as-modal-opened', {
                editor_source: values.editorSource,
            })
        },
        selectSaveAsTarget: ({ target }) => {
            // pinned: analytics event name and property - renaming breaks dashboards
            posthog.capture('sql-editor-save-as-target-selected', {
                target,
                editor_source: values.editorSource,
            })
        },
        convertToNotebook: () => {
            const { title, content } = buildSqlNotebook(
                values.queryInput ?? '',
                values.activeTab,
                values.editingInsight,
                values.featureFlags
            )
            actions.createNotebook(NotebookTarget.Scene, title, content)
        },
        openHistoryModal: () => {
            posthog.capture('sql-editor-history-modal-opened', {
                object: values.editingView ? 'view' : 'insight',
            })
        },
        shareTab: () => {
            const { activeTab, queryInput, sourceQuery } = values
            if (!activeTab) {
                return
            }

            const currentUrl = new URL(window.location.href)
            const shareUrl = new URL(currentUrl.origin + currentUrl.pathname)

            if (activeTab.insight) {
                shareUrl.searchParams.set('open_insight', activeTab.insight.short_id)

                if (activeTab.insight.query?.kind === NodeKind.DataVisualizationNode) {
                    const query = (activeTab.insight.query as DataVisualizationNode).source.query
                    if (queryInput !== query) {
                        shareUrl.searchParams.set('open_query', queryInput ?? '')
                    }
                }
            } else if (activeTab.view) {
                shareUrl.searchParams.set('open_view', activeTab.view.id)

                if (queryInput !== activeTab.view.query?.query) {
                    shareUrl.searchParams.set('open_query', queryInput ?? '')
                }
            } else {
                shareUrl.searchParams.set('open_query', queryInput ?? '')
            }

            setFiltersHashParam(shareUrl, sourceQuery.source.filters)
            if (values.editorUrl === urls.businessIntelligence() && activeTab.biEditorState) {
                const hashParams = new URLSearchParams(shareUrl.hash.slice(1))
                hashParams.set('mode', activeTab.biEditorState.editorView)
                hashParams.set('bi', JSON.stringify(activeTab.biEditorState.config))
                shareUrl.hash = hashParams.toString()
            }

            void copyToClipboard(shareUrl.toString(), 'share link')
        },
    })),
    selectors({
        [SIDE_PANEL_CONTEXT_KEY]: [
            () => [],
            (): SidePanelSceneContext => ({
                access_control_resource: 'warehouse_objects',
            }),
        ],
    }),
    subscriptions(({ actions, props }) => ({
        // A subscription, because several sqlEditorLogic actions move the editor in and out of the flow.
        isCreateViewFlow: (isCreateViewFlow: boolean) => {
            if (ownsLayoutNav(props)) {
                actions.setLayoutNavCollapsedOverride(layoutNavOverrideOwner(props), isCreateViewFlow ? true : null)
            }
        },
    })),
    beforeUnmount(({ actions, props }) => {
        if (ownsLayoutNav(props)) {
            actions.setLayoutNavCollapsedOverride(layoutNavOverrideOwner(props), null)
        }
    }),
])