CustomDomainDelete.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { Trash } from 'lucide-react'
  2. import { useEffect, useState } from 'react'
  3. import { toast } from 'sonner'
  4. import { Button, Checkbox } from 'ui'
  5. import ConfirmationModal from 'ui-patterns/Dialogs/ConfirmationModal'
  6. import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
  7. import { DocsButton } from '@/components/ui/DocsButton'
  8. import Panel from '@/components/ui/Panel'
  9. import { useCustomDomainDeleteMutation } from '@/data/custom-domains/custom-domains-delete-mutation'
  10. import type { CustomDomainResponse } from '@/data/custom-domains/custom-domains-query'
  11. import { DOCS_URL } from '@/lib/constants'
  12. export type CustomDomainDeleteProps = {
  13. projectRef?: string
  14. customDomain: CustomDomainResponse
  15. }
  16. export const CustomDomainDelete = ({ projectRef, customDomain }: CustomDomainDeleteProps) => {
  17. const [removeCustomDomain, setRemoveCustomDomain] = useState(false)
  18. const [isDeleteConfirmModalVisible, setIsDeleteConfirmModalVisible] = useState(false)
  19. const { mutate: deleteCustomDomain, isPending: isDeletingCustomDomain } =
  20. useCustomDomainDeleteMutation({
  21. onSuccess: () => {
  22. toast.success(
  23. `Successfully deleted custom domain. Refresh your browser to see the changes.`
  24. )
  25. setIsDeleteConfirmModalVisible(false)
  26. },
  27. })
  28. const onDeleteCustomDomain = async () => {
  29. if (!projectRef) return console.error('Project ref is required')
  30. deleteCustomDomain({ projectRef, removeAddon: removeCustomDomain })
  31. }
  32. useEffect(() => {
  33. if (!isDeleteConfirmModalVisible) setRemoveCustomDomain(false)
  34. }, [isDeleteConfirmModalVisible])
  35. return (
  36. <>
  37. <Panel.Content>
  38. <div className="w-full space-y-2">
  39. <p className="text-xs text-foreground-light">Active custom domain:</p>
  40. <div className="flex items-center space-x-2">
  41. <code className="text-lg mx-0 flex items-center space-x-2">
  42. <div className="h-2 w-2 rounded-full bg-brand" />
  43. <span>
  44. <code className="text-code-inline">{customDomain.hostname}</code>
  45. </span>
  46. </code>
  47. </div>
  48. <p className="text-sm text-foreground-light">
  49. Your custom domain is currently active and is serving traffic
  50. </p>
  51. </div>
  52. </Panel.Content>
  53. <div className="w-full border-t border-muted" />
  54. <Panel.Content className="w-full">
  55. <div className="flex items-center justify-between">
  56. <DocsButton href={`${DOCS_URL}/guides/platform/custom-domains`} />
  57. <Button
  58. type="danger"
  59. icon={<Trash />}
  60. onClick={() => setIsDeleteConfirmModalVisible(true)}
  61. >
  62. Delete custom domain
  63. </Button>
  64. </div>
  65. </Panel.Content>
  66. <ConfirmationModal
  67. visible={isDeleteConfirmModalVisible}
  68. variant="destructive"
  69. title="Delete custom domain"
  70. confirmLabel="Delete"
  71. confirmLabelLoading="Deleting"
  72. loading={isDeletingCustomDomain}
  73. onCancel={() => setIsDeleteConfirmModalVisible(false)}
  74. onConfirm={onDeleteCustomDomain}
  75. >
  76. <p className="text-sm mb-4">
  77. Are you sure you want to delete the custom domain{' '}
  78. <code className="text-code-inline break-normal!">{customDomain.hostname}</code> for your
  79. project? You will need to re-verify this domain if you want to use it again.
  80. </p>
  81. <FormItemLayout
  82. isReactForm={false}
  83. layout="flex"
  84. name="removeCustomDomain"
  85. label="Also remove custom domain add-on"
  86. description="Stops the monthly add-on charge for this project"
  87. className="[&>div:first-child>button]:translate-y-0.5"
  88. >
  89. <Checkbox
  90. checked={removeCustomDomain}
  91. onCheckedChange={(value) => setRemoveCustomDomain(!!value)}
  92. />
  93. </FormItemLayout>
  94. </ConfirmationModal>
  95. </>
  96. )
  97. }