: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(data-warehouse): stop save-as dialog freezing on churning onChange (#107731)
Jovan Sakovic1dc4264caa84
diff --git a/frontend/src/scenes/data-warehouse/editor/SaveTargetCycler.render.test.tsx b/frontend/src/scenes/data-warehouse/editor/SaveTargetCycler.render.test.tsxnew file mode 100644index 000000000000..5c40c67927d1--- /dev/null+++ b/frontend/src/scenes/data-warehouse/editor/SaveTargetCycler.render.test.tsx@@ -0,0 +1,40 @@+import { render } from '@testing-library/react'+import { useState } from 'react'++import { SaveCandidates, SaveTargetCycler } from './SaveTargetCycler'++// candidates is stable across renders at every call site (a const in the dialog listener closure),+// so the harness keeps it stable and lets only onChange churn — the shape that caused the freeze.+const CANDIDATES: SaveCandidates = {+ queries: ['SELECT 1', 'SELECT 2'],+ initialIndex: 0,+ selectionLabel: null,+}++describe('SaveTargetCycler', () => {+ it('does not loop when the caller passes a new onChange every render', () => {+ // Mirrors the LemonField wiring: a fresh onChange closure each render whose call forces a+ // re-render. When the report effect depended on that identity, it re-ran on the render its+ // own write caused, feeding an infinite loop until React aborted with "Maximum update depth+ // exceeded". The counter guarantees each call re-renders, so a revived dependency loops here.+ const selected = jest.fn()++ function Harness(): JSX.Element {+ const [, forceRender] = useState(0)+ return (+ <SaveTargetCycler+ candidates={CANDIDATES}+ onChange={(query, index) => {+ selected(query, index)+ forceRender((n) => n + 1)+ }}+ />+ )+ }++ expect(() => render(<Harness />)).not.toThrow()+ // The selection is reported for the initial index once, not stormed.+ expect(selected).toHaveBeenCalledTimes(1)+ expect(selected).toHaveBeenLastCalledWith('SELECT 1', 0)+ })+})diff --git a/frontend/src/scenes/data-warehouse/editor/SaveTargetCycler.tsx b/frontend/src/scenes/data-warehouse/editor/SaveTargetCycler.tsxindex 3974f606d21b..56158a4a813c 100644--- a/frontend/src/scenes/data-warehouse/editor/SaveTargetCycler.tsx+++ b/frontend/src/scenes/data-warehouse/editor/SaveTargetCycler.tsx@@ -1,4 +1,4 @@-import { useEffect, useState } from 'react'+import { useEffect, useLayoutEffect, useRef, useState } from 'react'import { IconChevronLeft, IconChevronRight } from '@posthog/icons'@@ -75,6 +75,15 @@ interface SaveTargetCyclerProps {export function SaveTargetCycler({ candidates, onChange, children }: SaveTargetCyclerProps): JSX.Element | null {const [index, setIndex] = useState(candidates.initialIndex)+ // Callers pass a fresh onChange every render (an inline closure in a LemonField render prop).+ // Held in a ref so the effect below fires on the selected value changing, not on that identity+ // churning: when onChange writes form state, the re-render must not re-run the effect, or the+ // effect and the write feed each other into an infinite render loop that freezes the dialog.+ const onChangeRef = useRef(onChange)+ useLayoutEffect(() => {+ onChangeRef.current = onChange+ }, [onChange])+// Clamp the active index whenever the candidate set shrinks so we never read past the end.useEffect(() => {if (candidates.queries.length > 0 && index >= candidates.queries.length) {@@ -85,9 +94,9 @@ export function SaveTargetCycler({ candidates, onChange, children }: SaveTargetCuseEffect(() => {const safeIndex = Math.min(index, candidates.queries.length - 1)if (safeIndex >= 0) {- onChange(candidates.queries[safeIndex], safeIndex)+ onChangeRef.current(candidates.queries[safeIndex], safeIndex)}- }, [index, candidates, onChange])+ }, [index, candidates])if (candidates.queries.length === 0) {return null