: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
feat(replay-vision): warn before discarding unsaved scanner edits (#67327)
Arno Hillen3c2b8b3dd411
diff --git a/products/replay_vision/frontend/replay_scanners/replayScannerLogic.test.ts b/products/replay_vision/frontend/replay_scanners/replayScannerLogic.test.tsindex f3c323829852..db56199cb80f 100644--- a/products/replay_vision/frontend/replay_scanners/replayScannerLogic.test.ts+++ b/products/replay_vision/frontend/replay_scanners/replayScannerLogic.test.ts@@ -1,6 +1,8 @@import { router } from 'kea-router'import { expectLogic } from 'kea-test-utils'+import { urls } from 'scenes/urls'+import { useMocks } from '~/mocks/jest'import { initKeaTests } from '~/test/init'@@ -12,6 +14,7 @@ import {parseCsvParam,parseSortParam,replayScannerLogic,+ shouldGuardScannerNavigation,} from './replayScannerLogic'import { defaultScannerTemplates } from './scannerTemplates'import { ClassifierScanner, ReplayScanner, ScorerScanner } from './types'@@ -554,6 +557,37 @@ describe('replayScannerLogic', () => {})})+ describe('shouldGuardScannerNavigation', () => {+ const scannerId = 'abc-123'+ const configure = urls.replayVisionScannerConfigure(scannerId)+ const triggers = urls.replayVisionScannerTriggers(scannerId)+ const template = urls.replayVisionScannerTemplate(scannerId)+ const detail = urls.replayVision(scannerId)+ const base = { hasUnsavedChanges: true, isSubmitting: false, scannerId, currentPathname: configure }++ it.each([+ // Nothing to lose, or the editor is mid-submit (save / step advance redirects itself).+ ['no unsaved changes', { ...base, hasUnsavedChanges: false, nextPathname: '/insights' }, false],+ ['mid-submit redirect to detail', { ...base, isSubmitting: true, nextPathname: detail }, false],+ // Moving between the wizard's own steps keeps the same draft mounted.+ ['forward to triggers step', { ...base, nextPathname: triggers }, false],+ ['back to template step', { ...base, currentPathname: triggers, nextPathname: template }, false],+ // Only guard while actually inside this scanner's editor.+ ['not currently in the editor', { ...base, currentPathname: detail, nextPathname: '/insights' }, false],+ // Genuinely leaving the editor with unsaved edits.+ ['out to the detail page', { ...base, nextPathname: detail }, true],+ ['out to an unrelated scene', { ...base, nextPathname: '/insights' }, true],+ ['closing the tab (no next location)', { ...base, nextPathname: undefined }, true],+ [+ 'over to a different scanner’s editor',+ { ...base, nextPathname: urls.replayVisionScannerConfigure('other-id') },+ true,+ ],+ ])('%s', (_label, params, expected) => {+ expect(shouldGuardScannerNavigation(params)).toBe(expected)+ })+ })+describe('triggerOnDemandObservation', () => {it.each([{ name: 'empty string', input: '' },diff --git a/products/replay_vision/frontend/replay_scanners/replayScannerLogic.ts b/products/replay_vision/frontend/replay_scanners/replayScannerLogic.tsindex e40d149b9917..a45417153ce0 100644--- a/products/replay_vision/frontend/replay_scanners/replayScannerLogic.ts+++ b/products/replay_vision/frontend/replay_scanners/replayScannerLogic.ts@@ -1,7 +1,8 @@import equal from 'fast-deep-equal'import { actions, afterMount, isBreakpoint, kea, key, listeners, path, props, reducers, selectors } from 'kea'import { forms } from 'kea-forms'-import { actionToUrl, router, urlToAction } from 'kea-router'+import { actionToUrl, beforeUnload, router, urlToAction } from 'kea-router'+import { CombinedLocation } from 'kea-router/lib/utils'import { dayjs } from 'lib/dayjs'import { lemonToast } from 'lib/lemon-ui/LemonToast'@@ -1052,6 +1053,23 @@ export const replayScannerLogic = kea<replayScannerLogicType>([},})),+ beforeUnload(({ values, actions, props }) => ({+ enabled: (newLocation?: CombinedLocation) =>+ shouldGuardScannerNavigation({+ hasUnsavedChanges: values.hasUnsavedChanges,+ isSubmitting: values.isScannerSubmitting,+ scannerId: props.id,+ currentPathname: router.values.location.pathname,+ nextPathname: newLocation?.pathname,+ }),+ message: 'Leave scanner editor?\nChanges you made will be discarded.',+ onConfirm: () => {+ if (values.originalScanner) {+ actions.resetScanner(values.originalScanner)+ }+ },+ })),+afterMount(({ actions, props }) => {actions.loadScanner()if (props.id !== 'new') {@@ -1063,6 +1081,42 @@ export const replayScannerLogic = kea<replayScannerLogicType>([const TABLE_URL_PARAM_KEYS = ['page', 'sort', 'status', 'triggered_by', 'verdict', 'tags', 'recording_subject'] as const+/** The three step URLs of a scanner's editor wizard. */+function scannerEditorPaths(scannerId: string): string[] {+ return [+ urls.replayVisionScannerTemplate(scannerId),+ urls.replayVisionScannerConfigure(scannerId),+ urls.replayVisionScannerTriggers(scannerId),+ ]+}++/**+ * Whether leaving the current location should warn about losing unsaved scanner edits.+ * Only guards while actually inside this scanner's editor, and stays quiet for the+ * navigation the editor triggers itself: moving between its own steps, and the+ * programmatic redirect that a save/advance fires while submitting.+ */+export function shouldGuardScannerNavigation(params: {+ hasUnsavedChanges: boolean+ isSubmitting: boolean+ scannerId: string+ currentPathname: string+ nextPathname?: string+}): boolean {+ const { hasUnsavedChanges, isSubmitting, scannerId, currentPathname, nextPathname } = params+ if (!hasUnsavedChanges || isSubmitting) {+ return false+ }+ const editorPaths = scannerEditorPaths(scannerId)+ if (!editorPaths.includes(currentPathname)) {+ return false+ }+ if (nextPathname && editorPaths.includes(nextPathname)) {+ return false+ }+ return true+}+export function parseSortParam(value: string | undefined): ObservationsSorting | null {if (typeof value !== 'string' || value.length === 0) {return null