: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
chore(web-analytics): Add loading state for heatmaps cohort filtering (#60456)
Jordan Mryyan9c166c45fae6
diff --git a/frontend/src/lib/components/heatmaps/HeatmapCanvas.tsx b/frontend/src/lib/components/heatmaps/HeatmapCanvas.tsxindex c0c58db16b2d..dfabbf1894b6 100644--- a/frontend/src/lib/components/heatmaps/HeatmapCanvas.tsx+++ b/frontend/src/lib/components/heatmaps/HeatmapCanvas.tsx@@ -15,6 +15,9 @@ import { useScrollSync } from './useScrollSync'// Radius in pixels to search for nearby heatmap elements when clickingconst CLICK_RADIUS_PX = 15+const TOOLTIP_OFFSET_PX = 12+const TOOLTIP_FLIP_THRESHOLD_PX = 160+const HEATMAP_CONFIG = {minOpacity: 0,maxOpacity: 0.8,@@ -32,12 +35,13 @@ function HeatmapMouseInfo({onHasValue?: (hasValue: boolean) => void}): JSX.Element | null {const shiftPressed = useShiftKeyPressed()- const { heatmapTooltipLabel } = useValues(heatmapDataLogic({ context }))+ const { heatmapTooltipLabel, rawHeatmapLoading } = useValues(heatmapDataLogic({ context }))- const mousePosition = useMousePosition(containerRef?.current)- const value = heatmapJsRef.current?.getValueAt(mousePosition)+ const containerMousePosition = useMousePosition(containerRef?.current)+ const viewportMousePosition = useMousePosition()+ const value = heatmapJsRef.current?.getValueAt(containerMousePosition)- const hasValue = !!(mousePosition && (value || shiftPressed))+ const hasValue = !!(containerMousePosition && (value || shiftPressed))useEffect(() => {onHasValue?.(hasValue)@@ -47,28 +51,20 @@ function HeatmapMouseInfo({return null}- const leftPosition = window.innerWidth - mousePosition.x < 100+ const flipLeft = window.innerWidth - viewportMousePosition.x < TOOLTIP_FLIP_THRESHOLD_PXreturn (<div- className="absolute z-10"+ className="fixed z-10 -translate-y-1/2 pointer-events-none"// eslint-disable-next-line react/forbid-dom-propsstyle={{- top: mousePosition.y,- left: mousePosition.x,+ top: viewportMousePosition.y,+ left: flipLeft ? undefined : viewportMousePosition.x + TOOLTIP_OFFSET_PX,+ right: flipLeft ? window.innerWidth - viewportMousePosition.x + TOOLTIP_OFFSET_PX : undefined,}}>- <div- className="absolute border rounded bg-surface-primary shadow-md p-2 mx-2 -translate-y-1/2"- // eslint-disable-next-line react/forbid-dom-props- style={{- left: leftPosition ? undefined : 0,- right: leftPosition ? 0 : undefined,- }}- >- <span className="font-semibold whitespace-nowrap">- {value} {heatmapTooltipLabel}- </span>+ <div className="border rounded bg-surface-primary shadow-md p-2 whitespace-nowrap font-semibold">+ {rawHeatmapLoading ? 'Loading…' : `${value ?? 0} ${heatmapTooltipLabel}`}</div></div>)@@ -217,20 +213,20 @@ export function HeatmapCanvas({)}+ const loadingClass = isReady+ ? 'heatmaps-ready opacity-100 transition-opacity duration-150'+ : 'heatmaps-loading opacity-40 pointer-events-none transition-opacity duration-150'+if (isToolbar) {return (<div- className={cn(- 'fixed inset-0 overflow-hidden',- isReady ? 'heatmaps-ready' : 'heatmaps-loading',- hasValueUnderMouse && 'cursor-pointer'- )}+ className={cn('fixed inset-0 overflow-hidden', hasValueUnderMouse && 'cursor-pointer')}data-attr="heatmap-canvas"onClick={handleCanvasClick}><divref={innerRef}- className="absolute top-0 left-0 w-full"+ className={cn('absolute top-0 left-0 w-full', loadingClass)}style={{ height: heightOverride, willChange: 'transform' }}><div@@ -252,12 +248,7 @@ export function HeatmapCanvas({return (<div- className={cn(- 'inset-0 overflow-hidden w-full h-full',- positioning,- isReady ? 'heatmaps-ready' : 'heatmaps-loading',- hasValueUnderMouse && 'cursor-pointer'- )}+ className={cn('inset-0 overflow-hidden w-full h-full', positioning, hasValueUnderMouse && 'cursor-pointer')}data-attr="heatmap-canvas"onClick={handleCanvasClick}>@@ -267,7 +258,7 @@ export function HeatmapCanvas({? 'export-heatmap': `${widthOverride ?? windowWidth}x${windowHeight}x${heightOverride}x${heatmapFixedPositionMode}`}- className="absolute inset-0"+ className={cn('absolute inset-0', loadingClass)}ref={setHeatmapContainer}/><HeatmapMouseInfodiff --git a/frontend/src/lib/components/heatmaps/ScrollDepthCanvas.tsx b/frontend/src/lib/components/heatmaps/ScrollDepthCanvas.tsxindex beb956f2c533..3249b9c3162e 100644--- a/frontend/src/lib/components/heatmaps/ScrollDepthCanvas.tsx+++ b/frontend/src/lib/components/heatmaps/ScrollDepthCanvas.tsx@@ -141,17 +141,13 @@ export function ScrollDepthCanvas({}return (- <div- className={cn(- 'inset-0 overflow-hidden w-full h-full',- positioning,- isReady ? 'heatmaps-ready' : 'heatmaps-loading'- )}- data-attr="scroll-depth-canvas"- >+ <div className={cn('inset-0 overflow-hidden w-full h-full', positioning)} data-attr="scroll-depth-canvas"><divref={innerRef}- className="absolute top-0 left-0 right-0"+ className={cn(+ 'absolute top-0 left-0 right-0 transition-opacity duration-150',+ isReady ? 'heatmaps-ready opacity-100' : 'heatmaps-loading opacity-40 pointer-events-none'+ )}// eslint-disable-next-line react/forbid-dom-propsstyle={{willChange: context === 'toolbar' ? 'transform' : undefined,diff --git a/frontend/src/lib/components/heatmaps/heatmapDataLogic.ts b/frontend/src/lib/components/heatmaps/heatmapDataLogic.tsindex 91bfc8cf273f..1b4f7b90d760 100644--- a/frontend/src/lib/components/heatmaps/heatmapDataLogic.ts+++ b/frontend/src/lib/components/heatmaps/heatmapDataLogic.ts@@ -20,6 +20,7 @@ import {calculateViewportRange,} from 'lib/components/IframedToolbarBrowser/utils'import { LemonSelectOption } from 'lib/lemon-ui/LemonSelect'+import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'import { dateFilterToText } from 'lib/utils'import { toolbarConfigLogic, toolbarFetch } from '~/toolbar/toolbarConfigLogic'@@ -35,6 +36,26 @@ export const HEATMAP_COLOR_PALETTE_OPTIONS: LemonSelectOption<string>[] = [{ value: 'blue', label: 'Blue (monocolor)' },]+async function parseHeatmapErrorMessage(response: Response): Promise<string> {+ try {+ const body = await response.clone().json()+ if (typeof body?.detail === 'string' && body.detail.length > 0) {+ return body.detail+ }+ for (const value of Object.values(body ?? {})) {+ if (Array.isArray(value) && typeof value[0] === 'string') {+ return value[0]+ }+ if (typeof value === 'string' && value.length > 0) {+ return value+ }+ }+ } catch {+ /* empty */+ }+ return `Heatmap request failed (status ${response.status})`+}+export interface HeatmapDataLogicProps {context: 'in-app' | 'toolbar'exportToken?: string | null@@ -205,7 +226,7 @@ export const heatmapDataLogic = kea<heatmapDataLogicType>([}if (response.status !== 200) {- throw new Error('API error')+ throw new Error(await parseHeatmapErrorMessage(response))}const data = await response.json()@@ -252,7 +273,7 @@ export const heatmapDataLogic = kea<heatmapDataLogicType>([breakpoint()if (response.status !== 200) {- throw new Error('API error')+ throw new Error(await parseHeatmapErrorMessage(response))}return await response.json()@@ -443,6 +464,7 @@ export const heatmapDataLogic = kea<heatmapDataLogicType>([: fetch(apiURL))if (response.status !== 200) {+ lemonToast.error(await parseHeatmapErrorMessage(response))return}@@ -454,6 +476,13 @@ export const heatmapDataLogic = kea<heatmapDataLogicType>([has_more: newData.has_more,})},+ loadHeatmapFailure: ({ error }) => {+ lemonToast.error(error || 'Heatmap query failed')+ actions.setIsReady(true)+ },+ loadAreaEventsFailure: ({ error }) => {+ lemonToast.error(error || 'Failed to load events for selected area')+ },})),subscriptions(({ actions }) => ({windowWidth: () => {diff --git a/frontend/src/toolbar/Toolbar.stories.tsx b/frontend/src/toolbar/Toolbar.stories.tsxindex 8dd86cfdc2cf..c240d2c2ebf3 100644--- a/frontend/src/toolbar/Toolbar.stories.tsx+++ b/frontend/src/toolbar/Toolbar.stories.tsx@@ -79,6 +79,7 @@ const meta: Meta<StoryArgs> = {},},'/api/element/stats/': listHeatmapStatsAPIResponse,+ '/api/heatmap/': { results: [] },'/api/projects/@current/feature_flags/my_flags': listMyFlagsAPIResponse,'/api/projects/@current/actions/': listActionsAPIResponse,'/api/projects/@current/web_experiments/': listExperimentsAPIResponse,