"use client" import { authClient } from "@lib/auth" import { useAuth } from "@lib/auth-context" import { Button } from "@repo/ui/components/button" import { HeadingH3Bold } from "@repo/ui/text/heading/heading-h3-bold" import { useCustomer } from "autumn-js/react" import { CheckCircle, CreditCard, LoaderIcon, LogOut, User, X, } from "lucide-react" import { motion } from "motion/react" import Link from "next/link" import { useRouter } from "next/navigation" import { useEffect, useState } from "react" import { analytics } from "@/lib/analytics" import { $fetch } from "@lib/api" export function ProfileView() { const router = useRouter() const { user: session } = useAuth() const { customer, isLoading: isCustomerLoading, openBillingPortal, attach, } = useCustomer() const [isLoading, setIsLoading] = useState(false) const [billingData, setBillingData] = useState<{ isPro: boolean memoriesUsed: number memoriesLimit: number connectionsUsed: number connectionsLimit: number }>({ isPro: false, memoriesUsed: 0, memoriesLimit: 0, connectionsUsed: 0, connectionsLimit: 0, }) useEffect(() => { if (!isCustomerLoading) { const memoriesFeature = customer?.features?.memories ?? { usage: 0, included_usage: 0, } const connectionsFeature = customer?.features?.connections ?? { usage: 0, included_usage: 0, } setBillingData({ isPro: customer?.products?.some( (product) => product.id === "consumer_pro", ) ?? false, memoriesUsed: memoriesFeature?.usage ?? 0, memoriesLimit: memoriesFeature?.included_usage ?? 0, connectionsUsed: connectionsFeature?.usage ?? 0, connectionsLimit: connectionsFeature?.included_usage ?? 0, }) } }, [isCustomerLoading, customer]) const handleLogout = () => { analytics.userSignedOut() authClient.signOut() router.push("/login") } const handleUpgrade = async () => { setIsLoading(true) try { const upgradeResult = await attach({ productId: "consumer_pro", successUrl: "https://app.supermemory.ai/", }) if ( upgradeResult.statusCode === 200 && upgradeResult.data && "code" in upgradeResult.data ) { const isProPlanActivated = upgradeResult.data.code === "new_product_attached" if (isProPlanActivated && session?.name && session?.email) { try { await $fetch("@post/emails/welcome/pro", { body: { email: session?.email, firstName: session?.name, }, }) } catch (error) { console.error(error) } } } } catch (error) { console.error(error) setIsLoading(false) } } // Handle manage billing const handleManageBilling = async () => { await openBillingPortal({ returnUrl: "https://app.supermemory.ai", }) } if (session?.isAnonymous) { return (

Sign in to access your profile and billing

) } return (
{/* Profile Section */}

{session?.email}

Logged in

{isCustomerLoading ? (
Loading...
) : ( <> {/* Billing Section */}
{billingData.isPro ? "Pro Plan" : "Free Plan"} {billingData.isPro && ( Active )}

{billingData.isPro ? "Expanded memory capacity" : "Basic plan"}

{/* Usage Stats */}
Memories = billingData.memoriesLimit ? "text-red-400" : "text-white/90"}`} > {billingData.memoriesUsed} / {billingData.memoriesLimit}
= billingData.memoriesLimit ? "bg-red-500" : billingData.isPro ? "bg-green-500" : "bg-blue-500" }`} style={{ width: `${Math.min((billingData.memoriesUsed / billingData.memoriesLimit) * 100, 100)}%`, }} />
{billingData.isPro && (
Connections {billingData.connectionsUsed} / 10
)} {/* Billing Actions */}
{billingData.isPro ? ( ) : ( )}
{/* Plan Comparison - Only show for free users */} {!billingData.isPro && (
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

$15/month (only for first 100 users) • Cancel anytime. No questions asked.

)} )}
) }