Files
supabase/apps/studio/data/projects/project-by-fly-extension-id-mutation.ts
Ivan Vasilov 8b657165b5 chore: Migrate to use custom type for ReactQuery queries and mutations (#40073)
* Add custom types for queries, mutations and infinite queries.

* Migrate all queries to use the new type.

* Migrate all infinite queries to useCustomInfiniteQueryOptions.

* Migrate all mutations to use useCustomMutationOptions.

* Add type to all imports in `types` folder.
2025-11-03 13:18:13 +01:00

53 lines
1.5 KiB
TypeScript

import { useMutation } from '@tanstack/react-query'
import { toast } from 'sonner'
import { get, handleError } from 'data/fetchers'
import type { ResponseError, UseCustomMutationOptions } from 'types'
export type ProjectByFlyExtensionIdVariables = {
flyExtensionId: string
}
// [Joshen] Should be converted into a query instead of a mutation
// Will need to update sign-in-fly-tos.tsx as well
export async function getProjectByFlyExtensionId({
flyExtensionId,
}: ProjectByFlyExtensionIdVariables) {
const { data, error } = await get('/platform/projects/fly/{fly_extension_id}', {
params: { path: { fly_extension_id: flyExtensionId } },
})
if (error) handleError(error)
return data as { ref: string }
}
type ProjectByFlyExtensionIdData = Awaited<ReturnType<typeof getProjectByFlyExtensionId>>
export const useProjectByFlyExtensionIdMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseCustomMutationOptions<
ProjectByFlyExtensionIdData,
ResponseError,
ProjectByFlyExtensionIdVariables
>,
'mutationFn'
> = {}) => {
return useMutation<ProjectByFlyExtensionIdData, ResponseError, ProjectByFlyExtensionIdVariables>({
mutationFn: (vars) => getProjectByFlyExtensionId(vars),
async onSuccess(data, variables, context) {
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to get project: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
})
}