Files
supabase/apps/studio/data/telemetry/send-groups-identify-mutation.ts
Joshen Lim 9b3d87207f Chore/add new telemetry triggers for ph (#29769)
* Init new telemetry mutations and add telemetry reset for logging out

* Add sendPageLeave on unloadBefore event in PageTelemetry

* Add sendGroupsIdentify and sendGroupsReset in PageTelemetry

* Ensure new telemetry triggers only if feature flag is enabled

* Update call to sendGroupsReset

* some fixes and ensure that telemetry opt in is true before firing telemetry events

* Ensure that telemetry events get triggered when landing on project or org
2024-10-09 17:52:43 +08:00

45 lines
1.3 KiB
TypeScript

import { useMutation, UseMutationOptions } from '@tanstack/react-query'
import { components } from 'api-types'
import { handleError, post } from 'data/fetchers'
import type { ResponseError } from 'types'
export type SendGroupsIdentifyVariables = components['schemas']['TelemetryGroupsIdentityBody']
export async function sendGroupsIdentify(body: SendGroupsIdentifyVariables) {
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(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,
}
)
}