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.

6.0 KB
Raw Blame
import { useValues } from 'kea'
import { useCallback } from 'react'

import { LemonButton, LemonTab, LemonTabs } from '@posthog/lemon-ui'
import { Button } from '@posthog/quill'

import { AccessControlAction } from 'lib/components/AccessControlAction'
import { AccessDenied } from 'lib/components/AccessDenied'
import { Shortcut } from 'lib/components/Shortcuts/Shortcut'
import { keyBinds } from 'lib/components/Shortcuts/shortcuts'
import { useFeatureFlag } from 'lib/hooks/useFeatureFlag'
import { LinkPrimitive } from 'lib/lemon-ui/Link'
import { userHasAccess } from 'lib/utils/accessControlUtils'
import { sceneConfigurations } from 'scenes/scenes'
import { Scene, SceneExport } from 'scenes/sceneTypes'
import { urls } from 'scenes/urls'

import { SceneContent } from '~/layout/scenes/components/SceneContent'
import { SceneTitleSection } from '~/layout/scenes/components/SceneTitleSection'
import { ProductKey } from '~/queries/schema/schema-general'
import { AccessControlLevel, AccessControlResourceType, DataWarehouseSavedQuery } from '~/types'

import { DataQualityOverview } from 'products/data_quality/frontend/overview/DataQualityOverview'

import { ModelsLineageTab } from './lineage/ModelsLineageTab'
import { ModelsOverviewTab } from './ModelsOverviewTab'
import { ModelsSceneTab, modelsSceneLogic } from './modelsSceneLogic'
import { ModelsUsageTab } from './ModelsUsageTab'
import { ViewsTab } from './ViewsTab'

export const scene: SceneExport = {
    component: ModelsScene,
    logic: modelsSceneLogic,
    productKey: ProductKey.DATA_WAREHOUSE_SAVED_QUERY,
}

export function ModelsScene(): JSX.Element {
    const { savedQueryIdToNodeId, activeTab, lineageTabVisited, suspensionBySavedQueryId, usageEnabled } =
        useValues(modelsSceneLogic)

    const getViewUrl = useCallback(
        (view: Pick<DataWarehouseSavedQuery, 'id'>): string => {
            const nodeId = savedQueryIdToNodeId[view.id]
            return nodeId ? urls.nodeDetail(nodeId) : urls.sqlEditor({ view_id: view.id })
        },
        [savedQueryIdToNodeId]
    )
    const todayRailNav = useFeatureFlag('TODAY_RAIL_NAV')

    if (!userHasAccess(AccessControlResourceType.WarehouseObjects, AccessControlLevel.Viewer)) {
        return (
            <AccessDenied reason="You don't have access to Data warehouse tables & views, so this page isn't available." />
        )
    }

    const tabs: LemonTab<ModelsSceneTab>[] = [
        {
            key: 'overview',
            label: 'Overview',
            link: urls.models(),
            content: <ModelsOverviewTab />,
            'data-attr': 'models-tab-overview',
        },
        {
            key: 'models',
            label: 'Models',
            link: urls.models('models'),
            content: <ViewsTab getViewUrl={getViewUrl} suspensionByViewId={suspensionBySavedQueryId} />,
            'data-attr': 'models-tab-models',
        },
        ...(usageEnabled
            ? [
                  {
                      key: 'usage' as const,
                      label: 'Usage',
                      link: urls.models('usage'),
                      content: <ModelsUsageTab savedQueryIdToNodeId={savedQueryIdToNodeId} />,
                      'data-attr': 'models-tab-usage',
                  },
              ]
            : []),
        {
            key: 'lineage',
            label: 'Lineage',
            link: urls.models('lineage'),
            content: <ModelsLineageTab />,
            keepMounted: lineageTabVisited,
            'data-attr': 'models-tab-lineage',
        },
        {
            key: 'data-quality',
            label: 'Data quality',
            link: urls.models('data-quality'),
            content: <DataQualityOverview />,
            'data-attr': 'models-tab-data-quality',
        },
    ]

    return (
        <SceneContent>
            <SceneTitleSection
                name={sceneConfigurations[Scene.Models].name}
                description={sceneConfigurations[Scene.Models].description}
                resourceType={{
                    type: sceneConfigurations[Scene.Models].iconType || 'default_icon_type',
                }}
                actions={
                    <div className="flex gap-2">
                        <Shortcut
                            name="NewModel"
                            keybind={[keyBinds.new]}
                            intent="New view"
                            interaction="click"
                            scope={Scene.Models}
                        >
                            <AccessControlAction
                                resourceType={AccessControlResourceType.WarehouseObjects}
                                minAccessLevel={AccessControlLevel.Editor}
                            >
                                {todayRailNav ? (
                                    <Button
                                        variant="primary"
                                        render={<LinkPrimitive to={urls.sqlEditor({ source: 'view' })} />}
                                        tooltip="Create view"
                                        data-attr="new-view-button"
                                    >
                                        Create view
                                    </Button>
                                ) : (
                                    <LemonButton
                                        type="primary"
                                        to={urls.sqlEditor({ source: 'view' })}
                                        size="small"
                                        tooltip="Create view"
                                        data-attr="new-view-button"
                                    >
                                        Create view
                                    </LemonButton>
                                )}
                            </AccessControlAction>
                        </Shortcut>
                    </div>
                }
            />
            <LemonTabs activeKey={activeTab} tabs={tabs} sceneInset />
        </SceneContent>
    )
}