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.

909 B
Raw Blame
import { useContext, useEffect, useMemo } from 'react'

import { pluralize } from 'lib/utils/strings'

import { SelectorCountContext } from './SelectorCountContext'

interface SelectorCountProps {
    selector: string | null
}

export function SelectorCount({ selector }: SelectorCountProps): JSX.Element | null {
    const counter = useContext(SelectorCountContext)

    useEffect(() => {
        if (selector) {
            counter.request(selector)
        }
    }, [counter, selector])

    const count = useMemo(() => (selector ? counter.count(selector) : 0), [counter, selector])

    if (selector === null || count === undefined) {
        return null
    }

    return (
        <>
            <small className={`float-right ${count === null && 'text-danger'}`}>
                {count === null ? 'Invalid selector' : `Matches ${pluralize(count, 'element')}`}
            </small>
        </>
    )
}