mirror of
https://github.com/supabase/supabase.git
synced 2026-10-05 17:35:10 +03:00
129 lines
3.7 KiB
TypeScript
129 lines
3.7 KiB
TypeScript
import { useMutation } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
// End of third-party imports
|
|
|
|
import type { ExtendedSupportCategories } from '@/components/interfaces/Support/Support.constants'
|
|
import { NO_ORG_MARKER, NO_PROJECT_MARKER } from '@/components/interfaces/Support/SupportForm.utils'
|
|
import { handleError, post } from '@/data/fetchers'
|
|
import { ResponseError } from '@/types'
|
|
import type { UseCustomMutationOptions } from '@/types'
|
|
|
|
export type sendSupportTicketVariables = {
|
|
subject: string
|
|
message: string
|
|
category: ExtendedSupportCategories
|
|
severity: string
|
|
projectRef?: string
|
|
organizationSlug?: string
|
|
library?: string
|
|
affectedServices?: string
|
|
browserInformation?: string
|
|
allowSupportAccess: boolean
|
|
siteUrl?: string
|
|
additionalRedirectUrls?: string
|
|
dashboardSentryIssueId?: string
|
|
dashboardLogs?: string
|
|
dashboardStudioVersion?: string
|
|
// Stable Front thread_ref so the AI support chat can later be appended to the
|
|
// same Front conversation that this submission creates.
|
|
threadRef?: string
|
|
}
|
|
|
|
const RATE_LIMIT_FALLBACK_SECONDS = 60
|
|
|
|
export async function sendSupportTicket({
|
|
subject,
|
|
message,
|
|
category,
|
|
severity,
|
|
projectRef,
|
|
organizationSlug,
|
|
library,
|
|
affectedServices,
|
|
browserInformation,
|
|
allowSupportAccess,
|
|
siteUrl,
|
|
additionalRedirectUrls,
|
|
dashboardSentryIssueId,
|
|
dashboardLogs,
|
|
dashboardStudioVersion,
|
|
threadRef,
|
|
}: sendSupportTicketVariables) {
|
|
const { data, error, response } = await post('/platform/feedback/send', {
|
|
body: {
|
|
subject,
|
|
message,
|
|
category,
|
|
severity,
|
|
// Skip `NO_PROJECT_MARKER` values so we don't need to have special handling for it's value in API validation.
|
|
projectRef: projectRef === NO_PROJECT_MARKER ? undefined : projectRef,
|
|
organizationSlug: organizationSlug === NO_ORG_MARKER ? undefined : organizationSlug,
|
|
library,
|
|
verified: true,
|
|
tags: ['dashboard-support-form'],
|
|
siteUrl,
|
|
additionalRedirectUrls,
|
|
affectedServices,
|
|
browserInformation,
|
|
allowSupportAccess,
|
|
dashboardSentryIssueId,
|
|
dashboardLogs,
|
|
dashboardStudioVersion,
|
|
threadRef,
|
|
},
|
|
})
|
|
|
|
if (error) {
|
|
const httpResponse: unknown = response
|
|
if (httpResponse instanceof Response && httpResponse.status === 429) {
|
|
const resetHeader =
|
|
httpResponse.headers.get('Retry-After') ?? httpResponse.headers.get('X-RateLimit-Reset')
|
|
const parsedReset = resetHeader ? parseInt(resetHeader, 10) : NaN
|
|
const waitSeconds = Number.isFinite(parsedReset) ? parsedReset : RATE_LIMIT_FALLBACK_SECONDS
|
|
throw new ResponseError(
|
|
`You have submitted too many support requests. Please try again in ${waitSeconds} second${waitSeconds === 1 ? '' : 's'}.`,
|
|
429,
|
|
undefined,
|
|
waitSeconds
|
|
)
|
|
}
|
|
|
|
handleError(error, {
|
|
alwaysCapture: true,
|
|
sentryContext: {
|
|
tags: {
|
|
dashboardSupportForm: true,
|
|
},
|
|
},
|
|
})
|
|
}
|
|
return data
|
|
}
|
|
|
|
type sendSupportTicketData = Awaited<ReturnType<typeof sendSupportTicket>>
|
|
|
|
export const useSendSupportTicketMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<sendSupportTicketData, ResponseError, sendSupportTicketVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
return useMutation<sendSupportTicketData, ResponseError, sendSupportTicketVariables>({
|
|
mutationFn: (vars) => sendSupportTicket(vars),
|
|
async onSuccess(data, variables, context) {
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to submit support ticket: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|