"use client"; import { useEffect, useMemo, useRef } from "react"; import { BackButton } from "@/app/_components"; import { Checkbox, MobileShell } from "@/app/_components/core"; import { AppConstants } from "@/core/app_constants"; import type { PayChannel } from "@/data/dto/payment"; import { useUserState } from "@/stores/user/user-context"; import { SubscriptionCheckoutButton, SubscriptionCoinsOfferSection, SubscriptionPaymentMethod, SubscriptionPaymentSuccessDialog, SubscriptionVipOfferSection, } from "./components"; import styles from "./components/subscription-screen.module.css"; import { findSelectedSubscriptionPlan, canChooseSubscriptionPayChannel, getFirstRechargeOfferView, getDefaultSubscriptionPlanId, resolveSubscriptionPayChannel, toCoinsOfferPlanViews, toVipOfferPlanViews, type SubscriptionType, } from "./subscription-screen.helpers"; import { useSubscriptionPaymentFlow } from "./use-subscription-payment-flow"; export type { SubscriptionType } from "./subscription-screen.helpers"; export interface SubscriptionScreenProps { subscriptionType?: SubscriptionType; shouldResumePendingOrder?: boolean; returnTo?: "chat" | null; initialPayChannel?: PayChannel | null; } export function SubscriptionScreen({ subscriptionType = "vip", shouldResumePendingOrder = false, returnTo = null, initialPayChannel = null, }: SubscriptionScreenProps) { const userState = useUserState(); const countryCode = userState.currentUser?.countryCode; const canChoosePaymentMethod = canChooseSubscriptionPayChannel(countryCode); const resolvedInitialPayChannel = resolveSubscriptionPayChannel({ countryCode, requestedPayChannel: initialPayChannel, }); const phDefaultPayChannelAppliedRef = useRef(false); const { payment, paymentDispatch, showPaymentSuccessDialog, handleBackClick, handlePaymentSuccessClose, } = useSubscriptionPaymentFlow({ subscriptionType, shouldResumePendingOrder, returnTo, initialPayChannel: resolvedInitialPayChannel, }); const canSubscribeVip = subscriptionType === "vip"; const vipOfferPlans = useMemo( () => toVipOfferPlanViews(payment.plans), [payment.plans], ); const directCoinsPlans = useMemo( () => toCoinsOfferPlanViews(payment.plans), [payment.plans], ); const firstRechargeOffer = useMemo( () => getFirstRechargeOfferView({ isFirstRecharge: payment.isFirstRecharge, vipPlans: vipOfferPlans, coinPlans: directCoinsPlans, }), [directCoinsPlans, payment.isFirstRecharge, vipOfferPlans], ); const selectedPlan = findSelectedSubscriptionPlan({ canSubscribeVip, selectedPlanId: payment.selectedPlanId, vipPlans: vipOfferPlans, coinPlans: directCoinsPlans, }); const isPaymentBusy = payment.isCreatingOrder || payment.isPollingOrder; const canActivate = selectedPlan !== null && payment.agreed && !isPaymentBusy; useEffect(() => { if (isPaymentBusy) return; if (!canChoosePaymentMethod) { if (payment.payChannel === "stripe") return; paymentDispatch({ type: "PaymentPayChannelChanged", payChannel: "stripe", }); return; } if (initialPayChannel !== null) return; if (phDefaultPayChannelAppliedRef.current) return; phDefaultPayChannelAppliedRef.current = true; if (payment.payChannel === resolvedInitialPayChannel) return; paymentDispatch({ type: "PaymentPayChannelChanged", payChannel: resolvedInitialPayChannel, }); }, [ canChoosePaymentMethod, initialPayChannel, isPaymentBusy, payment.payChannel, paymentDispatch, resolvedInitialPayChannel, ]); useEffect(() => { const defaultPlanId = getDefaultSubscriptionPlanId({ canSubscribeVip, selectedPlanId: payment.selectedPlanId, status: payment.status, isLoadingPlans: payment.isLoadingPlans, selectedPlan, vipPlans: vipOfferPlans, coinPlans: directCoinsPlans, }); if (!defaultPlanId) return; paymentDispatch({ type: "PaymentPlanSelected", planId: defaultPlanId, }); }, [ canSubscribeVip, directCoinsPlans, payment.isLoadingPlans, paymentDispatch, payment.selectedPlanId, payment.status, selectedPlan, vipOfferPlans, ]); return (
{firstRechargeOffer ? (
{firstRechargeOffer.badgeText}

{firstRechargeOffer.title}

{firstRechargeOffer.subtitle}

) : null}
{canSubscribeVip ? ( paymentDispatch({ type: "PaymentPlanSelected", planId, }) } /> ) : null} paymentDispatch({ type: "PaymentPlanSelected", planId, }) } />
{canChoosePaymentMethod ? (
{ paymentDispatch({ type: "PaymentPayChannelChanged", payChannel, }); }} />
) : null}
paymentDispatch({ type: "PaymentAgreementChanged", agreed, }) } label={ I agree to the{" "} VIP Membership Benefits Agreement {" "} and{" "} Automatic renewal Agreement } />
); }