Skip to content

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

fix(studio): migrate email template reset dialog to async AlertDialog (#47705)

Danny Whitefa20667ec1c2
diff --git a/apps/studio/components/interfaces/Auth/EmailTemplates/ResetTemplateDialog.tsx b/apps/studio/components/interfaces/Auth/EmailTemplates/ResetTemplateDialog.tsx
index 6c54b1dd732..233e993f042 100644
--- a/apps/studio/components/interfaces/Auth/EmailTemplates/ResetTemplateDialog.tsx
+++ b/apps/studio/components/interfaces/Auth/EmailTemplates/ResetTemplateDialog.tsx
@@ -5,6 +5,7 @@ import { toast } from 'sonner'
import {
AlertDialog,
AlertDialogAction,
+ AlertDialogBody,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
@@ -14,6 +15,7 @@ import {
AlertDialogTrigger,
Button,
} from 'ui'
+import { Admonition } from 'ui-patterns/admonition'
import { type AuthTemplate } from './EmailTemplates.types'
import { getAuthTemplateType } from './EmailTemplates.utils'
@@ -32,6 +34,7 @@ export const ResetTemplateDialog = ({
}) => {
const { ref: projectRef } = useParams()
const [open, setOpen] = useState(false)
+ const [error, setError] = useState<string | null>(null)
const { can: canUpdateConfig } = useAsyncCheckPermissions(
PermissionAction.UPDATE,
'custom_config_gotrue'
@@ -40,25 +43,40 @@ export const ResetTemplateDialog = ({
const { id } = template
const templateType = getAuthTemplateType(id)
- const { mutate: resetAuthTemplate, isPending: isResetting } = useAuthTemplateResetMutation()
+ const { mutateAsync: resetAuthTemplate, isPending: isResetting } = useAuthTemplateResetMutation({
+ onSuccess: (config) => {
+ toast.success('Email template reset to default')
+ onResetSuccess(config)
+ },
+ onError: () => {},
+ })
const resetTemplateToDefault = async () => {
if (!projectRef) throw new Error('Project ref is required')
if (!templateType) throw new Error('Template type is required')
- resetAuthTemplate(
- { projectRef, template: templateType },
- {
- onSuccess: (config) => {
- toast.success('Email template reset to default')
- onResetSuccess(config)
- },
- }
- )
+ setError(null)
+
+ try {
+ await resetAuthTemplate({ projectRef, template: templateType })
+ } catch (error) {
+ setError(
+ error instanceof Error
+ ? error.message
+ : 'An unknown error occurred while resetting the template'
+ )
+ throw error
+ }
}
return (
- <AlertDialog open={open} onOpenChange={setOpen}>
+ <AlertDialog
+ open={open}
+ onOpenChange={(next) => {
+ setOpen(next)
+ if (!next) setError(null)
+ }}
+ >
<AlertDialogTrigger asChild>
<Button variant="default" type="button" disabled={!canUpdateConfig}>
Reset template
@@ -73,15 +91,21 @@ export const ResetTemplateDialog = ({
: 'This will remove your custom subject line and email body content. The default values will be used instead.'}
</AlertDialogDescription>
</AlertDialogHeader>
+ {error && (
+ <AlertDialogBody>
+ <Admonition
+ type="destructive"
+ title="Unable to reset email template"
+ description={error}
+ />
+ </AlertDialogBody>
+ )}
<AlertDialogFooter>
- <AlertDialogCancel>Cancel</AlertDialogCancel>
+ <AlertDialogCancel disabled={isResetting}>Cancel</AlertDialogCancel>
<AlertDialogAction
variant="warning"
loading={isResetting}
- onClick={(e) => {
- e.preventDefault()
- resetTemplateToDefault()
- }}
+ onClick={resetTemplateToDefault}
>
Reset
</AlertDialogAction>
diff --git a/apps/studio/components/interfaces/Auth/EmailTemplates/TemplateEditor.test.tsx b/apps/studio/components/interfaces/Auth/EmailTemplates/TemplateEditor.test.tsx
index 933268de677..eff4d0e4d77 100644
--- a/apps/studio/components/interfaces/Auth/EmailTemplates/TemplateEditor.test.tsx
+++ b/apps/studio/components/interfaces/Auth/EmailTemplates/TemplateEditor.test.tsx
@@ -124,7 +124,10 @@ const renderTemplateEditor = ({
})
useAsyncCheckPermissionsMock.mockReturnValue({ can: canUpdateConfig })
useAuthConfigUpdateMutationMock.mockReturnValue({ mutate: updateAuthConfigMock })
- useAuthTemplateResetMutationMock.mockReturnValue({ mutate: resetTemplateMock, isPending: false })
+ useAuthTemplateResetMutationMock.mockImplementation((options = {}) => ({
+ mutateAsync: (vars: unknown) => resetTemplateMock(vars, options),
+ isPending: false,
+ }))
return render(<TemplateEditor template={confirmationTemplate} />)
}
@@ -253,4 +256,22 @@ describe('TemplateEditor reset to default', () => {
expect(screen.getByRole('button', { name: 'Reset template' })).toBeDisabled()
})
+
+ it('keeps the dialog open and shows an inline error when reset fails', async () => {
+ const user = userEvent.setup()
+ resetTemplateMock.mockImplementation(async () => {
+ throw new Error('Reset endpoint unavailable')
+ })
+
+ renderTemplateEditor({ hasCustomBody: true })
+
+ await user.click(screen.getByRole('button', { name: 'Reset template' }))
+ const dialog = await screen.findByRole('alertdialog')
+ await user.click(within(dialog).getByRole('button', { name: 'Reset' }))
+
+ expect(await within(dialog).findByText('Reset endpoint unavailable')).toBeInTheDocument()
+ expect(within(dialog).getByText('Unable to reset email template')).toBeInTheDocument()
+ expect(within(dialog).getByRole('button', { name: 'Reset' })).toBeInTheDocument()
+ expect(toast.error).not.toHaveBeenCalled()
+ })
})