"use client"; import { useEffect, useState } from "react"; import { useAuth } from "@/lib/auth-context"; import { useRouter } from "next/navigation"; import { Button } from "@/components/ui/button"; interface Subscription { id: string; tier: string; status: string; storage_limit_gb: number; current_usage_gb: number; created_at: string; } export default function SubscriptionsPage() { const { isAuthenticated, user, logout, token } = useAuth(); const router = useRouter(); const [subscription, setSubscription] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [authInitialized, setAuthInitialized] = useState(false); // Wait for auth to initialize from localStorage useEffect(() => { // Small delay to let auth context initialize const timer = setTimeout(() => { setAuthInitialized(true); }, 100); return () => clearTimeout(timer); }, []); useEffect(() => { if (!authInitialized) return; // Check if user is authenticated if (user && token) { // User is authenticated, fetch subscription fetchSubscription(); } else { // Not authenticated, redirect to login router.push("/login?redirect=/subscriptions"); } }, [authInitialized, user, token, router]); const fetchSubscription = async () => { if (!user?.id) return; setLoading(true); try { const response = await fetch("/api/subscriptions/me", { headers: { "user-id": user.id, }, }); if (response.ok) { const data = await response.json(); setSubscription(data.subscription); } else { setError("Failed to fetch subscription data"); } } catch (err) { setError("Error loading subscription"); } finally { setLoading(false); } }; const handleCancelSubscription = async () => { if (!confirm("Are you sure you want to cancel your subscription?")) { return; } try { const response = await fetch("/api/subscriptions/cancel", { method: "POST", headers: { "user-id": user?.id || "", }, }); if (response.ok) { alert("Subscription cancelled successfully"); fetchSubscription(); // Refresh data } else { const data = await response.json(); alert(data.error || "Failed to cancel subscription"); } } catch (err) { alert("Error cancelling subscription"); } }; // Don't show content until auth has initialized if (!authInitialized) { return (
Loading...
); } if (!isAuthenticated) { return null; // Will redirect to login } if (loading) { return (
Loading subscription...
); } if (error) { return (
{error}
); } const getTierDisplayName = (tier: string) => { const tierMap: Record = { BASIC: "Basic", STANDARD: "Standard", PREMIUM: "Premium", }; return tierMap[tier.toUpperCase()] || tier; }; const getTierPrice = (tier: string) => { const priceMap: Record = { BASIC: "$9.99", STANDARD: "$19.99", PREMIUM: "$39.99", }; return priceMap[tier.toUpperCase()] || "N/A"; }; return (
{/* Header */}

My Subscriptions

Manage your subscription and billing information

{!subscription ? ( // No subscription
📦

No Active Subscription

You don't have an active subscription. Subscribe to a plan to start using our services.

) : ( // Active subscription
{/* Subscription Card */}

{getTierDisplayName(subscription.tier)} Plan

Current subscription details

{subscription.status}
{/* Subscription Details */}

Plan

{getTierDisplayName(subscription.tier)}

Monthly Price

{getTierPrice(subscription.tier)}/month

Storage Limit

{subscription.storage_limit_gb} GB

Current Usage

{parseFloat(subscription.current_usage_gb).toFixed(2)} GB

{/* Storage Usage Bar */}

Storage Usage

{parseFloat(subscription.current_usage_gb).toFixed(2)} GB of{" "} {subscription.storage_limit_gb} GB used

{/* Billing Info */}

Member Since

{new Date(subscription.created_at).toLocaleDateString( "en-US", { year: "numeric", month: "long", day: "numeric", } )}

{/* Actions */} {subscription.status === "ACTIVE" && (
)}
{/* Upgrade/Downgrade Options */}

Change Plan

Looking to upgrade or downgrade? Cancel your current subscription and choose a new plan.

)}
); }