: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
fix(insights): drop the oversized date variable Update button (#80245)
Paul D'Ambrad885c879ae76
diff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex a2891d03ac47..dbb064eb6b9a 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -2840,6 +2840,18 @@ snapshots:hash: v1.k794b7964.056dd0e8337b4d7b9a1538c2fad85bb041d948491e7d57c4b54e101934761aa1.27wPyYu93YTRS_Qy9ka5CoSBccIWATkpwimC7uYNuRsinsights-trendsslopechart--with-legend--light:hash: v1.k794b7964.3937154d2e4552549004489d9c6e51b4dfc1d0c8dc9fa807068f76afad58c07a.8rKiYquuAFHDf5OYem57vBg6axGT4ooMwJSX_U4-UvE+ insights-variables--date-variable-input--dark:+ hash: v1.k794b7964.9f55263cf42996613af3d9a4e643fd80f6cc106a45365d2dd4e271894850e217.v6mqgP6NMvMvOWrF2XuxcO5OS03GKEoGV9wMH6hRZ4o+ insights-variables--date-variable-input--light:+ hash: v1.k794b7964.c1777f08d6a5dfa0dd5c18d3e4a0721c6f68e0b5e83850a01ffa549f5232fe0e.qTeNMD1AqBtCmBYDTSU2gjHY1kGCeXFPp0s7WhTrxgA+ insights-variables--relative-date-variable-input--dark:+ hash: v1.k794b7964.993cb47aaab3f9408430d0dcdda87e8eca9cb16c7681dcde25e58a4611f64828.c_7HSBE3vhm-UjRbxmi5mdL2bQKskYYJfcxIP1-a-Pw+ insights-variables--relative-date-variable-input--light:+ hash: v1.k794b7964.974544944d723cb3297d29d1d3e97402c96eec121e63d85bf8d6cb73c96c815f.y85S6FKEXRK96A-UT-SmqHRJU3LUG1TQ0pH6R3Mi-0g+ insights-variables--string-variable-input--dark:+ hash: v1.k794b7964.fb86ee464cee03254af846a3a5fb59318232c364d07f74d0d4cb9490ce97253b.obwWiNo8N0HtcPQfj5341SkN49zWCuNHJOSHvXxEJJY+ insights-variables--string-variable-input--light:+ hash: v1.k794b7964.f0f59479f355452fbfd500bbaf7e29afb26fbbd7ad278d6fba5cef1ca901e21f.rCf431wIXk3tw7X_PNnGs7eUvondDzS1oDxTbU-MCtoinsights-worldmap--default--dark:hash: v1.k794b7964.33dac53a2c2f3c8945bb52917b010f00a24b8d8b213106939af1037022d4ed5e.QNBv_MCq5SJyqzj3G7AuQxGF1qLYQLIQotnO3MfqpREinsights-worldmap--default--light:diff --git a/frontend/src/queries/nodes/DataVisualization/Components/Variables/VariableFields.tsx b/frontend/src/queries/nodes/DataVisualization/Components/Variables/VariableFields.tsxindex 57ef4dab4670..f95e66257865 100644--- a/frontend/src/queries/nodes/DataVisualization/Components/Variables/VariableFields.tsx+++ b/frontend/src/queries/nodes/DataVisualization/Components/Variables/VariableFields.tsx@@ -352,7 +352,14 @@ const RELATIVE_DATE_UNITS: Array<{ value: RelativeDateUnit; label: string }> = [{ value: 'y', label: 'years' },]-export const DateField = ({ variable, updateVariable }: DirectFieldProps<DateVariable>): JSX.Element => {+export const DateField = ({+ variable,+ updateVariable,+ onApply,+}: DirectFieldProps<DateVariable> & {+ /** Called when the calendar's own Apply button is pressed, so a caller can treat it as the commit. */+ onApply?: (value: string) => void+}): JSX.Element => {const isRelative = isRelativeDateValue(variable.default_value)const relativeValue = parseRelativeDateValue(variable.default_value) ?? { amount: 0, unit: 'd' as RelativeDateUnit }@@ -403,7 +410,10 @@ export const DateField = ({ variable, updateVariable }: DirectFieldProps<DateVar<VariableCalendarvalue={dayjs(variable.default_value)}rawValue={variable.default_value}- updateVariable={(date) => updateVariable({ ...variable, default_value: date })}+ updateVariable={(date) => {+ updateVariable({ ...variable, default_value: date })+ onApply?.(date)+ }}/>)}{isRelative && (diff --git a/frontend/src/queries/nodes/DataVisualization/Components/Variables/Variables.stories.tsx b/frontend/src/queries/nodes/DataVisualization/Components/Variables/Variables.stories.tsxnew file mode 100644index 000000000000..48ba90d01fc2--- /dev/null+++ b/frontend/src/queries/nodes/DataVisualization/Components/Variables/Variables.stories.tsx@@ -0,0 +1,54 @@+import { Meta, StoryObj } from '@storybook/react'++import { DateVariable, StringVariable } from '../../types'+import { VariableInput } from './Variables'++const meta: Meta<typeof VariableInput> = {+ title: 'Insights/Variables',+ component: VariableInput,+ parameters: {+ layout: 'centered',+ // The fixed-date calendar marks today, so the snapshot needs a stable clock.+ mockDate: '2024-05-15',+ testOptions: { snapshotBrowsers: ['chromium'] },+ },+ args: {+ showEditingUI: false,+ closePopover: () => {},+ onChange: () => {},+ },+}+export default meta++type Story = StoryObj<typeof VariableInput>++const stringVariable: StringVariable = {+ id: 'string-variable',+ name: 'Chain',+ code_name: 'chain',+ type: 'String',+ default_value: 'ALL',+}++const dateVariable: DateVariable = {+ id: 'date-variable',+ name: 'start',+ code_name: 'start',+ type: 'Date',+ default_value: '2024-05-10',+}++export const StringVariableInput: Story = {+ args: { variable: stringVariable },+}++/** The calendar's own Apply button commits a fixed date, so there is no second commit button here. */+export const DateVariableInput: Story = {+ args: { variable: dateVariable },+}++/** The relative-date tab has no Apply of its own, so it keeps the Update button. Guards against that+ * button stretching to the height of the column beside it. */+export const RelativeDateVariableInput: Story = {+ args: { variable: { ...dateVariable, default_value: '-7d' } },+}diff --git a/frontend/src/queries/nodes/DataVisualization/Components/Variables/Variables.test.tsx b/frontend/src/queries/nodes/DataVisualization/Components/Variables/Variables.test.tsxnew file mode 100644index 000000000000..e9b621e10040--- /dev/null+++ b/frontend/src/queries/nodes/DataVisualization/Components/Variables/Variables.test.tsx@@ -0,0 +1,77 @@+import '@testing-library/jest-dom'++import { cleanup, render, screen } from '@testing-library/react'+import userEvent from '@testing-library/user-event'+import { Provider } from 'kea'++import { initKeaTests } from '~/test/init'++import { DateVariable } from '../../types'+import { VariableInput } from './Variables'++const dateVariable: DateVariable = {+ id: 'date-variable',+ name: 'start',+ code_name: 'start',+ type: 'Date',+ default_value: '2026-03-31',+}++describe('VariableInput', () => {+ beforeEach(() => {+ window.HTMLElement.prototype.scrollIntoView = jest.fn()+ initKeaTests()+ })++ afterEach(() => {+ cleanup()+ })++ const renderInput = (variable: DateVariable): { onChange: jest.Mock; closePopover: jest.Mock } => {+ const onChange = jest.fn()+ const closePopover = jest.fn()++ render(+ <Provider>+ <VariableInput+ variable={variable}+ showEditingUI={false}+ onChange={onChange}+ closePopover={closePopover}+ />+ </Provider>+ )++ return { onChange, closePopover }+ }++ it('commits a fixed date from the calendar Apply button', async () => {+ const { onChange, closePopover } = renderInput(dateVariable)++ expect(screen.queryByText('Update')).not.toBeInTheDocument()++ await userEvent.click(screen.getAllByText('15')[0])+ await userEvent.click(screen.getByText('Apply'))++ expect(onChange).toHaveBeenCalledWith('date-variable', '2026-03-15', false)+ expect(closePopover).toHaveBeenCalled()+ })++ it('does not commit when switching between fixed and relative dates', async () => {+ const { onChange, closePopover } = renderInput({ ...dateVariable, default_value: '-7d' })++ await userEvent.click(screen.getByText('Fixed date'))++ expect(onChange).not.toHaveBeenCalled()+ expect(closePopover).not.toHaveBeenCalled()+ })++ it('keeps the Update button for relative dates, which have no Apply of their own', async () => {+ const { onChange, closePopover } = renderInput({ ...dateVariable, default_value: '-7d' })++ await userEvent.click(screen.getByText('Update'))++ expect(onChange).toHaveBeenCalledWith('date-variable', '-7d', false)+ expect(closePopover).toHaveBeenCalled()+ })+})diff --git a/frontend/src/queries/nodes/DataVisualization/Components/Variables/Variables.tsx b/frontend/src/queries/nodes/DataVisualization/Components/Variables/Variables.tsxindex 7beb5fce42d6..d3112eab93df 100644--- a/frontend/src/queries/nodes/DataVisualization/Components/Variables/Variables.tsx+++ b/frontend/src/queries/nodes/DataVisualization/Components/Variables/Variables.tsx@@ -144,6 +144,10 @@ export const VariableInput = ({})const [isNull, setIsNull] = useState<boolean>(variable.isNull ?? false)+ // A fixed date is committed by the calendar's own Apply button, so a second one would be redundant.+ // The relative-date tab has no such button, and still needs ours.+ const calendarOwnsApply = variable.type === 'Date' && !isRelativeDateValue(String(localInputValue))+const inputRef = useRef<HTMLInputElement>(null)const codeRef = useRef<HTMLElement>(null)@@ -151,6 +155,11 @@ export const VariableInput = ({inputRef.current?.focus()}, [])+ const commit = (value: any): void => {+ onChange(variable.id, value, isNull)+ closePopover()+ }+const variableAsHogQL = `{variables.${variable.code_name}}`return (@@ -163,10 +172,7 @@ export const VariableInput = ({className="flex flex-1"value={String(localInputValue)}onChange={(value) => setLocalInputValue(value)}- onPressEnter={() => {- onChange(variable.id, localInputValue, isNull)- closePopover()- }}+ onPressEnter={() => commit(localInputValue)}/>)}{variable.type === 'Number' && (@@ -177,10 +183,7 @@ export const VariableInput = ({className="flex flex-1"value={Number(localInputValue)}onChange={(value) => setLocalInputValue(String(value ?? 0))}- onPressEnter={() => {- onChange(variable.id, Number(localInputValue), isNull)- closePopover()- }}+ onPressEnter={() => commit(Number(localInputValue))}/>)}{variable.type === 'Boolean' && (@@ -213,22 +216,21 @@ export const VariableInput = ({<DateFieldvariable={{ ...variable, default_value: String(localInputValue) } as DateVariable}updateVariable={(updatedVariable) => setLocalInputValue(updatedVariable.default_value)}+ onApply={commit}onSave={() => {}}/>)}- <LemonButton- type="primary"- onClick={() => {- onChange(- variable.id,- variable.type === 'Number' ? Number(localInputValue) : localInputValue,- isNull- )- closePopover()- }}- >- {showEditingUI ? 'Save' : 'Update'}- </LemonButton>+ {!calendarOwnsApply && (+ <LemonButton+ type="primary"+ // Without this the button stretches to the tallest sibling, which for a date+ // variable is the relative-date column.+ className="self-start"+ onClick={() => commit(variable.type === 'Number' ? Number(localInputValue) : localInputValue)}+ >+ {showEditingUI ? 'Save' : 'Update'}+ </LemonButton>+ )}</div>{showEditingUI ? (<>