import { useAuth } from "@lib/auth-context"; import { fetchConnectionsFeature, fetchMemoriesFeature, fetchSubscriptionStatus, } from "@lib/queries"; import { Button } from "@ui/components/button"; import { HeadingH3Bold } from "@ui/text/heading/heading-h3-bold"; import { useCustomer } from "autumn-js/react"; import { CheckCircle, LoaderIcon, X } from "lucide-react"; import { motion } from "motion/react"; import Link from "next/link"; import { useEffect, useState } from "react"; import { analytics } from "@/lib/analytics"; export function BillingView() { const autumn = useCustomer(); const { user } = useAuth(); const [isLoading, setIsLoading] = useState(false); useEffect(() => { analytics.billingViewed(); }, []); const { data: memoriesCheck } = fetchMemoriesFeature(autumn as any); const memoriesUsed = memoriesCheck?.usage ?? 0; const memoriesLimit = memoriesCheck?.included_usage ?? 0; const { data: connectionsCheck } = fetchConnectionsFeature(autumn as any); const connectionsUsed = connectionsCheck?.usage ?? 0; // Fetch subscription status with React Query const { data: status = { consumer_pro: null, }, isLoading: isCheckingStatus, } = fetchSubscriptionStatus(autumn as any); // Handle upgrade const handleUpgrade = async () => { analytics.upgradeInitiated(); setIsLoading(true); try { await autumn.attach({ productId: "consumer_pro", successUrl: "https://app.supermemory.ai/", }); analytics.upgradeCompleted(); window.location.reload(); } catch (error) { console.error(error); setIsLoading(false); } }; // Handle manage billing const handleManageBilling = async () => { analytics.billingPortalOpened(); await autumn.openBillingPortal({ returnUrl: "https://app.supermemory.ai", }); }; const isPro = status.consumer_pro; if (user?.isAnonymous) { return ( Sign in to unlock premium features Sign in ); } if (isPro) { return ( Pro Plan Active You're enjoying expanded memory capacity with supermemory Pro! {/* Current Usage */} Current Usage Memories {memoriesUsed} / {memoriesLimit} Connections {connectionsUsed} / 10 Manage Billing ); } return ( {/* Current Usage - Free Plan */} Current Plan: Free Memories = memoriesLimit ? "text-red-400" : "text-white/90"}`} > {memoriesUsed} / {memoriesLimit} = memoriesLimit ? "bg-red-500" : "bg-blue-500" }`} style={{ width: `${Math.min((memoriesUsed / memoriesLimit) * 100, 100)}%`, }} /> {/* Comparison */} Upgrade to Pro {/* Free Plan */} Free Plan 200 memories No connections Basic search {/* Pro Plan */} Pro Plan Recommended 5000 memories 10 connections Advanced search Priority support {isLoading || isCheckingStatus ? ( <> Upgrading... > ) : ( Upgrade to Pro - $15/month (only for first 100 users) )} Cancel anytime. No questions asked. ); }
Sign in to unlock premium features
You're enjoying expanded memory capacity with supermemory Pro!
Cancel anytime. No questions asked.