Files
supabase/apps/studio/data/storage/bucket-object-delete-mutation.ts
1e108491c1 chore: Refactor the StorageExplorerStore to use API calls from the data folder (#23222)
* Migrate all API calls to data folder.

* Remove unused mutation.

* MInor leftover type fixes.

* chore: Make the `StorageExplorerStore` fully typed (#23397)

* Migrate all API calls to data folder.

* Convert the poor man enums into proper enums.

* Add types to most of the methods.

* Add types around the methods for uploading files.

* Add types for file cache.

* Add types for buckets.

* Fix bunch of type errors.

* Add types to all store methods. Fix all types in components related to the store.

* Remove all anys from the Storage components.

* Fix the multiple selection.

* Fix the selected checkbox.

* Set all private methods to private and delete some unneeded code.

* Remove unneeded mutation.

* Update all mutations for storage explorer to follow template syntax

* Update apps/studio/components/to-be-cleaned/Storage/StorageExplorer/PreviewPane.tsx

Co-authored-by: Alaister Young <alaister@users.noreply.github.com>

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
Co-authored-by: Alaister Young <alaister@users.noreply.github.com>
2024-06-07 14:01:45 +07:00

64 lines
1.6 KiB
TypeScript

import { UseMutationOptions, useMutation, useQueryClient } from '@tanstack/react-query'
import { del, handleError } from 'data/fetchers'
import toast from 'react-hot-toast'
import { ResponseError } from 'types'
type DeleteBucketObjectParams = {
projectRef: string
bucketId?: string
paths: string[]
}
export const deleteBucketObject = async (
{ projectRef, bucketId, paths }: DeleteBucketObjectParams,
signal?: AbortSignal
) => {
if (!bucketId) throw new Error('bucketId is required')
const { data, error } = await del('/platform/storage/{ref}/buckets/{id}/objects', {
params: {
path: {
ref: projectRef,
id: bucketId,
},
},
body: {
paths,
},
signal,
})
if (error) handleError(error)
return data
}
type BucketObjectDeleteData = Awaited<ReturnType<typeof deleteBucketObject>>
export const useBucketObjectDeleteMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<BucketObjectDeleteData, ResponseError, DeleteBucketObjectParams>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<BucketObjectDeleteData, ResponseError, DeleteBucketObjectParams>(
(vars) => deleteBucketObject(vars),
{
async onSuccess(data, variables, context) {
// [Joshen] TODO figure out what queries to invalidate
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to delete bucket object: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
}
)
}