: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(workflows): warn before discarding unsaved message template (#66812)
Harley Alexanderc763aeff1e09
diff --git a/products/workflows/frontend/TemplateLibrary/messageTemplateLogic.test.ts b/products/workflows/frontend/TemplateLibrary/messageTemplateLogic.test.tsnew file mode 100644index 000000000000..a1b195ac7568--- /dev/null+++ b/products/workflows/frontend/TemplateLibrary/messageTemplateLogic.test.ts@@ -0,0 +1,44 @@+import { router } from 'kea-router'+import { expectLogic } from 'kea-test-utils'++import { initKeaTests } from '~/test/init'++import { messageTemplateLogic } from './messageTemplateLogic'++describe('messageTemplateLogic unsaved-changes guard', () => {+ let logic: ReturnType<typeof messageTemplateLogic.build>+ let confirmSpy: jest.SpyInstance++ beforeEach(() => {+ initKeaTests()+ // window.confirm is the boundary kea-router calls to block in-app navigation.+ confirmSpy = jest.spyOn(window, 'confirm').mockReturnValue(true)+ logic = messageTemplateLogic({ id: 'new' })+ logic.mount()+ })++ afterEach(() => {+ logic.unmount()+ confirmSpy.mockRestore()+ })++ it.each([+ { description: 'blocks navigation away from an unsaved new template', changed: true, expectedCalls: 1 },+ {+ description: 'does not block navigation when there are no unsaved changes',+ changed: false,+ expectedCalls: 0,+ },+ ])('$description', async ({ changed, expectedCalls }) => {+ if (changed) {+ logic.actions.setTemplateValue('name', 'My one-hour template')+ await expectLogic(logic).toMatchValues({ templateChanged: true })+ } else {+ await expectLogic(logic).toMatchValues({ templateChanged: false })+ }++ router.actions.push('/workflows/library')++ expect(confirmSpy).toHaveBeenCalledTimes(expectedCalls)+ })+})diff --git a/products/workflows/frontend/TemplateLibrary/messageTemplateLogic.ts b/products/workflows/frontend/TemplateLibrary/messageTemplateLogic.tsindex d3178329f074..bee180ad30fe 100644--- a/products/workflows/frontend/TemplateLibrary/messageTemplateLogic.ts+++ b/products/workflows/frontend/TemplateLibrary/messageTemplateLogic.ts@@ -1,7 +1,7 @@import { actions, afterMount, connect, kea, key, listeners, path, props, reducers, selectors } from 'kea'import { forms } from 'kea-forms'import { loaders } from 'kea-loaders'-import { router } from 'kea-router'+import { beforeUnload, router } from 'kea-router'import api from 'lib/api'import { lemonToast } from 'lib/lemon-ui/LemonToast'@@ -108,9 +108,11 @@ export const messageTemplateLogic = kea<messageTemplateLogicType>([},saveTemplateSuccess: async ({ template }) => {lemonToast.success('Template saved')- template.id && router.actions.replace(urls.workflowsLibraryTemplate(template.id))+ // Clear the unsaved-changes state before navigating so the beforeUnload guard+ // does not intercept the post-save redirect.actions.resetTemplate(template)actions.setOriginalTemplate(template)+ template.id && router.actions.replace(urls.workflowsLibraryTemplate(template.id))},loadMessageSuccess: async ({ message }) => {if (!message) {@@ -173,4 +175,20 @@ export const messageTemplateLogic = kea<messageTemplateLogicType>([actions.resetTemplate(NEW_TEMPLATE)}}),+ beforeUnload(({ values, actions }) => ({+ // Guards both new and existing templates - a brand-new template only exists in form+ // state until "Create" is pressed, so leaving without saving would discard it entirely.+ enabled: (newLocation) => {+ if (!values.templateChanged) {+ return false+ }+ // Allow same-path changes (e.g. query params) through without warning.+ if (newLocation && newLocation.pathname === router.values.location.pathname) {+ return false+ }+ return true+ },+ message: 'Leave template?\nChanges you made will be discarded.',+ onConfirm: () => actions.resetTemplate(values.originalTemplate),+ })),])