mirror of
https://github.com/supabase/supabase.git
synced 2026-10-13 13:19:09 +03:00
* chore: unify telemetry event sending * organize some imports * remove final useTelemetryProps * add www telemetry actions typing and docs * remove gaevents * fix build * move telemetry identify into PageTelemetry * fix telemetry constants import --------- Co-authored-by: Pamela Chia <pamelachiamayyee@gmail.com> Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
import { useMutation, UseMutationOptions } from '@tanstack/react-query'
|
|
|
|
import { sendTelemetryEvent } from 'common'
|
|
import { TelemetryEvent } from 'common/telemetry-constants'
|
|
import { handleError } from 'data/fetchers'
|
|
import { API_URL } from 'lib/constants'
|
|
import { useRouter } from 'next/router'
|
|
import type { ResponseError } from 'types'
|
|
|
|
interface SendEventVariables {
|
|
event: TelemetryEvent
|
|
pathname?: string
|
|
}
|
|
|
|
export async function sendEvent({ event, pathname }: SendEventVariables) {
|
|
try {
|
|
await sendTelemetryEvent(API_URL, event, pathname)
|
|
} catch (error) {
|
|
handleError(error)
|
|
}
|
|
}
|
|
|
|
type SendEventData = Awaited<ReturnType<typeof sendEvent>>
|
|
|
|
export const useSendEventMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<UseMutationOptions<SendEventData, ResponseError, TelemetryEvent>, 'mutationFn'> = {}) => {
|
|
const router = useRouter()
|
|
|
|
return useMutation<SendEventData, ResponseError, TelemetryEvent>(
|
|
(event) => {
|
|
return sendEvent({ event, pathname: router.pathname })
|
|
},
|
|
{
|
|
async onSuccess(data, variables, context) {
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
console.error(`Failed to send Telemetry event: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
}
|
|
)
|
|
}
|