Skip to content

: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.

fix(charts): stop a shorter funnel white-screening the results view (#109999)

Ben Lea0509cf518049
diff --git a/frontend/src/lib/utils/numbers.test.ts b/frontend/src/lib/utils/numbers.test.ts
index 16c31b6a53ca..8bfd01a68cbb 100644
--- a/frontend/src/lib/utils/numbers.test.ts
+++ b/frontend/src/lib/utils/numbers.test.ts
@@ -3,6 +3,7 @@ import {
compactNumber,
formatPercentageDiff,
humanFriendlyLargeNumber,
+ humanFriendlyNumber,
median,
roundToDecimal,
} from 'lib/utils/numbers'
@@ -68,6 +69,17 @@ describe('numbers utils', () => {
})
})
+ describe('humanFriendlyNumber()', () => {
+ it('formats number correctly', () => {
+ expect(humanFriendlyNumber(1234)).toEqual('1,234')
+ expect(humanFriendlyNumber(1234.567)).toEqual('1,234.57')
+ // Callers render this during a React render pass, so a missing count has to come back
+ // as a string. Throwing here unmounts the whole view over one label.
+ expect(humanFriendlyNumber(undefined)).toEqual('-')
+ expect(humanFriendlyNumber(null)).toEqual('-')
+ })
+ })
+
describe('formatPercentageDiff()', () => {
it.each([
{ current: 150, previous: 100, expected: '(+50.0%)' },
diff --git a/frontend/src/lib/utils/numbers.ts b/frontend/src/lib/utils/numbers.ts
index 0f15dbefb00a..40f94cbb9f11 100644
--- a/frontend/src/lib/utils/numbers.ts
+++ b/frontend/src/lib/utils/numbers.ts
@@ -81,10 +81,13 @@ export function significantDecimalPlaces(value: number, minimum?: number | null)
/** Format number with comma as the thousands separator. */
export function humanFriendlyNumber(
- d: number,
+ d: number | null | undefined,
maximumFractionDigits: number = DEFAULT_DECIMAL_PLACES,
minimumFractionDigits: number = 0
): string {
+ if (d == null) {
+ return '-'
+ }
return d.toLocaleString('en-US', {
maximumFractionDigits: validateFractionDigits(maximumFractionDigits, DEFAULT_DECIMAL_PLACES),
minimumFractionDigits: validateFractionDigits(minimumFractionDigits, 0),
diff --git a/packages/quill/packages/charts/src/charts/FunnelChart/FunnelChart.test.tsx b/packages/quill/packages/charts/src/charts/FunnelChart/FunnelChart.test.tsx
index 0431b0ef5f1e..8d67d43305ff 100644
--- a/packages/quill/packages/charts/src/charts/FunnelChart/FunnelChart.test.tsx
+++ b/packages/quill/packages/charts/src/charts/FunnelChart/FunnelChart.test.tsx
@@ -130,6 +130,47 @@ describe('FunnelChart', () => {
expect(chart.xTicks()).toHaveLength(0)
})
+ it('never asks for a step footer past the current step count when a shorter funnel replaces a longer one', async () => {
+ const THREE_STEPS = ['Exposure', 'Add to cart', 'Purchase']
+ const requested: number[] = []
+ const { rerender } = renderHogChart(
+ <FunnelChart
+ steps={THREE_STEPS}
+ series={[{ key: 'all', label: 'All', data: [100, 60, 20] }]}
+ theme={THEME}
+ stepFooter={(stepIndex) => {
+ requested.push(stepIndex)
+ return <span>{stepIndex}</span>
+ }}
+ />
+ )
+ await waitFor(() => {
+ expect(document.querySelectorAll('[data-attr="hog-funnel-step-footer-cell"]')).toHaveLength(
+ THREE_STEPS.length
+ )
+ })
+
+ requested.length = 0
+ rerender(
+ <FunnelChart
+ steps={STEPS}
+ series={SERIES}
+ theme={THEME}
+ stepFooter={(stepIndex) => {
+ requested.push(stepIndex)
+ return <span>{stepIndex}</span>
+ }}
+ />
+ )
+ await waitFor(() => {
+ expect(document.querySelectorAll('[data-attr="hog-funnel-step-footer-cell"]')).toHaveLength(STEPS.length)
+ })
+
+ // The measured bands arrive a render late, so without clamping the first commit after this
+ // rerender asks for index 2 while the consumer only holds data for two steps.
+ expect(Math.max(...requested)).toBeLessThan(STEPS.length)
+ })
+
it('floors the chart region height with chartMinHeight so a tall footer cannot collapse the canvas', async () => {
renderHogChart(
<FunnelChart
diff --git a/packages/quill/packages/charts/src/charts/FunnelChart/FunnelChart.tsx b/packages/quill/packages/charts/src/charts/FunnelChart/FunnelChart.tsx
index c811c7b15d37..e5caeb7be7cf 100644
--- a/packages/quill/packages/charts/src/charts/FunnelChart/FunnelChart.tsx
+++ b/packages/quill/packages/charts/src/charts/FunnelChart/FunnelChart.tsx
@@ -278,7 +278,9 @@ export function FunnelChart<Meta = unknown>({
>
{chart}
</div>
- {bands && bands.length > 0 && <StepFooterRow bands={bands} stepFooter={stepFooter} />}
+ {bands && bands.length > 0 && (
+ <StepFooterRow bands={bands.slice(0, steps.length)} stepFooter={stepFooter} />
+ )}
</div>
)
}