'use client'; import { useState, useEffect } from 'react'; import { useRouter, useSearchParams } from 'next/navigation'; import Link from 'next/link'; import { ArrowLeft, Loader2, Globe, Lock, UserPlus, FolderPlus, Building2 } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Textarea } from '@/components/ui/textarea'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; type Visibility = 'PRIVATE' | 'INVITE' | 'PUBLIC'; interface Workspace { id: string; name: string; } const visibilityOptions: { value: Visibility; label: string; description: string; icon: React.ReactNode }[] = [ { value: 'PRIVATE', label: 'Private', description: 'Only workspace members and project members can access', icon: , }, { value: 'INVITE', label: 'Invite Only', description: 'Share with specific people via email', icon: , }, { value: 'PUBLIC', label: 'Public', description: 'Anyone with the link can view', icon: , }, ]; export default function NewProjectPage() { const router = useRouter(); const searchParams = useSearchParams(); const preselectedWorkspace = searchParams.get('workspace'); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(''); const [workspaces, setWorkspaces] = useState([]); const [isLoadingWorkspaces, setIsLoadingWorkspaces] = useState(true); const [formData, setFormData] = useState({ name: '', description: '', visibility: 'PRIVATE' as Visibility, workspaceId: preselectedWorkspace || '', }); useEffect(() => { async function fetchWorkspaces() { try { const res = await fetch('/api/workspaces'); if (res.ok) { const data = await res.json(); const workspacesData = data.workspaces || []; setWorkspaces(workspacesData); // Auto-select if only one workspace and none preselected if (!preselectedWorkspace && workspacesData.length === 1) { setFormData(prev => ({ ...prev, workspaceId: workspacesData[0].id })); } } } catch { // ignore } finally { setIsLoadingWorkspaces(false); } } fetchWorkspaces(); }, [preselectedWorkspace]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!formData.workspaceId) { setError('Please select a workspace'); return; } setIsLoading(true); setError(''); try { const response = await fetch('/api/projects', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(formData), }); const data = await response.json(); if (!response.ok) { setError(data.error || 'Failed to create project'); return; } router.push(`/projects/${data.id}`); } catch { setError('Something went wrong. Please try again.'); } finally { setIsLoading(false); } }; return ( Back to Projects Create New Project Set up a new project to organize your videos and collect feedback {/* Workspace selector */} Workspace {isLoadingWorkspaces ? ( Loading workspaces... ) : workspaces.length === 0 ? ( You need a workspace first. Every project belongs to a workspace. Create Workspace ) : ( setFormData(prev => ({ ...prev, workspaceId: v }))} > {workspaces.map((ws) => ( {ws.name} ))} )} Project Name setFormData(prev => ({ ...prev, name: e.target.value }))} required disabled={isLoading} className="h-11" /> Description (optional) setFormData(prev => ({ ...prev, description: e.target.value }))} rows={3} disabled={isLoading} className="resize-none" /> Who can access? {visibilityOptions.map((option) => ( setFormData(prev => ({ ...prev, visibility: option.value }))} disabled={isLoading} className={`w-full flex items-center gap-4 p-4 rounded-xl border-2 text-left transition-all ${formData.visibility === option.value ? 'border-primary bg-primary/5 ring-1 ring-primary/20' : 'border-border hover:border-border/80 hover:bg-accent/50' }`} > {option.icon} {option.label} {option.description} {formData.visibility === option.value && ( )} ))} {error && ( {error} )} {isLoading && } Create Project router.back()} className="h-11 px-6" > Cancel ); }
You need a workspace first. Every project belongs to a workspace.