import { useEffect, useState } from "react";
import { ThemeToggle } from "../components/ThemeToggle";
import { Button } from "../components/ui/button";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "../components/ui/card";
import { useAuth } from "../hooks/useAuth";
import { useSubscription } from "../hooks/useSubscription";
import { getPlanName } from "../lib/plans";
interface DashboardPageProps {
navigate: (
route:
| "/"
| "/login"
| "/signup"
| "/pricing"
| "/dashboard"
| "/forgot-password"
| "/reset-password",
) => void;
}
export default function DashboardPage({ navigate }: DashboardPageProps) {
const { user, signOut } = useAuth();
const {
plan,
isSubscribed,
isCancelling,
periodEnd,
isLoading: isSubscriptionLoading,
} = useSubscription();
const [showUpgradeSuccess, setShowUpgradeSuccess] = useState(false);
// Check for upgrade success
useEffect(() => {
const hash = window.location.hash;
if (hash.includes("upgraded=true")) {
setShowUpgradeSuccess(true);
// Clean up URL
window.history.replaceState(null, "", window.location.pathname + "#/dashboard");
}
}, []);
const handleSignOut = async () => {
await signOut();
navigate("/");
};
const getPlanBadgeClasses = (planName: string) => {
switch (planName) {
case "pro":
return "bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-200";
case "enterprise":
return "bg-purple-100 text-purple-800 dark:bg-purple-900 dark:text-purple-200";
default:
return "bg-gray-100 text-gray-800 dark:bg-gray-800 dark:text-gray-200";
}
};
// Stats based on plan
const getStats = () => {
const currentPlan = isSubscribed ? plan : "free";
switch (currentPlan) {
case "enterprise":
return {
apiCalls: { value: "Unlimited", limit: null },
projects: { value: "42", limit: "Unlimited" },
teamMembers: { value: "18", limit: "Unlimited" },
};
case "pro":
return {
apiCalls: { value: "8,234", limit: "50,000" },
projects: { value: "7", limit: "Unlimited" },
teamMembers: { value: "5", limit: "10" },
};
default:
return {
apiCalls: { value: "847", limit: "1,000" },
projects: { value: "1", limit: "1" },
teamMembers: { value: "1", limit: "1" },
};
}
};
const stats = getStats();
const currentPlan = isSubscribed ? plan : "free";
return (
{/* Header */}
navigate("/")}
className="text-xl font-bold hover:opacity-80 transition-opacity"
>
jack-template
{user?.email}
Sign out
{/* Upgrade Success Celebration */}
{showUpgradeSuccess && (
setShowUpgradeSuccess(false)}
className="absolute top-4 right-4 text-white/80 hover:text-white"
>
You're all set!
Your upgrade to{" "}
{plan === "enterprise" ? "Enterprise" : "Pro"}
{" "}
is complete. Here's what you just unlocked:
Unlimited Projects
Create as many as you need
Advanced Analytics
Deep insights into your data
Priority Support
Get help when you need it
setShowUpgradeSuccess(false)}
>
Start Using Pro Features
)}
{/* Welcome Section */}
Welcome back, {user?.name || "there"}!
Here's what's happening with your account today.
{/* Plan Status Card */}
Subscription Status
Your current plan and usage
{isSubscriptionLoading ? (
) : (
{getPlanName(currentPlan)}
{isCancelling && (
Cancelling
)}
)}
{isCancelling && (
Your subscription is set to cancel
You'll have access to {getPlanName(currentPlan)} features until{" "}
{periodEnd
? new Date(periodEnd).toLocaleDateString()
: "the end of your billing period"}
.
navigate("/pricing")}>
Resubscribe
)}
{!isSubscribed && !isSubscriptionLoading && (
Upgrade to unlock more features
Get unlimited projects, advanced analytics, and priority support.
navigate("/pricing")}>View Plans
)}
{isSubscribed && currentPlan === "pro" && (
You're on the Pro plan
Enjoying unlimited projects and advanced analytics.
)}
{isSubscribed && currentPlan === "enterprise" && (
Enterprise plan active
Full access to all features with dedicated support.
)}
{/* Stats Grid */}
API Calls
{stats.apiCalls.value}
{stats.apiCalls.limit && (
of {stats.apiCalls.limit} this month
)}
Projects
{stats.projects.value}
{stats.projects.limit === "Unlimited"
? "Unlimited"
: `of ${stats.projects.limit} available`}
Team Members
{stats.teamMembers.value}
{stats.teamMembers.limit === "Unlimited"
? "Unlimited seats"
: `of ${stats.teamMembers.limit} seats`}
{/* Quick Actions */}
Quick Actions
Common tasks and shortcuts
New Project
View Analytics
Invite Team
Settings
{/* Recent Activity */}
Recent Activity
Your latest actions and updates
Signed in successfully
Just now
Account created
Welcome to the platform!
More activity will appear here as you use the app.
{/* Back to home link */}
navigate("/")}
className="text-sm text-muted-foreground hover:text-foreground transition-colors"
>
Back to home
);
}