Files
supabase/apps/studio/data/telemetry/send-groups-reset-mutation.ts
T
Ivan VasilovandJoshen Lim f29003e0e7 fix: Use protocol property when building API url (#30332)
* Fix the types for API settings to include protocol.

* Use the protocol property in all usages of endpoints.

* Update

* Lint

* Revert changes to retrieving protocol

* Remove console.logs

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2024-11-06 22:22:24 +01:00

54 lines
1.6 KiB
TypeScript

import { useMutation, UseMutationOptions } from '@tanstack/react-query'
import { components } from 'api-types'
import { LOCAL_STORAGE_KEYS } 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 =
(typeof window !== 'undefined'
? localStorage.getItem(LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT)
: null) === 'true'
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,
}
)
}