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(replay): save comments made at the start of a recording (#80407)

Tue Haulund549bf76eb195
diff --git a/frontend/src/scenes/session-recordings/player/commenting/playerFrameCommentOverlayLogic.test.ts b/frontend/src/scenes/session-recordings/player/commenting/playerFrameCommentOverlayLogic.test.ts
index 0a2d4b1c6807..adf7880added 100644
--- a/frontend/src/scenes/session-recordings/player/commenting/playerFrameCommentOverlayLogic.test.ts
+++ b/frontend/src/scenes/session-recordings/player/commenting/playerFrameCommentOverlayLogic.test.ts
@@ -2,10 +2,16 @@ import { MOCK_DEFAULT_BASIC_USER, MOCK_SECOND_BASIC_USER } from 'lib/api.mock'
import { expectLogic } from 'kea-test-utils'
+import api from 'lib/api'
+import { dayjs } from 'lib/dayjs'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { membersLogic } from 'scenes/organization/membersLogic'
+import { sessionRecordingDataCoordinatorLogic } from 'scenes/session-recordings/player/sessionRecordingDataCoordinatorLogic'
import { sessionRecordingPlayerLogic } from 'scenes/session-recordings/player/sessionRecordingPlayerLogic'
+import { CommentType } from '~/types'
+
+import { recordingMetaJson } from '../../__mocks__/recording_meta'
import { setupSessionRecordingTest } from '../__mocks__/test-setup'
import { playerCommentOverlayLogic } from './playerFrameCommentOverlayLogic'
@@ -65,6 +71,34 @@ describe('playerFrameCommentOverlayLogic', () => {
expect(membersLogic.values.meFirstMembers).toHaveLength(mockMembers.length)
})
+ // Regression test: currentTimestamp is unset until the player seeks, so a comment made before
+ // the playhead moves off 00:00 used to submit an invalid date and fail silently inside the
+ // kea-forms submit, saving nothing and reporting nothing.
+ it('saves a comment made while the playhead is still at the start', async () => {
+ const createSpy = jest.spyOn(api.comments, 'create').mockResolvedValue({} as CommentType)
+
+ await expectLogic(sessionRecordingPlayerLogic(playerLogicProps)).toDispatchActions([
+ sessionRecordingDataCoordinatorLogic({ sessionRecordingId: '1' }).actionTypes.loadRecordingMetaSuccess,
+ ])
+ expect(logic.values.currentPlayerTime).toBe(0)
+
+ logic.actions.setIsCommenting(true)
+ logic.actions.setRichContent({
+ type: 'doc',
+ content: [{ type: 'paragraph', content: [{ type: 'text', text: 'a comment' }] }],
+ })
+
+ await expectLogic(logic, () => {
+ logic.actions.submitRecordingComment()
+ }).toDispatchActions(['submitRecordingCommentSuccess'])
+
+ expect(createSpy).toHaveBeenCalledTimes(1)
+ expect(createSpy.mock.calls[0][0].item_context).toMatchObject({
+ milliseconds_into_recording: 0,
+ time_in_recording: dayjs(recordingMetaJson.start_time).toISOString(),
+ })
+ })
+
it('does not load all members again when the overlay closes', async () => {
// Prime the loaded state first so the close path is exercised in isolation.
await expectLogic(membersLogic, () => {
diff --git a/frontend/src/scenes/session-recordings/player/commenting/playerFrameCommentOverlayLogic.ts b/frontend/src/scenes/session-recordings/player/commenting/playerFrameCommentOverlayLogic.ts
index e66b18688b63..17dea0d4bf17 100644
--- a/frontend/src/scenes/session-recordings/player/commenting/playerFrameCommentOverlayLogic.ts
+++ b/frontend/src/scenes/session-recordings/player/commenting/playerFrameCommentOverlayLogic.ts
@@ -41,6 +41,7 @@ export interface playerCommentOverlayLogicValues {
currentTimestamp: number | undefined // sessionRecordingPlayerLogic
sessionPlayerData: SessionPlayerData // sessionRecordingPlayerLogic
asTask: boolean
+ dateForCurrentTimestamp: Dayjs | null
formattedTimestamp: string
isEmpty: boolean
isLoading: boolean
@@ -132,6 +133,11 @@ export interface playerCommentOverlayLogicMeta {
__keaTypeGenInternalSelectorTypes: {
timestampUnits: (sessionPlayerData: SessionPlayerData) => 2 | 3
formattedTimestamp: (currentPlayerTime: number, timestampUnits: 2 | 3) => string
+ dateForCurrentTimestamp: (
+ currentTimestamp: number | undefined,
+ currentPlayerTime: number,
+ sessionPlayerData: SessionPlayerData
+ ) => Dayjs | null
}
}
@@ -202,13 +208,28 @@ export const playerCommentOverlayLogic = kea<playerCommentOverlayLogicType>([
return colonDelimitedDuration(currentPlayerTime / 1000, timestampUnits)
},
],
+ // currentTimestamp is unset until the player seeks, so fall back to the recording start
+ // plus the playhead offset rather than leaving a comment with no date to anchor to
+ dateForCurrentTimestamp: [
+ (s) => [s.currentTimestamp, s.currentPlayerTime, s.sessionPlayerData],
+ (
+ currentTimestamp: number | undefined,
+ currentPlayerTime: number,
+ sessionPlayerData: SessionPlayerData
+ ): Dayjs | null => {
+ if (currentTimestamp) {
+ return dayjs(currentTimestamp)
+ }
+ return sessionPlayerData?.start ? sessionPlayerData.start.add(currentPlayerTime, 'milliseconds') : null
+ },
+ ],
}),
subscriptions(({ actions, values }) => ({
formattedTimestamp: (formattedTimestamp) => {
// as the timestamp from the player changes we track three representations of it
actions.setRecordingCommentValue('timeInRecording', formattedTimestamp)
actions.setRecordingCommentValue('timestampInRecording', values.currentPlayerTime)
- actions.setRecordingCommentValue('dateForTimestamp', dayjs(values.currentTimestamp))
+ actions.setRecordingCommentValue('dateForTimestamp', values.dateForCurrentTimestamp)
},
})),
listeners(({ actions, props, values }) => ({
@@ -248,6 +269,11 @@ export const playerCommentOverlayLogic = kea<playerCommentOverlayLogicType>([
lemonToast.error(`Emoji comments must be emojis 🙈, this string was too long: "${emoji}"`)
return
}
+ const dateForTimestamp = values.dateForCurrentTimestamp
+ if (!dateForTimestamp) {
+ lemonToast.error('Could not save your comment: the recording has no timestamp yet.')
+ return
+ }
const loadingTimeout = setTimeout(() => {
actions.setLoading(true)
}, 250)
@@ -259,12 +285,14 @@ export const playerCommentOverlayLogic = kea<playerCommentOverlayLogicType>([
item_id: props.recordingId,
item_context: {
is_emoji: true,
- time_in_recording: dayjs(values.currentTimestamp).toISOString(),
+ time_in_recording: dateForTimestamp.toISOString(),
milliseconds_into_recording: values.currentPlayerTime,
},
slug: `/replay/${props.recordingId}#panel=discussion`,
})
playerCommentModel.actions.commentEdited(props.recordingId)
+ } catch (e) {
+ lemonToast.error(`Could not save your comment: ${(e as Error).message}`)
} finally {
if (loadingTimeout) {
clearTimeout(loadingTimeout)
@@ -292,7 +320,11 @@ export const playerCommentOverlayLogic = kea<playerCommentOverlayLogicType>([
}
},
submit: async (data) => {
- const { commentId, content, richContent, dateForTimestamp } = data
+ const { commentId, content, richContent } = data
+
+ // the form's copy only refreshes on whole-second changes, so read the live position
+ // to keep this in step with milliseconds_into_recording below
+ const dateForTimestamp = values.dateForCurrentTimestamp
if (!dateForTimestamp) {
throw new Error('Cannot comment without a timestamp.')
@@ -323,4 +355,14 @@ export const playerCommentOverlayLogic = kea<playerCommentOverlayLogicType>([
},
},
})),
+ listeners(({ values }) => ({
+ // kea-forms discards anything thrown in submit, so a failed save is otherwise silent
+ submitRecordingCommentFailure: ({ error }) => {
+ if (values.recordingCommentHasErrors) {
+ // already rendered against the field
+ return
+ }
+ lemonToast.error(`Could not save your comment: ${error.message}`)
+ },
+ })),
])