: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(replay): nudge overlapping comment and emoji seekbar ticks apart (#73943)
Arno Hillen6cacbb09bdbb
diff --git a/frontend/src/scenes/session-recordings/player/controller/PlayerSeekbarTicks.tsx b/frontend/src/scenes/session-recordings/player/controller/PlayerSeekbarTicks.tsxindex 92622efd03e6..ae44f009477e 100644--- a/frontend/src/scenes/session-recordings/player/controller/PlayerSeekbarTicks.tsx+++ b/frontend/src/scenes/session-recordings/player/controller/PlayerSeekbarTicks.tsx@@ -2,6 +2,7 @@ import clsx from 'clsx'import { useActions, useValues } from 'kea'import posthog from 'posthog-js'import React, { MutableRefObject, memo, useEffect, useMemo } from 'react'+import useResizeObserver from 'use-resize-observer'import { IconComment } from '@posthog/icons'@@ -25,8 +26,13 @@ import { isSingleEmoji } from 'scenes/session-recordings/utils'import { primaryEventPropertiesModel } from '~/models/primaryEventPropertiesModel'+import { SeekbarGlyph, resolveOverlappingGlyphs } from './seekbarTickOffsets'import { UserActivity } from './UserActivity'+// keep in sync with --emoji-width / --comment-width in Seekbar.scss+const EMOJI_TICK_WIDTH_PX = 16+const COMMENT_TICK_WIDTH_PX = 12+function isEventItem(x: InspectorListItem): x is InspectorListItemEvent {return 'data' in x && !!x.data && 'event' in x.data}@@ -67,6 +73,7 @@ function PlayerSeekbarTick({zIndex,onClick,primaryProperties,+ offsetPx = 0,}: {item:| InspectorListItemComment@@ -78,6 +85,7 @@ function PlayerSeekbarTick({zIndex: numberonClick: (e: React.MouseEvent) => voidprimaryProperties: Record<string, string>+ offsetPx?: number}): JSX.Element | null {const position = (item.timeInRecording / endTimeMs) * 100@@ -93,7 +101,7 @@ function PlayerSeekbarTick({className={clsx('PlayerSeekbarTick', item.highlightColor && `PlayerSeekbarTick--${item.highlightColor}`)}// eslint-disable-next-line react/forbid-dom-propsstyle={{- left: `${position}%`,+ left: offsetPx ? `calc(${position}% + ${offsetPx}px)` : `${position}%`,zIndex: zIndex,}}onClick={onClick}@@ -172,6 +180,8 @@ const MemoisedPlayerSeekbarTicks = memo(seekToTime,hoverRef,primaryProperties,+ ticksRef,+ containerWidth,}: {seekbarItems: (| InspectorListItemEvent@@ -184,9 +194,30 @@ const MemoisedPlayerSeekbarTicks = memo(seekToTime: (timeInMilliseconds: number) => voidhoverRef: MutableRefObject<HTMLDivElement | null>primaryProperties: Record<string, string>+ ticksRef: (node: HTMLDivElement | null) => void+ containerWidth: number}): JSX.Element {+ const glyphOffsets = useMemo(() => {+ const glyphs: SeekbarGlyph[] = []+ seekbarItems.forEach((item, index) => {+ if (!isAnyComment(item)) {+ return+ }+ const position = (item.timeInRecording / endTimeMs) * 100+ if (position < 0 || position > 100) {+ return+ }+ glyphs.push({+ index,+ position,+ widthPx: isEmojiComment(item) ? EMOJI_TICK_WIDTH_PX : COMMENT_TICK_WIDTH_PX,+ })+ })+ return resolveOverlappingGlyphs(glyphs, containerWidth)+ }, [seekbarItems, endTimeMs, containerWidth])+return (- <div className="PlayerSeekbarTicks">+ <div className="PlayerSeekbarTicks" ref={ticksRef}><UserActivity hoverRef={hoverRef} />{seekbarItems.map((item, i) => {return (@@ -200,6 +231,7 @@ const MemoisedPlayerSeekbarTicks = memo(seekToTime(item.timeInRecording)}}primaryProperties={primaryProperties}+ offsetPx={glyphOffsets.get(i)}/>)})}@@ -215,7 +247,8 @@ const MemoisedPlayerSeekbarTicks = memo(seekbarItemsAreEqual &&prev.endTimeMs === next.endTimeMs &&prev.seekToTime === next.seekToTime &&- prev.primaryProperties === next.primaryProperties+ prev.primaryProperties === next.primaryProperties &&+ prev.containerWidth === next.containerWidth)})@@ -239,6 +272,7 @@ export function PlayerSeekbarTicks({}): JSX.Element {const { primaryProperties } = useValues(primaryEventPropertiesModel)const { ensureLoadedForEvents } = useActions(primaryEventPropertiesModel)+ const { ref: ticksRef, width: containerWidth } = useResizeObserver<HTMLDivElement>({})const distinctEventNames = useMemo(() => Array.from(new Set(seekbarItems.filter(isEventItem).map((i) => i.data.event))),[seekbarItems]@@ -254,6 +288,8 @@ export function PlayerSeekbarTicks({seekToTime={seekToTime}hoverRef={hoverRef}primaryProperties={primaryProperties}+ ticksRef={ticksRef}+ containerWidth={containerWidth ?? 0}/>)}diff --git a/frontend/src/scenes/session-recordings/player/controller/seekbarTickOffsets.test.ts b/frontend/src/scenes/session-recordings/player/controller/seekbarTickOffsets.test.tsnew file mode 100644index 000000000000..7bd6ae1bb429--- /dev/null+++ b/frontend/src/scenes/session-recordings/player/controller/seekbarTickOffsets.test.ts@@ -0,0 +1,68 @@+import {+ GLYPH_GAP_PX,+ SeekbarGlyph,+ resolveOverlappingGlyphs,+} from 'scenes/session-recordings/player/controller/seekbarTickOffsets'++const CONTAINER_WIDTH = 600++function leftEdges(glyphs: SeekbarGlyph[], containerWidth: number = CONTAINER_WIDTH): number[] {+ const offsets = resolveOverlappingGlyphs(glyphs, containerWidth)+ return glyphs.map((glyph) => {+ const naturalLeft = (glyph.position / 100) * containerWidth - glyph.widthPx / 2+ return naturalLeft + (offsets.get(glyph.index) ?? 0)+ })+}++function glyph(index: number, position: number, widthPx: number = 12): SeekbarGlyph {+ return { index, position, widthPx }+}++describe('resolveOverlappingGlyphs', () => {+ it('separates a comment and an emoji recorded at the same moment', () => {+ const glyphs = [glyph(0, 50, 12), glyph(1, 50, 16)]++ const edges = leftEdges(glyphs)++ const [first, second] = edges[0] <= edges[1] ? [0, 1] : [1, 0]+ expect(edges[second]).toBeGreaterThanOrEqual(edges[first] + glyphs[first].widthPx + GLYPH_GAP_PX)+ })++ it.each([+ ['the end', [glyph(0, 99.5), glyph(1, 99.7), glyph(2, 100)]],+ ['the start', [glyph(0, 0), glyph(1, 0.2), glyph(2, 0.4)]],+ ])('keeps glyphs inside the seekbar and still apart when they pile up at %s', (_name, glyphs) => {+ const edges = leftEdges(glyphs as SeekbarGlyph[])++ const ordered = edges+ .map((left, i) => ({ left, width: (glyphs as SeekbarGlyph[])[i].widthPx }))+ .sort((a, b) => a.left - b.left)+ ordered.forEach(({ left, width }) => {+ expect(left).toBeGreaterThanOrEqual(0)+ expect(left + width).toBeLessThanOrEqual(CONTAINER_WIDTH)+ })+ ordered.slice(1).forEach(({ left }, i) => {+ const previous = ordered[i]+ expect(left).toBeGreaterThanOrEqual(previous.left + previous.width + GLYPH_GAP_PX)+ })+ })++ it('resolves collisions by position, not input order', () => {+ const glyphs = [glyph(0, 60), glyph(1, 20), glyph(2, 20.5)]++ const offsets = resolveOverlappingGlyphs(glyphs, CONTAINER_WIDTH)++ // the isolated glyph at 60% is untouched, the pair at ~20% is pushed apart+ expect(offsets.has(0)).toBe(false)+ const edges = leftEdges(glyphs)+ expect(Math.abs(edges[2] - edges[1])).toBeGreaterThanOrEqual(12 + GLYPH_GAP_PX)+ })++ it.each([+ ['a single glyph', [glyph(0, 50)], CONTAINER_WIDTH],+ ['glyphs that are far apart', [glyph(0, 10), glyph(1, 50), glyph(2, 90)], CONTAINER_WIDTH],+ ['an unmeasured container', [glyph(0, 50), glyph(1, 50)], 0],+ ])('leaves %s alone', (_name, glyphs, containerWidth) => {+ expect(resolveOverlappingGlyphs(glyphs as SeekbarGlyph[], containerWidth as number).size).toBe(0)+ })+})diff --git a/frontend/src/scenes/session-recordings/player/controller/seekbarTickOffsets.ts b/frontend/src/scenes/session-recordings/player/controller/seekbarTickOffsets.tsnew file mode 100644index 000000000000..bff013946bba--- /dev/null+++ b/frontend/src/scenes/session-recordings/player/controller/seekbarTickOffsets.ts@@ -0,0 +1,55 @@+/**+ * Comment and emoji ticks are wide glyphs centred on their timestamp, so two of them recorded+ * close together render on top of each other and become unreadable.+ */+export interface SeekbarGlyph {+ index: number+ /** Horizontal centre of the glyph, as a percentage (0-100) of the seekbar width. */+ position: number+ /** Rendered width of the glyph in pixels. */+ widthPx: number+}++export const GLYPH_GAP_PX = 2++/** Offsets in pixels keyed by tick index. A glyph that needs no moving is absent from the map. */+export function resolveOverlappingGlyphs(+ glyphs: SeekbarGlyph[],+ containerWidthPx: number,+ gapPx: number = GLYPH_GAP_PX+): Map<number, number> {+ const offsets = new Map<number, number>()+ if (containerWidthPx <= 0 || glyphs.length < 2) {+ return offsets+ }++ const sorted = [...glyphs].sort((a, b) => a.position - b.position)+ const naturalLefts = sorted.map((glyph) => (glyph.position / 100) * containerWidthPx - glyph.widthPx / 2)+ const lefts: number[] = []++ // left to right, pushing each glyph clear of the one before it+ for (let i = 0; i < sorted.length; i++) {+ const earliest = i === 0 ? 0 : lefts[i - 1] + sorted[i - 1].widthPx + gapPx+ lefts.push(Math.max(naturalLefts[i], earliest, 0))+ }++ // Right to left, so a run that would extend past the end of the seekbar gets packed back towards+ // the start. Without this every glyph in the run pins to the same maximum left edge and they+ // overlap again, which is the problem this whole module exists to avoid.+ for (let i = sorted.length - 1; i >= 0; i--) {+ const rightBound = i === sorted.length - 1 ? containerWidthPx : lefts[i + 1] - gapPx+ lefts[i] = Math.min(lefts[i], rightBound - sorted[i].widthPx)+ }++ sorted.forEach((glyph, i) => {+ // A run wider than the whole seekbar can't be separated, so keep it on screen and let it+ // overlap rather than pushing glyphs off the left edge where they'd be invisible.+ const left = Math.max(0, lefts[i])+ const offset = left - naturalLefts[i]+ if (offset !== 0) {+ offsets.set(glyph.index, offset)+ }+ })++ return offsets+}