Repository: supabase/supabase
File: apps/studio/components/interfaces/Storage/StoragePolicies/StoragePolicies.tsx (TypeScript)

Code before:
  const {
    data: policies = [],
    refetch,
    isPending: isLoadingPolicies,
  } = useDatabasePoliciesQuery({
    projectRef: project?.ref,
    connectionString: project?.connectionString,
    schema: 'storage',
  })

  const isLoading = isLoadingBuckets || isLoadingPolicies

  const { mutateAsync: createDatabasePolicy } = useDatabasePolicyCreateMutation({
    onError: () => {},
  })
  const { mutateAsync: updateDatabasePolicy } = useDatabasePolicyUpdateMutation()
  const { mutate: deleteDatabasePolicy } = useDatabasePolicyDeleteMutation({
    onSuccess: async () => {
      await refetch()
      toast.success('Successfully deleted policy!')
      setSelectedPolicyToDelete(undefined)
    },
  })

  // Only use storage policy editor when creating new policies for buckets
  const showStoragePolicyEditor =
    isEmpty(selectedPolicyToEdit) &&
    !isEmpty(isEditingPolicyForBucket) &&
    (isEditingPolicyForBucket.bucket ?? '').length > 0

  const showGeneralPolicyEditor = !isEmpty(isEditingPolicyForBucket) && !showStoragePolicyEditor

  // Policies under storage.buckets
  const storageBucketPolicies = policies.filter(
    (x) => x.schema === 'storage' && x.table === 'buckets'
  )
  // Policies under storage.objects
  const storageObjectsPolicies = policies.filter(
    (x) => x.schema === 'storage' && x.table === 'objects'
  )

  const formattedStorageObjectPolicies = useMemo(
    () => formatPoliciesForStorage(buckets, storageObjectsPolicies),
    [buckets, storageObjectsPolicies]
  )

  const ungroupedPolicies =
    formattedStorageObjectPolicies.find((x) => x.name === UNGROUPED_POLICY_SYMBOL)?.policies ?? []

  const bucketsWithPolicies = useMemo(() => {
    // Get policies for filtered buckets (show all policies, don't filter them)

Review comment (suggestion):
btw there's default error handlers within the mutation already so technically unnecessary unless we want to have a specific error handling behaviour based on the context

no biggie though, just an fyi

Code after:
  const {
    data: policies = [],
    refetch,
    isPending: isLoadingPolicies,
  } = useDatabasePoliciesQuery({
    projectRef: project?.ref,
    connectionString: project?.connectionString,
    schema: 'storage',
  })

  const isLoading = isLoadingBuckets || isLoadingPolicies

  const { mutateAsync: createDatabasePolicy } = useDatabasePolicyCreateMutation({
    onError: () => {},
  })
  const { mutateAsync: updateDatabasePolicy } = useDatabasePolicyUpdateMutation()
  const { mutate: deleteDatabasePolicy, isPending: isDeletingPolicy } =
    useDatabasePolicyDeleteMutation({
      onSuccess: async () => {
        await refetch()
        toast.success('Successfully deleted policy!')
        setSelectedPolicyToDelete(undefined)
      },
      onError: (error: any) => {
        toast.error(`Failed to delete policy: ${error.message}`)
      },
    })

  // Only use storage policy editor when creating new policies for buckets
  const showStoragePolicyEditor =
    isEmpty(selectedPolicyToEdit) &&
    !isEmpty(isEditingPolicyForBucket) &&
    (isEditingPolicyForBucket.bucket ?? '').length > 0

  const showGeneralPolicyEditor = !isEmpty(isEditingPolicyForBucket) && !showStoragePolicyEditor

  // Policies under storage.buckets
  const storageBucketPolicies = policies.filter(
    (x) => x.schema === 'storage' && x.table === 'buckets'
  )
  // Policies under storage.objects
  const storageObjectsPolicies = policies.filter(
    (x) => x.schema === 'storage' && x.table === 'objects'
  )

  const formattedStorageObjectPolicies = useMemo(
    () => formatPoliciesForStorage(buckets, storageObjectsPolicies),
    [buckets, storageObjectsPolicies]
  )

  const ungroupedPolicies =