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.

3.9 KB
Raw Blame
import { useActions, useValues } from 'kea'
import { useEffect, useRef, useState } from 'react'

import { IconSparkles } from '@posthog/icons'

import { CodeSnippet } from 'lib/components/CodeSnippet/CodeSnippet'
import { cn } from 'lib/utils/css-classes'

import { AgentBadgeRotator } from './AgentBadgeRotator'
import { MCPHintActions } from './MCPHintActions'
import { mcpHintLogic } from './mcpHintLogic'
import { getSurfacePrompts, type SurfaceKey } from './prompts'

const FIRST_SEEN_KEY_PREFIX = 'mcp-use-case-card-first-seen:'

function getExpiryState(surfaceKey: SurfaceKey, expiresAfterMs: number): { expired: boolean; firstSeenAt: number } {
    const key = FIRST_SEEN_KEY_PREFIX + surfaceKey
    const stored = localStorage.getItem(key)
    if (stored) {
        const firstSeenAt = parseInt(stored, 10)
        return { expired: Date.now() - firstSeenAt > expiresAfterMs, firstSeenAt }
    }
    const now = Date.now()
    localStorage.setItem(key, String(now))
    return { expired: false, firstSeenAt: now }
}

export function MCPUseCaseCard({
    surfaceKey,
    className,
    /**
     * If set, the card hides itself after this many ms have passed since the first time it appeared
     * for this user (per-surface, persisted in localStorage). Use in contexts that aren't a one-shot
     * empty state (e.g. the SQL editor, which a user revisits often).
     */
    expiresAfterMs,
    // Force display, only useful in storybook to simplify flags
    forceDisplay = false,
}: {
    surfaceKey: SurfaceKey
    className?: string
    expiresAfterMs?: number
    forceDisplay?: boolean
}): JSX.Element | null {
    const { effectiveOptOut, userRole, topEvents } = useValues(mcpHintLogic)
    const { loadTopEvents, reportUseCaseCardShown } = useActions(mcpHintLogic)
    const [expired] = useState(() => (expiresAfterMs ? getExpiryState(surfaceKey, expiresAfterMs).expired : false))
    const triedLoadingEvents = useRef(false)

    const willRender = forceDisplay || (!effectiveOptOut && !expired)

    useEffect(() => {
        // Only the SQL editor surface benefits from the team's real event names — keep the call narrow.
        // Fetch at most once per mount; a failed call returns [] and must not retry on re-render.
        if (willRender && surfaceKey === 'sql.execute' && topEvents.length === 0 && !triedLoadingEvents.current) {
            triedLoadingEvents.current = true
            loadTopEvents()
        }
    }, [willRender, surfaceKey, topEvents.length, loadTopEvents])

    useEffect(() => {
        if (willRender) {
            reportUseCaseCardShown(surfaceKey)
        }
    }, [willRender, surfaceKey, reportUseCaseCardShown])

    if (!willRender) {
        return null
    }

    const { display = 'list', examples } = getSurfacePrompts(surfaceKey, { role: userRole, topEvents })

    return (
        <div
            className={cn(
                'mt-6 rounded-lg border border-dashed border-primary bg-bg-light p-4 flex flex-col gap-3',
                className
            )}
        >
            <div className="flex items-center gap-2">
                <IconSparkles className="size-4 shrink-0" />
                <h4 className="m-0 text-sm font-semibold">Or do it from your agent</h4>
            </div>
            <div className="text-sm text-default">
                Ask <AgentBadgeRotator />:
            </div>
            {display === 'prompt' ? (
                <CodeSnippet compact wrap thing="prompt" className="w-full">
                    {examples[0]}
                </CodeSnippet>
            ) : (
                <ul className="m-0 pl-5 list-disc text-xs text-muted leading-relaxed">
                    {examples.map((example) => (
                        <li key={example}>{example}</li>
                    ))}
                </ul>
            )}
            <div className="pt-1">
                <MCPHintActions surfaceKey={surfaceKey} example={examples[0]} placement="card" />
            </div>
        </div>
    )
}