: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(lemon-ui): drive all TZLabel relative timestamps from one shared ticker (#68549)
Paul D'Ambra3c4b94df39de
diff --git a/frontend/src/lib/components/TZLabel/index.test.tsx b/frontend/src/lib/components/TZLabel/index.test.tsxindex 852a36a24cdf..9840fd045c25 100644--- a/frontend/src/lib/components/TZLabel/index.test.tsx+++ b/frontend/src/lib/components/TZLabel/index.test.tsx@@ -1,6 +1,6 @@import '@testing-library/jest-dom'-import { render } from '@testing-library/react'+import { act, cleanup, render } from '@testing-library/react'import { TZLabel } from './index'@@ -29,4 +29,43 @@ describe('TZLabel', () => {expect(container).toHaveTextContent(/March\s+16,\s+2026\s+10:11:12\s+AM/)})++ describe('shared ticker', () => {+ // Relative labels used to run one 1s setInterval per instance — hundreds of timer+ // wake-ups per second in a table. These lock in the single shared interval and that+ // sharing didn't break the ticking or unmount cleanup.+ beforeEach(() => {+ // Unmount labels leaked by earlier tests so the shared ticker singleton starts idle+ // under this test's fake clock.+ cleanup()+ })+ it('mounted labels share a single interval and text still advances', () => {+ const first = render(<TZLabel time="2026-03-16T10:11:12Z" showPopover={false} />)+ const second = render(<TZLabel time="2026-03-16T10:10:15Z" showPopover={false} />)++ expect(jest.getTimerCount()).toBe(1)+ expect(second.container).toHaveTextContent('a minute ago')++ act(() => {+ jest.advanceTimersByTime(65_000)+ })++ expect(second.container).toHaveTextContent('2 minutes ago')++ first.unmount()+ second.unmount()+ expect(jest.getTimerCount()).toBe(0)+ })++ it('keeps ticking for a label mounted after all previous labels unmounted', () => {+ render(<TZLabel time="2026-03-16T10:11:12Z" showPopover={false} />).unmount()++ const remounted = render(<TZLabel time="2026-03-16T10:10:15Z" showPopover={false} />)+ act(() => {+ jest.advanceTimersByTime(65_000)+ })++ expect(remounted.container).toHaveTextContent('2 minutes ago')+ })+ })})diff --git a/frontend/src/lib/components/TZLabel/index.tsx b/frontend/src/lib/components/TZLabel/index.tsxindex ad482a1def41..be175fdbac7f 100644--- a/frontend/src/lib/components/TZLabel/index.tsx+++ b/frontend/src/lib/components/TZLabel/index.tsx@@ -23,6 +23,26 @@ import { teamLogic } from '../../../scenes/teamLogic'const BASE_OUTPUT_FORMAT = 'ddd, MMM D, YYYY h:mm A'const BASE_OUTPUT_FORMAT_WITH_SECONDS = 'ddd, MMM D, YYYY h:mm:ss A'+// One shared 1s ticker for every mounted TZLabel: a table can render hundreds of them, and an+// interval per instance means hundreds of timer wake-ups per second for text that changes about+// once a minute. The interval only runs while at least one label is subscribed.+const tickerListeners = new Set<() => void>()+let tickerId: number | null = null++function subscribeToTicker(listener: () => void): () => void {+ tickerListeners.add(listener)+ if (tickerId === null) {+ tickerId = window.setInterval(() => tickerListeners.forEach((tick) => tick()), 1000)+ }+ return () => {+ tickerListeners.delete(listener)+ if (tickerListeners.size === 0 && tickerId !== null) {+ window.clearInterval(tickerId)+ tickerId = null+ }+ }+}+export type TZLabelProps = Omit<LemonDropdownProps, 'overlay' | 'trigger' | 'children'> & {time: string | dayjs.DayjsshowSeconds?: boolean@@ -219,7 +239,7 @@ const TZLabelRaw = forwardRef<HTMLElement, TZLabelProps>(function TZLabelRaw(const { isVisible: isPageVisible } = usePageVisibility()- // NOTE: This is an optimization to make sure we don't needlessly re-render the component every second.+ // NOTE: The guarded setState makes sure we don't needlessly re-render the component every second.useEffect(() => {if (!isPageVisible) {return@@ -230,10 +250,9 @@ const TZLabelRaw = forwardRef<HTMLElement, TZLabelProps>(function TZLabelRaw(setFormattedContent((current) => (newContent !== current ? newContent : current))}- const interval = setInterval(run, 1000)run()- return () => clearInterval(interval)+ return subscribeToTicker(run)}, [displayTime, format, isPageVisible])const innerContent = children ?? (