import { useState, useEffect } from "react"; import { useNavigate } from "react-router-dom"; import { updatePassword } from "firebase/auth"; import { auth } from "../../firebase/config"; import { getAccount, createPortalSession } from "../../api/account"; import "./Account.css"; const TIER_LABELS = { free: "Free Trial", premium: "Premium", pro: "Pro", }; export default function Account() { const navigate = useNavigate(); const [account, setAccount] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [newPassword, setNewPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [passwordMsg, setPasswordMsg] = useState(null); const [passwordErr, setPasswordErr] = useState(null); const [changingPassword, setChangingPassword] = useState(false); const [portalLoading, setPortalLoading] = useState(false); useEffect(() => { async function fetchAccount() { try { const data = await getAccount(); setAccount(data); } catch (err) { setError(err.message); } finally { setLoading(false); } } fetchAccount(); }, []); async function handlePasswordChange(e) { e.preventDefault(); setPasswordMsg(null); setPasswordErr(null); if (newPassword.length < 6) { setPasswordErr("Password must be at least 6 characters"); return; } if (newPassword !== confirmPassword) { setPasswordErr("Passwords do not match"); return; } try { setChangingPassword(true); await updatePassword(auth.currentUser, newPassword); setPasswordMsg("Password updated successfully"); setNewPassword(""); setConfirmPassword(""); } catch (err) { setPasswordErr(err.message); } finally { setChangingPassword(false); } } async function handleManageSubscription() { try { setPortalLoading(true); const { url } = await createPortalSession(); window.location.href = url; } catch (err) { setError(err.message); } finally { setPortalLoading(false); } } if (loading) { return
{hasPaidSub ? "Cancel subscription, update payment method, or view invoices via Stripe." : "Upgrade to unlock more addresses, alert types, and notification channels."}