RemoveRestrictionModal.tsx 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { useParams } from 'common'
  2. import { toast } from 'sonner'
  3. import { Button, Modal } from 'ui'
  4. import { Admonition } from 'ui-patterns'
  5. import { useNetworkRestrictionsQuery } from '@/data/network-restrictions/network-restrictions-query'
  6. import { useNetworkRestrictionsApplyMutation } from '@/data/network-restrictions/network-retrictions-apply-mutation'
  7. interface RemoveRestrictionModalProps {
  8. visible: boolean
  9. selectedRestriction?: string
  10. onClose: () => void
  11. }
  12. const RemoveRestrictionModal = ({
  13. visible,
  14. selectedRestriction,
  15. onClose,
  16. }: RemoveRestrictionModalProps) => {
  17. const { ref } = useParams()
  18. const { data } = useNetworkRestrictionsQuery({ projectRef: ref }, { enabled: visible })
  19. const ipv4Restrictions = data?.config?.dbAllowedCidrs ?? []
  20. // @ts-ignore [Joshen] API typing issue
  21. const ipv6Restrictions: string[] = data?.config?.dbAllowedCidrsV6 ?? []
  22. const restrictedIps = ipv4Restrictions.concat(ipv6Restrictions)
  23. const { mutate: applyNetworkRestrictions, isPending: isApplying } =
  24. useNetworkRestrictionsApplyMutation({
  25. onSuccess: () => onClose(),
  26. onError: (error) => {
  27. toast.error(`Failed to remove restriction: ${error.message}`)
  28. },
  29. })
  30. const isRemovingOnlyRestriction =
  31. restrictedIps.length === 1 && restrictedIps[0] === selectedRestriction
  32. const onSubmit = async () => {
  33. if (!ref) return console.error('Project ref is required')
  34. if (!selectedRestriction) return console.error('Missing selected restriction')
  35. const dbAllowedCidrs = ipv4Restrictions.includes(selectedRestriction)
  36. ? ipv4Restrictions.filter((ip) => ip !== selectedRestriction)
  37. : ipv4Restrictions
  38. const dbAllowedCidrsV6 = ipv6Restrictions.includes(selectedRestriction)
  39. ? ipv6Restrictions.filter((ip) => ip !== selectedRestriction)
  40. : ipv6Restrictions
  41. if (dbAllowedCidrs.length === 0 && dbAllowedCidrsV6.length === 0) {
  42. applyNetworkRestrictions({
  43. projectRef: ref,
  44. dbAllowedCidrs: ['0.0.0.0/0'],
  45. dbAllowedCidrsV6: ['::/0'],
  46. })
  47. } else {
  48. applyNetworkRestrictions({ projectRef: ref, dbAllowedCidrs, dbAllowedCidrsV6 })
  49. }
  50. }
  51. return (
  52. <Modal
  53. hideFooter
  54. size="medium"
  55. visible={visible}
  56. onCancel={onClose}
  57. header="Confirm to remove restriction"
  58. >
  59. <Modal.Content className="space-y-4">
  60. <p className="text-sm text-foreground-light">
  61. The IPv4 address <code className="text-code-inline">{selectedRestriction}</code> will be
  62. removed from your list of network restrictions
  63. {isRemovingOnlyRestriction
  64. ? '.'
  65. : ", and no longer have access to your project's database."}
  66. </p>
  67. {isRemovingOnlyRestriction && (
  68. <Admonition
  69. type="warning"
  70. title="Database access will no longer be restricted"
  71. description="Removing all network restrictions will default to your database being accessible from
  72. all IP addresses."
  73. />
  74. )}
  75. </Modal.Content>
  76. <Modal.Content className="flex items-center justify-end space-x-2">
  77. <Button type="default" disabled={isApplying} onClick={() => onClose()}>
  78. Cancel
  79. </Button>
  80. <Button loading={isApplying} disabled={isApplying} onClick={() => onSubmit()}>
  81. Remove restriction
  82. </Button>
  83. </Modal.Content>
  84. </Modal>
  85. )
  86. }
  87. export default RemoveRestrictionModal