Skip to content

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

fix(studio): map email template anchors to docs (#46952)

Danny White7e25c27e021e
diff --git a/apps/studio/components/interfaces/Auth/EmailTemplates/EmailTemplates.constants.test.ts b/apps/studio/components/interfaces/Auth/EmailTemplates/EmailTemplates.constants.test.ts
new file mode 100644
index 00000000000..0efbcd78e99
--- /dev/null
+++ b/apps/studio/components/interfaces/Auth/EmailTemplates/EmailTemplates.constants.test.ts
@@ -0,0 +1,45 @@
+import { describe, expect, it } from 'vitest'
+
+import { EMAIL_TEMPLATE_DOCS_ANCHORS } from './EmailTemplates.constants'
+import { AUTH_TEMPLATE_TYPES, type AuthTemplateType } from './EmailTemplates.types'
+
+/** Docs section headings from customizing-email-templates.mdx */
+const DOCS_HEADINGS: Record<AuthTemplateType, string> = {
+ CONFIRMATION: 'auth.email.template.confirmation',
+ INVITE: 'auth.email.template.invite',
+ MAGIC_LINK: 'auth.email.template.magic_link',
+ EMAIL_CHANGE: 'auth.email.template.email_change',
+ RECOVERY: 'auth.email.template.recovery',
+ REAUTHENTICATION: 'auth.email.template.reauthentication',
+ PASSWORD_CHANGED_NOTIFICATION: 'auth.email.notification.password_changed',
+ EMAIL_CHANGED_NOTIFICATION: 'auth.email.notification.email_changed',
+ PHONE_CHANGED_NOTIFICATION: 'auth.email.notification.phone_changed',
+ IDENTITY_LINKED_NOTIFICATION: 'auth.email.notification.identity_linked',
+ IDENTITY_UNLINKED_NOTIFICATION: 'auth.email.notification.identity_unlinked',
+ MFA_FACTOR_ENROLLED_NOTIFICATION: 'auth.email.notification.mfa_factor_enrolled',
+ MFA_FACTOR_UNENROLLED_NOTIFICATION: 'auth.email.notification.mfa_factor_unenrolled',
+}
+
+/**
+ * Matches docs `Heading` anchor generation in
+ * packages/ui/src/components/CustomHTMLElements/CustomHTMLElements.utils.ts
+ */
+function docsHeadingToAnchor(heading: string) {
+ return heading
+ .toLowerCase()
+ .trim()
+ .replace(/[^a-z0-9- ]/g, '')
+ .replace(/[ ]/g, '-')
+}
+
+describe('EmailTemplates.constants: EMAIL_TEMPLATE_DOCS_ANCHORS', () => {
+ it('covers every auth template type', () => {
+ expect(Object.keys(EMAIL_TEMPLATE_DOCS_ANCHORS).sort()).toEqual([...AUTH_TEMPLATE_TYPES].sort())
+ })
+
+ it.each(AUTH_TEMPLATE_TYPES)('matches docs heading slug for %s', (templateType) => {
+ const expectedAnchor = docsHeadingToAnchor(DOCS_HEADINGS[templateType])
+
+ expect(EMAIL_TEMPLATE_DOCS_ANCHORS[templateType]).toBe(expectedAnchor)
+ })
+})
diff --git a/apps/studio/components/interfaces/Auth/EmailTemplates/EmailTemplates.constants.ts b/apps/studio/components/interfaces/Auth/EmailTemplates/EmailTemplates.constants.ts
index 3885b5ab234..ad52828a963 100644
--- a/apps/studio/components/interfaces/Auth/EmailTemplates/EmailTemplates.constants.ts
+++ b/apps/studio/components/interfaces/Auth/EmailTemplates/EmailTemplates.constants.ts
@@ -1,5 +1,26 @@
-import { AUTH_TEMPLATE_TYPES, type AuthTemplateResetType } from './EmailTemplates.types'
+import {
+ AUTH_TEMPLATE_TYPES,
+ type AuthTemplateResetType,
+ type AuthTemplateType,
+} from './EmailTemplates.types'
import { getAuthTemplateType } from './EmailTemplates.utils'
export const AUTH_TEMPLATE_RESET_TYPES: AuthTemplateResetType[] =
AUTH_TEMPLATE_TYPES.map(getAuthTemplateType)
+
+/** Docs heading anchors for customizing-email-templates.mdx (ui Heading slugify output). */
+export const EMAIL_TEMPLATE_DOCS_ANCHORS = {
+ CONFIRMATION: 'authemailtemplateconfirmation',
+ INVITE: 'authemailtemplateinvite',
+ MAGIC_LINK: 'authemailtemplatemagiclink',
+ EMAIL_CHANGE: 'authemailtemplateemailchange',
+ RECOVERY: 'authemailtemplaterecovery',
+ REAUTHENTICATION: 'authemailtemplatereauthentication',
+ PASSWORD_CHANGED_NOTIFICATION: 'authemailnotificationpasswordchanged',
+ EMAIL_CHANGED_NOTIFICATION: 'authemailnotificationemailchanged',
+ PHONE_CHANGED_NOTIFICATION: 'authemailnotificationphonechanged',
+ IDENTITY_LINKED_NOTIFICATION: 'authemailnotificationidentitylinked',
+ IDENTITY_UNLINKED_NOTIFICATION: 'authemailnotificationidentityunlinked',
+ MFA_FACTOR_ENROLLED_NOTIFICATION: 'authemailnotificationmfafactorenrolled',
+ MFA_FACTOR_UNENROLLED_NOTIFICATION: 'authemailnotificationmfafactorunenrolled',
+} as const satisfies Record<AuthTemplateType, string>
diff --git a/apps/studio/pages/project/[ref]/auth/templates/[templateId].tsx b/apps/studio/pages/project/[ref]/auth/templates/[templateId].tsx
index baa9cd6665e..0f4bc4fd79c 100644
--- a/apps/studio/pages/project/[ref]/auth/templates/[templateId].tsx
+++ b/apps/studio/pages/project/[ref]/auth/templates/[templateId].tsx
@@ -37,6 +37,7 @@ import * as z from 'zod'
import { TEMPLATES_SCHEMAS } from '@/components/interfaces/Auth/EmailTemplates/AuthTemplatesValidation'
import { CustomEmailTemplateRestrictionAdmonition } from '@/components/interfaces/Auth/EmailTemplates/CustomEmailTemplateRestrictionAdmonition'
+import { EMAIL_TEMPLATE_DOCS_ANCHORS } from '@/components/interfaces/Auth/EmailTemplates/EmailTemplates.constants'
import {
isCustomEmailTemplateEditingRestricted,
isCustomEmailTemplateRestrictionStatusKnown,
@@ -106,10 +107,6 @@ const RedirectToTemplates = () => {
? TEMPLATES_SCHEMAS.find((template) => slugifyTitle(template.title) === templateId)
: null
- // Convert templateId slug to one lowercase word to match docs anchor tag
- const templateIdForDocs =
- typeof templateId === 'string' ? templateId.replace(/-/g, '').toLowerCase() : ''
-
// Determine if this is a security notification template
const isSecurityTemplate = template?.misc?.emailTemplateType === 'security'
@@ -205,7 +202,7 @@ const RedirectToTemplates = () => {
</PageHeaderSummary>
<PageHeaderAside>
<DocsButton
- href={`${DOCS_URL}/guides/local-development/customizing-email-templates#${isSecurityTemplate ? 'security' : 'auth'}emailtemplate${templateIdForDocs}`}
+ href={`${DOCS_URL}/guides/local-development/customizing-email-templates#${EMAIL_TEMPLATE_DOCS_ANCHORS[template.id]}`}
/>
</PageHeaderAside>
</PageHeaderMeta>