: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { useActions, useValues } from 'kea'
import { useEffect, useState } from 'react'
import { LemonInput } from '@posthog/lemon-ui'
import { RestrictionScope, useRestrictedArea } from 'lib/components/RestrictedArea'
import { TeamMembershipLevel } from 'lib/constants'
import { DEFAULT_LOOKBACK_DAYS, MAX_LOOKBACK_DAYS, MIN_LOOKBACK_DAYS } from 'products/experiments/frontend/constants'
import { experimentsConfigLogic } from './experimentsConfigLogic'
export function DefaultCupedLookbackDays(): JSX.Element {
const { experimentsConfig, experimentsConfigLoading } = useValues(experimentsConfigLogic)
const { updateExperimentsConfig } = useActions(experimentsConfigLogic)
const restrictionReason = useRestrictedArea({
scope: RestrictionScope.Project,
minimumAccessLevel: TeamMembershipLevel.Admin,
})
const savedValue = experimentsConfig?.default_cuped_lookback_days ?? null
const displayValue = savedValue ?? DEFAULT_LOOKBACK_DAYS
const [localValue, setLocalValue] = useState<number | undefined>(displayValue)
useEffect(() => {
setLocalValue(displayValue)
}, [displayValue])
const isOutOfRange =
localValue !== undefined &&
Number.isFinite(localValue) &&
(localValue < MIN_LOOKBACK_DAYS || localValue > MAX_LOOKBACK_DAYS)
const isNonFinite = localValue !== undefined && !Number.isFinite(localValue)
const showDanger = isOutOfRange || isNonFinite
const commit = (): void => {
if (showDanger) {
setLocalValue(displayValue)
return
}
if (localValue === undefined) {
if (savedValue === null) {
setLocalValue(displayValue)
return
}
updateExperimentsConfig({ default_cuped_lookback_days: null })
return
}
const rounded = Math.round(localValue)
if (rounded === savedValue) {
return
}
updateExperimentsConfig({ default_cuped_lookback_days: rounded })
}
return (
<LemonInput
type="number"
min={MIN_LOOKBACK_DAYS}
max={MAX_LOOKBACK_DAYS}
value={localValue}
onChange={(value) => setLocalValue(value)}
onBlur={commit}
onPressEnter={commit}
status={showDanger ? 'danger' : 'default'}
disabled={!!restrictionReason || experimentsConfigLoading}
className="w-32"
/>
)
}