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.

refactor(surveys): drop #preserveLocalChanges hash flag, always preserve state (#58587)

Lucas Faria76ec20c4c856
diff --git a/frontend/src/scenes/surveys/SurveyEdit.tsx b/frontend/src/scenes/surveys/SurveyEdit.tsx
index 31d14d7fa624..3986fc82a3d5 100644
--- a/frontend/src/scenes/surveys/SurveyEdit.tsx
+++ b/frontend/src/scenes/surveys/SurveyEdit.tsx
@@ -436,7 +436,7 @@ export default function SurveyEdit({ id }: { id: string }): JSX.Element {
data-attr="switch-to-wizard"
type="tertiary"
size="small"
- to={`${urls.surveyWizard(id)}#preserveLocalChanges=true`}
+ to={urls.surveyWizard(id)}
onClick={() => setPreferredEditor('guided')}
>
Guided editor
diff --git a/frontend/src/scenes/surveys/surveyLogic.test.ts b/frontend/src/scenes/surveys/surveyLogic.test.ts
index 5aae47dd16ef..38e28f8abc37 100644
--- a/frontend/src/scenes/surveys/surveyLogic.test.ts
+++ b/frontend/src/scenes/surveys/surveyLogic.test.ts
@@ -148,7 +148,7 @@ describe('editor sync', () => {
surveyChanged: true,
})
- router.actions.push('/surveys/test-survey?edit=true#preserveLocalChanges=true')
+ router.actions.push('/surveys/test-survey?edit=true')
await expectLogic(logic).toFinishAllListeners()
diff --git a/frontend/src/scenes/surveys/surveyLogic.tsx b/frontend/src/scenes/surveys/surveyLogic.tsx
index 0130ac21bb1f..ffa667ce7b92 100644
--- a/frontend/src/scenes/surveys/surveyLogic.tsx
+++ b/frontend/src/scenes/surveys/surveyLogic.tsx
@@ -3207,8 +3207,10 @@ export const surveyLogic = kea<surveyLogicType>([
],
})),
afterMount(({ props, actions, values }) => {
- const shouldPreserveLocalChanges =
- router.values.hashParams.preserveLocalChanges && values.surveyChanged && values.survey.id === props.id
+ // Preserve any in-memory edits when re-mounting on the same survey id (e.g.
+ // navigating between the guided wizard and the full editor). No URL flag —
+ // surveyChanged is in-memory only, so a fresh session can never trigger this.
+ const shouldPreserveLocalChanges = values.surveyChanged && values.survey.id === props.id
if (props.id !== 'new' && !shouldPreserveLocalChanges) {
actions.loadSurvey()
diff --git a/frontend/src/scenes/surveys/wizard/SurveyWizard.tsx b/frontend/src/scenes/surveys/wizard/SurveyWizard.tsx
index c0345edb0037..aa9b284fa48c 100644
--- a/frontend/src/scenes/surveys/wizard/SurveyWizard.tsx
+++ b/frontend/src/scenes/surveys/wizard/SurveyWizard.tsx
@@ -78,8 +78,8 @@ function SurveyWizard({ id }: SurveyWizardLogicProps): JSX.Element {
// Redirect existing surveys that use wizard-unsupported fields to the full
// editor. Brand-new surveys should always start on template selection,
// regardless of the user's editor preference.
- // Hash-carrying deep links (#fromTemplate, #preserveLocalChanges) are
- // respected and bypass the redirect.
+ // Hash-carrying deep links (e.g. #fromTemplate) are respected and bypass the
+ // redirect.
useEffect(() => {
if (window.location.hash) {
return
@@ -124,9 +124,9 @@ function SurveyWizard({ id }: SurveyWizardLogicProps): JSX.Element {
const handleCustomizeMore = (): void => {
setPreferredEditor('full')
- const target = isEditing
- ? `${urls.survey(id)}?edit=true#preserveLocalChanges=true`
- : `${urls.survey(id)}#fromTemplate=true&preserveLocalChanges=true`
+ // Unsaved edits are preserved automatically by surveyLogic when the route
+ // re-mounts with the same survey id — no URL flag needed.
+ const target = isEditing ? `${urls.survey(id)}?edit=true` : `${urls.survey(id)}#fromTemplate=true`
router.actions.push(target)
}
diff --git a/frontend/src/scenes/surveys/wizard/surveyWizardLogic.test.ts b/frontend/src/scenes/surveys/wizard/surveyWizardLogic.test.ts
index 84eb1aa480b6..3c4c318722b1 100644
--- a/frontend/src/scenes/surveys/wizard/surveyWizardLogic.test.ts
+++ b/frontend/src/scenes/surveys/wizard/surveyWizardLogic.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 { ProductIntentContext, ProductKey } from '~/queries/schema/schema-general'
import { initKeaTests } from '~/test/init'
@@ -241,7 +243,7 @@ describe('surveyWizardLogic', () => {
surveyChanged: true,
})
- router.actions.push('/surveys/guided/new#preserveLocalChanges=true')
+ router.actions.push('/surveys/guided/new')
const logic = surveyWizardLogic({ id: 'new' })
logic.mount()
@@ -265,14 +267,14 @@ describe('surveyWizardLogic', () => {
surveyChanged: true,
})
- router.actions.push('/surveys/new#preserveLocalChanges=true')
+ router.actions.push('/surveys/new')
unmountWizard()
expect(surveyFormLogic.values.survey.description).toBe('Edited in the guided editor')
expect(surveyFormLogic.values.surveyChanged).toBe(true)
})
- it('resets new survey state when leaving the guided editor without preserveLocalChanges', async () => {
+ it('resets new survey state when navigating away from the survey editor', async () => {
const surveyFormLogic = surveyLogic({ id: 'new' })
surveyFormLogic.mount()
@@ -290,6 +292,28 @@ describe('surveyWizardLogic', () => {
expect(surveyFormLogic.values.survey.description).toBe('')
})
+
+ it('preserves the in-memory draft when switching to the full editor without any URL flag', async () => {
+ // Regression test: previously the wizard had to push `#preserveLocalChanges=true`
+ // to keep state across editor swaps. Verify the flag-free path works.
+ const surveyFormLogic = surveyLogic({ id: 'new' })
+ surveyFormLogic.mount()
+
+ const wizardLogic = surveyWizardLogic({ id: 'new' })
+ const unmountWizard = wizardLogic.mount()
+
+ await expectLogic(wizardLogic, () => {
+ wizardLogic.actions.setSurveyValue('name', 'Drafted in the wizard')
+ }).toMatchValues({
+ surveyChanged: true,
+ })
+
+ router.actions.push(urls.survey('new'))
+ unmountWizard()
+
+ expect(surveyFormLogic.values.survey.name).toBe('Drafted in the wizard')
+ expect(surveyFormLogic.values.surveyChanged).toBe(true)
+ })
})
describe('template selection', () => {
diff --git a/frontend/src/scenes/surveys/wizard/surveyWizardLogic.ts b/frontend/src/scenes/surveys/wizard/surveyWizardLogic.ts
index 218269e15cfd..b31292e278f4 100644
--- a/frontend/src/scenes/surveys/wizard/surveyWizardLogic.ts
+++ b/frontend/src/scenes/surveys/wizard/surveyWizardLogic.ts
@@ -387,8 +387,10 @@ export const surveyWizardLogic = kea<surveyWizardLogicType>([
})),
afterMount(({ actions, props, values }) => {
- const shouldPreserveLocalChanges =
- router.values.hashParams.preserveLocalChanges && values.surveyChanged && values.survey.id === props.id
+ // Preserve any in-memory edits when re-mounting on the same survey id (e.g.
+ // navigating between the full editor and the wizard). surveyChanged is
+ // in-memory only, so a fresh session can never trigger this branch.
+ const shouldPreserveLocalChanges = values.surveyChanged && values.survey.id === props.id
if (props.id === 'new') {
if (shouldPreserveLocalChanges) {
@@ -411,8 +413,18 @@ export const surveyWizardLogic = kea<surveyWizardLogicType>([
beforeUnmount(({ actions, props }) => {
actions.resetWizard()
- if (props.id === 'new' && !router.values.hashParams.preserveLocalChanges) {
- actions.resetSurvey()
+ // For an in-progress "new" survey, preserve the in-memory draft only if the
+ // user is navigating to the other survey-editor view for the same id (full
+ // editor for new). Anywhere else (list, dashboards, etc.) means the user
+ // abandoned the draft, so we clear it. Using `endsWith` so this still works
+ // when the app is mounted under a project-scoped path (`/project/<id>/...`).
+ if (props.id === 'new') {
+ const destination = router.values.location.pathname
+ const stayingInNewSurveyEditor =
+ destination.endsWith(urls.survey('new')) || destination.endsWith(urls.surveyWizard('new'))
+ if (!stayingInNewSurveyEditor) {
+ actions.resetSurvey()
+ }
}
}),
])