Files
supabase/apps/studio/data/storage/object-move-mutation.ts
Ivan Vasilov 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

65 lines
1.5 KiB
TypeScript

import { UseMutationOptions, useMutation } from '@tanstack/react-query'
import toast from 'react-hot-toast'
import { handleError, post } from 'data/fetchers'
import { ResponseError } from 'types'
type MoveStorageObjectParams = {
projectRef: string
bucketId?: string
from: string
to: string
}
export const moveStorageObject = async ({
projectRef,
bucketId,
from,
to,
}: MoveStorageObjectParams) => {
if (!bucketId) throw new Error('bucketId is required')
const { data, error } = await post('/platform/storage/{ref}/buckets/{id}/objects/move', {
params: {
path: {
ref: projectRef,
id: bucketId,
},
},
body: {
from,
to,
},
})
if (error) handleError(error)
return data
}
type MoveBucketObjectData = Awaited<ReturnType<typeof moveStorageObject>>
export const useGetSignBucketObjectMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<MoveBucketObjectData, ResponseError, MoveStorageObjectParams>,
'mutationFn'
> = {}) => {
return useMutation<MoveBucketObjectData, ResponseError, MoveStorageObjectParams>(
(vars) => moveStorageObject(vars),
{
async onSuccess(data, variables, context) {
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to move bucket object: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
}
)
}