ResetTemplateDialog.tsx 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { PermissionAction } from '@supabase/shared-types/out/constants'
  2. import { useParams } from 'common'
  3. import { useState } from 'react'
  4. import { toast } from 'sonner'
  5. import {
  6. AlertDialog,
  7. AlertDialogCancel,
  8. AlertDialogContent,
  9. AlertDialogDescription,
  10. AlertDialogFooter,
  11. AlertDialogHeader,
  12. AlertDialogTitle,
  13. AlertDialogTrigger,
  14. Button,
  15. } from 'ui'
  16. import { type AuthTemplate } from './EmailTemplates.types'
  17. import { getAuthTemplateType } from './EmailTemplates.utils'
  18. import { AuthConfigResponse } from '@/data/auth/auth-config-query'
  19. import { useAuthTemplateResetMutation } from '@/data/auth/auth-template-reset-mutation'
  20. import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions'
  21. export const ResetTemplateDialog = ({
  22. template,
  23. hasUnsavedChanges,
  24. onResetSuccess,
  25. }: {
  26. template: AuthTemplate
  27. hasUnsavedChanges: boolean
  28. onResetSuccess: (config: AuthConfigResponse) => void
  29. }) => {
  30. const { ref: projectRef } = useParams()
  31. const [open, setOpen] = useState(false)
  32. const { can: canUpdateConfig } = useAsyncCheckPermissions(
  33. PermissionAction.UPDATE,
  34. 'custom_config_gotrue'
  35. )
  36. const { id } = template
  37. const templateType = getAuthTemplateType(id)
  38. const { mutate: resetAuthTemplate, isPending: isResettingTemplate } =
  39. useAuthTemplateResetMutation()
  40. const resetTemplateToDefault = () => {
  41. if (!projectRef) return console.error('Project ref is required')
  42. if (!templateType) return console.error('Template type is required')
  43. resetAuthTemplate(
  44. {
  45. projectRef,
  46. template: templateType,
  47. },
  48. {
  49. onSuccess: (config) => {
  50. toast.success('Email template reset to default')
  51. onResetSuccess(config)
  52. setOpen(false)
  53. },
  54. }
  55. )
  56. }
  57. return (
  58. <AlertDialog open={open} onOpenChange={setOpen}>
  59. <AlertDialogTrigger asChild>
  60. <Button type="default" htmlType="button" disabled={!canUpdateConfig || isResettingTemplate}>
  61. Reset template
  62. </Button>
  63. </AlertDialogTrigger>
  64. <AlertDialogContent>
  65. <AlertDialogHeader>
  66. <AlertDialogTitle>Reset template to default</AlertDialogTitle>
  67. <AlertDialogDescription>
  68. {hasUnsavedChanges
  69. ? 'This will discard your unsaved changes and use the default subject line and email body content.'
  70. : 'This will remove your custom subject line and email body content. The default values will be used instead.'}
  71. </AlertDialogDescription>
  72. </AlertDialogHeader>
  73. <AlertDialogFooter>
  74. <AlertDialogCancel disabled={isResettingTemplate}>Cancel</AlertDialogCancel>
  75. <Button type="warning" onClick={resetTemplateToDefault} loading={isResettingTemplate}>
  76. Reset
  77. </Button>
  78. </AlertDialogFooter>
  79. </AlertDialogContent>
  80. </AlertDialog>
  81. )
  82. }