import './LaunchChatSupport.styles.scss'; import { Button, Modal, Tooltip, Typography } from 'antd'; import updateCreditCardApi from 'api/billing/checkout'; import logEvent from 'api/common/logEvent'; import cx from 'classnames'; import { SOMETHING_WENT_WRONG } from 'constants/api'; import { FeatureKeys } from 'constants/features'; import useFeatureFlags from 'hooks/useFeatureFlag'; import useLicense from 'hooks/useLicense'; import { useNotifications } from 'hooks/useNotifications'; import { defaultTo } from 'lodash-es'; import { CreditCard, HelpCircle, X } from 'lucide-react'; import { useEffect, useState } from 'react'; import { useMutation } from 'react-query'; import { useLocation } from 'react-router-dom'; import { ErrorResponse, SuccessResponse } from 'types/api'; import { CheckoutSuccessPayloadProps } from 'types/api/billing/checkout'; import { License } from 'types/api/licenses/def'; import { isCloudUser } from 'utils/app'; export interface LaunchChatSupportProps { eventName: string; attributes: Record; message?: string; buttonText?: string; className?: string; onHoverText?: string; intercomMessageDisabled?: boolean; } // eslint-disable-next-line sonarjs/cognitive-complexity function LaunchChatSupport({ attributes, eventName, message = '', buttonText = '', className = '', onHoverText = '', intercomMessageDisabled = false, }: LaunchChatSupportProps): JSX.Element | null { const isChatSupportEnabled = useFeatureFlags(FeatureKeys.CHAT_SUPPORT)?.active; const isCloudUserVal = isCloudUser(); const { notifications } = useNotifications(); const { data: licenseData, isFetching } = useLicense(); const [activeLicense, setActiveLicense] = useState(null); const [isAddCreditCardModalOpen, setIsAddCreditCardModalOpen] = useState( false, ); const { pathname } = useLocation(); const isPremiumChatSupportEnabled = useFeatureFlags(FeatureKeys.PREMIUM_SUPPORT)?.active || false; const showAddCreditCardModal = !isPremiumChatSupportEnabled && !licenseData?.payload?.trialConvertedToSubscription; useEffect(() => { const activeValidLicense = licenseData?.payload?.licenses?.find( (license) => license.isCurrent === true, ) || null; setActiveLicense(activeValidLicense); }, [licenseData, isFetching]); const handleFacingIssuesClick = (): void => { if (showAddCreditCardModal) { logEvent('Disabled Chat Support: Clicked', { source: `facing issues button`, page: pathname, ...attributes, }); setIsAddCreditCardModalOpen(true); } else { logEvent(eventName, attributes); if (window.Intercom && !intercomMessageDisabled) { window.Intercom('showNewMessage', defaultTo(message, '')); } } }; const handleBillingOnSuccess = ( data: ErrorResponse | SuccessResponse, ): void => { if (data?.payload?.redirectURL) { const newTab = document.createElement('a'); newTab.href = data.payload.redirectURL; newTab.target = '_blank'; newTab.rel = 'noopener noreferrer'; newTab.click(); } }; const handleBillingOnError = (): void => { notifications.error({ message: SOMETHING_WENT_WRONG, }); }; const { mutate: updateCreditCard, isLoading: isLoadingBilling } = useMutation( updateCreditCardApi, { onSuccess: (data) => { handleBillingOnSuccess(data); }, onError: handleBillingOnError, }, ); const handleAddCreditCard = (): void => { logEvent('Add Credit card modal: Clicked', { source: `facing issues button`, page: pathname, ...attributes, }); updateCreditCard({ licenseKey: activeLicense?.key || '', successURL: window.location.href, cancelURL: window.location.href, }); }; return isCloudUserVal && isChatSupportEnabled ? ( // Note: we would need to move this condition to license based in future
{/* Add Credit Card Modal */} Add Credit Card for Chat Support} open={isAddCreditCardModalOpen} closable onCancel={(): void => setIsAddCreditCardModalOpen(false)} destroyOnClose footer={[ , , ]} > You're currently on Trial plan . Add a credit card to access SigNoz chat support to your workspace.
) : null; } LaunchChatSupport.defaultProps = { message: '', buttonText: '', className: '', onHoverText: '', intercomMessageDisabled: false, }; export default LaunchChatSupport;