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.7 KB
Raw Blame
import { useEffect, useState } from 'react'

import { ButtonPrimitiveProps } from 'lib/ui/Button/ButtonPrimitives'
import {
    SelectPrimitive,
    SelectPrimitiveContent,
    SelectPrimitiveItem,
    SelectPrimitiveTrigger,
} from 'lib/ui/SelectPrimitive/SelectPrimitive'

import { ScenePanelLabel } from '~/layout/scenes/SceneLayout'

import { SceneSaveCancelButtons, SceneSelectProps as SceneSelectPropsBase } from './utils'

type SceneSelectProps = SceneSelectPropsBase & {
    buttonProps?: Omit<ButtonPrimitiveProps, 'children' | 'size'>
}

export function SceneSelect({
    onSave,
    dataAttrKey,
    name,
    canEdit = true,
    options,
    value,
    buttonProps,
}: SceneSelectProps): JSX.Element {
    const [localValue, setLocalValue] = useState(value || undefined)
    const [hasChanged, setHasChanged] = useState(false)

    const handleSave = (): void => {
        localValue && onSave(localValue)
        setHasChanged(false)
    }

    useEffect(() => {
        setHasChanged(localValue !== value)
    }, [localValue, value])

    return (
        <div className="flex flex-col gap-1">
            <ScenePanelLabel htmlFor={`page-${name}-select`} title={name}>
                <SelectPrimitive
                    disabled={!canEdit || buttonProps?.disabled}
                    onValueChange={(value) => {
                        setLocalValue(value)
                    }}
                    defaultValue={localValue}
                    value={localValue}
                >
                    <SelectPrimitiveTrigger
                        buttonProps={{
                            ...buttonProps,
                            fullWidth: true,
                            tooltip: buttonProps?.tooltip,
                            variant: 'panel',
                        }}
                    >
                        {options.find((option) => option.value === localValue)?.label || 'Select a value'}
                    </SelectPrimitiveTrigger>
                    <SelectPrimitiveContent matchTriggerWidth>
                        {options.map((option) => (
                            <SelectPrimitiveItem key={option.value} {...option}>
                                {option.label}
                            </SelectPrimitiveItem>
                        ))}
                    </SelectPrimitiveContent>
                </SelectPrimitive>
            </ScenePanelLabel>

            {hasChanged && (
                <SceneSaveCancelButtons
                    name={name}
                    onSave={handleSave}
                    onCancel={() => {
                        setLocalValue(value)
                    }}
                    hasChanged={hasChanged}
                    dataAttrKey={dataAttrKey}
                />
            )}
        </div>
    )
}