: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 { useMemo, useState } from 'react'
import { useFeatureFlag } from 'lib/hooks/useFeatureFlag'
import { useKeyboardHotkeys } from 'lib/hooks/useKeyboardHotkeys'
import {
AggregationAxisFormat,
defaultAggregationAxisFormatForDisplay,
INSIGHT_UNIT_OPTIONS,
INSIGHT_UNIT_OPTIONS_SHORT,
} from 'lib/insights/aggregationAxisFormat'
import { LemonButton, LemonButtonWithDropdown } from 'lib/lemon-ui/LemonButton'
import { LemonDivider } from 'lib/lemon-ui/LemonDivider'
import { eventUsageLogic } from 'lib/utils/eventUsageLogic'
import { insightLogic } from 'scenes/insights/insightLogic'
import { insightVizDataLogic } from 'scenes/insights/insightVizDataLogic'
import { QuillUnitPicker } from './QuillUnitPicker'
import { unitPickerModalLogic } from './unitPickerModalLogic'
export interface HandleUnitChange {
format?: AggregationAxisFormat
prefix?: string
postfix?: string
close?: boolean
}
export function UnitPicker(): JSX.Element {
const { insightProps } = useValues(insightLogic)
const { trendsFilter, display } = useValues(insightVizDataLogic(insightProps))
const { updateInsightFilter } = useActions(insightVizDataLogic(insightProps))
const { showCustomUnitModal } = useActions(unitPickerModalLogic)
const { reportAxisUnitsChanged } = useActions(eventUsageLogic)
const todayRailNav = useFeatureFlag('TODAY_RAIL_NAV')
const [isVisible, setIsVisible] = useState(false)
const [localAxisFormat, setLocalAxisFormat] = useState(trendsFilter?.aggregationAxisFormat || undefined)
// Some display types (e.g. Metric) render a default unit when none is explicitly set — reflect it here.
const effectiveAxisFormat = localAxisFormat ?? defaultAggregationAxisFormatForDisplay(display)
useKeyboardHotkeys(
{
escape: {
action: function () {
setIsVisible(false)
},
},
},
[]
)
const handleChange = ({ format, prefix, postfix }: HandleUnitChange): void => {
setLocalAxisFormat(format)
updateInsightFilter({
aggregationAxisFormat: format,
aggregationAxisPrefix: prefix,
aggregationAxisPostfix: postfix,
})
reportAxisUnitsChanged({
format,
prefix,
postfix,
display,
unitIsSet: !!prefix || !!postfix || (format && format !== 'numeric'),
})
setIsVisible(false)
}
const displayValue = useMemo(() => {
let displayValue: React.ReactNode = 'None'
// 'numeric' is the explicit "None" option, so keep the "None" label rather than its empty short symbol.
if (effectiveAxisFormat && effectiveAxisFormat !== 'numeric') {
displayValue = INSIGHT_UNIT_OPTIONS_SHORT[effectiveAxisFormat]
}
if (trendsFilter?.aggregationAxisPrefix?.length) {
displayValue = `Prefix: ${trendsFilter?.aggregationAxisPrefix}`
}
if (trendsFilter?.aggregationAxisPostfix?.length) {
displayValue = `Postfix: ${trendsFilter?.aggregationAxisPostfix}`
}
return displayValue
}, [effectiveAxisFormat, trendsFilter])
const handleCustomPrefix = (): void => {
showCustomUnitModal({
type: 'prefix',
currentValue: trendsFilter?.aggregationAxisPrefix || '',
callback: (value: string) => handleChange({ prefix: value }),
})
}
const handleCustomPostfix = (): void => {
showCustomUnitModal({
type: 'postfix',
currentValue: trendsFilter?.aggregationAxisPostfix || '',
callback: (value: string) => handleChange({ postfix: value }),
})
}
if (todayRailNav) {
return (
<QuillUnitPicker
displayValue={displayValue}
format={effectiveAxisFormat}
prefix={trendsFilter?.aggregationAxisPrefix}
postfix={trendsFilter?.aggregationAxisPostfix}
onFormatChange={(format) => handleChange({ format })}
onCustomPrefix={handleCustomPrefix}
onCustomPostfix={handleCustomPostfix}
/>
)
}
return (
<div className="flex-1">
<LemonButtonWithDropdown
onClick={() => setIsVisible(!isVisible)}
size="small"
type="secondary"
data-attr="chart-aggregation-axis-format"
fullWidth
truncate
dropdown={{
onClickOutside: () => setIsVisible(false),
visible: isVisible,
overlay: (
<>
{INSIGHT_UNIT_OPTIONS.map(({ value, label }, index) => (
<LemonButton
key={index}
onClick={() => handleChange({ format: value })}
active={value === effectiveAxisFormat}
fullWidth
>
{label}
</LemonButton>
))}
<>
<LemonDivider />
<LemonButton
onClick={handleCustomPrefix}
active={!!trendsFilter?.aggregationAxisPrefix}
fullWidth
>
Custom prefix
{trendsFilter?.aggregationAxisPrefix
? `: ${trendsFilter?.aggregationAxisPrefix}...`
: '...'}
</LemonButton>
<LemonButton
onClick={handleCustomPostfix}
active={!!trendsFilter?.aggregationAxisPostfix}
fullWidth
>
Custom postfix
{trendsFilter?.aggregationAxisPostfix
? `: ${trendsFilter?.aggregationAxisPostfix}...`
: '...'}
</LemonButton>
</>
</>
),
placement: 'bottom-start',
actionable: true,
closeOnClickInside: false,
}}
>
<span className="min-w-0 truncate">{displayValue}</span>
</LemonButtonWithDropdown>
</div>
)
}