overleaf/services/web/frontend/js/features/subscription/components/dashboard/personal-subscription.tsx
Eric Mc Sween 34fa1e12e7 Merge pull request #21898 from overleaf/em-ai-add-on-checkout-page
Adapt checkout page to standalone AI add-on

GitOrigin-RevId: ed877bc10c1dd4362c981000e0d783e370f3593a
2024-11-19 09:05:04 +00:00

104 lines
3.2 KiB
TypeScript

import { Trans, useTranslation } from 'react-i18next'
import { RecurlySubscription } from '../../../../../../types/subscription/dashboard/subscription'
import { ActiveSubscription } from './states/active/active'
import { ActiveAiAddonSubscription } from './states/active/active-ai-addon'
import { CanceledSubscription } from './states/canceled'
import { ExpiredSubscription } from './states/expired'
import { useSubscriptionDashboardContext } from '../../context/subscription-dashboard-context'
import PersonalSubscriptionRecurlySyncEmail from './personal-subscription-recurly-sync-email'
import OLNotification from '@/features/ui/components/ol/ol-notification'
import { isStandaloneAiPlanCode, AI_ADD_ON_CODE } from '../../data/add-on-codes'
function PastDueSubscriptionAlert({
subscription,
}: {
subscription: RecurlySubscription
}) {
const { t } = useTranslation()
return (
<OLNotification
type="error"
content={
<>
{t('account_has_past_due_invoice_change_plan_warning')}{' '}
<a
href={subscription.recurly.accountManagementLink}
target="_blank"
rel="noreferrer noopener"
>
{t('view_your_invoices')}
</a>
</>
}
/>
)
}
function PersonalSubscriptionStates({
subscription,
}: {
subscription: RecurlySubscription
}) {
const { t } = useTranslation()
const state = subscription?.recurly.state
const hasAiAddon = subscription?.addOns?.some(
addOn => addOn.addOnCode === AI_ADD_ON_CODE
)
const onAiStandalonePlan = isStandaloneAiPlanCode(subscription.planCode)
const planHasAi = onAiStandalonePlan || hasAiAddon
if (state === 'active' && planHasAi) {
return <ActiveAiAddonSubscription subscription={subscription} />
} else if (state === 'active') {
return <ActiveSubscription subscription={subscription} />
} else if (state === 'canceled') {
return <CanceledSubscription subscription={subscription} />
} else if (state === 'expired') {
return <ExpiredSubscription subscription={subscription} />
} else {
return <>{t('problem_with_subscription_contact_us')}</>
}
}
function PersonalSubscription() {
const { t } = useTranslation()
const { personalSubscription, recurlyLoadError } =
useSubscriptionDashboardContext()
if (!personalSubscription) return null
if (!('recurly' in personalSubscription)) {
return (
<p>
<Trans
i18nKey="please_contact_support_to_makes_change_to_your_plan"
components={[<a href="/contact" />]} // eslint-disable-line react/jsx-key, jsx-a11y/anchor-has-content
/>
</p>
)
}
return (
<>
{personalSubscription.recurly.account.has_past_due_invoice._ ===
'true' && (
<PastDueSubscriptionAlert subscription={personalSubscription} />
)}
<PersonalSubscriptionStates
subscription={personalSubscription as RecurlySubscription}
/>
{recurlyLoadError && (
<OLNotification
type="warning"
content={<strong>{t('payment_provider_unreachable_error')}</strong>}
/>
)}
<hr />
<PersonalSubscriptionRecurlySyncEmail />
</>
)
}
export default PersonalSubscription