Files
supabase/apps/studio/data/config/disk-attributes-update-mutation.ts
Joshen LimandJonathan Summers-Muir 07699ad76b Chore/disk attributes mangement (#29219)
* init

* moar

* moat

* moar

* add read replica bar

* moar

* Update DiskMangementPanelForm.tsx

* added temp state mang to test

* moar

* Prepare react queries + data from API for disk mgt

* moat

* moat

* moar

* Update DiskMangementReviewAndSubmitDialog.tsx

* badge updates

* Hook up actual endpoints for E2E testing, but commented out for now for local dev

* Hook up real data + clean up files and add tests

* Update APi types

* Hook up E2E and fix all validation logic

* Only show new disk mgt UI for orb billing

* Add note on RRs 25% more disk size

* Add state to handle free plan for disk mgt

* Update increase disk size CTA in reports/database

* Fix file spelling errors

* Address feedback

* Fix missing framer motion package in ui

* Address comments

* Address feedabck

* Minor fix

* Use new disk util endpoint

* Remove unused import

* Address feedback

* More addressing of feedback

* Update pricing details for IO2 - no included disk size

---------

Co-authored-by: Jonathan Summers-Muir <MildTomato@users.noreply.github.com>
2024-09-13 11:51:51 +08:00

82 lines
2.3 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { handleError, post } from 'data/fetchers'
import type { ResponseError } from 'types'
import { components } from 'api-types'
import { configKeys } from './keys'
type UpdateGP3Body = components['schemas']['DiskRequestAttributesGP3']
type UpdateIO2Body = components['schemas']['DiskRequestAttributesIO2']
export const COOLDOWN_DURATION = 60 * 60 * 6
export type UpdateDiskAttributesVariables = {
ref: string
storageType: 'gp3' | 'io2'
provisionedIOPS: number
throughput?: number
totalSize: number
}
// [Joshen] Need to fix this
export async function updateDiskAttributes({
ref,
storageType,
provisionedIOPS,
throughput,
totalSize,
}: UpdateDiskAttributesVariables) {
const attributes =
storageType === 'gp3'
? ({
iops: provisionedIOPS,
throughput_mbps: throughput,
size_gb: totalSize,
type: storageType,
} as UpdateGP3Body)
: ({
iops: provisionedIOPS,
size_gb: totalSize,
type: storageType,
} as UpdateIO2Body)
const { data, error } = await post('/platform/projects/{ref}/disk', {
params: { path: { ref } },
body: { attributes },
})
if (error) handleError(error)
return data
}
type UpdateDiskAttributesData = Awaited<ReturnType<typeof updateDiskAttributes>>
export const useUpdateDiskAttributesMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<UpdateDiskAttributesData, ResponseError, UpdateDiskAttributesVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<UpdateDiskAttributesData, ResponseError, UpdateDiskAttributesVariables>(
(vars) => updateDiskAttributes(vars),
{
async onSuccess(data, variables, context) {
const { ref } = variables
await queryClient.invalidateQueries(configKeys.diskAttributes(ref))
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to update disk attributes: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
}
)
}