: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): sort blank chart preview tiles after previewed ones (#106492)
Sam Penningtonc89f65dec0de
diff --git a/products/product_analytics/frontend/insights/chartAlternatives/ChartPreviewTile.tsx b/products/product_analytics/frontend/insights/chartAlternatives/ChartPreviewTile.tsxindex 5adcb74d9275..8fedce6fc568 100644--- a/products/product_analytics/frontend/insights/chartAlternatives/ChartPreviewTile.tsx+++ b/products/product_analytics/frontend/insights/chartAlternatives/ChartPreviewTile.tsx@@ -60,12 +60,16 @@ export function ChartPreviewTile({<spanclassName={clsx('flex h-full flex-col [&_.text-7xl]:text-lg [&_.text-7xl]:leading-tight',- option.display === ChartDisplayType.CalendarHeatmap &&+ response && option.display === ChartDisplayType.BoldNumber && '[&_.BoldNumber]:p-4',+ response &&+ option.display === ChartDisplayType.CalendarHeatmap &&'[zoom:0.3] [&_.CalendarHeatMapContainer+div]:hidden',// MetricCard hardcodes its headline at text-4xl, which truncates at tile width.- option.display === ChartDisplayType.Metric &&+ response &&+ option.display === ChartDisplayType.Metric &&'[&_.text-4xl]:text-lg [&_.text-4xl]:leading-tight',- option.display === ChartDisplayType.ActionsTable &&+ response &&+ option.display === ChartDisplayType.ActionsTable &&'[zoom:0.6] [&_.ScrollableShadows::before]:shadow-none! [&_.ScrollableShadows::after]:shadow-none! [&_.LemonTable__cell--sticky::before]:shadow-none! [&_.LemonTable__header--sticky::before]:shadow-none!')}>diff --git a/products/product_analytics/frontend/insights/chartAlternatives/chartPreviewData.ts b/products/product_analytics/frontend/insights/chartAlternatives/chartPreviewData.tsindex f64d59fbb23f..4a877620a664 100644--- a/products/product_analytics/frontend/insights/chartAlternatives/chartPreviewData.ts+++ b/products/product_analytics/frontend/insights/chartAlternatives/chartPreviewData.ts@@ -1,6 +1,7 @@import type { AnyResponseType, TrendsQuery } from '~/queries/schema/schema-general'import { ChartDisplayType, type TrendResult } from '~/types'+import { hasTrendsChartData } from '../shared/hasTrendsChartData'import { breakdownProperties, hasTrendsFormula } from './chartDisplayOptions'import { sampleBoxPlotRows, sampleCalendarHeatmapRows, sampleWorldMapRows } from './chartPreviewSamples'@@ -34,6 +35,12 @@ function resultsOf(response: AnyResponseType): TrendResult[] {return Array.isArray(raw) ? (raw as TrendResult[]) : []}+export function hasPreviewData(response: AnyResponseType): boolean {+ const results = resultsOf(response)+ const shape = shapeOf(results)+ return shape === 'boxPlot' || shape === 'heatmap' || hasTrendsChartData(results)+}+// insightDataLogic rebuilds `result` from `results`, so both keys must carry the derived rows.function withResults(response: AnyResponseType, results: unknown[]): AnyResponseType {return { ...(response as object), results, result: results } as AnyResponseTypediff --git a/products/product_analytics/frontend/insights/chartAlternatives/chartPreviewsLogic.test.ts b/products/product_analytics/frontend/insights/chartAlternatives/chartPreviewsLogic.test.tsindex b60fea6be604..10be4b5577c4 100644--- a/products/product_analytics/frontend/insights/chartAlternatives/chartPreviewsLogic.test.ts+++ b/products/product_analytics/frontend/insights/chartAlternatives/chartPreviewsLogic.test.ts@@ -11,7 +11,7 @@ import { useMocks } from '~/mocks/jest'import { NodeKind } from '~/queries/schema/schema-general'import type { TrendsQuery } from '~/queries/schema/schema-general'import { initKeaTests } from '~/test/init'-import { BaseMathType, ChartDisplayType, InsightShortId } from '~/types'+import { BaseMathType, ChartDisplayType, InsightShortId, PropertyMathType } from '~/types'import { chartAlternativesLogic } from './chartAlternativesLogic'import { chartPreviewsLogic } from './chartPreviewsLogic'@@ -85,6 +85,62 @@ describe('chartPreviewsLogic', () => {)}+ it('orders tiles as suggested, previewed, blank, then disabled', () => {+ insightVizDataLogic(insightProps).actions.updateQuerySource({+ ...trendsQuery(ChartDisplayType.ActionsLineGraph),+ breakdownFilter: { breakdowns: [{ property: '$browser', type: 'event' }] },+ })+ insightDataLogic(insightProps).actions.setInsightData({+ results: [timeSeriesRow],+ hasMore: true,+ last_refresh: FIRST_REFRESH,+ })++ const bands = chartPreviewsLogic(logicProps).values.previews.map((preview) =>+ preview.suggested+ ? 'suggested'+ : preview.option.disabledReason+ ? 'disabled'+ : preview.response+ ? 'previewed'+ : 'blank'+ )+ const order = ['suggested', 'previewed', 'blank', 'disabled']+ expect(new Set(bands)).toEqual(new Set(order))+ expect(bands).toEqual([...bands].sort((a, b) => order.indexOf(a) - order.indexOf(b)))+ })++ it('shows blank tiles, and keeps the suggested row, when the result has no matching data', () => {+ load(trendsQuery(ChartDisplayType.ActionsLineGraph), { ...timeSeriesRow, data: [0, 0, 0], count: 0 })++ const previews = chartPreviewsLogic(logicProps).values.previews+ expect(previews.length).toBeGreaterThan(0)+ expect(previews.every((preview) => preview.response === null && !preview.sample)).toBe(true)+ expect(previews.some((preview) => preview.suggested)).toBe(true)+ })++ it('keeps the box plot preview when the loaded result is a box plot', () => {+ load(+ {+ ...trendsQuery(ChartDisplayType.BoxPlot),+ series: [+ {+ kind: NodeKind.EventsNode,+ event: '$pageview',+ math: PropertyMathType.Median,+ math_property: 'duration',+ },+ ],+ },+ { day: '2026-01-01', label: '1-Jan', min: 1, p25: 2, median: 3, p75: 4, max: 5, mean: 3 }+ )++ const boxPlot = chartPreviewsLogic(logicProps).values.previews.find(+ (preview) => preview.option.display === ChartDisplayType.BoxPlot+ )+ expect(boxPlot?.response).not.toBeNull()+ })+it('reuses the time series it saw before the chart became a total value, even when the total loaded later', async () => {load(trendsQuery(ChartDisplayType.ActionsLineGraph), timeSeriesRow)load(trendsQuery(ChartDisplayType.ActionsPie), totalValueRow, SECOND_REFRESH)diff --git a/products/product_analytics/frontend/insights/chartAlternatives/chartPreviewsLogic.ts b/products/product_analytics/frontend/insights/chartAlternatives/chartPreviewsLogic.tsindex 597e5369cc86..da966f66835b 100644--- a/products/product_analytics/frontend/insights/chartAlternatives/chartPreviewsLogic.ts+++ b/products/product_analytics/frontend/insights/chartAlternatives/chartPreviewsLogic.ts@@ -12,7 +12,7 @@ import type { ChartAlternativesLogicProps } from './chartAlternativesLogic'import { chartAlternativesLogic } from './chartAlternativesLogic'import { applyChartDisplay } from './chartDisplayOptions'import type { ChartDisplayOption, ChartDisplayOptionGroup } from './chartDisplayOptions'-import { RAW_TIME_SERIES_DISPLAYS, deriveChartPreview } from './chartPreviewData'+import { RAW_TIME_SERIES_DISPLAYS, deriveChartPreview, hasPreviewData } from './chartPreviewData'export interface ChartPreview {option: ChartDisplayOption@@ -282,14 +282,10 @@ export const chartPreviewsLogic = kea<chartPreviewsLogicType>([const remaining = options.flatMap((group) =>group.options.filter((option) => !suggestedDisplays.has(option.display)))- const ordered = [- ...alternatives,- ...remaining.filter((option) => !option.disabledReason),- ...remaining.filter((option) => !!option.disabledReason),- ]- return ordered.map((option) => {+ const canDerive = !!freeResponse && !insightDataLoading && hasPreviewData(freeResponse)+ const previews = [...alternatives, ...remaining].map((option) => {const derived =- option.disabledReason || !freeResponse || insightDataLoading+ option.disabledReason || !canDerive? null: deriveChartPreview(option.display, trendsSource, freeResponse, timeSeriesResponse)return {@@ -301,6 +297,9 @@ export const chartPreviewsLogic = kea<chartPreviewsLogicType>([uniqueKey: `chart-preview-${logicKey}-${option.display}`,}})+ const band = (preview: ChartPreview): number =>+ preview.suggested ? 0 : preview.option.disabledReason ? 3 : canDerive && !preview.response ? 2 : 1+ return previews.sort((a, b) => band(a) - band(b))},],}),diff --git a/products/product_analytics/frontend/insights/shared/hasTrendsChartData.ts b/products/product_analytics/frontend/insights/shared/hasTrendsChartData.tsindex b1a98334d1c1..352a6c0d2a23 100644--- a/products/product_analytics/frontend/insights/shared/hasTrendsChartData.ts+++ b/products/product_analytics/frontend/insights/shared/hasTrendsChartData.ts@@ -1,13 +1,13 @@-import type { IndexedTrendResult } from 'products/product_analytics/frontend/insights/trends/types'+import type { TrendResult } from '~/types'// Gate the empty state on the whole result set, not just the first series — trendsDataLogic// re-sorts and interleaves results for compare mode, breakdown ordering, and unstacked bars, so// index 0 isn't a stable "does this tile have data" signal.-export function hasTrendsChartData(indexedResults: IndexedTrendResult[] | undefined): boolean {+export function hasTrendsChartData(indexedResults: TrendResult[] | undefined): boolean {if (!indexedResults?.length) {return false}- return indexedResults.some((result: IndexedTrendResult) => {+ return indexedResults.some((result: TrendResult) => {if (Number.isFinite(result.aggregated_value) && result.aggregated_value !== 0) {return true}