mirror of
https://github.com/supabase/supabase.git
synced 2026-06-23 02:11:02 +08:00
## What kind of change does this PR introduce? Feature. Resolves AUTH-879. ## What is the current behavior? Studio does not provide a way to reset an Auth email template back to the default subject and body once it has been customised. ## What is the new behavior? Studio shows a `Reset template` action when Platform reports that the selected Auth email template subject or body has been customised. The action opens a confirmation dialog, calls the dedicated Platform reset endpoint, and refreshes the editor with the default subject and body returned by the API. The Auth config save/reset mutations now run their user-facing success handling before refreshing Auth lint data, so the success toast and local editor cleanup are not delayed by lint refetches. ## Additional context Depends on supabase/platform#32417. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Reset email templates to defaults via a confirmation dialog; button appears when custom content is detected and respects update permissions. * Typed email-template definitions and improved template variable display and descriptions. * **Tests** * Added tests covering template reset visibility, confirmation flow, state updates, permission handling, and toast notifications. * **Documentation** * Example email template placeholders updated for internationalization and provider snippets. <!-- review_stack_entry_start --> [](https://app.coderabbit.ai/change-stack/supabase/supabase/pull/45572) <!-- review_stack_entry_end --> <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
68 lines
2.2 KiB
TypeScript
68 lines
2.2 KiB
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import type { ProjectAuthConfigData } from './auth-config-query'
|
|
import { authKeys } from './keys'
|
|
import { type AuthTemplateResetType } from '@/components/interfaces/Auth/EmailTemplates/EmailTemplates.types'
|
|
import { handleError, post } from '@/data/fetchers'
|
|
import { lintKeys } from '@/data/lint/keys'
|
|
import type { ResponseError, UseCustomMutationOptions } from '@/types'
|
|
|
|
type AuthTemplateResetVariables = {
|
|
projectRef: string
|
|
template: AuthTemplateResetType
|
|
}
|
|
|
|
async function resetAuthTemplate({ projectRef, template }: AuthTemplateResetVariables) {
|
|
const { data, error } = await post('/platform/auth/{ref}/templates/{template}/reset', {
|
|
params: { path: { ref: projectRef, template } },
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
type AuthTemplateResetData = Awaited<ReturnType<typeof resetAuthTemplate>>
|
|
|
|
export const useAuthTemplateResetMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<AuthTemplateResetData, ResponseError, AuthTemplateResetVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<AuthTemplateResetData, ResponseError, AuthTemplateResetVariables>({
|
|
mutationFn: (vars) => resetAuthTemplate(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
queryClient.setQueryData<ProjectAuthConfigData>(authKeys.authConfig(projectRef), data)
|
|
await queryClient.invalidateQueries({
|
|
queryKey: authKeys.authConfig(projectRef),
|
|
refetchType: 'none',
|
|
})
|
|
await onSuccess?.(data, variables, context)
|
|
|
|
void queryClient
|
|
.invalidateQueries({ queryKey: lintKeys.lint(projectRef) })
|
|
.then(() =>
|
|
queryClient.refetchQueries({
|
|
queryKey: lintKeys.lint(projectRef),
|
|
type: 'active',
|
|
})
|
|
)
|
|
.catch(() => undefined)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to reset email template: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|