Files
supabase/apps/studio/data/telemetry/send-groups-identify-mutation.ts
T
Joshen Lim 7016b9db90 Smol tweak to consent toast on mobile (#30195)
* Smol tweak to consent toast on mobile

* Use useConsent hook from packages in dashboard, and remove manual calling of ConsentToast + deprecate dashboard's copy of telemetry local storage key

* Attempt to fix playwright tests

* Fix
2024-11-01 17:25:40 +08:00

53 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 type { ResponseError } from 'types'
export type SendGroupsIdentifyVariables = components['schemas']['TelemetryGroupsIdentityBody']
export async function sendGroupsIdentify({ body }: { body: SendGroupsIdentifyVariables }) {
const consent =
(typeof window !== 'undefined'
? localStorage.getItem(LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT)
: null) === 'true'
if (!consent) return undefined
const { data, error } = await post(`/platform/telemetry/groups/identify`, {
body,
credentials: 'include',
})
if (error) handleError(error)
return data
}
type SendGroupsIdentifyData = Awaited<ReturnType<typeof sendGroupsIdentify>>
export const useSendGroupsIdentifyMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<SendGroupsIdentifyData, ResponseError, SendGroupsIdentifyVariables>,
'mutationFn'
> = {}) => {
return useMutation<SendGroupsIdentifyData, ResponseError, SendGroupsIdentifyVariables>(
(vars) => sendGroupsIdentify({ 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 identify: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
}
)
}