import '../../../../stylesheets/components/compile-time-paywall-modal.scss'
import {
OLModal,
OLModalBody,
OLModalHeader,
OLModalTitle,
} from '@/shared/components/ol/ol-modal'
import OLButton from '@/shared/components/ol/ol-button'
import MaterialIcon from '@/shared/components/material-icon'
import BillingPeriodToggle, {
type BillingPeriod,
} from '@/shared/components/billing-period-toggle'
import getMeta from '@/utils/meta'
import { sendMB } from '@/infrastructure/event-tracking'
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
const PLAN_CODE = {
monthly: 'collaborator',
annual: 'collaborator-annual',
} as const
export type CompileTimeoutPaywallModalProps = {
show: boolean
onHide: () => void
}
export function CompileTimeoutPaywallModal({
show,
onHide,
}: CompileTimeoutPaywallModalProps) {
const { t } = useTranslation()
const standardPlanPricing = getMeta('ol-standardPlanPricing')
const currency = getMeta('ol-recommendedCurrency')
const supportsAnnualPricing = Boolean(standardPlanPricing?.annual)
const [annual, setAnnual] = useState(false)
useEffect(() => {
if (!show) {
setAnnual(false)
}
}, [show])
const billingPeriod: BillingPeriod = annual ? 'annual' : 'monthly'
const planCode = PLAN_CODE[billingPeriod]
const hasShownRef = useRef(false)
useEffect(() => {
if (show && !hasShownRef.current) {
const countryCode = getMeta('ol-countryCode')
sendMB('paywall-plans-page-view', {
'paywall-type': 'compile-timeout',
currency,
countryCode,
version: 'compile-timeout',
})
hasShownRef.current = true
} else if (!show) {
hasShownRef.current = false
}
}, [show, currency])
const handleToggleBilling = useCallback(() => {
const nextAnnual = !annual
setAnnual(nextAnnual)
sendMB('paywall-plans-page-toggle', {
'billing-period': nextAnnual ? 'annual' : 'monthly',
checked: nextAnnual ? 'checked' : 'unchecked',
version: 'compile-timeout',
})
}, [annual])
const openCheckout = useCallback(() => {
const params = new URLSearchParams({
itm_campaign: 'compile-timeout',
})
window.open(
`/user/subscription/new?planCode=${planCode}&${params.toString()}`,
'_blank',
'noopener,noreferrer'
)
}, [planCode])
const handleUpgrade = useCallback(() => {
sendMB('paywall-plans-page-click', {
'paywall-type': 'compile-timeout',
plan: 'collaborator',
'billing-period': billingPeriod,
currency,
version: 'compile-timeout',
button: 'buy',
})
openCheckout()
}, [billingPeriod, currency, openCheckout])
const viewAllPlansHref = useMemo(() => {
const params = new URLSearchParams({
'paywall-type': 'compile-timeout',
itm_campaign: 'compile-timeout',
})
return `/user/subscription/choose-your-plan?${params.toString()}`
}, [])
const handleViewAllPlans = useCallback(() => {
sendMB('paywall-plans-page-click', {
'paywall-type': 'compile-timeout',
button: 'plans',
'billing-period': billingPeriod,
currency,
version: 'compile-timeout',
})
}, [billingPeriod, currency])
const price = annual
? standardPlanPricing?.annual
: standardPlanPricing?.monthly
const priceSubtext = annual ? t('per_year') : t('per_month')
const strikethroughPrice = annual
? standardPlanPricing?.monthlyTimesTwelve
: undefined
return (
{t('standard')} {t('plan')}
{t('compile_timeout_modal_intro')}