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.

1.9 KB
Raw Blame
import { Suspense } from 'react'

import { lazyWithRetry } from 'lib/utils/retryImport'

import { CoreFilterDefinition } from '~/types'

const LazyLink = lazyWithRetry(() => import('lib/lemon-ui/Link').then((m) => ({ default: m.Link })))

type RawCoreFilterDefinition = {
    label: string
    description?: string
    examples?: (string | number | boolean)[]
    system?: boolean
}

function transformDescription(description: string): React.ReactNode {
    if (!description.includes('\n') && !description.includes('`') && !description.includes('[')) {
        return description
    }

    const parts = description.split(/(\[.*?\]\(.*?\)|`[^`]+`|\n)/)
    return (
        <span>
            {parts.map((part, i) => {
                if (part === '\n') {
                    return <br key={i} />
                }
                if (part.startsWith('`') && part.endsWith('`')) {
                    return <code key={i}>{part.slice(1, -1)}</code>
                }
                const linkMatch = part.match(/\[(.*?)\]\((.*?)\)/)
                if (linkMatch) {
                    const [_, text, url] = linkMatch
                    return (
                        <Suspense key={i} fallback={text}>
                            <LazyLink to={url} target="_blank">
                                {text}
                            </LazyLink>
                        </Suspense>
                    )
                }
                return part
            })}
        </span>
    )
}

export function transformFilterDefinitions(
    group: Record<string, RawCoreFilterDefinition>
): Record<string, CoreFilterDefinition> {
    const result: Record<string, CoreFilterDefinition> = {}
    for (const [key, def] of Object.entries(group)) {
        result[key] = {
            ...def,
            description: def.description ? transformDescription(def.description) : undefined,
        }
    }
    return result
}