"use client"; import { $fetch } from "@lib/api"; 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"; 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
{session?.email}
Logged in
{billingData.isPro ? "Expanded memory capacity" : "Basic plan"}
$15/month (only for first 100 users) • Cancel anytime. No questions asked.