"use client"; import { useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import { useAuth } from "@/lib/auth-context"; import UploadArea from "@/components/UploadArea"; export default function UploadPage() { const { isAuthenticated, user } = useAuth(); const router = useRouter(); const [userSubscription, setUserSubscription] = useState(null); const [authInitialized, setAuthInitialized] = useState(false); const [selectedFiles, setSelectedFiles] = useState([]); const [uploading, setUploading] = useState(false); const [uploadMessage, setUploadMessage] = useState(null); function handleFilesSelected(newFiles: File[]) { setSelectedFiles((prev) => { const merged = [...prev]; for (const file of newFiles) { if ( !merged.some( (f) => f.name === file.name && f.size === file.size && f.lastModified === file.lastModified ) ) { merged.push(file); } } return merged; }); } // Wait for auth to initialize from localStorage useEffect(() => { const timer = setTimeout(() => { setAuthInitialized(true); }, 100); return () => clearTimeout(timer); }, []); // Check authentication and fetch subscription useEffect(() => { if (!authInitialized) return; if (!isAuthenticated || !user) { router.push("/login?redirect=/upload"); return; } // Fetch user subscription const fetchSubscription = async () => { if (!user?.id) return; try { const response = await fetch("/api/subscriptions/me", { headers: { "user-id": user.id, }, }); if (response.ok) { const data = await response.json(); setUserSubscription(data.subscription); } } catch (error) { console.error("Error fetching subscription:", error); } }; fetchSubscription(); }, [authInitialized, isAuthenticated, user, router]); // Show loading while auth initializes if (!authInitialized || !isAuthenticated) { return (
Loading...
); } // If no subscription found after loading, treat it as no subscription // Don't block the page from showing const hasActiveSubscription = userSubscription && userSubscription.status === "ACTIVE"; async function handleStartUpload() { if (!selectedFiles.length || !hasActiveSubscription || !user) return; setUploading(true); setUploadMessage(null); try { for (const file of selectedFiles) { const formData = new FormData(); formData.append("file", file); const res = await fetch("/api/upload", { method: "POST", headers: { "user-id": user.id, }, body: formData, }); const data = await res.json(); if (!res.ok) throw new Error(data?.error || "Upload failed"); } setUploadMessage("Upload successful!"); setSelectedFiles([]); // Reset picker // Optionally: also refresh renders dashboard, etc } catch (err: any) { setUploadMessage(err.message || "Upload failed"); } finally { setUploading(false); } } return (
{/* Header */}

Upload Files

{/*

Upload your .blend files to start rendering

*/}
{/* Subscription Warning */} {!hasActiveSubscription && (

Subscribe to use our services

You need an active subscription to upload files. Subscribe to a plan to continue.

)} {/* Upload Area */}

Start by uploading at least one .blend file

{/* Selected files indicator */} {selectedFiles.length > 0 && (
Selected files ({selectedFiles.length}):
    {selectedFiles.map((f, idx) => (
  • {f.name}{" "} ({(f.size / 1024).toFixed(1)} KB)
  • ))}
)} {uploadMessage && (
{uploadMessage}
)}
); }