The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Refactor error handling for network restrictions mutations
Joshen Lim7d42c6990d6d
diff --git a/studio/components/interfaces/Settings/Database/NetworkRestrictions/AddRestrictionModal.tsx b/studio/components/interfaces/Settings/Database/NetworkRestrictions/AddRestrictionModal.tsxindex 6944c059b4b..0b6ec624e33 100644--- a/studio/components/interfaces/Settings/Database/NetworkRestrictions/AddRestrictionModal.tsx+++ b/studio/components/interfaces/Settings/Database/NetworkRestrictions/AddRestrictionModal.tsx@@ -1,31 +1,32 @@-import { FC, useState } from 'react'+import * as Tooltip from '@radix-ui/react-tooltip'import { Address4 } from 'ip-address'+import { useState } from 'react'import { Button, Form, IconHelpCircle, Input, Modal } from 'ui'-import * as Tooltip from '@radix-ui/react-tooltip'-import { useStore } from 'hooks'import { useParams } from 'common/hooks'import InformationBox from 'components/ui/InformationBox'-import { checkIfPrivate, getAddressEndRange } from './NetworkRestrictions.utils'import { useNetworkRestrictionsApplyMutation } from 'data/network-restrictions/network-retrictions-apply-mutation'+import { useStore } from 'hooks'+import { checkIfPrivate, getAddressEndRange } from './NetworkRestrictions.utils'-interface Props {+interface AddRestrictionModalProps {restrictedIps: string[]visible: booleanhasOverachingRestriction: booleanonClose: () => void}-const AddRestrictionModal: FC<Props> = ({+const AddRestrictionModal = ({restrictedIps,visible,hasOverachingRestriction,onClose,-}) => {+}: AddRestrictionModalProps) => {const formId = 'add-restriction-form'const { ui } = useStore()const { ref } = useParams()- const { mutateAsync: applyNetworkRestrictions } = useNetworkRestrictionsApplyMutation()+ const { mutateAsync: applyNetworkRestrictions, isLoading: isApplying } =+ useNetworkRestrictionsApplyMutation()const validate = (values: any) => {const errors: any = {}@@ -48,10 +49,9 @@ const AddRestrictionModal: FC<Props> = ({return errors}- const onSubmit = async (values: any, { setSubmitting }: any) => {+ const onSubmit = async (values: any) => {if (!ref) return console.error('Project ref is required')- setSubmitting(true)const cidr = `${values.ipAddress}/${values.cidrBlockSize}`const alreadyExists = restrictedIps.includes(cidr)if (alreadyExists) {@@ -63,17 +63,8 @@ const AddRestrictionModal: FC<Props> = ({const dbAllowedCidrs = hasOverachingRestriction ? [cidr] : [...restrictedIps, cidr]- try {- await applyNetworkRestrictions({ projectRef: ref, dbAllowedCidrs })- onClose()- } catch (error: any) {- ui.setNotification({- category: 'error',- message: `Failed to add restriction: ${error.message}`,- })- } finally {- setSubmitting(false)- }+ await applyNetworkRestrictions({ projectRef: ref, dbAllowedCidrs })+ onClose()}return (@@ -93,7 +84,7 @@ const AddRestrictionModal: FC<Props> = ({validate={validate}onSubmit={onSubmit}>- {({ isSubmitting, values, resetForm }: any) => {+ {({ values, resetForm }: any) => {const isPrivate = Address4.isValid(values.ipAddress)? checkIfPrivate(values.ipAddress): false@@ -231,10 +222,10 @@ const AddRestrictionModal: FC<Props> = ({</Modal.Content>)}<div className="flex items-center justify-end px-6 py-4 border-t space-x-2">- <Button type="default" disabled={isSubmitting} onClick={() => onClose()}>+ <Button type="default" disabled={isApplying} onClick={() => onClose()}>Cancel</Button>- <Button htmlType="submit" loading={isSubmitting} disabled={isSubmitting}>+ <Button htmlType="submit" loading={isApplying} disabled={isApplying}>Save restriction</Button></div>diff --git a/studio/components/interfaces/Settings/Database/NetworkRestrictions/AllowAllModal.tsx b/studio/components/interfaces/Settings/Database/NetworkRestrictions/AllowAllModal.tsxindex 880c176f9bb..714d31c1e51 100644--- a/studio/components/interfaces/Settings/Database/NetworkRestrictions/AllowAllModal.tsx+++ b/studio/components/interfaces/Settings/Database/NetworkRestrictions/AllowAllModal.tsx@@ -1,36 +1,24 @@-import { FC, useState } from 'react'-import { Modal, Button } from 'ui'+import { FC } from 'react'+import { Button, Modal } from 'ui'-import { useStore } from 'hooks'import { useParams } from 'common/hooks'import { useNetworkRestrictionsApplyMutation } from 'data/network-restrictions/network-retrictions-apply-mutation'-interface Props {+interface AllowAllModalProps {visible: booleanonClose: () => void}-const AllowAllModal: FC<Props> = ({ visible, onClose }) => {- const { ui } = useStore()+const AllowAllModal = ({ visible, onClose }: AllowAllModalProps) => {const { ref } = useParams()- const [isSubmitting, setIsSubmitting] = useState(false)- const { mutateAsync: applyNetworkRestrictions } = useNetworkRestrictionsApplyMutation()+ const { mutateAsync: applyNetworkRestrictions, isLoading: isApplying } =+ useNetworkRestrictionsApplyMutation()const onSubmit = async () => {if (!ref) return console.error('Project ref is required')- setIsSubmitting(true)- try {- await applyNetworkRestrictions({ projectRef: ref, dbAllowedCidrs: ['0.0.0.0/0'] })- onClose()- } catch (error: any) {- ui.setNotification({- category: 'error',- message: `Failed to update restriction: ${error.message}`,- })- } finally {- setIsSubmitting(false)- }+ await applyNetworkRestrictions({ projectRef: ref, dbAllowedCidrs: ['0.0.0.0/0'] })+ onClose()}return (@@ -50,10 +38,10 @@ const AllowAllModal: FC<Props> = ({ visible, onClose }) => {</div></Modal.Content><div className="flex items-center justify-end px-6 py-4 border-t space-x-2">- <Button type="default" disabled={isSubmitting} onClick={() => onClose()}>+ <Button type="default" disabled={isApplying} onClick={() => onClose()}>Cancel</Button>- <Button loading={isSubmitting} disabled={isSubmitting} onClick={() => onSubmit()}>+ <Button loading={isApplying} disabled={isApplying} onClick={() => onSubmit()}>Confirm</Button></div>diff --git a/studio/components/interfaces/Settings/Database/NetworkRestrictions/DisallowAllModal.tsx b/studio/components/interfaces/Settings/Database/NetworkRestrictions/DisallowAllModal.tsxindex c843de5dcbc..0fc66a6cb90 100644--- a/studio/components/interfaces/Settings/Database/NetworkRestrictions/DisallowAllModal.tsx+++ b/studio/components/interfaces/Settings/Database/NetworkRestrictions/DisallowAllModal.tsx@@ -1,37 +1,24 @@-import { FC, useState } from 'react'-import { Modal, Button } from 'ui'+import { Button, Modal } from 'ui'-import { useStore } from 'hooks'import { useParams } from 'common/hooks'-import { useNetworkRestrictionsApplyMutation } from 'data/network-restrictions/network-retrictions-apply-mutation'import InformationBox from 'components/ui/InformationBox'+import { useNetworkRestrictionsApplyMutation } from 'data/network-restrictions/network-retrictions-apply-mutation'-interface Props {+interface DisallowAllModalProps {visible: booleanonClose: () => void}-const DisallowAllModal: FC<Props> = ({ visible, onClose }) => {- const { ui } = useStore()+const DisallowAllModal = ({ visible, onClose }: DisallowAllModalProps) => {const { ref } = useParams()- const [isSubmitting, setIsSubmitting] = useState(false)- const { mutateAsync: applyNetworkRestrictions } = useNetworkRestrictionsApplyMutation()+ const { mutateAsync: applyNetworkRestrictions, isLoading: isApplying } =+ useNetworkRestrictionsApplyMutation()const onSubmit = async () => {if (!ref) return console.error('Project ref is required')- setIsSubmitting(true)- try {- await applyNetworkRestrictions({ projectRef: ref, dbAllowedCidrs: ['127.0.0.1/32'] })- onClose()- } catch (error: any) {- ui.setNotification({- category: 'error',- message: `Failed to update restriction: ${error.message}`,- })- } finally {- setIsSubmitting(false)- }+ await applyNetworkRestrictions({ projectRef: ref, dbAllowedCidrs: ['127.0.0.1/32'] })+ onClose()}return (@@ -58,10 +45,10 @@ const DisallowAllModal: FC<Props> = ({ visible, onClose }) => {</div></Modal.Content><div className="flex items-center justify-end px-6 py-4 border-t space-x-2">- <Button type="default" disabled={isSubmitting} onClick={() => onClose()}>+ <Button type="default" disabled={isApplying} onClick={() => onClose()}>Cancel</Button>- <Button loading={isSubmitting} disabled={isSubmitting} onClick={() => onSubmit()}>+ <Button loading={isApplying} disabled={isApplying} onClick={() => onSubmit()}>Confirm</Button></div>diff --git a/studio/components/interfaces/Settings/Database/NetworkRestrictions/RemoveRestrictionModal.tsx b/studio/components/interfaces/Settings/Database/NetworkRestrictions/RemoveRestrictionModal.tsxindex c0aa9253ea7..c995a312d5c 100644--- a/studio/components/interfaces/Settings/Database/NetworkRestrictions/RemoveRestrictionModal.tsx+++ b/studio/components/interfaces/Settings/Database/NetworkRestrictions/RemoveRestrictionModal.tsx@@ -20,8 +20,15 @@ const RemoveRestrictionModal: FC<Props> = ({}) => {const { ui } = useStore()const { ref } = useParams()- const [isSubmitting, setIsSubmitting] = useState(false)- const { mutateAsync: applyNetworkRestrictions } = useNetworkRestrictionsApplyMutation()+ const { mutateAsync: applyNetworkRestrictions, isLoading: isApplying } =+ useNetworkRestrictionsApplyMutation({+ onError: (error) => {+ ui.setNotification({+ category: 'error',+ message: `Failed to remove restriction: ${error.message}`,+ })+ },+ })const isRemovingOnlyRestriction =restrictedIps.length === 1 && restrictedIps[0] === selectedRestriction@@ -29,24 +36,14 @@ const RemoveRestrictionModal: FC<Props> = ({const onSubmit = async () => {if (!ref) return console.error('Project ref is required')- setIsSubmitting(true)const dbAllowedCidrs = restrictedIps.filter((ip) => ip !== selectedRestriction)- try {- if (dbAllowedCidrs.length === 0) {- await applyNetworkRestrictions({ projectRef: ref, dbAllowedCidrs: ['0.0.0.0/0'] })- } else {- await applyNetworkRestrictions({ projectRef: ref, dbAllowedCidrs })- }- onClose()- } catch (error: any) {- ui.setNotification({- category: 'error',- message: `Failed to remove restriction: ${error.message}`,- })- } finally {- setIsSubmitting(false)+ if (dbAllowedCidrs.length === 0) {+ await applyNetworkRestrictions({ projectRef: ref, dbAllowedCidrs: ['0.0.0.0/0'] })+ } else {+ await applyNetworkRestrictions({ projectRef: ref, dbAllowedCidrs })}+ onClose()}return (@@ -76,10 +73,10 @@ const RemoveRestrictionModal: FC<Props> = ({</div></Modal.Content><div className="flex items-center justify-end px-6 py-4 border-t space-x-2">- <Button type="default" disabled={isSubmitting} onClick={() => onClose()}>+ <Button type="default" disabled={isApplying} onClick={() => onClose()}>Cancel</Button>- <Button loading={isSubmitting} disabled={isSubmitting} onClick={() => onSubmit()}>+ <Button loading={isApplying} disabled={isApplying} onClick={() => onSubmit()}>Remove restriction</Button></div>diff --git a/studio/data/network-restrictions/network-retrictions-apply-mutation.ts b/studio/data/network-restrictions/network-retrictions-apply-mutation.tsindex 7321b5d9f64..8a7b200f7a3 100644--- a/studio/data/network-restrictions/network-retrictions-apply-mutation.ts+++ b/studio/data/network-restrictions/network-retrictions-apply-mutation.ts@@ -1,6 +1,9 @@import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'+import { toast } from 'react-hot-toast'+import { post } from 'lib/common/fetch'import { API_ADMIN_URL } from 'lib/constants'+import { ResponseError } from 'types'import { networkRestrictionKeys } from './keys'export type NetworkRestrictionsApplyVariables = {@@ -27,22 +30,35 @@ type NetworkRestrictionsApplyData = Awaited<ReturnType<typeof applyNetworkRestriexport const useNetworkRestrictionsApplyMutation = ({onSuccess,+ onError,...options}: Omit<- UseMutationOptions<NetworkRestrictionsApplyData, unknown, NetworkRestrictionsApplyVariables>,+ UseMutationOptions<+ NetworkRestrictionsApplyData,+ ResponseError,+ NetworkRestrictionsApplyVariables+ >,'mutationFn'> = {}) => {const queryClient = useQueryClient()- return useMutation<NetworkRestrictionsApplyData, unknown, NetworkRestrictionsApplyVariables>(- (vars) => applyNetworkRestrictions(vars),- {- async onSuccess(data, variables, context) {- const { projectRef } = variables- await queryClient.invalidateQueries(networkRestrictionKeys.list(projectRef))- await onSuccess?.(data, variables, context)- },- ...options,- }- )+ return useMutation<+ NetworkRestrictionsApplyData,+ ResponseError,+ NetworkRestrictionsApplyVariables+ >((vars) => applyNetworkRestrictions(vars), {+ async onSuccess(data, variables, context) {+ const { projectRef } = variables+ await queryClient.invalidateQueries(networkRestrictionKeys.list(projectRef))+ await onSuccess?.(data, variables, context)+ },+ async onError(data, variables, context) {+ if (onError === undefined) {+ toast.error(`Failed to apply network restrictions: ${data.message}`)+ } else {+ onError(data, variables, context)+ }+ },+ ...options,+ })}