Skip to content

Commit c999727

Browse files
committed
properly handle whether session recording was already in progress
1 parent e6e5e98 commit c999727

File tree

2 files changed

+82
-5
lines changed

2 files changed

+82
-5
lines changed

packages/browser/src/__tests__/extensions/feedback-recording.test.ts

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,16 @@ describe('FeedbackRecordingManager', () => {
3232
config: {
3333
api_host: 'https://test.com',
3434
token: 'test-token',
35-
},
35+
} as any,
3636
capture: jest.fn(),
3737
startSessionRecording: jest.fn(),
38+
stopSessionRecording: jest.fn(),
39+
sessionRecordingStarted: jest.fn(),
3840
get_session_id: jest.fn().mockReturnValue('mock-session-id'),
3941
_send_request: jest.fn(),
4042
requestRouter: {
4143
endpointFor: jest.fn().mockReturnValue('https://test.com/api/feedback/audio'),
42-
},
44+
} as any,
4345
})
4446

4547
audioRecorderMock = {
@@ -166,6 +168,8 @@ describe('FeedbackRecordingManager', () => {
166168
let onRecordingEnded: jest.Mock
167169

168170
beforeEach(async () => {
171+
jest.spyOn(instance, 'sessionRecordingStarted').mockReturnValue(false)
172+
169173
onRecordingEnded = jest.fn()
170174
await manager.launchFeedbackRecordingUI(onRecordingEnded)
171175

@@ -214,6 +218,22 @@ describe('FeedbackRecordingManager', () => {
214218
$feedback_recording_id: feedbackId,
215219
})
216220
})
221+
222+
it('starts session recording if not already active', async () => {
223+
jest.spyOn(instance, 'sessionRecordingStarted').mockReturnValue(false)
224+
225+
feedbackId = await handleStartRecording()
226+
227+
expect(instance.startSessionRecording).toHaveBeenCalledWith(true)
228+
})
229+
230+
it('does not start session recording if already active', async () => {
231+
jest.spyOn(instance, 'sessionRecordingStarted').mockReturnValue(true)
232+
233+
feedbackId = await handleStartRecording()
234+
235+
expect(instance.startSessionRecording).not.toHaveBeenCalled()
236+
})
217237
})
218238

219239
describe('stop', () => {
@@ -334,6 +354,24 @@ describe('FeedbackRecordingManager', () => {
334354
expect(instance._send_request).not.toHaveBeenCalled()
335355
expect(onRecordingEnded).toHaveBeenCalledTimes(1)
336356
})
357+
358+
it('stops session recording when stopping if we started it', async () => {
359+
jest.spyOn(instance, 'sessionRecordingStarted').mockReturnValue(false)
360+
361+
feedbackId = await handleStartRecording()
362+
await stopCallback(feedbackId)
363+
364+
expect(instance.stopSessionRecording).toHaveBeenCalled()
365+
})
366+
367+
it('does not stop session recording if we did not start it', async () => {
368+
jest.spyOn(instance, 'sessionRecordingStarted').mockReturnValue(true)
369+
370+
feedbackId = await handleStartRecording()
371+
await stopCallback(feedbackId)
372+
373+
expect(instance.stopSessionRecording).not.toHaveBeenCalled()
374+
})
337375
})
338376

339377
describe('cleanup', () => {
@@ -357,6 +395,26 @@ describe('FeedbackRecordingManager', () => {
357395
expect(manager.isFeedbackRecordingActive()).toBe(false)
358396
expect(manager.getCurrentFeedbackRecordingId()).toBeNull()
359397
})
398+
399+
it('stops session recording when stopping if we started it', async () => {
400+
jest.spyOn(instance, 'sessionRecordingStarted').mockReturnValue(false)
401+
402+
feedbackId = await handleStartRecording()
403+
404+
manager.cleanup()
405+
406+
expect(instance.stopSessionRecording).toHaveBeenCalled()
407+
})
408+
409+
it('does not stop session recording if we did not start it', async () => {
410+
jest.spyOn(instance, 'sessionRecordingStarted').mockReturnValue(true)
411+
412+
feedbackId = await handleStartRecording()
413+
414+
manager.cleanup()
415+
416+
expect(instance.stopSessionRecording).not.toHaveBeenCalled()
417+
})
360418
})
361419

362420
describe('UI focus after completion', () => {

packages/browser/src/extensions/feedback-recording.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export class FeedbackRecordingManager {
1717
private _isLoaded: boolean = false
1818
private _isLoading: boolean = false
1919
private _isUIActive: boolean = false
20+
private _didStartSessionRecording: boolean = false
2021

2122
constructor(
2223
private _instance: PostHog,
@@ -39,6 +40,11 @@ export class FeedbackRecordingManager {
3940
this._audioRecorder.cancelRecording()
4041
}
4142

43+
if (this._didStartSessionRecording) {
44+
this._instance.stopSessionRecording()
45+
logger.info('Stopped session recording during cleanup')
46+
}
47+
4248
this._resetState()
4349
removeFeedbackRecordingUIFromDOM()
4450

@@ -127,9 +133,16 @@ export class FeedbackRecordingManager {
127133
logger.warn('Failed to start audio recording:', error)
128134
}
129135

130-
//TODO: at the moment always just start recording - we can mess with this later
131-
// by storing whether reocrding is already in progress so we know whether to stop it later
132-
this._instance.startSessionRecording(true)
136+
// Start session recording if it's not already active
137+
const wasSessionRecordingActive = this._instance.sessionRecordingStarted()
138+
if (!wasSessionRecordingActive) {
139+
this._instance.startSessionRecording(true)
140+
this._didStartSessionRecording = true
141+
logger.info('Started session recording for feedback')
142+
} else {
143+
this._didStartSessionRecording = false
144+
logger.info('Session recording already active, reusing existing recording')
145+
}
133146

134147
return feedbackId
135148
}
@@ -187,6 +200,11 @@ export class FeedbackRecordingManager {
187200
feedbackId: string,
188201
onRecordingEnded: (result: UserFeedbackRecordingResult) => void
189202
): Promise<void> {
203+
if (this._didStartSessionRecording) {
204+
this._instance.stopSessionRecording()
205+
logger.info('Stopped session recording after feedback recording ended')
206+
}
207+
190208
const recordingResult = await this._audioRecorder.stopRecording()
191209

192210
if (recordingResult?.blob) {
@@ -221,6 +239,7 @@ export class FeedbackRecordingManager {
221239
private _resetState(): void {
222240
this._feedbackRecordingId = null
223241
this._isUIActive = false
242+
this._didStartSessionRecording = false
224243
}
225244
}
226245

0 commit comments

Comments
 (0)