diff --git a/src/components/Premium/PremiumPage.tsx b/src/components/Premium/PremiumPage.tsx index c4a3d3faa..39dbf9443 100644 --- a/src/components/Premium/PremiumPage.tsx +++ b/src/components/Premium/PremiumPage.tsx @@ -9,11 +9,12 @@ import { Zap, } from 'lucide-react'; import { useState, useEffect } from 'react'; -import { useMutation } from '@tanstack/react-query'; +import { useMutation, useQuery } from '@tanstack/react-query'; import { useToast } from '../../hooks/use-toast'; import { httpPost } from '../../lib/query-http'; import { USER_SUBSCRIPTION_PLAN_PRICES, + billingDetailsOptions, type AllowedSubscriptionInterval, } from '../../queries/billing'; import { queryClient } from '../../stores/query-client'; @@ -24,12 +25,17 @@ import { StatsItem } from './StatsItem'; import { features, paidFeaturesList } from './constants'; import { isLoggedIn } from '../../lib/jwt'; import { showLoginPopup } from '../../lib/popup'; +import { UpdatePlanConfirmation } from '../Billing/UpdatePlanConfirmation'; export function PremiumPage() { const [activeVideoId, setActiveVideoId] = useState(null); const [selectedPlan, setSelectedPlan] = useState(null); + // State for plan switching confirmation modal + const [switchPlanDetails, setSwitchPlanDetails] = + useState<(typeof USER_SUBSCRIPTION_PLAN_PRICES)[number] | null>(null); + const toast = useToast(); useEffect(() => { @@ -73,6 +79,20 @@ export function PremiumPage() { (p) => p.interval === 'year', ); + const { data: billingDetails } = useQuery( + billingDetailsOptions(), + queryClient, + ); + + const isSubscriptionCanceled = ['canceled', 'incomplete_expired'].includes( + billingDetails?.status || '', + ); + const currentPriceId = isSubscriptionCanceled ? null : billingDetails?.priceId; + + const currentPlan = USER_SUBSCRIPTION_PLAN_PRICES.find( + (plan) => plan.priceId === currentPriceId, + ); + const activeVideoStartTime = features.find((feature) => feature.videoId === activeVideoId)?.startTime || '0'; @@ -89,6 +109,12 @@ export function PremiumPage() { setSelectedPlan(plan.interval); + // If user already has an active subscription and is selecting a different plan, initiate switch flow + if (currentPriceId && plan.priceId !== currentPriceId) { + setSwitchPlanDetails(plan); + return; + } + const currentUrlPath = window.location.pathname; const encodedCurrentUrlPath = encodeURIComponent(currentUrlPath); const successPage = `/thank-you?next=${encodedCurrentUrlPath}&s=1`; @@ -219,14 +245,24 @@ export function PremiumPage() {