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.

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

import { IconPiggyBank } from '@posthog/icons'
import { LemonSwitch, Link } from '@posthog/lemon-ui'

import { AccessControlAction } from 'lib/components/AccessControlAction'
import { teamLogic } from 'scenes/teamLogic'

import { DataWarehouseManagedViewsetKind } from '~/queries/schema/schema-general'

import { AccessControlLevel, AccessControlResourceType } from '../../../types'
import {
    DataWarehouseManagedViewsetsLogicProps,
    dataWarehouseManagedViewsetsLogic,
} from './dataWarehouseManagedViewsetsLogic'

const VIEWSET_DESCRIPTIONS: Record<
    DataWarehouseManagedViewsetKind,
    { title: string; description: string; docsUrl: string; configUrl: string; icon: JSX.Element }
> = {
    revenue_analytics: {
        title: 'Revenue Analytics',
        description:
            'Track and analyze revenue data from your payment providers and custom events. Automatically creates optimized views for revenue metrics, trends, and customer lifetime value.',
        docsUrl: 'https://posthog.com/docs/revenue-analytics',
        configUrl: '/data-management/revenue',
        icon: <IconPiggyBank className="text-xl" />,
    },
}

export interface DataWarehouseManagedViewsetCardProps {
    type: DataWarehouseManagedViewsetsLogicProps['type']
    kind: DataWarehouseManagedViewsetKind
    resourceType: AccessControlResourceType
    displayDocsLink?: boolean
    displayConfigLink?: boolean
}

export function DataWarehouseManagedViewsetCard({
    type,
    kind,
    resourceType,
    displayDocsLink = true,
    displayConfigLink = true,
}: DataWarehouseManagedViewsetCardProps): JSX.Element | null {
    const { currentTeam } = useValues(teamLogic)
    const { toggleViewset } = useActions(dataWarehouseManagedViewsetsLogic({ type }))
    const { togglingViewset, toggleResultLoading } = useValues(dataWarehouseManagedViewsetsLogic({ type }))

    const viewsetInfo = VIEWSET_DESCRIPTIONS[kind]

    if (!viewsetInfo) {
        return null
    }

    const { title, description, docsUrl, configUrl, icon } = viewsetInfo
    const isEnabled = !!currentTeam?.managed_viewsets?.[kind]
    const isToggling = togglingViewset === kind && toggleResultLoading

    return (
        <div className="border rounded p-4">
            <div className="flex items-center justify-between gap-4">
                <div className="flex-1">
                    <div className="flex items-center gap-2 mb-2">
                        {icon}
                        <h3 className="font-semibold text-lg mb-0">{title}</h3>
                    </div>
                    <p className="text-muted mb-2">{description}</p>
                    <div className="flex items-center gap-2">
                        {displayConfigLink && (
                            <Link to={configUrl} className="text-sm">
                                Configure
                            </Link>
                        )}
                        {displayDocsLink && displayConfigLink && <span>|</span>}
                        {displayDocsLink && (
                            <Link to={docsUrl} target="_blank" className="text-sm">
                                Learn more in the docs
                            </Link>
                        )}
                    </div>
                </div>

                <AccessControlAction resourceType={resourceType} minAccessLevel={AccessControlLevel.Editor}>
                    {({ disabledReason }) => (
                        <div className="flex flex-col gap-2 flex-start items-start">
                            <LemonSwitch
                                checked={isEnabled}
                                onChange={(enabled) => toggleViewset(kind, enabled)}
                                disabledReason={
                                    isToggling ? 'Saving, this might take a few seconds...' : disabledReason
                                }
                                label={
                                    isToggling
                                        ? isEnabled
                                            ? 'Disabling, please wait a moment...'
                                            : 'Enabling, please wait a moment...'
                                        : isEnabled
                                          ? 'Enabled'
                                          : 'Disabled'
                                }
                                bordered
                                data-attr="managed-viewset-toggle"
                            />
                        </div>
                    )}
                </AccessControlAction>
            </div>
        </div>
    )
}