Files
supabase/apps/studio/data/telemetry/send-groups-reset-mutation.ts
Alaister Young 2b419c70a1 chore: add usercentrics for consent management (#35384)
* chore: add usercentrics for consent management

* client component to make next.js happy

* address feedback

* move consent state to common

* fix import

* ensure page events are sent correctly

* add feature flag provider to ui library site

* fix ui lib 500 error

* skip in test env

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
Co-authored-by: Jordi Enric <jordi.err@gmail.com>
2025-05-05 13:18:48 +08:00

51 lines
1.5 KiB
TypeScript

import { useMutation, UseMutationOptions } from '@tanstack/react-query'
import { components } from 'api-types'
import { hasConsented } from 'common'
import { handleError, post } from 'data/fetchers'
import { IS_PLATFORM } from 'lib/constants'
import type { ResponseError } from 'types'
export type SendGroupsResetVariables = components['schemas']['TelemetryGroupsResetBody']
export async function sendGroupsReset({ body }: { body: SendGroupsResetVariables }) {
const consent = hasConsented()
if (!consent || !IS_PLATFORM) return undefined
const { data, error } = await post(`/platform/telemetry/groups/reset`, {
body,
credentials: 'include',
})
if (error) handleError(error)
return data
}
type SendGroupsResetData = Awaited<ReturnType<typeof sendGroupsReset>>
export const useSendGroupsResetMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<SendGroupsResetData, ResponseError, SendGroupsResetVariables>,
'mutationFn'
> = {}) => {
return useMutation<SendGroupsResetData, ResponseError, SendGroupsResetVariables>(
(vars) => sendGroupsReset({ body: vars }),
{
async onSuccess(data, variables, context) {
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
console.error(`Failed to send Telemetry groups reset: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
}
)
}