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.

2.4 KB
Raw Blame
import { useActions, useValues } from 'kea'
import { useEffect } from 'react'

import { LemonLabel } from '@posthog/lemon-ui'

import { newDashboardLogic } from 'scenes/dashboard/newDashboardLogic'
import { SeriesActionFilter } from 'scenes/insights/filters/ActionFilter/ActionFilter'

import { ChartDisplayCategory } from '~/types'

import { seriesToTemplateVariableStep, templateVariableStepToSeries } from './dashboardTemplateEntities'
import { dashboardTemplateVariablesLogic } from './dashboardTemplateVariablesLogic'

export function DashboardTemplateVariables(): JSX.Element {
    const { activeDashboardTemplate } = useValues(newDashboardLogic)
    const theDashboardTemplateVariablesLogic = dashboardTemplateVariablesLogic({
        variables: activeDashboardTemplate?.variables || [],
    })
    const { variables } = useValues(theDashboardTemplateVariablesLogic)
    const { setVariable, setVariables } = useActions(theDashboardTemplateVariablesLogic)

    // this is a hack, I'm not sure why it's not set properly initially. Figure it out.
    useEffect(() => {
        setVariables(activeDashboardTemplate?.variables || [])
    }, [activeDashboardTemplate]) // oxlint-disable-line react-hooks/exhaustive-deps

    return (
        <div className="mb-4 DashboardTemplateVariables max-w-192">
            {variables.map((variable, index) => (
                <div key={index} className="mb-6">
                    <div className="mb-2">
                        <LemonLabel showOptional={!variable.required} info={<>{variable.description}</>}>
                            {variable.name}
                        </LemonLabel>
                        <p className="text-sm text-secondary">{variable.description}</p>
                    </div>
                    <div>
                        <SeriesActionFilter
                            series={templateVariableStepToSeries(variable.default)}
                            onChange={(series) => setVariable(variable.name, seriesToTemplateVariableStep(series))}
                            trendsDisplayCategory={ChartDisplayCategory.TimeSeries}
                            typeKey={'variable_' + variable.name}
                            hideDeleteBtn={true}
                            hideRename={true}
                            hideDuplicate={true}
                            entitiesLimit={1}
                        />
                    </div>
                </div>
            ))}
        </div>
    )
}