Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Chore/unsaved changes auth template (#29296)

Terry Suttona6b2940dd7ec
diff --git a/apps/studio/components/interfaces/Auth/EmailTemplates/TemplateEditor.tsx b/apps/studio/components/interfaces/Auth/EmailTemplates/TemplateEditor.tsx
index 03830bf765a..13eea3e7e65 100644
--- a/apps/studio/components/interfaces/Auth/EmailTemplates/TemplateEditor.tsx
+++ b/apps/studio/components/interfaces/Auth/EmailTemplates/TemplateEditor.tsx
@@ -1,5 +1,5 @@
import { PermissionAction } from '@supabase/shared-types/out/constants'
-import { useMemo, useState } from 'react'
+import { useEffect, useMemo, useState } from 'react'
import ReactMarkdown from 'react-markdown'
import { toast } from 'sonner'
@@ -38,6 +38,8 @@ const TemplateEditor = ({ template, authConfig }: TemplateEditorProps) => {
const messageSlug = `MAILER_TEMPLATES_${id}_CONTENT`
const messageProperty = properties[messageSlug]
const [bodyValue, setBodyValue] = useState((authConfig && authConfig[messageSlug]) ?? '')
+ const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false)
+
const onSubmit = (values: any, { resetForm }: any) => {
const payload = { ...values }
@@ -56,11 +58,27 @@ const TemplateEditor = ({ template, authConfig }: TemplateEditorProps) => {
values: values,
initialValues: values,
})
+ setHasUnsavedChanges(false) // Reset the unsaved changes state
},
}
)
}
+ useEffect(() => {
+ const handleBeforeUnload = (e: BeforeUnloadEvent) => {
+ if (hasUnsavedChanges) {
+ e.preventDefault()
+ e.returnValue = '' // deprecated, but older browsers still require this
+ }
+ }
+
+ window.addEventListener('beforeunload', handleBeforeUnload)
+
+ return () => {
+ window.removeEventListener('beforeunload', handleBeforeUnload)
+ }
+ }, [hasUnsavedChanges])
+
return (
<Form id={formId} className="!border-t-0" initialValues={INITIAL_VALUES} onSubmit={onSubmit}>
{({ resetForm, values, initialValues }: any) => {
@@ -135,7 +153,12 @@ const TemplateEditor = ({ template, authConfig }: TemplateEditorProps) => {
language="html"
isReadOnly={!canUpdateConfig}
className="!mb-0 h-96 overflow-hidden rounded border"
- onInputChange={(e: string | undefined) => setBodyValue(e ?? '')}
+ onInputChange={(e: string | undefined) => {
+ setBodyValue(e ?? '')
+ if (bodyValue !== e) {
+ setHasUnsavedChanges(true)
+ }
+ }}
options={{ wordWrap: 'off', contextmenu: false }}
value={bodyValue}
/>