"use client"; import { useState, useEffect } from 'react'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { auth } from '@/lib/firebase/config'; import { toast } from 'sonner'; import { DollarSign, TrendingUp, TrendingDown, Calendar } from 'lucide-react'; import { useParams } from 'next/navigation'; interface CostData { total: number; thisMonth: number; lastMonth: number; byProject: Array<{ projectId: string; projectName: string; cost: number; }>; byDate: Array<{ date: string; cost: number; }>; } export default function CostsPage() { const params = useParams(); const workspace = params.workspace as string; const [costs, setCosts] = useState(null); const [loading, setLoading] = useState(true); useEffect(() => { loadCosts(); }, []); const loadCosts = async () => { try { const user = auth.currentUser; if (!user) return; const token = await user.getIdToken(); const response = await fetch('/api/costs', { headers: { 'Authorization': `Bearer ${token}`, }, }); if (response.ok) { const data = await response.json(); setCosts(data); } } catch (error) { console.error('Error loading costs:', error); toast.error('Failed to load cost data'); } finally { setLoading(false); } }; const percentageChange = costs && costs.lastMonth > 0 ? ((costs.thisMonth - costs.lastMonth) / costs.lastMonth) * 100 : 0; return (
{/* Header */}

Costs

Track your AI usage costs across all projects

{/* Summary Cards */} {loading ? (

Loading cost data...

) : ( <>
Total Costs
${costs?.total.toFixed(2) || '0.00'}

All time

This Month
${costs?.thisMonth.toFixed(2) || '0.00'}

{new Date().toLocaleDateString('en-US', { month: 'long', year: 'numeric' })}

vs Last Month {percentageChange >= 0 ? ( ) : ( )}
{percentageChange >= 0 ? '+' : ''}{percentageChange.toFixed(1)}%

Last month: ${costs?.lastMonth.toFixed(2) || '0.00'}

{/* Costs by Project */} Costs by Project Your spending broken down by project {costs?.byProject && costs.byProject.length > 0 ? (
{costs.byProject.map((project) => (

{project.projectName}

Project ID: {project.projectId}

${project.cost.toFixed(2)}

{((project.cost / (costs.total || 1)) * 100).toFixed(1)}% of total

))}
) : (

No project costs yet

)}
{/* Info Card */} About Cost Tracking

📊 Automatic Tracking: All AI API costs are automatically tracked when you use Vibn features.

💰 Your Keys, Your Costs: Costs reflect usage of your own API keys - Vibn doesn't add any markup.

📈 Project Attribution: Costs are attributed to projects based on session metadata.

)}
); }