Files
supabase/apps/studio/data/organizations/organization-update-mutation.ts
Charis df742bdb3b refactor(studio): rename ResponseError to StudioError (#52707)
## What

Moves the `ResponseError` class (`types/base.ts`) and the classified
error classes (`types/api-errors.ts`) into
`lib/errors/studio-errors.ts`, and renames `ResponseError` to
`StudioError`.

## Why

Studio's error classification is being expanded to cover errors that
don't come from API responses (thrown network failures, aborted
requests, unexpected client errors). The API-specific name and location
no longer fit. Landing the rename on its own keeps the follow-up PR,
which introduces the classified error model, small and reviewable.

## Notes for reviewers

- Mechanical change, no behavior change. Most of the diff is a one-line
import change per file plus import re-sorting by Prettier.
- Files that only use the type now use `import type { StudioError }`.
- `ResponseError` is no longer re-exported from `@/types`; there are no
compatibility shims at the old paths.
- Unrelated identifiers like `QueryResponseError` and
`parseResponseErrors` are unchanged.
2026-11-03 11:09:30 -05:00

119 lines
3.5 KiB
TypeScript

import { useMutation, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { organizationKeys } from './keys'
import type { components } from '@/data/api'
import { handleError, patch } from '@/data/fetchers'
import type { StudioError } from '@/lib/errors/studio-errors'
import type { UseCustomMutationOptions } from '@/types'
export type OrganizationUpdateVariables = {
slug: string
name?: string
billing_email?: string
additional_billing_emails?: string[]
opt_in_tags?: string[]
}
export async function updateOrganization({
slug,
name,
billing_email,
opt_in_tags,
additional_billing_emails,
}: OrganizationUpdateVariables) {
// @ts-ignore [Joshen] API spec is wrong
const payload: components['schemas']['UpdateOrganizationBody'] = {}
if (name) payload.name = name
if (billing_email) payload.billing_email = billing_email
if (opt_in_tags) payload.opt_in_tags = opt_in_tags as any
if (additional_billing_emails) payload.additional_billing_emails = additional_billing_emails
const { data, error } = await patch('/platform/organizations/{slug}', {
params: { path: { slug } },
body: payload,
})
if (error) handleError(error)
return data
}
type OrganizationUpdateData = Awaited<ReturnType<typeof updateOrganization>>
export const useOrganizationUpdateMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseCustomMutationOptions<OrganizationUpdateData, StudioError, OrganizationUpdateVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<OrganizationUpdateData, StudioError, OrganizationUpdateVariables>({
mutationFn: (vars) => updateOrganization(vars),
async onSuccess(data, variables, context) {
queryClient.setQueriesData(
{
queryKey: organizationKeys.list(),
exact: true,
},
(prev: components['schemas']['OrganizationResponse_Output'][] | undefined) => {
if (!prev) return prev
return prev.map((org) => {
if (org.slug !== variables.slug) return org
return {
...org,
name: variables.name || org.name,
billing_email: variables.billing_email || org.billing_email,
opt_in_tags: variables.opt_in_tags || org.opt_in_tags,
}
})
}
)
queryClient.setQueriesData(
{
queryKey: organizationKeys.customerProfile(data.slug),
exact: true,
},
(prev: components['schemas']['CustomerResponse_Output'] | undefined) => {
if (!prev) return prev
return {
...prev,
additional_emails: variables.additional_billing_emails || prev.additional_emails,
}
}
)
queryClient.setQueriesData(
{
queryKey: organizationKeys.detail(data.slug),
exact: true,
},
(prev: components['schemas']['OrganizationSlugResponse_Output'] | undefined) => {
if (!prev) return prev
return {
...prev,
name: variables.name || prev.name,
billing_email: variables.billing_email || prev.billing_email,
opt_in_tags: variables.opt_in_tags || prev.opt_in_tags,
}
}
)
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to update organization: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
})
}