import { useParams } from 'common' import { includes, without } from 'lodash' import { useReducer, useState } from 'react' import { toast } from 'sonner' import { Modal, TextArea } from 'ui' import { generateUpgradeReasons } from '../helpers' import { useSendUpgradeFeedbackMutation } from '@/data/feedback/upgrade-survey-send' import type { OrgSubscription } from '@/data/subscriptions/types' export interface UpgradeSurveyModalProps { visible: boolean originalPlan?: string subscription?: OrgSubscription onClose: (success?: boolean) => void } // Upgrade survey is from Free Plan to Pro/Team Plan and from Pro to Team Plan const UpgradeSurveyModal = ({ visible, originalPlan, subscription, onClose, }: UpgradeSurveyModalProps) => { const { slug } = useParams() const [message, setMessage] = useState('') const [selectedReasons, dispatchSelectedReasons] = useReducer(reducer, []) const upgradeReasons = generateUpgradeReasons(originalPlan, subscription?.plan.id) const { mutate: sendUpgradeSurvey, isPending: isSubmitting } = useSendUpgradeFeedbackMutation({ onError: (error) => { toast.error(`Failed to submit survey: ${error.message}`) }, onSuccess: () => { onClose(true) window.scrollTo({ top: 0, left: 0, behavior: 'smooth' }) }, }) function reducer(state: any, action: any) { if (includes(state, action.target.value)) { return without(state, action.target.value) } else { return [...state, action.target.value] } } const onSubmit = async () => { if (selectedReasons.length === 0) { return toast.error('Please select at least one reason for upgrading your subscription') } sendUpgradeSurvey({ orgSlug: slug, prevPlan: originalPlan, currentPlan: subscription?.plan?.id, reasons: selectedReasons, message: message.trim() || undefined, }) } return ( <>

What reasons motivated your decision to upgrade? Your feedback helps us improve Briven as much as we can.

{upgradeReasons.map((option) => { const active = selectedReasons.find((x) => x === option) return ( ) })}