'use client'; import { useState, useRef } from 'react'; import { useAuth } from '@/hooks/use-auth'; import { usePortfolios } from '@/hooks/use-portfolios'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from '@/components/ui/card'; import { Upload, Rocket, Plus, LogOut } from 'lucide-react'; export default function DashboardPage() { const { user, logout } = useAuth(); const { portfolios, isLoading, error, createPortfolio, uploadPortfolio, deployPortfolio, } = usePortfolios(); const [showCreateForm, setShowCreateForm] = useState(false); const [newPortfolio, setNewPortfolio] = useState({ name: '', domain: '' }); const [uploadingId, setUploadingId] = useState(null); const [deployingId, setDeployingId] = useState(null); const fileInputRefs = useRef<{ [key: number]: HTMLInputElement | null }>({}); const handleCreate = async (e: React.FormEvent) => { e.preventDefault(); try { await createPortfolio(newPortfolio.name, newPortfolio.domain); setNewPortfolio({ name: '', domain: '' }); setShowCreateForm(false); } catch (err) { console.error('Failed to create portfolio:', err); } }; const handleUpload = async (id: number, file: File) => { try { setUploadingId(id); await uploadPortfolio(id, file); } catch (err) { console.error('Failed to upload portfolio:', err); } finally { setUploadingId(null); } }; const handleDeploy = async (id: number) => { try { setDeployingId(id); await deployPortfolio(id); } catch (err) { console.error('Failed to deploy portfolio:', err); } finally { setDeployingId(null); } }; const getStatusBadge = (portfolio: any) => { if (!portfolio.active) { return Pending Payment; } if (!portfolio.path) { return Pending Upload; } return Uploaded; }; return (
{/* Header */}

Portfolio Dashboard

Welcome, {user?.name}
{/* Stats */}
Total Portfolios
{portfolios.length}
Active
{portfolios.filter(p => p.active).length}
Uploaded
{portfolios.filter(p => p.path).length}
{/* Create Portfolio Button */}

Your Portfolios

{/* Create Portfolio Form */} {showCreateForm && ( Create New Portfolio Enter the details for your new portfolio
setNewPortfolio({ ...newPortfolio, name: e.target.value })} required />
setNewPortfolio({ ...newPortfolio, domain: e.target.value })} required />
)} {/* Portfolios List */} {isLoading ? (
Loading portfolios...
) : error ? (
{error}
) : portfolios.length === 0 ? (
No portfolios yet. Create your first one!
) : (
{portfolios.map((portfolio) => (
{portfolio.name} {portfolio.domain}
{getStatusBadge(portfolio)}
Created: {new Date(portfolio.created_at).toLocaleDateString()}
{portfolio.active && !portfolio.path && ( <> { fileInputRefs.current[portfolio.id] = el; }} className="hidden" onChange={(e) => { const file = e.target.files?.[0]; if (file) handleUpload(portfolio.id, file); }} /> )} {portfolio.path && ( )}
))}
)}
); }