import { zodResolver } from '@hookform/resolvers/zod' import { Link2 } from 'lucide-react' import { useEffect } from 'react' import type { SubmitHandler } from 'react-hook-form' import { useForm } from 'react-hook-form' import { toast } from 'sonner' import { Button, DialogSectionSeparator, Form, FormControl, FormField, Input } from 'ui' import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout' import { CategoryAndSeverityInfo } from './CategoryAndSeverityInfo' import { LinkSupportTicketFormSchema, type LinkSupportTicketFormValues, } from './LinkSupportTicketForm.schema' import { OrganizationSelector } from './OrganizationSelector' import { ProjectAndPlanInfo } from './ProjectAndPlanInfo' import { DISABLE_SUPPORT_ACCESS_CATEGORIES, SupportAccessToggle } from './SupportAccessToggle' import { getOrgSubscriptionPlan, NO_ORG_MARKER, NO_PROJECT_MARKER } from './SupportForm.utils' import { useLinkSupportTicketMutation } from '@/data/feedback/link-support-ticket-mutation' import { useOrganizationsQuery } from '@/data/organizations/organizations-query' interface LinkSupportTicketFormProps { conversationId: string onSuccess: () => void } // [Joshen] Am reusing the SupportFormV2 components here but am not sure how to type things properly // hence marking all the `form` as `any` when passing as props to the components for now export const LinkSupportTicketForm = ({ conversationId, onSuccess, }: LinkSupportTicketFormProps) => { const { data: organizations, isSuccess } = useOrganizationsQuery() const form = useForm({ resolver: zodResolver(LinkSupportTicketFormSchema as any), defaultValues: { conversation_id: conversationId, organizationSlug: NO_ORG_MARKER, projectRef: NO_PROJECT_MARKER, category: '' as any, allowSupportAccess: true, }, mode: 'onSubmit', reValidateMode: 'onBlur', }) const { category, organizationSlug, projectRef } = form.watch() const selectedOrgSlug = organizationSlug === NO_ORG_MARKER ? null : organizationSlug const selectedProjectRef = projectRef === NO_PROJECT_MARKER ? null : projectRef const subscriptionPlanId = getOrgSubscriptionPlan(organizations, selectedOrgSlug) const { mutate: linkSupportTicket, isPending } = useLinkSupportTicketMutation({ onSuccess: () => { toast.success('Support ticket linked successfully!') onSuccess() }, onError: (error) => { let errorMessage = error.message try { const parsed = JSON.parse(error.message) errorMessage = parsed?._error?.message || parsed?.message || error.message } catch {} // If parsing fails, use the original message toast.error(`Failed to link support ticket: ${errorMessage}`) }, }) const onSubmit: SubmitHandler = (values) => { if (!organizations) return toast.error('Organizations not loaded. Please try again.') const selectedOrg = organizations.find((org) => org.slug === values.organizationSlug) if (!selectedOrg) return toast.error('Selected organization not found. Please try again.') linkSupportTicket({ conversation_id: values.conversation_id, org_id: selectedOrg.id, project_ref: values.projectRef && values.projectRef !== NO_PROJECT_MARKER ? values.projectRef : undefined, category: values.category, allow_support_access: values.category && !DISABLE_SUPPORT_ACCESS_CATEGORIES.includes(values.category) ? values.allowSupportAccess : false, }) } useEffect(() => { if (!!organizations && organizations.length > 0) { form.reset({ conversation_id: conversationId, organizationSlug: organizations[0].slug, projectRef: NO_PROJECT_MARKER, category: '' as any, allowSupportAccess: true, }) } // eslint-disable-next-line react-hooks/exhaustive-deps }, [isSuccess]) return (

Link support ticket to account

( )} /> {organizationSlug !== NO_ORG_MARKER && ( )}
{!!category && !DISABLE_SUPPORT_ACCESS_CATEGORIES.includes(category) && ( <>
)}
) }