Files
supabase/apps/studio/data/database-integrations/stripe/stripe-sync-install-mutation.ts
Pamela Chia 2346baf51c chore(telemetry): standardize event naming conventions (#41786)
* docs(telemetry-constants): add naming conventions and review note

* refactor(telemetry): rename event interfaces to include Event suffix

* refactor(telemetry): rename branch merge success event to completed

* refactor(telemetry): rename cron job deleted event to removed

* refactor(telemetry): rename events to submitted/completed

* docs: add previous event names to telemetry comments
2026-01-08 17:11:48 +07:00

92 lines
2.5 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { getAccessToken } from 'common'
import { databaseKeys } from 'data/database/keys'
import { BASE_PATH } from 'lib/constants'
import { useTrack } from 'lib/telemetry/track'
import type { ResponseError } from 'types'
import { stripeSyncKeys } from './keys'
export type StripeSyncInstallVariables = {
projectRef: string
stripeSecretKey: string
}
export type StripeSyncInstallResponse = {
data: { message: string } | null
error: { message: string } | null
}
export async function installStripeSync({
projectRef,
stripeSecretKey,
}: StripeSyncInstallVariables): Promise<StripeSyncInstallResponse> {
const accessToken = await getAccessToken()
const response = await fetch(`${BASE_PATH}/api/integrations/stripe-sync`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify({
projectRef,
stripeSecretKey,
}),
})
const result = await response.json()
if (!response.ok) {
throw new Error(result.error?.message || 'Failed to install Stripe Sync')
}
return result
}
type StripeSyncInstallData = Awaited<ReturnType<typeof installStripeSync>>
export const useStripeSyncInstallMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<StripeSyncInstallData, ResponseError, StripeSyncInstallVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
const track = useTrack()
return useMutation<StripeSyncInstallData, ResponseError, StripeSyncInstallVariables>({
mutationFn: (vars) => installStripeSync(vars),
async onSuccess(data, variables, context) {
const { projectRef } = variables
track('integration_install_submitted', {
integrationName: 'stripe_sync_engine',
})
// Invalidate schemas query to refresh installation status
await queryClient.invalidateQueries({
queryKey: databaseKeys.schemas(projectRef),
})
// Also invalidate any stripe sync related queries
await queryClient.invalidateQueries({
queryKey: stripeSyncKeys.all,
})
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to install Stripe Sync: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
})
}