Files
supabase/apps/studio/data/lint/create-lint-rule-mutation.ts
T
Joshen Lim bcf943ee88 Advisor rules update for LW15 (#37202)
* Advisor rules update for LW15

* Simplify UX to confirmation modal

* Rename section to settings

* fix

* patchy

* further simplify

* Smol styling fix

* Create + Delete lint rules to also invalidate lints
2025-07-16 22:20:08 +08:00

61 lines
1.8 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { components } from 'api-types'
import { handleError, post } from 'data/fetchers'
import type { ResponseError } from 'types'
import { lintKeys } from './keys'
type ExceptionPayload = components['schemas']['CreateNotificationExceptionsBody']['exceptions'][0]
export type LintRuleCreateVariables = {
projectRef: string
exception: ExceptionPayload
}
export async function createLintRule({ projectRef, exception }: LintRuleCreateVariables) {
const { data, error } = await post('/platform/projects/{ref}/notifications/advisor/exceptions', {
params: { path: { ref: projectRef } },
body: {
exceptions: [exception],
},
})
if (error) handleError(error)
return data
}
type LintRuleCreateData = Awaited<ReturnType<typeof createLintRule>>
export const useLintRuleCreateMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<LintRuleCreateData, ResponseError, LintRuleCreateVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<LintRuleCreateData, ResponseError, LintRuleCreateVariables>(
(vars) => createLintRule(vars),
{
async onSuccess(data, variables, context) {
const { projectRef } = variables
await Promise.all([
queryClient.invalidateQueries(lintKeys.lintRules(projectRef)),
queryClient.invalidateQueries(lintKeys.lint(projectRef)),
])
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to create lint rule: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
}
)
}